1 /*
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
22 #include "Effects.h"
23
24 #include "Client.h"
25 #include "EffectConfiguration.h"
26
27 #include <afutils/DumpTryLock.h>
28 #include <audio_utils/channels.h>
29 #include <audio_utils/primitives.h>
30 #include <media/AudioCommonTypes.h>
31 #include <media/AudioContainers.h>
32 #include <media/AudioDeviceTypeAddr.h>
33 #include <media/AudioEffect.h>
34 #include <media/EffectClientAsyncProxy.h>
35 #include <media/ShmemCompat.h>
36 #include <media/TypeConverter.h>
37 #include <media/audiohal/EffectHalInterface.h>
38 #include <media/audiohal/EffectsFactoryHalInterface.h>
39 #include <mediautils/MethodStatistics.h>
40 #include <mediautils/ServiceUtilities.h>
41 #include <mediautils/TimeCheck.h>
42 #include <system/audio_effects/effect_aec.h>
43 #include <system/audio_effects/effect_downmix.h>
44 #include <system/audio_effects/effect_dynamicsprocessing.h>
45 #include <system/audio_effects/effect_hapticgenerator.h>
46 #include <system/audio_effects/effect_ns.h>
47 #include <system/audio_effects/effect_spatializer.h>
48 #include <system/audio_effects/effect_visualizer.h>
49 #include <utils/Log.h>
50
51 #include <algorithm>
52
53 // ----------------------------------------------------------------------------
54
55 // Note: the following macro is used for extremely verbose logging message. In
56 // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
57 // 0; but one side effect of this is to turn all LOGV's as well. Some messages
58 // are so verbose that we want to suppress them even when we have ALOG_ASSERT
59 // turned on. Do not uncomment the #def below unless you really know what you
60 // are doing and want to see all of the extremely verbose messages.
61 //#define VERY_VERY_VERBOSE_LOGGING
62 #ifdef VERY_VERY_VERBOSE_LOGGING
63 #define ALOGVV ALOGV
64 #else
65 #define ALOGVV(a...) do { } while(0)
66 #endif
67
68 #define DEFAULT_OUTPUT_SAMPLE_RATE 48000
69
70 namespace android {
71
72 using aidl_utils::statusTFromBinderStatus;
73 using audioflinger::EffectConfiguration;
74 using binder::Status;
75
76 namespace {
77
78 // Append a POD value into a vector of bytes.
79 template<typename T>
appendToBuffer(const T & value,std::vector<uint8_t> * buffer)80 void appendToBuffer(const T& value, std::vector<uint8_t>* buffer) {
81 const uint8_t* ar(reinterpret_cast<const uint8_t*>(&value));
82 buffer->insert(buffer->end(), ar, ar + sizeof(T));
83 }
84
85 // Write a POD value into a vector of bytes (clears the previous buffer
86 // content).
87 template<typename T>
writeToBuffer(const T & value,std::vector<uint8_t> * buffer)88 void writeToBuffer(const T& value, std::vector<uint8_t>* buffer) {
89 buffer->clear();
90 appendToBuffer(value, buffer);
91 }
92
93 } // namespace
94
95 // ----------------------------------------------------------------------------
96 // EffectBase implementation
97 // ----------------------------------------------------------------------------
98
99 #undef LOG_TAG
100 #define LOG_TAG "EffectBase"
101
EffectBase(const sp<EffectCallbackInterface> & callback,effect_descriptor_t * desc,int id,audio_session_t sessionId,bool pinned)102 EffectBase::EffectBase(const sp<EffectCallbackInterface>& callback,
103 effect_descriptor_t *desc,
104 int id,
105 audio_session_t sessionId,
106 bool pinned)
107 : mPinned(pinned),
108 mCallback(callback), mId(id), mSessionId(sessionId),
109 mDescriptor(*desc)
110 {
111 }
112
113 // must be called with EffectModule::mutex() held
setEnabled_l(bool enabled)114 status_t EffectBase::setEnabled_l(bool enabled)
115 {
116
117 ALOGV("setEnabled %p enabled %d", this, enabled);
118
119 if (enabled != isEnabled()) {
120 switch (mState) {
121 // going from disabled to enabled
122 case IDLE:
123 mState = STARTING;
124 break;
125 case STOPPED:
126 mState = RESTART;
127 break;
128 case STOPPING:
129 mState = ACTIVE;
130 break;
131
132 // going from enabled to disabled
133 case RESTART:
134 mState = STOPPED;
135 break;
136 case STARTING:
137 mState = IDLE;
138 break;
139 case ACTIVE:
140 mState = STOPPING;
141 break;
142 case DESTROYED:
143 return NO_ERROR; // simply ignore as we are being destroyed
144 }
145 for (size_t i = 1; i < mHandles.size(); i++) {
146 IAfEffectHandle *h = mHandles[i];
147 if (h != NULL && !h->disconnected()) {
148 h->setEnabled(enabled);
149 }
150 }
151 }
152 return NO_ERROR;
153 }
154
setEnabled(bool enabled,bool fromHandle)155 status_t EffectBase::setEnabled(bool enabled, bool fromHandle)
156 {
157 status_t status;
158 {
159 audio_utils::lock_guard _l(mutex());
160 status = setEnabled_l(enabled);
161 }
162 if (fromHandle) {
163 if (enabled) {
164 if (status != NO_ERROR) {
165 getCallback()->checkSuspendOnEffectEnabled(this, false, false /*threadLocked*/);
166 } else {
167 getCallback()->onEffectEnable(this);
168 }
169 } else {
170 getCallback()->onEffectDisable(this);
171 }
172 }
173 return status;
174 }
175
isEnabled() const176 bool EffectBase::isEnabled() const
177 {
178 switch (mState) {
179 case RESTART:
180 case STARTING:
181 case ACTIVE:
182 return true;
183 case IDLE:
184 case STOPPING:
185 case STOPPED:
186 case DESTROYED:
187 default:
188 return false;
189 }
190 }
191
setSuspended(bool suspended)192 void EffectBase::setSuspended(bool suspended)
193 {
194 audio_utils::lock_guard _l(mutex());
195 mSuspended = suspended;
196 }
197
suspended() const198 bool EffectBase::suspended() const
199 {
200 audio_utils::lock_guard _l(mutex());
201 return mSuspended;
202 }
203
addHandle(IAfEffectHandle * handle)204 status_t EffectBase::addHandle(IAfEffectHandle *handle)
205 {
206 status_t status;
207
208 audio_utils::lock_guard _l(mutex());
209 int priority = handle->priority();
210 size_t size = mHandles.size();
211 IAfEffectHandle *controlHandle = nullptr;
212 size_t i;
213 for (i = 0; i < size; i++) {
214 IAfEffectHandle *h = mHandles[i];
215 if (h == NULL || h->disconnected()) {
216 continue;
217 }
218 // first non destroyed handle is considered in control
219 if (controlHandle == NULL) {
220 controlHandle = h;
221 }
222 if (h->priority() <= priority) {
223 break;
224 }
225 }
226 // if inserted in first place, move effect control from previous owner to this handle
227 if (i == 0) {
228 bool enabled = false;
229 if (controlHandle != NULL) {
230 enabled = controlHandle->enabled();
231 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
232 }
233 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
234 status = NO_ERROR;
235 } else {
236 status = ALREADY_EXISTS;
237 }
238 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
239 mHandles.insertAt(handle, i);
240 return status;
241 }
242
updatePolicyState()243 status_t EffectBase::updatePolicyState()
244 {
245 status_t status = NO_ERROR;
246 bool doRegister = false;
247 bool registered = false;
248 bool doEnable = false;
249 bool enabled = false;
250 audio_io_handle_t io = AUDIO_IO_HANDLE_NONE;
251 product_strategy_t strategy = PRODUCT_STRATEGY_NONE;
252
253 {
254 audio_utils::lock_guard _l(mutex());
255
256 if ((isInternal_l() && !mPolicyRegistered)
257 || !getCallback()->isAudioPolicyReady()) {
258 return NO_ERROR;
259 }
260
261 // register effect when first handle is attached and unregister when last handle is removed
262 if (mPolicyRegistered != mHandles.size() > 0) {
263 doRegister = true;
264 mPolicyRegistered = mHandles.size() > 0;
265 if (mPolicyRegistered) {
266 const auto callback = getCallback();
267 io = callback->io();
268 strategy = callback->strategy();
269 }
270 }
271 // enable effect when registered according to enable state requested by controlling handle
272 if (mHandles.size() > 0) {
273 IAfEffectHandle *handle = controlHandle_l();
274 if (handle != nullptr && mPolicyEnabled != handle->enabled()) {
275 doEnable = true;
276 mPolicyEnabled = handle->enabled();
277 }
278 }
279 registered = mPolicyRegistered;
280 enabled = mPolicyEnabled;
281 // The simultaneous release of two EffectHandles with the same EffectModule
282 // may cause us to call this method at the same time.
283 // This may deadlock under some circumstances (b/180941720). Avoid this.
284 if (!doRegister && !(registered && doEnable)) {
285 return NO_ERROR;
286 }
287 }
288 policyMutex().lock();
289 ALOGV("%s name %s id %d session %d doRegister %d registered %d doEnable %d enabled %d",
290 __func__, mDescriptor.name, mId, mSessionId, doRegister, registered, doEnable, enabled);
291 if (doRegister) {
292 if (registered) {
293 status = AudioSystem::registerEffect(
294 &mDescriptor,
295 io,
296 strategy,
297 mSessionId,
298 mId);
299 } else {
300 status = AudioSystem::unregisterEffect(mId);
301 }
302 }
303 if (registered && doEnable) {
304 status = AudioSystem::setEffectEnabled(mId, enabled);
305 }
306 policyMutex().unlock();
307
308 return status;
309 }
310
311
removeHandle(IAfEffectHandle * handle)312 ssize_t EffectBase::removeHandle(IAfEffectHandle *handle)
313 {
314 audio_utils::lock_guard _l(mutex());
315 return removeHandle_l(handle);
316 }
317
removeHandle_l(IAfEffectHandle * handle)318 ssize_t EffectBase::removeHandle_l(IAfEffectHandle *handle)
319 {
320 size_t size = mHandles.size();
321 size_t i;
322 for (i = 0; i < size; i++) {
323 if (mHandles[i] == handle) {
324 break;
325 }
326 }
327 if (i == size) {
328 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
329 return BAD_VALUE;
330 }
331 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
332
333 mHandles.removeAt(i);
334 // if removed from first place, move effect control from this handle to next in line
335 if (i == 0) {
336 IAfEffectHandle *h = controlHandle_l();
337 if (h != NULL) {
338 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
339 }
340 }
341
342 // Prevent calls to process() and other functions on effect interface from now on.
343 // The effect engine will be released by the destructor when the last strong reference on
344 // this object is released which can happen after next process is called.
345 if (mHandles.size() == 0 && !mPinned) {
346 mState = DESTROYED;
347 }
348
349 return mHandles.size();
350 }
351
352 // must be called with EffectModule::mutex() held
controlHandle_l()353 IAfEffectHandle *EffectBase::controlHandle_l()
354 {
355 // the first valid handle in the list has control over the module
356 for (size_t i = 0; i < mHandles.size(); i++) {
357 IAfEffectHandle *h = mHandles[i];
358 if (h != NULL && !h->disconnected()) {
359 return h;
360 }
361 }
362
363 return NULL;
364 }
365
366 // unsafe method called when the effect parent thread has been destroyed
disconnectHandle(IAfEffectHandle * handle,bool unpinIfLast)367 ssize_t EffectBase::disconnectHandle(IAfEffectHandle *handle, bool unpinIfLast)
368 {
369 const auto callback = getCallback();
370 ALOGV("disconnect() %p handle %p", this, handle);
371 if (callback->disconnectEffectHandle(handle, unpinIfLast)) {
372 return mHandles.size();
373 }
374
375 audio_utils::lock_guard _l(mutex());
376 ssize_t numHandles = removeHandle_l(handle);
377 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
378 mutex().unlock();
379 callback->updateOrphanEffectChains(this);
380 mutex().lock();
381 }
382 return numHandles;
383 }
384
purgeHandles()385 bool EffectBase::purgeHandles()
386 {
387 bool enabled = false;
388 audio_utils::lock_guard _l(mutex());
389 IAfEffectHandle *handle = controlHandle_l();
390 if (handle != NULL) {
391 enabled = handle->enabled();
392 }
393 mHandles.clear();
394 return enabled;
395 }
396
checkSuspendOnEffectEnabled(bool enabled,bool threadLocked)397 void EffectBase::checkSuspendOnEffectEnabled(bool enabled, bool threadLocked) {
398 getCallback()->checkSuspendOnEffectEnabled(this, enabled, threadLocked);
399 }
400
effectFlagsToString(uint32_t flags)401 static String8 effectFlagsToString(uint32_t flags) {
402 String8 s;
403
404 s.append("conn. mode: ");
405 switch (flags & EFFECT_FLAG_TYPE_MASK) {
406 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
407 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
408 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
409 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
410 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
411 default: s.append("unknown/reserved"); break;
412 }
413 s.append(", ");
414
415 s.append("insert pref: ");
416 switch (flags & EFFECT_FLAG_INSERT_MASK) {
417 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
418 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
419 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
420 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
421 default: s.append("unknown/reserved"); break;
422 }
423 s.append(", ");
424
425 s.append("volume mgmt: ");
426 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
427 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
428 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
429 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
430 case EFFECT_FLAG_VOLUME_MONITOR: s.append("monitors volume"); break;
431 default: s.append("unknown/reserved"); break;
432 }
433 s.append(", ");
434
435 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
436 if (devind) {
437 s.append("device indication: ");
438 switch (devind) {
439 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
440 default: s.append("unknown/reserved"); break;
441 }
442 s.append(", ");
443 }
444
445 s.append("input mode: ");
446 switch (flags & EFFECT_FLAG_INPUT_MASK) {
447 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
448 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
449 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
450 default: s.append("not set"); break;
451 }
452 s.append(", ");
453
454 s.append("output mode: ");
455 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
456 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
457 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
458 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
459 default: s.append("not set"); break;
460 }
461 s.append(", ");
462
463 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
464 if (accel) {
465 s.append("hardware acceleration: ");
466 switch (accel) {
467 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
468 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
469 default: s.append("unknown/reserved"); break;
470 }
471 s.append(", ");
472 }
473
474 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
475 if (modeind) {
476 s.append("mode indication: ");
477 switch (modeind) {
478 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
479 default: s.append("unknown/reserved"); break;
480 }
481 s.append(", ");
482 }
483
484 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
485 if (srcind) {
486 s.append("source indication: ");
487 switch (srcind) {
488 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
489 default: s.append("unknown/reserved"); break;
490 }
491 s.append(", ");
492 }
493
494 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
495 s.append("offloadable, ");
496 }
497
498 int len = s.length();
499 if (s.length() > 2) {
500 (void) s.lockBuffer(len);
501 s.unlockBuffer(len - 2);
502 }
503 return s;
504 }
505
dump(int fd,const Vector<String16> & args __unused) const506 void EffectBase::dump(int fd, const Vector<String16>& args __unused) const
507 NO_THREAD_SAFETY_ANALYSIS // conditional try lock
508 {
509 String8 result;
510
511 result.appendFormat("\tEffect ID %d:\n", mId);
512
513 const bool locked = afutils::dumpTryLock(mutex());
514 // failed to lock - AudioFlinger is probably deadlocked
515 if (!locked) {
516 result.append("\t\tCould not lock Fx mutex:\n");
517 }
518 bool isInternal = isInternal_l();
519 result.append("\t\tSession State Registered Internal Enabled Suspended:\n");
520 result.appendFormat("\t\t%05d %03d %s %s %s %s\n",
521 mSessionId, mState, mPolicyRegistered ? "y" : "n", isInternal ? "y" : "n",
522 ((isInternal && isEnabled()) || (!isInternal && mPolicyEnabled)) ? "y" : "n",
523 mSuspended ? "y" : "n");
524
525 result.append("\t\tDescriptor:\n");
526 char uuidStr[64];
527 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
528 result.appendFormat("\t\t- UUID: %s\n", uuidStr);
529 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
530 result.appendFormat("\t\t- TYPE: %s\n", uuidStr);
531 result.appendFormat("\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
532 mDescriptor.apiVersion,
533 mDescriptor.flags,
534 effectFlagsToString(mDescriptor.flags).c_str());
535 result.appendFormat("\t\t- name: %s\n",
536 mDescriptor.name);
537
538 result.appendFormat("\t\t- implementor: %s\n",
539 mDescriptor.implementor);
540
541 result.appendFormat("\t\t%zu Clients:\n", mHandles.size());
542 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
543 char buffer[256];
544 for (size_t i = 0; i < mHandles.size(); ++i) {
545 IAfEffectHandle *handle = mHandles[i];
546 if (handle != NULL && !handle->disconnected()) {
547 handle->dumpToBuffer(buffer, sizeof(buffer));
548 result.append(buffer);
549 }
550 }
551 if (locked) {
552 mutex().unlock();
553 }
554
555 write(fd, result.c_str(), result.length());
556 }
557
558 // ----------------------------------------------------------------------------
559 // EffectModule implementation
560 // ----------------------------------------------------------------------------
561
562 #undef LOG_TAG
563 #define LOG_TAG "EffectModule"
564
EffectModule(const sp<EffectCallbackInterface> & callback,effect_descriptor_t * desc,int id,audio_session_t sessionId,bool pinned,audio_port_handle_t deviceId)565 EffectModule::EffectModule(const sp<EffectCallbackInterface>& callback, effect_descriptor_t* desc,
566 int id, audio_session_t sessionId, bool pinned,
567 audio_port_handle_t deviceId)
568 : EffectBase(callback, desc, id, sessionId, pinned),
569 // clear mConfig to ensure consistent initial value of buffer framecount
570 // in case buffers are associated by setInBuffer() or setOutBuffer()
571 // prior to configure_l().
572 mConfig{{}, {}},
573 mStatus(NO_INIT),
574 mMaxDisableWaitCnt(1), // set by configure_l(), should be >= 1
575 mDisableWaitCnt(0), // set by process() and updateState()
576 mOffloaded(false),
577 mIsOutput(false),
578 mSupportsFloat(false),
579 mEffectInterfaceDebug(desc->name) {
580 ALOGV("Constructor %p pinned %d", this, pinned);
581 int lStatus;
582
583 // create effect engine from effect factory
584 mStatus = callback->createEffectHal(
585 &desc->uuid, sessionId, deviceId, &mEffectInterface);
586 if (mStatus != NO_ERROR) {
587 ALOGE("%s createEffectHal failed: %d", __func__, mStatus);
588 return;
589 }
590 lStatus = init_l();
591 if (lStatus < 0) {
592 mStatus = lStatus;
593 goto Error;
594 }
595
596 setOffloaded_l(callback->isOffload(), callback->io());
597 ALOGV("%s Constructor success name %s, Interface %p", __func__, mDescriptor.name,
598 mEffectInterface.get());
599
600 return;
601 Error:
602 mEffectInterface.clear();
603 mEffectInterfaceDebug += " init failed:" + std::to_string(lStatus);
604 ALOGE("%s Constructor Error %d", __func__, mStatus);
605 }
606
~EffectModule()607 EffectModule::~EffectModule()
608 {
609 ALOGV("Destructor %p", this);
610 if (mEffectInterface != 0) {
611 char uuidStr[64];
612 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
613 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
614 this, uuidStr);
615 release_l("~EffectModule");
616 }
617
618 }
619
620 // return true if any effect started or stopped
updateState_l()621 bool EffectModule::updateState_l() {
622 audio_utils::lock_guard _l(mutex());
623
624 bool startedOrStopped = false;
625 switch (mState) {
626 case RESTART:
627 reset_l();
628 FALLTHROUGH_INTENDED;
629
630 case STARTING:
631 // clear auxiliary effect input buffer for next accumulation
632 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
633 memset(mConfig.inputCfg.buffer.raw,
634 0,
635 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
636 }
637 if (start_ll() == NO_ERROR) {
638 mState = ACTIVE;
639 startedOrStopped = true;
640 } else {
641 mState = IDLE;
642 }
643 break;
644 case STOPPING:
645 // volume control for offload and direct threads must take effect immediately.
646 if (stop_ll() == NO_ERROR
647 && !(isVolumeControl() && isOffloadedOrDirect_l())) {
648 mDisableWaitCnt = mMaxDisableWaitCnt;
649 } else {
650 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
651 }
652 mState = STOPPED;
653 break;
654 case STOPPED:
655 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
656 // turn off sequence.
657 if (--mDisableWaitCnt == 0) {
658 reset_l();
659 startedOrStopped = true;
660 mState = IDLE;
661 }
662 break;
663 case ACTIVE:
664 for (size_t i = 0; i < mHandles.size(); i++) {
665 if (!mHandles[i]->disconnected()) {
666 mHandles[i]->framesProcessed(mConfig.inputCfg.buffer.frameCount);
667 }
668 }
669 break;
670 default: //IDLE , ACTIVE, DESTROYED
671 break;
672 }
673
674 return startedOrStopped;
675 }
676
process()677 void EffectModule::process()
678 {
679 audio_utils::lock_guard _l(mutex());
680
681 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
682 return;
683 }
684
685 const uint32_t inChannelCount =
686 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
687 const uint32_t outChannelCount =
688 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
689 const bool auxType =
690 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
691
692 // safeInputOutputSampleCount is 0 if the channel count between input and output
693 // buffers do not match. This prevents automatic accumulation or copying between the
694 // input and output effect buffers without an intermediary effect process.
695 // TODO: consider implementing channel conversion.
696 const size_t safeInputOutputSampleCount =
697 mInChannelCountRequested != mOutChannelCountRequested ? 0
698 : mOutChannelCountRequested * std::min(
699 mConfig.inputCfg.buffer.frameCount,
700 mConfig.outputCfg.buffer.frameCount);
701 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
702 accumulate_float(
703 mConfig.outputCfg.buffer.f32,
704 mConfig.inputCfg.buffer.f32,
705 safeInputOutputSampleCount);
706 };
707 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
708 memcpy(
709 mConfig.outputCfg.buffer.f32,
710 mConfig.inputCfg.buffer.f32,
711 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
712 };
713
714 if (isProcessEnabled()) {
715 int ret;
716 if (isProcessImplemented()) {
717 if (auxType) {
718 // We overwrite the aux input buffer here and clear after processing.
719 // aux input is always mono.
720
721 if (!mSupportsFloat) {
722 memcpy_to_i16_from_float(
723 mConfig.inputCfg.buffer.s16,
724 mConfig.inputCfg.buffer.f32,
725 mConfig.inputCfg.buffer.frameCount);
726 }
727 }
728 sp<EffectBufferHalInterface> inBuffer = mInBuffer;
729 sp<EffectBufferHalInterface> outBuffer = mOutBuffer;
730
731 if (!auxType && mInChannelCountRequested != inChannelCount) {
732 adjust_channels(
733 inBuffer->audioBuffer()->f32, mInChannelCountRequested,
734 mInConversionBuffer->audioBuffer()->f32, inChannelCount,
735 sizeof(float),
736 sizeof(float)
737 * mInChannelCountRequested * mConfig.inputCfg.buffer.frameCount);
738 inBuffer = mInConversionBuffer;
739 }
740 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE
741 && mOutChannelCountRequested != outChannelCount) {
742 adjust_selected_channels(
743 outBuffer->audioBuffer()->f32, mOutChannelCountRequested,
744 mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
745 sizeof(float),
746 sizeof(float)
747 * mOutChannelCountRequested * mConfig.outputCfg.buffer.frameCount);
748 outBuffer = mOutConversionBuffer;
749 }
750 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
751 if (!auxType) {
752 if (mInConversionBuffer == nullptr) {
753 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
754 goto data_bypass;
755 }
756 memcpy_to_i16_from_float(
757 mInConversionBuffer->audioBuffer()->s16,
758 inBuffer->audioBuffer()->f32,
759 inChannelCount * mConfig.inputCfg.buffer.frameCount);
760 inBuffer = mInConversionBuffer;
761 }
762 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
763 if (mOutConversionBuffer == nullptr) {
764 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
765 goto data_bypass;
766 }
767 memcpy_to_i16_from_float(
768 mOutConversionBuffer->audioBuffer()->s16,
769 outBuffer->audioBuffer()->f32,
770 outChannelCount * mConfig.outputCfg.buffer.frameCount);
771 outBuffer = mOutConversionBuffer;
772 }
773 }
774 ret = mEffectInterface->process();
775 if (!mSupportsFloat) { // convert output int16_t back to float.
776 sp<EffectBufferHalInterface> target =
777 mOutChannelCountRequested != outChannelCount
778 ? mOutConversionBuffer : mOutBuffer;
779
780 memcpy_to_float_from_i16(
781 target->audioBuffer()->f32,
782 mOutConversionBuffer->audioBuffer()->s16,
783 outChannelCount * mConfig.outputCfg.buffer.frameCount);
784 }
785 if (mOutChannelCountRequested != outChannelCount) {
786 adjust_selected_channels(mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
787 mOutBuffer->audioBuffer()->f32, mOutChannelCountRequested,
788 sizeof(float),
789 sizeof(float) * outChannelCount * mConfig.outputCfg.buffer.frameCount);
790 }
791 } else {
792 data_bypass:
793 if (!auxType /* aux effects do not require data bypass */
794 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
795 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
796 accumulateInputToOutput();
797 } else {
798 copyInputToOutput();
799 }
800 }
801 ret = -ENODATA;
802 }
803
804 // force transition to IDLE state when engine is ready
805 if (mState == STOPPED && ret == -ENODATA) {
806 mDisableWaitCnt = 1;
807 }
808
809 // clear auxiliary effect input buffer for next accumulation
810 if (auxType) {
811 const size_t size =
812 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(float);
813 memset(mConfig.inputCfg.buffer.raw, 0, size);
814 }
815 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
816 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
817 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
818 // If an insert effect is idle and input buffer is different from output buffer,
819 // accumulate input onto output
820 if (getCallback()->activeTrackCnt() != 0) {
821 // similar handling with data_bypass above.
822 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
823 accumulateInputToOutput();
824 } else { // EFFECT_BUFFER_ACCESS_WRITE
825 copyInputToOutput();
826 }
827 }
828 }
829 }
830
reset_l()831 void EffectModule::reset_l()
832 {
833 if (mStatus != NO_ERROR || mEffectInterface == 0) {
834 return;
835 }
836
837 int reply = 0;
838 uint32_t replySize = sizeof(reply);
839 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, &replySize, &reply);
840 }
841
configure_l()842 status_t EffectModule::configure_l()
843 {
844 ALOGVV("%s started", __func__);
845 status_t status;
846 uint32_t size;
847 audio_channel_mask_t channelMask;
848 sp<EffectCallbackInterface> callback;
849
850 if (mEffectInterface == 0) {
851 status = NO_INIT;
852 goto exit;
853 }
854
855 // TODO: handle configuration of effects replacing track process
856 // TODO: handle configuration of input (record) SW effects above the HAL,
857 // similar to output EFFECT_FLAG_TYPE_INSERT/REPLACE,
858 // in which case input channel masks should be used here.
859 callback = getCallback();
860 channelMask = callback->inChannelMask(mId);
861 mConfig.inputCfg.channels = channelMask;
862 mConfig.outputCfg.channels = callback->outChannelMask();
863
864 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
865 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_MONO) {
866 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
867 ALOGV("Overriding auxiliary effect input channels %#x as MONO",
868 mConfig.inputCfg.channels);
869 }
870 }
871 if (isHapticGenerator()) {
872 audio_channel_mask_t hapticChannelMask = callback->hapticChannelMask();
873 mConfig.inputCfg.channels |= hapticChannelMask;
874 mConfig.outputCfg.channels |= hapticChannelMask;
875 }
876 mInChannelCountRequested =
877 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
878 mOutChannelCountRequested =
879 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
880
881 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_FLOAT;
882 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_FLOAT;
883
884 // Don't use sample rate for thread if effect isn't offloadable.
885 if (callback->isOffloadOrDirect() && !isOffloaded_l()) {
886 mConfig.inputCfg.samplingRate = DEFAULT_OUTPUT_SAMPLE_RATE;
887 ALOGV("Overriding effect input as 48kHz");
888 } else {
889 mConfig.inputCfg.samplingRate = callback->sampleRate();
890 }
891 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
892 mConfig.inputCfg.bufferProvider.cookie = NULL;
893 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
894 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
895 mConfig.outputCfg.bufferProvider.cookie = NULL;
896 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
897 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
898 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
899 // Insert effect:
900 // - in global sessions (e.g AUDIO_SESSION_OUTPUT_MIX),
901 // always overwrites output buffer: input buffer == output buffer
902 // - in other sessions:
903 // last effect in the chain accumulates in output buffer: input buffer != output buffer
904 // other effect: overwrites output buffer: input buffer == output buffer
905 // Auxiliary effect:
906 // accumulates in output buffer: input buffer != output buffer
907 // Therefore: accumulate <=> input buffer != output buffer
908 mConfig.outputCfg.accessMode = requiredEffectBufferAccessMode();
909 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
910 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
911 mConfig.inputCfg.buffer.frameCount = callback->frameCount();
912 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
913 mIsOutput = callback->isOutput();
914
915 ALOGV("%s %p chain %p buffer %p framecount %zu", __func__, this,
916 callback->chain().promote().get(), mConfig.inputCfg.buffer.raw,
917 mConfig.inputCfg.buffer.frameCount);
918
919 status_t cmdStatus;
920 size = sizeof(int);
921 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
922 sizeof(mConfig),
923 &mConfig,
924 &size,
925 &cmdStatus);
926 if (status == NO_ERROR) {
927 status = cmdStatus;
928 }
929
930 if (status != NO_ERROR &&
931 EffectConfiguration::isHidl() && // only HIDL effects support channel conversion
932 mIsOutput &&
933 (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
934 || mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO)) {
935 // Older effects may require exact STEREO position mask.
936 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
937 && (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
938 ALOGV("Overriding effect input channels %#x as STEREO", mConfig.inputCfg.channels);
939 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
940 }
941 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
942 ALOGV("Overriding effect output channels %#x as STEREO", mConfig.outputCfg.channels);
943 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
944 }
945 size = sizeof(int);
946 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
947 sizeof(mConfig),
948 &mConfig,
949 &size,
950 &cmdStatus);
951 if (status == NO_ERROR) {
952 status = cmdStatus;
953 }
954 }
955
956 if (status == NO_ERROR) {
957 mSupportsFloat = true;
958 }
959
960 // only HIDL effects support integer conversion.
961 if (status != NO_ERROR && EffectConfiguration::isHidl()) {
962 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
963 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
964 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
965 size = sizeof(int);
966 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
967 sizeof(mConfig),
968 &mConfig,
969 &size,
970 &cmdStatus);
971 if (status == NO_ERROR) {
972 status = cmdStatus;
973 }
974 if (status == NO_ERROR) {
975 mSupportsFloat = false;
976 ALOGVV("config worked with 16 bit");
977 } else {
978 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
979 }
980 }
981
982 if (status == NO_ERROR) {
983 // Establish Buffer strategy
984 setInBuffer(mInBuffer);
985 setOutBuffer(mOutBuffer);
986
987 // Update visualizer latency
988 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
989 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
990 effect_param_t *p = (effect_param_t *)buf32;
991
992 p->psize = sizeof(uint32_t);
993 p->vsize = sizeof(uint32_t);
994 size = sizeof(int);
995 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
996
997 uint32_t latency = callback->latency();
998
999 *((int32_t *)p->data + 1)= latency;
1000 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
1001 sizeof(effect_param_t) + 8,
1002 &buf32,
1003 &size,
1004 &cmdStatus);
1005 }
1006 }
1007
1008 // mConfig.outputCfg.buffer.frameCount cannot be zero.
1009 mMaxDisableWaitCnt = (uint32_t)std::max(
1010 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
1011 (uint64_t)mConfig.outputCfg.buffer.frameCount == 0 ? 1
1012 : (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
1013 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount)));
1014
1015 exit:
1016 // TODO: consider clearing mConfig on error.
1017 mStatus = status;
1018 ALOGVV("%s ended", __func__);
1019 return status;
1020 }
1021
init_l()1022 status_t EffectModule::init_l()
1023 {
1024 audio_utils::lock_guard _l(mutex());
1025 if (mEffectInterface == 0) {
1026 return NO_INIT;
1027 }
1028 status_t cmdStatus;
1029 uint32_t size = sizeof(status_t);
1030 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
1031 0,
1032 NULL,
1033 &size,
1034 &cmdStatus);
1035 if (status == 0) {
1036 status = cmdStatus;
1037 }
1038 return status;
1039 }
1040
addEffectToHal_l()1041 void EffectModule::addEffectToHal_l()
1042 {
1043 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1044 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
1045 if (mCurrentHalStream == getCallback()->io()) {
1046 return;
1047 }
1048
1049 (void)getCallback()->addEffectToHal(mEffectInterface);
1050 mCurrentHalStream = getCallback()->io();
1051 }
1052 }
1053
1054 // start_l() must be called with EffectChain::mutex() held
start_l()1055 status_t EffectModule::start_l()
1056 {
1057 status_t status;
1058 {
1059 audio_utils::lock_guard _l(mutex());
1060 status = start_ll();
1061 }
1062 if (status == NO_ERROR) {
1063 getCallback()->resetVolume_l();
1064 }
1065 return status;
1066 }
1067
start_ll()1068 status_t EffectModule::start_ll()
1069 {
1070 if (mEffectInterface == 0) {
1071 return NO_INIT;
1072 }
1073 if (mStatus != NO_ERROR) {
1074 return mStatus;
1075 }
1076 status_t cmdStatus;
1077 uint32_t size = sizeof(status_t);
1078 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
1079 0,
1080 NULL,
1081 &size,
1082 &cmdStatus);
1083 if (status == 0) {
1084 status = cmdStatus;
1085 }
1086 if (status == 0) {
1087 addEffectToHal_l();
1088 }
1089 return status;
1090 }
1091
stop_l()1092 status_t EffectModule::stop_l()
1093 {
1094 audio_utils::lock_guard _l(mutex());
1095 return stop_ll();
1096 }
1097
stop_ll()1098 status_t EffectModule::stop_ll()
1099 {
1100 if (mEffectInterface == 0) {
1101 return NO_INIT;
1102 }
1103 if (mStatus != NO_ERROR) {
1104 return mStatus;
1105 }
1106 status_t cmdStatus = NO_ERROR;
1107 uint32_t size = sizeof(status_t);
1108
1109 if (isVolumeControl() && isOffloadedOrDirect_l()) {
1110 // We have the EffectChain and EffectModule lock, permit a reentrant call to setVolume:
1111 // resetVolume_l --> setVolume_l --> EffectModule::setVolume
1112 mSetVolumeReentrantTid = gettid();
1113 getCallback()->resetVolume_l();
1114 mSetVolumeReentrantTid = INVALID_PID;
1115 }
1116
1117 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
1118 0,
1119 NULL,
1120 &size,
1121 &cmdStatus);
1122 if (status == NO_ERROR) {
1123 status = cmdStatus;
1124 }
1125 if (status == NO_ERROR) {
1126 status = removeEffectFromHal_l();
1127 }
1128 return status;
1129 }
1130
1131 // must be called with EffectChain::mutex() held
release_l(const std::string & from)1132 void EffectModule::release_l(const std::string& from)
1133 {
1134 if (mEffectInterface != 0) {
1135 removeEffectFromHal_l();
1136 // release effect engine
1137 mEffectInterface->close();
1138 mEffectInterface.clear();
1139 mEffectInterfaceDebug += " released by: " + from;
1140 }
1141 }
1142
removeEffectFromHal_l()1143 status_t EffectModule::removeEffectFromHal_l()
1144 {
1145 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1146 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
1147 if (mCurrentHalStream != getCallback()->io()) {
1148 return (mCurrentHalStream == AUDIO_IO_HANDLE_NONE) ? NO_ERROR : INVALID_OPERATION;
1149 }
1150 getCallback()->removeEffectFromHal(mEffectInterface);
1151 mCurrentHalStream = AUDIO_IO_HANDLE_NONE;
1152 }
1153 return NO_ERROR;
1154 }
1155
1156 // round up delta valid if value and divisor are positive.
1157 template <typename T>
roundUpDelta(const T & value,const T & divisor)1158 static T roundUpDelta(const T &value, const T &divisor) {
1159 T remainder = value % divisor;
1160 return remainder == 0 ? 0 : divisor - remainder;
1161 }
1162
command(int32_t cmdCode,const std::vector<uint8_t> & cmdData,int32_t maxReplySize,std::vector<uint8_t> * reply)1163 status_t EffectModule::command(int32_t cmdCode,
1164 const std::vector<uint8_t>& cmdData,
1165 int32_t maxReplySize,
1166 std::vector<uint8_t>* reply)
1167 {
1168 audio_utils::lock_guard _l(mutex());
1169 ALOGVV("%s, cmdCode: %d, mEffectInterface: %p", __func__, cmdCode, mEffectInterface.get());
1170
1171 if (mState == DESTROYED || mEffectInterface == 0) {
1172 return NO_INIT;
1173 }
1174 if (mStatus != NO_ERROR) {
1175 return mStatus;
1176 }
1177 if (maxReplySize < 0 || maxReplySize > EFFECT_PARAM_SIZE_MAX) {
1178 return -EINVAL;
1179 }
1180 size_t cmdSize = cmdData.size();
1181 const effect_param_t* param = cmdSize >= sizeof(effect_param_t)
1182 ? reinterpret_cast<const effect_param_t*>(cmdData.data())
1183 : nullptr;
1184 if (cmdCode == EFFECT_CMD_GET_PARAM &&
1185 (param == nullptr || param->psize > cmdSize - sizeof(effect_param_t))) {
1186 android_errorWriteLog(0x534e4554, "32438594");
1187 android_errorWriteLog(0x534e4554, "33003822");
1188 return -EINVAL;
1189 }
1190 if (cmdCode == EFFECT_CMD_GET_PARAM &&
1191 (maxReplySize < static_cast<signed>(sizeof(effect_param_t)) ||
1192 param->psize > maxReplySize - sizeof(effect_param_t))) {
1193 android_errorWriteLog(0x534e4554, "29251553");
1194 return -EINVAL;
1195 }
1196 if (cmdCode == EFFECT_CMD_GET_PARAM &&
1197 (static_cast<signed>(sizeof(effect_param_t)) > maxReplySize
1198 || param->psize > maxReplySize - sizeof(effect_param_t)
1199 || param->vsize > maxReplySize - sizeof(effect_param_t)
1200 - param->psize
1201 || roundUpDelta(param->psize, (uint32_t) sizeof(int)) >
1202 maxReplySize
1203 - sizeof(effect_param_t)
1204 - param->psize
1205 - param->vsize)) {
1206 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
1207 android_errorWriteLog(0x534e4554, "32705438");
1208 return -EINVAL;
1209 }
1210 if ((cmdCode == EFFECT_CMD_SET_PARAM
1211 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED)
1212 && // DEFERRED not generally used
1213 (param == nullptr
1214 || param->psize > cmdSize - sizeof(effect_param_t)
1215 || param->vsize > cmdSize - sizeof(effect_param_t)
1216 - param->psize
1217 || roundUpDelta(param->psize,
1218 (uint32_t) sizeof(int)) >
1219 cmdSize
1220 - sizeof(effect_param_t)
1221 - param->psize
1222 - param->vsize)) {
1223 android_errorWriteLog(0x534e4554, "30204301");
1224 return -EINVAL;
1225 }
1226 uint32_t replySize = maxReplySize;
1227 reply->resize(replySize);
1228 status_t status = mEffectInterface->command(cmdCode,
1229 cmdSize,
1230 const_cast<uint8_t*>(cmdData.data()),
1231 &replySize,
1232 reply->data());
1233 reply->resize(status == NO_ERROR ? replySize : 0);
1234 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
1235 for (size_t i = 1; i < mHandles.size(); i++) {
1236 IAfEffectHandle *h = mHandles[i];
1237 if (h != NULL && !h->disconnected()) {
1238 h->commandExecuted(cmdCode, cmdData, *reply);
1239 }
1240 }
1241 }
1242 return status;
1243 }
1244
isProcessEnabled() const1245 bool EffectModule::isProcessEnabled() const
1246 {
1247 if (mStatus != NO_ERROR) {
1248 return false;
1249 }
1250
1251 switch (mState) {
1252 case RESTART:
1253 case ACTIVE:
1254 case STOPPING:
1255 case STOPPED:
1256 return true;
1257 case IDLE:
1258 case STARTING:
1259 case DESTROYED:
1260 default:
1261 return false;
1262 }
1263 }
1264
isOffloadedOrDirect_l() const1265 bool EffectModule::isOffloadedOrDirect_l() const
1266 {
1267 return getCallback()->isOffloadOrDirect();
1268 }
1269
isVolumeControlEnabled_l() const1270 bool EffectModule::isVolumeControlEnabled_l() const
1271 {
1272 return (isVolumeControl() && (isOffloadedOrDirect_l() ? isEnabled() : isProcessEnabled()));
1273 }
1274
setInBuffer(const sp<EffectBufferHalInterface> & buffer)1275 void EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
1276 ALOGVV("setInBuffer %p",(&buffer));
1277
1278 // mConfig.inputCfg.buffer.frameCount may be zero if configure_l() is not called yet.
1279 if (buffer != 0) {
1280 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
1281 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
1282 } else {
1283 mConfig.inputCfg.buffer.raw = NULL;
1284 }
1285 mInBuffer = buffer;
1286 mEffectInterface->setInBuffer(buffer);
1287
1288 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
1289 // Theoretically insert effects can also do in-place conversions (destroying
1290 // the original buffer) when the output buffer is identical to the input buffer,
1291 // but we don't optimize for it here.
1292 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
1293 const uint32_t inChannelCount =
1294 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
1295 const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount;
1296 if (!auxType && formatMismatch && mInBuffer != nullptr) {
1297 // we need to translate - create hidl shared buffer and intercept
1298 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
1299 // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo.
1300 const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested);
1301 const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float));
1302
1303 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
1304 __func__, inChannels, inFrameCount, size);
1305
1306 if (size > 0 && (mInConversionBuffer == nullptr
1307 || size > mInConversionBuffer->getSize())) {
1308 mInConversionBuffer.clear();
1309 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
1310 (void)getCallback()->allocateHalBuffer(size, &mInConversionBuffer);
1311 }
1312 if (mInConversionBuffer != nullptr) {
1313 mInConversionBuffer->setFrameCount(inFrameCount);
1314 mEffectInterface->setInBuffer(mInConversionBuffer);
1315 } else if (size > 0) {
1316 ALOGE("%s cannot create mInConversionBuffer", __func__);
1317 }
1318 }
1319 }
1320
setOutBuffer(const sp<EffectBufferHalInterface> & buffer)1321 void EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
1322 ALOGVV("setOutBuffer %p",(&buffer));
1323
1324 // mConfig.outputCfg.buffer.frameCount may be zero if configure_l() is not called yet.
1325 if (buffer != 0) {
1326 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1327 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1328 } else {
1329 mConfig.outputCfg.buffer.raw = NULL;
1330 }
1331 mOutBuffer = buffer;
1332 mEffectInterface->setOutBuffer(buffer);
1333
1334 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
1335 // can do in-place conversion from int16_t to float. We don't optimize here.
1336 const uint32_t outChannelCount =
1337 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1338 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
1339 if (formatMismatch && mOutBuffer != nullptr) {
1340 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
1341 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1342 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1343 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
1344
1345 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1346 __func__, outChannels, outFrameCount, size);
1347
1348 if (size > 0 && (mOutConversionBuffer == nullptr
1349 || size > mOutConversionBuffer->getSize())) {
1350 mOutConversionBuffer.clear();
1351 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
1352 (void)getCallback()->allocateHalBuffer(size, &mOutConversionBuffer);
1353 }
1354 if (mOutConversionBuffer != nullptr) {
1355 mOutConversionBuffer->setFrameCount(outFrameCount);
1356 mEffectInterface->setOutBuffer(mOutConversionBuffer);
1357 } else if (size > 0) {
1358 ALOGE("%s cannot create mOutConversionBuffer", __func__);
1359 }
1360 }
1361 }
1362
setVolume_l(uint32_t * left,uint32_t * right,bool controller,bool force)1363 status_t EffectModule::setVolume_l(uint32_t* left, uint32_t* right, bool controller, bool force) {
1364 AutoLockReentrant _l(mutex(), mSetVolumeReentrantTid);
1365 if (mStatus != NO_ERROR) {
1366 return mStatus;
1367 }
1368 status_t status = NO_ERROR;
1369 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1370 // if controller flag is set (Note that controller == TRUE => the volume controller effect in
1371 // the effect chain)
1372 if (((isOffloadedOrDirect_l() ? isEnabled() : isProcessEnabled()) || force) &&
1373 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
1374 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND ||
1375 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_MONITOR)) {
1376 status = setVolumeInternal(left, right, controller);
1377 }
1378 return status;
1379 }
1380
setVolumeInternal(uint32_t * left,uint32_t * right,bool controller)1381 status_t EffectModule::setVolumeInternal(
1382 uint32_t *left, uint32_t *right, bool controller) {
1383 if (mVolume.has_value() && *left == mVolume.value()[0] && *right == mVolume.value()[1] &&
1384 !controller) {
1385 LOG_ALWAYS_FATAL_IF(
1386 !mReturnedVolume.has_value(),
1387 "The cached returned volume must not be null when the cached volume has value");
1388 *left = mReturnedVolume.value()[0];
1389 *right = mReturnedVolume.value()[1];
1390 return NO_ERROR;
1391 }
1392 LOG_ALWAYS_FATAL_IF(mEffectInterface == nullptr, "%s", mEffectInterfaceDebug.c_str());
1393 uint32_t volume[2] = {*left, *right};
1394 uint32_t* pVolume = isVolumeControl() ? volume : nullptr;
1395 uint32_t size = sizeof(volume);
1396 status_t status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1397 size,
1398 volume,
1399 &size,
1400 pVolume);
1401 if (pVolume && status == NO_ERROR && size == sizeof(volume)) {
1402 mVolume = {*left, *right}; // Cache the value that has been set
1403 *left = volume[0];
1404 *right = volume[1];
1405 mReturnedVolume = {*left, *right};
1406 }
1407 return status;
1408 }
1409
setVolumeForOutput_l(uint32_t left,uint32_t right)1410 void EffectChain::setVolumeForOutput_l(uint32_t left, uint32_t right)
1411 {
1412 // for offload or direct thread, if the effect chain has non-offloadable
1413 // effect and any effect module within the chain has volume control, then
1414 // volume control is delegated to effect, otherwise, set volume to hal.
1415 if (mEffectCallback->isOffloadOrDirect() &&
1416 !(isNonOffloadableEnabled_l() && hasVolumeControlEnabled_l())) {
1417 float vol_l = (float)left / (1 << 24);
1418 float vol_r = (float)right / (1 << 24);
1419 mEffectCallback->setVolumeForOutput(vol_l, vol_r);
1420 }
1421 }
1422
sendSetAudioDevicesCommand(const AudioDeviceTypeAddrVector & devices,uint32_t cmdCode)1423 status_t EffectModule::sendSetAudioDevicesCommand(
1424 const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode)
1425 {
1426 audio_devices_t deviceType = deviceTypesToBitMask(getAudioDeviceTypes(devices));
1427 if (deviceType == AUDIO_DEVICE_NONE) {
1428 return NO_ERROR;
1429 }
1430
1431 audio_utils::lock_guard _l(mutex());
1432 if (mStatus != NO_ERROR) {
1433 return mStatus;
1434 }
1435 status_t status = NO_ERROR;
1436 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
1437 status_t cmdStatus;
1438 uint32_t size = sizeof(status_t);
1439 // FIXME: use audio device types and addresses when the hal interface is ready.
1440 status = mEffectInterface->command(cmdCode,
1441 sizeof(uint32_t),
1442 &deviceType,
1443 &size,
1444 &cmdStatus);
1445 }
1446 return status;
1447 }
1448
setDevices(const AudioDeviceTypeAddrVector & devices)1449 status_t EffectModule::setDevices(const AudioDeviceTypeAddrVector &devices)
1450 {
1451 return sendSetAudioDevicesCommand(devices, EFFECT_CMD_SET_DEVICE);
1452 }
1453
setInputDevice(const AudioDeviceTypeAddr & device)1454 status_t EffectModule::setInputDevice(const AudioDeviceTypeAddr &device)
1455 {
1456 return sendSetAudioDevicesCommand({device}, EFFECT_CMD_SET_INPUT_DEVICE);
1457 }
1458
setMode(audio_mode_t mode)1459 status_t EffectModule::setMode(audio_mode_t mode)
1460 {
1461 audio_utils::lock_guard _l(mutex());
1462 if (mStatus != NO_ERROR) {
1463 return mStatus;
1464 }
1465 status_t status = NO_ERROR;
1466 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1467 status_t cmdStatus;
1468 uint32_t size = sizeof(status_t);
1469 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1470 sizeof(audio_mode_t),
1471 &mode,
1472 &size,
1473 &cmdStatus);
1474 if (status == NO_ERROR) {
1475 status = cmdStatus;
1476 }
1477 }
1478 return status;
1479 }
1480
setAudioSource(audio_source_t source)1481 status_t EffectModule::setAudioSource(audio_source_t source)
1482 {
1483 audio_utils::lock_guard _l(mutex());
1484 if (mStatus != NO_ERROR) {
1485 return mStatus;
1486 }
1487 status_t status = NO_ERROR;
1488 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1489 uint32_t size = 0;
1490 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1491 sizeof(audio_source_t),
1492 &source,
1493 &size,
1494 NULL);
1495 }
1496 return status;
1497 }
1498
setOffloaded_l(bool offloaded,audio_io_handle_t io)1499 status_t EffectModule::setOffloaded_l(bool offloaded, audio_io_handle_t io)
1500 {
1501 audio_utils::lock_guard _l(mutex());
1502 if (mStatus != NO_ERROR) {
1503 return mStatus;
1504 }
1505 status_t status = NO_ERROR;
1506 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1507 status_t cmdStatus;
1508 uint32_t size = sizeof(status_t);
1509 effect_offload_param_t cmd;
1510
1511 cmd.isOffload = offloaded;
1512 cmd.ioHandle = io;
1513 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1514 sizeof(effect_offload_param_t),
1515 &cmd,
1516 &size,
1517 &cmdStatus);
1518 if (status == NO_ERROR) {
1519 status = cmdStatus;
1520 }
1521 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1522 } else {
1523 if (offloaded) {
1524 status = INVALID_OPERATION;
1525 }
1526 mOffloaded = false;
1527 }
1528 ALOGV("%s offloaded %d io %d status %d", __func__, offloaded, io, status);
1529 return status;
1530 }
1531
isOffloaded_l() const1532 bool EffectModule::isOffloaded_l() const
1533 {
1534 audio_utils::lock_guard _l(mutex());
1535 return mOffloaded;
1536 }
1537
1538 /*static*/
isHapticGenerator(const effect_uuid_t * type)1539 bool IAfEffectModule::isHapticGenerator(const effect_uuid_t *type) {
1540 return memcmp(type, FX_IID_HAPTICGENERATOR, sizeof(effect_uuid_t)) == 0;
1541 }
1542
isHapticGenerator() const1543 bool EffectModule::isHapticGenerator() const {
1544 return IAfEffectModule::isHapticGenerator(&mDescriptor.type);
1545 }
1546
1547 /*static*/
isSpatializer(const effect_uuid_t * type)1548 bool IAfEffectModule::isSpatializer(const effect_uuid_t *type) {
1549 return memcmp(type, FX_IID_SPATIALIZER, sizeof(effect_uuid_t)) == 0;
1550 }
1551
isSpatializer() const1552 bool EffectModule::isSpatializer() const {
1553 return IAfEffectModule::isSpatializer(&mDescriptor.type);
1554 }
1555
setHapticScale_l(int id,os::HapticScale hapticScale)1556 status_t EffectModule::setHapticScale_l(int id, os::HapticScale hapticScale) {
1557 if (mStatus != NO_ERROR) {
1558 return mStatus;
1559 }
1560 if (!isHapticGenerator()) {
1561 ALOGW("Should not set haptic intensity for effects that are not HapticGenerator");
1562 return INVALID_OPERATION;
1563 }
1564
1565 std::vector<uint8_t> request(sizeof(effect_param_t) + 3 * sizeof(uint32_t) + sizeof(float));
1566 effect_param_t *param = (effect_param_t*) request.data();
1567 param->psize = sizeof(int32_t);
1568 param->vsize = sizeof(int32_t) * 2 + sizeof(float);
1569 *(int32_t*)param->data = HG_PARAM_HAPTIC_INTENSITY;
1570 int32_t* hapticScalePtr = reinterpret_cast<int32_t*>(param->data + sizeof(int32_t));
1571 hapticScalePtr[0] = id;
1572 hapticScalePtr[1] = static_cast<int32_t>(hapticScale.getLevel());
1573 float* adaptiveScaleFactorPtr = reinterpret_cast<float*>(param->data + 3 * sizeof(int32_t));
1574 *adaptiveScaleFactorPtr = hapticScale.getAdaptiveScaleFactor();
1575 std::vector<uint8_t> response;
1576 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
1577 if (status == NO_ERROR) {
1578 LOG_ALWAYS_FATAL_IF(response.size() != 4);
1579 status = *reinterpret_cast<const status_t*>(response.data());
1580 }
1581 return status;
1582 }
1583
setVibratorInfo_l(const media::AudioVibratorInfo & vibratorInfo)1584 status_t EffectModule::setVibratorInfo_l(const media::AudioVibratorInfo& vibratorInfo) {
1585 if (mStatus != NO_ERROR) {
1586 return mStatus;
1587 }
1588 if (!isHapticGenerator()) {
1589 ALOGW("Should not set vibrator info for effects that are not HapticGenerator");
1590 return INVALID_OPERATION;
1591 }
1592
1593 const size_t paramCount = 3;
1594 std::vector<uint8_t> request(
1595 sizeof(effect_param_t) + sizeof(int32_t) + paramCount * sizeof(float));
1596 effect_param_t *param = (effect_param_t*) request.data();
1597 param->psize = sizeof(int32_t);
1598 param->vsize = paramCount * sizeof(float);
1599 *(int32_t*)param->data = HG_PARAM_VIBRATOR_INFO;
1600 float* vibratorInfoPtr = reinterpret_cast<float*>(param->data + sizeof(int32_t));
1601 vibratorInfoPtr[0] = vibratorInfo.resonantFrequency;
1602 vibratorInfoPtr[1] = vibratorInfo.qFactor;
1603 vibratorInfoPtr[2] = vibratorInfo.maxAmplitude;
1604 std::vector<uint8_t> response;
1605 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
1606 if (status == NO_ERROR) {
1607 LOG_ALWAYS_FATAL_IF(response.size() != sizeof(status_t));
1608 status = *reinterpret_cast<const status_t*>(response.data());
1609 }
1610 return status;
1611 }
1612
getConfigs_l(audio_config_base_t * inputCfg,audio_config_base_t * outputCfg,bool * isOutput) const1613 status_t EffectModule::getConfigs_l(audio_config_base_t* inputCfg, audio_config_base_t* outputCfg,
1614 bool* isOutput) const {
1615 audio_utils::lock_guard _l(mutex());
1616 if (mConfig.inputCfg.mask == 0 || mConfig.outputCfg.mask == 0) {
1617 return NO_INIT;
1618 }
1619 inputCfg->sample_rate = mConfig.inputCfg.samplingRate;
1620 inputCfg->channel_mask = static_cast<audio_channel_mask_t>(mConfig.inputCfg.channels);
1621 inputCfg->format = static_cast<audio_format_t>(mConfig.inputCfg.format);
1622 outputCfg->sample_rate = mConfig.outputCfg.samplingRate;
1623 outputCfg->channel_mask = static_cast<audio_channel_mask_t>(mConfig.outputCfg.channels);
1624 outputCfg->format = static_cast<audio_format_t>(mConfig.outputCfg.format);
1625 *isOutput = mIsOutput;
1626 return NO_ERROR;
1627 }
1628
sendMetadata_ll(const std::vector<playback_track_metadata_v7_t> & metadata)1629 status_t EffectModule::sendMetadata_ll(const std::vector<playback_track_metadata_v7_t>& metadata) {
1630 if (mStatus != NO_ERROR) {
1631 return mStatus;
1632 }
1633 // TODO b/307368176: send all metadata to effects if requested by the implementation.
1634 // For now only send channel mask to Spatializer.
1635 if (!isSpatializer()) {
1636 return INVALID_OPERATION;
1637 }
1638
1639 std::vector<uint8_t> request(
1640 sizeof(effect_param_t) + sizeof(int32_t) + metadata.size() * sizeof(uint32_t));
1641 effect_param_t *param = (effect_param_t*) request.data();
1642 param->psize = sizeof(int32_t);
1643 param->vsize = metadata.size() * sizeof(uint32_t);
1644 *(int32_t*)param->data = SPATIALIZER_PARAM_INPUT_CHANNEL_MASK;
1645 uint32_t* channelMasks = reinterpret_cast<uint32_t*>(param->data + sizeof(int32_t));
1646 for (auto m : metadata) {
1647 *channelMasks++ = m.channel_mask;
1648 }
1649 std::vector<uint8_t> response;
1650 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
1651 if (status == NO_ERROR) {
1652 LOG_ALWAYS_FATAL_IF(response.size() != sizeof(status_t));
1653 status = *reinterpret_cast<const status_t*>(response.data());
1654 }
1655 return status;
1656 }
1657
dumpInOutBuffer(bool isInput,const sp<EffectBufferHalInterface> & buffer)1658 static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1659 std::stringstream ss;
1660
1661 if (buffer == nullptr) {
1662 return "nullptr"; // make different than below
1663 } else if (buffer->externalData() != nullptr) {
1664 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1665 << " -> "
1666 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1667 } else {
1668 ss << buffer->audioBuffer()->raw;
1669 }
1670 return ss.str();
1671 }
1672
dump(int fd,const Vector<String16> & args) const1673 void EffectModule::dump(int fd, const Vector<String16>& args) const
1674 NO_THREAD_SAFETY_ANALYSIS // conditional try lock
1675 {
1676 EffectBase::dump(fd, args);
1677
1678 String8 result;
1679 const bool locked = afutils::dumpTryLock(mutex());
1680
1681 result.append("\t\tStatus Engine:\n");
1682 result.appendFormat("\t\t%03d %p\n",
1683 mStatus, mEffectInterface.get());
1684
1685 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
1686
1687 result.append("\t\t- Input configuration:\n");
1688 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1689 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1690 mConfig.inputCfg.buffer.raw,
1691 mConfig.inputCfg.buffer.frameCount,
1692 mConfig.inputCfg.samplingRate,
1693 mConfig.inputCfg.channels,
1694 mConfig.inputCfg.format,
1695 toString(static_cast<audio_format_t>(mConfig.inputCfg.format)).c_str());
1696
1697 result.append("\t\t- Output configuration:\n");
1698 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1699 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1700 mConfig.outputCfg.buffer.raw,
1701 mConfig.outputCfg.buffer.frameCount,
1702 mConfig.outputCfg.samplingRate,
1703 mConfig.outputCfg.channels,
1704 mConfig.outputCfg.format,
1705 toString(static_cast<audio_format_t>(mConfig.outputCfg.format)).c_str());
1706
1707 result.appendFormat("\t\t- HAL buffers:\n"
1708 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1709 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1710 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1711 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1712 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
1713
1714 write(fd, result.c_str(), result.length());
1715
1716 if (mEffectInterface != 0) {
1717 dprintf(fd, "\tEffect ID %d HAL dump:\n", mId);
1718 (void)mEffectInterface->dump(fd);
1719 }
1720
1721 if (locked) {
1722 mutex().unlock();
1723 }
1724 }
1725
1726 // ----------------------------------------------------------------------------
1727 // EffectHandle implementation
1728 // ----------------------------------------------------------------------------
1729
1730 #undef LOG_TAG
1731 #define LOG_TAG "EffectHandle"
1732
1733 /* static */
create(const sp<IAfEffectBase> & effect,const sp<Client> & client,const sp<media::IEffectClient> & effectClient,int32_t priority,bool notifyFramesProcessed)1734 sp<IAfEffectHandle> IAfEffectHandle::create(
1735 const sp<IAfEffectBase>& effect,
1736 const sp<Client>& client,
1737 const sp<media::IEffectClient>& effectClient,
1738 int32_t priority, bool notifyFramesProcessed)
1739 {
1740 return sp<EffectHandle>::make(
1741 effect, client, effectClient, priority, notifyFramesProcessed);
1742 }
1743
EffectHandle(const sp<IAfEffectBase> & effect,const sp<Client> & client,const sp<media::IEffectClient> & effectClient,int32_t priority,bool notifyFramesProcessed)1744 EffectHandle::EffectHandle(const sp<IAfEffectBase>& effect,
1745 const sp<Client>& client,
1746 const sp<media::IEffectClient>& effectClient,
1747 int32_t priority, bool notifyFramesProcessed)
1748 : BnEffect(),
1749 mEffect(effect), mEffectClient(media::EffectClientAsyncProxy::makeIfNeeded(effectClient)),
1750 mClient(client), mCblk(nullptr),
1751 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false),
1752 mNotifyFramesProcessed(notifyFramesProcessed)
1753 {
1754 ALOGV("constructor %p client %p", this, client.get());
1755 setMinSchedulerPolicy(SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
1756 setInheritRt(true);
1757
1758 if (client == 0) {
1759 return;
1760 }
1761 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1762 mCblkMemory = client->allocator().allocate(mediautils::NamedAllocRequest{
1763 {static_cast<size_t>(EFFECT_PARAM_BUFFER_SIZE + bufOffset)},
1764 std::string("Effect ID: ")
1765 .append(std::to_string(effect->id()))
1766 .append(" Session ID: ")
1767 .append(std::to_string(static_cast<int>(effect->sessionId())))
1768 .append(" \n")
1769 });
1770 if (mCblkMemory == 0 ||
1771 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->unsecurePointer())) == NULL) {
1772 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
1773 sizeof(effect_param_cblk_t));
1774 mCblkMemory.clear();
1775 return;
1776 }
1777 new(mCblk) effect_param_cblk_t();
1778 mBuffer = (uint8_t *)mCblk + bufOffset;
1779 }
1780
~EffectHandle()1781 EffectHandle::~EffectHandle()
1782 {
1783 ALOGV("Destructor %p", this);
1784 disconnect(false);
1785 }
1786
1787 // Creates an association between Binder code to name for IEffect.
1788 #define IEFFECT_BINDER_METHOD_MACRO_LIST \
1789 BINDER_METHOD_ENTRY(enable) \
1790 BINDER_METHOD_ENTRY(disable) \
1791 BINDER_METHOD_ENTRY(command) \
1792 BINDER_METHOD_ENTRY(disconnect) \
1793 BINDER_METHOD_ENTRY(getCblk) \
1794 BINDER_METHOD_ENTRY(getConfig) \
1795
1796 // singleton for Binder Method Statistics for IEffect
getIEffectStatistics()1797 mediautils::MethodStatistics<int>& getIEffectStatistics() {
1798 using Code = int;
1799
1800 #pragma push_macro("BINDER_METHOD_ENTRY")
1801 #undef BINDER_METHOD_ENTRY
1802 #define BINDER_METHOD_ENTRY(ENTRY) \
1803 {(Code)media::BnEffect::TRANSACTION_##ENTRY, #ENTRY},
1804
1805 static mediautils::MethodStatistics<Code> methodStatistics{
1806 IEFFECT_BINDER_METHOD_MACRO_LIST
1807 METHOD_STATISTICS_BINDER_CODE_NAMES(Code)
1808 };
1809 #pragma pop_macro("BINDER_METHOD_ENTRY")
1810
1811 return methodStatistics;
1812 }
1813
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)1814 status_t EffectHandle::onTransact(
1815 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
1816 const std::string methodName = getIEffectStatistics().getMethodForCode(code);
1817 mediautils::TimeCheck check(
1818 std::string("IEffect::").append(methodName),
1819 [code](bool timeout, float elapsedMs) {
1820 if (timeout) {
1821 ; // we don't timeout right now on the effect interface.
1822 } else {
1823 getIEffectStatistics().event(code, elapsedMs);
1824 }
1825 }, {} /* timeoutDuration */, {} /* secondChanceDuration */, false /* crashOnTimeout */);
1826 return BnEffect::onTransact(code, data, reply, flags);
1827 }
1828
initCheck() const1829 status_t EffectHandle::initCheck() const
1830 {
1831 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1832 }
1833
1834 #define RETURN(code) \
1835 *_aidl_return = (code); \
1836 return Status::ok();
1837
1838 #define VALUE_OR_RETURN_STATUS_AS_OUT(exp) \
1839 ({ \
1840 auto _tmp = (exp); \
1841 if (!_tmp.ok()) { RETURN(_tmp.error()); } \
1842 std::move(_tmp.value()); \
1843 })
1844
enable(int32_t * _aidl_return)1845 Status EffectHandle::enable(int32_t* _aidl_return)
1846 {
1847 audio_utils::lock_guard _l(mutex());
1848 ALOGV("enable %p", this);
1849 sp<IAfEffectBase> effect = mEffect.promote();
1850 if (effect == 0 || mDisconnected) {
1851 RETURN(DEAD_OBJECT);
1852 }
1853 if (!mHasControl) {
1854 RETURN(INVALID_OPERATION);
1855 }
1856
1857 if (mEnabled) {
1858 RETURN(NO_ERROR);
1859 }
1860
1861 mEnabled = true;
1862
1863 status_t status = effect->updatePolicyState();
1864 if (status != NO_ERROR) {
1865 mEnabled = false;
1866 RETURN(status);
1867 }
1868
1869 effect->checkSuspendOnEffectEnabled(true, false /*threadLocked*/);
1870
1871 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
1872 if (effect->suspended()) {
1873 RETURN(NO_ERROR);
1874 }
1875
1876 status = effect->setEnabled(true, true /*fromHandle*/);
1877 if (status != NO_ERROR) {
1878 mEnabled = false;
1879 }
1880 RETURN(status);
1881 }
1882
disable(int32_t * _aidl_return)1883 Status EffectHandle::disable(int32_t* _aidl_return)
1884 {
1885 ALOGV("disable %p", this);
1886 audio_utils::lock_guard _l(mutex());
1887 sp<IAfEffectBase> effect = mEffect.promote();
1888 if (effect == 0 || mDisconnected) {
1889 RETURN(DEAD_OBJECT);
1890 }
1891 if (!mHasControl) {
1892 RETURN(INVALID_OPERATION);
1893 }
1894
1895 if (!mEnabled) {
1896 RETURN(NO_ERROR);
1897 }
1898 mEnabled = false;
1899
1900 effect->updatePolicyState();
1901
1902 if (effect->suspended()) {
1903 RETURN(NO_ERROR);
1904 }
1905
1906 status_t status = effect->setEnabled(false, true /*fromHandle*/);
1907 RETURN(status);
1908 }
1909
disconnect()1910 Status EffectHandle::disconnect()
1911 {
1912 ALOGV("%s %p", __FUNCTION__, this);
1913 disconnect(true);
1914 return Status::ok();
1915 }
1916
disconnect(bool unpinIfLast)1917 void EffectHandle::disconnect(bool unpinIfLast)
1918 {
1919 audio_utils::lock_guard _l(mutex());
1920 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1921 if (mDisconnected) {
1922 if (unpinIfLast) {
1923 android_errorWriteLog(0x534e4554, "32707507");
1924 }
1925 return;
1926 }
1927 mDisconnected = true;
1928 {
1929 sp<IAfEffectBase> effect = mEffect.promote();
1930 if (effect != 0) {
1931 if (effect->disconnectHandle(this, unpinIfLast) > 0) {
1932 ALOGW("%s Effect handle %p disconnected after thread destruction",
1933 __func__, this);
1934 }
1935 effect->updatePolicyState();
1936 }
1937 }
1938
1939 if (mClient != 0) {
1940 if (mCblk != NULL) {
1941 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1942 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1943 }
1944 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
1945 // Client destructor must run with AudioFlinger client mutex locked
1946 audio_utils::lock_guard _l2(mClient->afClientCallback()->clientMutex());
1947 mClient.clear();
1948 }
1949 }
1950
getCblk(media::SharedFileRegion * _aidl_return)1951 Status EffectHandle::getCblk(media::SharedFileRegion* _aidl_return) {
1952 LOG_ALWAYS_FATAL_IF(!convertIMemoryToSharedFileRegion(mCblkMemory, _aidl_return));
1953 return Status::ok();
1954 }
1955
getConfig(media::EffectConfig * _config,int32_t * _aidl_return)1956 Status EffectHandle::getConfig(
1957 media::EffectConfig* _config, int32_t* _aidl_return) {
1958 audio_utils::lock_guard _l(mutex());
1959 sp<IAfEffectBase> effect = mEffect.promote();
1960 if (effect == nullptr || mDisconnected) {
1961 RETURN(DEAD_OBJECT);
1962 }
1963 sp<IAfEffectModule> effectModule = effect->asEffectModule();
1964 if (effectModule == nullptr) {
1965 RETURN(INVALID_OPERATION);
1966 }
1967 audio_config_base_t inputCfg = AUDIO_CONFIG_BASE_INITIALIZER;
1968 audio_config_base_t outputCfg = AUDIO_CONFIG_BASE_INITIALIZER;
1969 bool isOutput;
1970 status_t status = effectModule->getConfigs_l(&inputCfg, &outputCfg, &isOutput);
1971 if (status == NO_ERROR) {
1972 constexpr bool isInput = false; // effects always use 'OUT' channel masks.
1973 _config->inputCfg = VALUE_OR_RETURN_STATUS_AS_OUT(
1974 legacy2aidl_audio_config_base_t_AudioConfigBase(inputCfg, isInput));
1975 _config->outputCfg = VALUE_OR_RETURN_STATUS_AS_OUT(
1976 legacy2aidl_audio_config_base_t_AudioConfigBase(outputCfg, isInput));
1977 _config->isOnInputStream = !isOutput;
1978 }
1979 RETURN(status);
1980 }
1981
command(int32_t cmdCode,const std::vector<uint8_t> & cmdData,int32_t maxResponseSize,std::vector<uint8_t> * response,int32_t * _aidl_return)1982 Status EffectHandle::command(int32_t cmdCode,
1983 const std::vector<uint8_t>& cmdData,
1984 int32_t maxResponseSize,
1985 std::vector<uint8_t>* response,
1986 int32_t* _aidl_return)
1987 {
1988 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
1989 cmdCode, mHasControl, mEffect.unsafe_get());
1990
1991 // reject commands reserved for internal use by audio framework if coming from outside
1992 // of audioserver
1993 switch(cmdCode) {
1994 case EFFECT_CMD_ENABLE:
1995 case EFFECT_CMD_DISABLE:
1996 case EFFECT_CMD_SET_PARAM:
1997 case EFFECT_CMD_SET_PARAM_DEFERRED:
1998 case EFFECT_CMD_SET_PARAM_COMMIT:
1999 case EFFECT_CMD_GET_PARAM:
2000 break;
2001 default:
2002 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
2003 break;
2004 }
2005 android_errorWriteLog(0x534e4554, "62019992");
2006 RETURN(BAD_VALUE);
2007 }
2008
2009 if (cmdCode == EFFECT_CMD_ENABLE) {
2010 if (maxResponseSize < static_cast<signed>(sizeof(int))) {
2011 android_errorWriteLog(0x534e4554, "32095713");
2012 RETURN(BAD_VALUE);
2013 }
2014 writeToBuffer(NO_ERROR, response);
2015 return enable(_aidl_return);
2016 } else if (cmdCode == EFFECT_CMD_DISABLE) {
2017 if (maxResponseSize < static_cast<signed>(sizeof(int))) {
2018 android_errorWriteLog(0x534e4554, "32095713");
2019 RETURN(BAD_VALUE);
2020 }
2021 writeToBuffer(NO_ERROR, response);
2022 return disable(_aidl_return);
2023 }
2024
2025 audio_utils::lock_guard _l(mutex());
2026 sp<IAfEffectBase> effect = mEffect.promote();
2027 if (effect == 0 || mDisconnected) {
2028 RETURN(DEAD_OBJECT);
2029 }
2030 // only get parameter command is permitted for applications not controlling the effect
2031 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
2032 RETURN(INVALID_OPERATION);
2033 }
2034
2035 // handle commands that are not forwarded transparently to effect engine
2036 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
2037 if (mClient == 0) {
2038 RETURN(INVALID_OPERATION);
2039 }
2040
2041 if (maxResponseSize < (signed)sizeof(int)) {
2042 android_errorWriteLog(0x534e4554, "32095713");
2043 RETURN(BAD_VALUE);
2044 }
2045 writeToBuffer(NO_ERROR, response);
2046
2047 // No need to trylock() here as this function is executed in the binder thread serving a
2048 // particular client process: no risk to block the whole media server process or mixer
2049 // threads if we are stuck here
2050 Mutex::Autolock _l2(mCblk->lock);
2051 // keep local copy of index in case of client corruption b/32220769
2052 const uint32_t clientIndex = mCblk->clientIndex;
2053 const uint32_t serverIndex = mCblk->serverIndex;
2054 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
2055 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
2056 mCblk->serverIndex = 0;
2057 mCblk->clientIndex = 0;
2058 RETURN(BAD_VALUE);
2059 }
2060 status_t status = NO_ERROR;
2061 std::vector<uint8_t> param;
2062 for (uint32_t index = serverIndex; index < clientIndex;) {
2063 int *p = (int *)(mBuffer + index);
2064 const int size = *p++;
2065 if (size < 0
2066 || size > EFFECT_PARAM_BUFFER_SIZE
2067 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
2068 ALOGW("command(): invalid parameter block size");
2069 status = BAD_VALUE;
2070 break;
2071 }
2072
2073 std::copy(reinterpret_cast<const uint8_t*>(p),
2074 reinterpret_cast<const uint8_t*>(p) + size,
2075 std::back_inserter(param));
2076
2077 std::vector<uint8_t> replyBuffer;
2078 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
2079 param,
2080 sizeof(int),
2081 &replyBuffer);
2082 int reply = *reinterpret_cast<const int*>(replyBuffer.data());
2083
2084 // verify shared memory: server index shouldn't change; client index can't go back.
2085 if (serverIndex != mCblk->serverIndex
2086 || clientIndex > mCblk->clientIndex) {
2087 android_errorWriteLog(0x534e4554, "32220769");
2088 status = BAD_VALUE;
2089 break;
2090 }
2091
2092 // stop at first error encountered
2093 if (ret != NO_ERROR) {
2094 status = ret;
2095 writeToBuffer(reply, response);
2096 break;
2097 } else if (reply != NO_ERROR) {
2098 writeToBuffer(reply, response);
2099 break;
2100 }
2101 index += size;
2102 }
2103 mCblk->serverIndex = 0;
2104 mCblk->clientIndex = 0;
2105 RETURN(status);
2106 }
2107
2108 status_t status = effect->command(cmdCode,
2109 cmdData,
2110 maxResponseSize,
2111 response);
2112 RETURN(status);
2113 }
2114
setControl(bool hasControl,bool signal,bool enabled)2115 void EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
2116 {
2117 ALOGV("setControl %p control %d", this, hasControl);
2118
2119 mHasControl = hasControl;
2120 mEnabled = enabled;
2121
2122 if (signal && mEffectClient != 0) {
2123 mEffectClient->controlStatusChanged(hasControl);
2124 }
2125 }
2126
commandExecuted(uint32_t cmdCode,const std::vector<uint8_t> & cmdData,const std::vector<uint8_t> & replyData)2127 void EffectHandle::commandExecuted(uint32_t cmdCode,
2128 const std::vector<uint8_t>& cmdData,
2129 const std::vector<uint8_t>& replyData)
2130 {
2131 if (mEffectClient != 0) {
2132 mEffectClient->commandExecuted(cmdCode, cmdData, replyData);
2133 }
2134 }
2135
2136
2137
setEnabled(bool enabled)2138 void EffectHandle::setEnabled(bool enabled)
2139 {
2140 if (mEffectClient != 0) {
2141 mEffectClient->enableStatusChanged(enabled);
2142 }
2143 }
2144
framesProcessed(int32_t frames) const2145 void EffectHandle::framesProcessed(int32_t frames) const
2146 {
2147 if (mEffectClient != 0 && mNotifyFramesProcessed) {
2148 mEffectClient->framesProcessed(frames);
2149 }
2150 }
2151
dumpToBuffer(char * buffer,size_t size) const2152 void EffectHandle::dumpToBuffer(char* buffer, size_t size) const
2153 NO_THREAD_SAFETY_ANALYSIS // conditional try lock
2154 {
2155 const bool locked = mCblk != nullptr && afutils::dumpTryLock(mCblk->lock);
2156
2157 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
2158 (mClient == 0) ? getpid() : mClient->pid(),
2159 mPriority,
2160 mHasControl ? "yes" : "no",
2161 locked ? "yes" : "no",
2162 mCblk ? mCblk->clientIndex : 0,
2163 mCblk ? mCblk->serverIndex : 0
2164 );
2165
2166 if (locked) {
2167 mCblk->lock.unlock();
2168 }
2169 }
2170
2171 #undef LOG_TAG
2172 #define LOG_TAG "EffectChain"
2173
2174 /* static */
create(const sp<IAfThreadBase> & thread,audio_session_t sessionId,const sp<IAfThreadCallback> & afThreadCallback)2175 sp<IAfEffectChain> IAfEffectChain::create(
2176 const sp<IAfThreadBase>& thread,
2177 audio_session_t sessionId,
2178 const sp<IAfThreadCallback>& afThreadCallback)
2179 {
2180 return sp<EffectChain>::make(thread, sessionId, afThreadCallback);
2181 }
2182
EffectChain(const sp<IAfThreadBase> & thread,audio_session_t sessionId,const sp<IAfThreadCallback> & afThreadCallback)2183 EffectChain::EffectChain(const sp<IAfThreadBase>& thread, audio_session_t sessionId,
2184 const sp<IAfThreadCallback>& afThreadCallback)
2185 : mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
2186 mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
2187 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX),
2188 mEffectCallback(new EffectCallback(wp<EffectChain>(this), thread, afThreadCallback))
2189 {
2190 if (thread != nullptr) {
2191 mStrategy = thread->getStrategyForStream(AUDIO_STREAM_MUSIC);
2192 mMaxTailBuffers =
2193 ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
2194 thread->frameCount();
2195 }
2196 }
2197
getEffectFromDesc(effect_descriptor_t * descriptor) const2198 sp<IAfEffectModule> EffectChain::getEffectFromDesc(
2199 effect_descriptor_t *descriptor) const
2200 {
2201 audio_utils::lock_guard _l(mutex());
2202 size_t size = mEffects.size();
2203
2204 for (size_t i = 0; i < size; i++) {
2205 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
2206 return mEffects[i];
2207 }
2208 }
2209 return 0;
2210 }
2211
2212 // getEffectFromId_l() must be called with IAfThreadBase::mutex() held
getEffectFromId_l(int id) const2213 sp<IAfEffectModule> EffectChain::getEffectFromId_l(int id) const
2214 {
2215 audio_utils::lock_guard _l(mutex());
2216 size_t size = mEffects.size();
2217
2218 for (size_t i = 0; i < size; i++) {
2219 // by convention, return first effect if id provided is 0 (0 is never a valid id)
2220 if (id == 0 || mEffects[i]->id() == id) {
2221 return mEffects[i];
2222 }
2223 }
2224 return 0;
2225 }
2226
2227 // getEffectFromType_l() must be called with IAfThreadBase::mutex() held
getEffectFromType_l(const effect_uuid_t * type) const2228 sp<IAfEffectModule> EffectChain::getEffectFromType_l(
2229 const effect_uuid_t *type) const
2230 {
2231 audio_utils::lock_guard _l(mutex());
2232 size_t size = mEffects.size();
2233
2234 for (size_t i = 0; i < size; i++) {
2235 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
2236 return mEffects[i];
2237 }
2238 }
2239 return 0;
2240 }
2241
getEffectIds_l() const2242 std::vector<int> EffectChain::getEffectIds_l() const
2243 {
2244 std::vector<int> ids;
2245 audio_utils::lock_guard _l(mutex());
2246 for (size_t i = 0; i < mEffects.size(); i++) {
2247 ids.push_back(mEffects[i]->id());
2248 }
2249 return ids;
2250 }
2251
clearInputBuffer()2252 void EffectChain::clearInputBuffer()
2253 {
2254 audio_utils::lock_guard _l(mutex());
2255 clearInputBuffer_l();
2256 }
2257
2258 // Must be called with EffectChain::mutex() locked
clearInputBuffer_l()2259 void EffectChain::clearInputBuffer_l()
2260 {
2261 if (mInBuffer == NULL) {
2262 return;
2263 }
2264 const size_t frameSize = audio_bytes_per_sample(AUDIO_FORMAT_PCM_FLOAT)
2265 * mEffectCallback->inChannelCount(mEffects[0]->id());
2266
2267 memset(mInBuffer->audioBuffer()->raw, 0, mEffectCallback->frameCount() * frameSize);
2268 mInBuffer->commit();
2269 }
2270
2271 // Must be called with EffectChain::mutex() locked
process_l()2272 void EffectChain::process_l() {
2273 // never process effects when:
2274 // - on an OFFLOAD thread
2275 // - no more tracks are on the session and the effect tail has been rendered
2276 bool doProcess = !mEffectCallback->isOffloadOrMmap();
2277 if (!audio_is_global_session(mSessionId)) {
2278 bool tracksOnSession = (trackCnt() != 0);
2279
2280 if (!tracksOnSession && mTailBufferCount == 0) {
2281 doProcess = false;
2282 }
2283
2284 if (activeTrackCnt() == 0) {
2285 // if no track is active and the effect tail has not been rendered,
2286 // the input buffer must be cleared here as the mixer process will not do it
2287 if (tracksOnSession || mTailBufferCount > 0) {
2288 clearInputBuffer_l();
2289 if (mTailBufferCount > 0) {
2290 mTailBufferCount--;
2291 }
2292 }
2293 }
2294 }
2295
2296 size_t size = mEffects.size();
2297 if (doProcess) {
2298 // Only the input and output buffers of the chain can be external,
2299 // and 'update' / 'commit' do nothing for allocated buffers, thus
2300 // it's not needed to consider any other buffers here.
2301 mInBuffer->update();
2302 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2303 mOutBuffer->update();
2304 }
2305 for (size_t i = 0; i < size; i++) {
2306 mEffects[i]->process();
2307 }
2308 mInBuffer->commit();
2309 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2310 mOutBuffer->commit();
2311 }
2312 }
2313 bool doResetVolume = false;
2314 for (size_t i = 0; i < size; i++) {
2315 // reset volume when any effect just started or stopped.
2316 // resetVolume_l will check if the volume controller effect in the chain needs update and
2317 // apply the correct volume
2318 doResetVolume = mEffects[i]->updateState_l() || doResetVolume;
2319 }
2320 if (doResetVolume) {
2321 resetVolume_l();
2322 }
2323 }
2324
createEffect(sp<IAfEffectModule> & effect,effect_descriptor_t * desc,int id,audio_session_t sessionId,bool pinned)2325 status_t EffectChain::createEffect(sp<IAfEffectModule>& effect,
2326 effect_descriptor_t *desc,
2327 int id,
2328 audio_session_t sessionId,
2329 bool pinned)
2330 {
2331 audio_utils::lock_guard _l(mutex());
2332 effect = new EffectModule(mEffectCallback, desc, id, sessionId, pinned, AUDIO_PORT_HANDLE_NONE);
2333 status_t lStatus = effect->status();
2334 if (lStatus == NO_ERROR) {
2335 lStatus = addEffect_l(effect);
2336 }
2337 if (lStatus != NO_ERROR) {
2338 effect.clear();
2339 }
2340 return lStatus;
2341 }
2342
addEffect(const sp<IAfEffectModule> & effect)2343 status_t EffectChain::addEffect(const sp<IAfEffectModule>& effect)
2344 {
2345 audio_utils::lock_guard _l(mutex());
2346 return addEffect_l(effect);
2347 }
2348 // addEffect_l() must be called with EffectChain::mutex() held
addEffect_l(const sp<IAfEffectModule> & effect)2349 status_t EffectChain::addEffect_l(const sp<IAfEffectModule>& effect)
2350 {
2351 effect->setCallback(mEffectCallback);
2352
2353 effect_descriptor_t desc = effect->desc();
2354 ssize_t idx_insert = 0;
2355 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2356 // Auxiliary effects are inserted at the beginning of mEffects vector as
2357 // they are processed first and accumulated in chain input buffer
2358 mEffects.insertAt(effect, idx_insert);
2359
2360 // the input buffer for auxiliary effect contains mono samples in
2361 // 32 bit format. This is to avoid saturation in AudoMixer
2362 // accumulation stage. Saturation is done in EffectModule::process() before
2363 // calling the process in effect engine
2364 size_t numSamples = mEffectCallback->frameCount();
2365 sp<EffectBufferHalInterface> halBuffer;
2366
2367 status_t result = mEffectCallback->allocateHalBuffer(
2368 numSamples * sizeof(float), &halBuffer);
2369 if (result != OK) return result;
2370
2371 effect->configure_l();
2372
2373 effect->setInBuffer(halBuffer);
2374 // auxiliary effects output samples to chain input buffer for further processing
2375 // by insert effects
2376 effect->setOutBuffer(mInBuffer);
2377 } else {
2378 idx_insert = getInsertIndex_l(desc);
2379 if (idx_insert < 0) {
2380 return INVALID_OPERATION;
2381 }
2382
2383 size_t previousSize = mEffects.size();
2384 mEffects.insertAt(effect, idx_insert);
2385
2386 effect->configure_l();
2387
2388 // - By default:
2389 // All effects read samples from chain input buffer.
2390 // The last effect in the chain, writes samples to chain output buffer,
2391 // otherwise to chain input buffer
2392 // - In the OUTPUT_STAGE chain of a spatializer mixer thread:
2393 // The spatializer effect (first effect) reads samples from the input buffer
2394 // and writes samples to the output buffer.
2395 // All other effects read and writes samples to the output buffer
2396 if (mEffectCallback->isSpatializer()
2397 && mSessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2398 effect->setOutBuffer(mOutBuffer);
2399 if (idx_insert == 0) {
2400 if (previousSize != 0) {
2401 mEffects[1]->configure_l();
2402 mEffects[1]->setInBuffer(mOutBuffer);
2403 mEffects[1]->updateAccessMode_l(); // reconfig if needed.
2404 }
2405 effect->setInBuffer(mInBuffer);
2406 } else {
2407 effect->setInBuffer(mOutBuffer);
2408 }
2409 } else {
2410 effect->setInBuffer(mInBuffer);
2411 if (idx_insert == static_cast<ssize_t>(previousSize)) {
2412 if (idx_insert != 0) {
2413 mEffects[idx_insert-1]->configure_l();
2414 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2415 mEffects[idx_insert - 1]->updateAccessMode_l(); // reconfig if needed.
2416 }
2417 effect->setOutBuffer(mOutBuffer);
2418 } else {
2419 effect->setOutBuffer(mInBuffer);
2420 }
2421 }
2422 ALOGV("%s effect %p, added in chain %p at rank %zu",
2423 __func__, effect.get(), this, idx_insert);
2424 }
2425 effect->configure_l();
2426
2427 if (effect->isVolumeControl()) {
2428 const auto volumeControlIndex = findVolumeControl_l(0, mEffects.size());
2429 if (!volumeControlIndex.has_value() || (ssize_t)volumeControlIndex.value() < idx_insert) {
2430 // If this effect will be the new volume control effect when it is enabled, force
2431 // initializing the volume as 0 for volume control effect for safer ramping. The actual
2432 // volume will be set from setVolume_l.
2433 uint32_t left = 0;
2434 uint32_t right = 0;
2435 effect->setVolume_l(&left, &right, true /*controller*/, true /*force*/);
2436 }
2437 }
2438
2439 return NO_ERROR;
2440 }
2441
findVolumeControl_l(size_t from,size_t to) const2442 std::optional<size_t> EffectChain::findVolumeControl_l(size_t from, size_t to) const {
2443 for (size_t i = std::min(to, mEffects.size()); i > from; i--) {
2444 if (mEffects[i - 1]->isVolumeControlEnabled_l()) {
2445 return i - 1;
2446 }
2447 }
2448 return std::nullopt;
2449 }
2450
getInsertIndex_l(const effect_descriptor_t & desc)2451 ssize_t EffectChain::getInsertIndex_l(const effect_descriptor_t& desc) {
2452 // Insert effects are inserted at the end of mEffects vector as they are processed
2453 // after track and auxiliary effects.
2454 // Insert effect order as a function of indicated preference:
2455 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2456 // another effect is present
2457 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2458 // last effect claiming first position
2459 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2460 // first effect claiming last position
2461 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2462 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2463 // already present
2464 // Spatializer or Downmixer effects are inserted in first position because
2465 // they adapt the channel count for all other effects in the chain
2466 if (IAfEffectModule::isSpatializer(&desc.type)
2467 || (memcmp(&desc.type, EFFECT_UIID_DOWNMIX, sizeof(effect_uuid_t)) == 0)) {
2468 return 0;
2469 }
2470
2471 size_t size = mEffects.size();
2472 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2473 ssize_t idx_insert;
2474 ssize_t idx_insert_first = -1;
2475 ssize_t idx_insert_last = -1;
2476
2477 idx_insert = size;
2478 for (size_t i = 0; i < size; i++) {
2479 effect_descriptor_t d = mEffects[i]->desc();
2480 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2481 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2482 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2483 // check invalid effect chaining combinations
2484 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2485 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2486 ALOGW("%s could not insert effect %s: exclusive conflict with %s",
2487 __func__, desc.name, d.name);
2488 return -1;
2489 }
2490 // remember position of first insert effect and by default
2491 // select this as insert position for new effect
2492 if (idx_insert == static_cast<ssize_t>(size)) {
2493 idx_insert = i;
2494 }
2495 // remember position of last insert effect claiming
2496 // first position
2497 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2498 idx_insert_first = i;
2499 }
2500 // remember position of first insert effect claiming
2501 // last position
2502 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2503 idx_insert_last == -1) {
2504 idx_insert_last = i;
2505 }
2506 }
2507 }
2508
2509 // modify idx_insert from first position if needed
2510 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2511 if (idx_insert_last != -1) {
2512 idx_insert = idx_insert_last;
2513 } else {
2514 idx_insert = size;
2515 }
2516 } else {
2517 if (idx_insert_first != -1) {
2518 idx_insert = idx_insert_first + 1;
2519 }
2520 }
2521 return idx_insert;
2522 }
2523
removeEffect(const sp<IAfEffectModule> & effect,bool release)2524 size_t EffectChain::removeEffect(const sp<IAfEffectModule>& effect,
2525 bool release)
2526 {
2527 audio_utils::lock_guard _l(mutex());
2528 size_t size = mEffects.size();
2529 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2530
2531 const bool hasThreadAttached = mEffectCallback->hasThreadAttached();
2532 for (size_t i = 0; i < size; i++) {
2533 if (effect == mEffects[i]) {
2534 // calling stop here will remove pre-processing effect from the audio HAL.
2535 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2536 // the middle of a read from audio HAL
2537 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2538 mEffects[i]->state() == EffectModule::STOPPING) {
2539 mEffects[i]->stop_l();
2540 }
2541 if (release) {
2542 mEffects[i]->release_l("EffectChain::removeEffect");
2543 }
2544 // Skip operation when no thread attached (could lead to sigfpe as framecount is 0...)
2545 if (hasThreadAttached && type != EFFECT_FLAG_TYPE_AUXILIARY) {
2546 if (i == size - 1 && i != 0) {
2547 mEffects[i - 1]->configure_l();
2548 mEffects[i - 1]->setOutBuffer(mOutBuffer);
2549 mEffects[i - 1]->updateAccessMode_l(); // reconfig if needed.
2550 }
2551 }
2552 mEffects.removeAt(i);
2553
2554 // make sure the input buffer configuration for the new first effect in the chain
2555 // is updated if needed (can switch from HAL channel mask to mixer channel mask)
2556 if (type != EFFECT_FLAG_TYPE_AUXILIARY // TODO(b/284522658) breaks for aux FX, why?
2557 && hasThreadAttached && i == 0 && size > 1) {
2558 mEffects[0]->configure_l();
2559 mEffects[0]->setInBuffer(mInBuffer);
2560 mEffects[0]->updateAccessMode_l(); // reconfig if needed.
2561 }
2562
2563 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
2564 this, i);
2565 break;
2566 }
2567 }
2568
2569 return mEffects.size();
2570 }
2571
2572 // setDevices_l() must be called with IAfThreadBase::mutex() held
setDevices_l(const AudioDeviceTypeAddrVector & devices)2573 void EffectChain::setDevices_l(const AudioDeviceTypeAddrVector &devices)
2574 {
2575 audio_utils::lock_guard _l(mutex());
2576 size_t size = mEffects.size();
2577 for (size_t i = 0; i < size; i++) {
2578 mEffects[i]->setDevices(devices);
2579 }
2580 }
2581
2582 // setInputDevice_l() must be called with IAfThreadBase::mutex() held
setInputDevice_l(const AudioDeviceTypeAddr & device)2583 void EffectChain::setInputDevice_l(const AudioDeviceTypeAddr &device)
2584 {
2585 audio_utils::lock_guard _l(mutex());
2586 size_t size = mEffects.size();
2587 for (size_t i = 0; i < size; i++) {
2588 mEffects[i]->setInputDevice(device);
2589 }
2590 }
2591
2592 // setMode_l() must be called with IAfThreadBase::mutex() held
setMode_l(audio_mode_t mode)2593 void EffectChain::setMode_l(audio_mode_t mode)
2594 {
2595 audio_utils::lock_guard _l(mutex());
2596 size_t size = mEffects.size();
2597 for (size_t i = 0; i < size; i++) {
2598 mEffects[i]->setMode(mode);
2599 }
2600 }
2601
2602 // setAudioSource_l() must be called with IAfThreadBase::mutex() held
setAudioSource_l(audio_source_t source)2603 void EffectChain::setAudioSource_l(audio_source_t source)
2604 {
2605 audio_utils::lock_guard _l(mutex());
2606 size_t size = mEffects.size();
2607 for (size_t i = 0; i < size; i++) {
2608 mEffects[i]->setAudioSource(source);
2609 }
2610 }
2611
hasVolumeControlEnabled_l() const2612 bool EffectChain::hasVolumeControlEnabled_l() const {
2613 for (const auto &effect : mEffects) {
2614 if (effect->isVolumeControlEnabled_l()) return true;
2615 }
2616 return false;
2617 }
2618
2619 // setVolume() must be called without EffectChain::mutex()
setVolume(uint32_t * left,uint32_t * right,bool force)2620 bool EffectChain::setVolume(uint32_t* left, uint32_t* right, bool force) {
2621 audio_utils::lock_guard _l(mutex());
2622 return setVolume_l(left, right, force);
2623 }
2624
2625 // setVolume_l() must be called with EffectChain::mutex() held
setVolume_l(uint32_t * left,uint32_t * right,bool force)2626 bool EffectChain::setVolume_l(uint32_t* left, uint32_t* right, bool force) {
2627 uint32_t newLeft = *left;
2628 uint32_t newRight = *right;
2629 const size_t size = mEffects.size();
2630
2631 // first update volume controller
2632 const auto volumeControlIndex = findVolumeControl_l(0, size);
2633 // index of the effect chain volume controller
2634 const int ctrlIdx = volumeControlIndex.value_or(-1);
2635 const sp<IAfEffectModule> volumeControlEffect =
2636 volumeControlIndex.has_value() ? mEffects[ctrlIdx] : nullptr;
2637 const sp<IAfEffectModule> cachedVolumeControlEffect = mVolumeControlEffect.promote();
2638
2639 if (!force && volumeControlEffect == cachedVolumeControlEffect &&
2640 *left == mLeftVolume && *right == mRightVolume) {
2641 if (volumeControlIndex.has_value()) {
2642 *left = mNewLeftVolume;
2643 *right = mNewRightVolume;
2644 }
2645 return volumeControlIndex.has_value();
2646 }
2647 mVolumeControlEffect = volumeControlEffect;
2648
2649 for (int i = 0; i < ctrlIdx; ++i) {
2650 // For all effects before the effect that controls volume, they are not controlling the
2651 // effect chain volume, if these effects has the volume control capability, set the volume
2652 // to maximum to avoid double attenuation.
2653 if (mEffects[i]->isVolumeControl()) {
2654 uint32_t leftMax = 1 << 24;
2655 uint32_t rightMax = 1 << 24;
2656 mEffects[i]->setVolume_l(&leftMax, &rightMax,
2657 false /* not an effect chain volume controller */,
2658 true /* force */);
2659 }
2660 }
2661
2662 mLeftVolume = newLeft;
2663 mRightVolume = newRight;
2664
2665 // second get volume update from volume controller
2666 if (ctrlIdx >= 0) {
2667 mEffects[ctrlIdx]->setVolume_l(&newLeft, &newRight,
2668 true /* effect chain volume controller */);
2669 mNewLeftVolume = newLeft;
2670 mNewRightVolume = newRight;
2671 ALOGD("%s sessionId %d volume controller effect %s set (%d, %d), ret (%d, %d)", __func__,
2672 mSessionId, mEffects[ctrlIdx]->desc().name, mLeftVolume, mRightVolume, newLeft,
2673 newRight);
2674 }
2675 // then indicate volume to all other effects in chain.
2676 // Pass altered volume to effects before volume controller
2677 // and requested volume to effects after controller or with volume monitor flag
2678 uint32_t lVol = newLeft;
2679 uint32_t rVol = newRight;
2680
2681 for (size_t i = 0; i < size; i++) {
2682 if ((int)i == ctrlIdx) {
2683 continue;
2684 }
2685 // this also works for ctrlIdx == -1 when there is no volume controller
2686 if ((int)i > ctrlIdx) {
2687 lVol = *left;
2688 rVol = *right;
2689 }
2690 // Pass requested volume directly if this is volume monitor module
2691 if (mEffects[i]->isVolumeMonitor()) {
2692 mEffects[i]->setVolume_l(left, right,
2693 false /* not an effect chain volume controller */);
2694 } else {
2695 mEffects[i]->setVolume_l(&lVol, &rVol,
2696 false /* not an effect chain volume controller */);
2697 }
2698 }
2699 *left = newLeft;
2700 *right = newRight;
2701
2702 setVolumeForOutput_l(*left, *right);
2703
2704 return volumeControlIndex.has_value();
2705 }
2706
2707 // resetVolume_l() must be called with EffectChain::mutex() held
resetVolume_l()2708 void EffectChain::resetVolume_l()
2709 {
2710 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2711 uint32_t left = mLeftVolume;
2712 uint32_t right = mRightVolume;
2713 (void)setVolume_l(&left, &right, true);
2714 }
2715 }
2716
containsHapticGeneratingEffect()2717 bool EffectChain::containsHapticGeneratingEffect()
2718 {
2719 audio_utils::lock_guard _l(mutex());
2720 return containsHapticGeneratingEffect_l();
2721 }
2722 // containsHapticGeneratingEffect_l must be called with EffectChain::mutex() held
containsHapticGeneratingEffect_l()2723 bool EffectChain::containsHapticGeneratingEffect_l()
2724 {
2725 for (size_t i = 0; i < mEffects.size(); ++i) {
2726 if (mEffects[i]->isHapticGenerator()) {
2727 return true;
2728 }
2729 }
2730 return false;
2731 }
2732
setHapticScale_l(int id,os::HapticScale hapticScale)2733 void EffectChain::setHapticScale_l(int id, os::HapticScale hapticScale)
2734 {
2735 audio_utils::lock_guard _l(mutex());
2736 for (size_t i = 0; i < mEffects.size(); ++i) {
2737 mEffects[i]->setHapticScale_l(id, hapticScale);
2738 }
2739 }
2740
syncHalEffectsState_l()2741 void EffectChain::syncHalEffectsState_l()
2742 {
2743 audio_utils::lock_guard _l(mutex());
2744 for (size_t i = 0; i < mEffects.size(); i++) {
2745 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2746 mEffects[i]->state() == EffectModule::STOPPING) {
2747 mEffects[i]->addEffectToHal_l();
2748 }
2749 }
2750 }
2751
dump(int fd,const Vector<String16> & args) const2752 void EffectChain::dump(int fd, const Vector<String16>& args) const
2753 NO_THREAD_SAFETY_ANALYSIS // conditional try lock
2754 {
2755 String8 result;
2756
2757 const size_t numEffects = mEffects.size();
2758 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
2759
2760 if (numEffects) {
2761 const bool locked = afutils::dumpTryLock(mutex());
2762 // failed to lock - AudioFlinger is probably deadlocked
2763 if (!locked) {
2764 result.append("\tCould not lock mutex:\n");
2765 }
2766
2767 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2768 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2769 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2770 (int)inBufferStr.size(), "In buffer ",
2771 (int)outBufferStr.size(), "Out buffer ");
2772 result.appendFormat("\t%s %s %d\n",
2773 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
2774 write(fd, result.c_str(), result.size());
2775
2776 for (size_t i = 0; i < numEffects; ++i) {
2777 sp<IAfEffectModule> effect = mEffects[i];
2778 if (effect != 0) {
2779 effect->dump(fd, args);
2780 }
2781 }
2782
2783 if (locked) {
2784 mutex().unlock();
2785 }
2786 } else {
2787 write(fd, result.c_str(), result.size());
2788 }
2789 }
2790
2791 // must be called with IAfThreadBase::mutex() held
setEffectSuspended_l(const effect_uuid_t * type,bool suspend)2792 void EffectChain::setEffectSuspended_l(
2793 const effect_uuid_t *type, bool suspend)
2794 {
2795 sp<SuspendedEffectDesc> desc;
2796 // use effect type UUID timelow as key as there is no real risk of identical
2797 // timeLow fields among effect type UUIDs.
2798 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2799 if (suspend) {
2800 if (index >= 0) {
2801 desc = mSuspendedEffects.valueAt(index);
2802 } else {
2803 desc = new SuspendedEffectDesc();
2804 desc->mType = *type;
2805 mSuspendedEffects.add(type->timeLow, desc);
2806 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2807 }
2808
2809 if (desc->mRefCount++ == 0) {
2810 sp<IAfEffectModule> effect = getEffectIfEnabled_l(type);
2811 if (effect != 0) {
2812 desc->mEffect = effect;
2813 effect->setSuspended(true);
2814 effect->setEnabled(false, false /*fromHandle*/);
2815 }
2816 }
2817 } else {
2818 if (index < 0) {
2819 return;
2820 }
2821 desc = mSuspendedEffects.valueAt(index);
2822 if (desc->mRefCount <= 0) {
2823 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
2824 desc->mRefCount = 0;
2825 return;
2826 }
2827 if (--desc->mRefCount == 0) {
2828 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2829 if (desc->mEffect != 0) {
2830 sp<IAfEffectModule> effect = desc->mEffect.promote();
2831 if (effect != 0) {
2832 effect->setSuspended(false);
2833 effect->mutex().lock();
2834 IAfEffectHandle *handle = effect->controlHandle_l();
2835 if (handle != NULL && !handle->disconnected()) {
2836 effect->setEnabled_l(handle->enabled());
2837 }
2838 effect->mutex().unlock();
2839 }
2840 desc->mEffect.clear();
2841 }
2842 mSuspendedEffects.removeItemsAt(index);
2843 }
2844 }
2845 }
2846
2847 // must be called with IAfThreadBase::mutex() held
setEffectSuspendedAll_l(bool suspend)2848 void EffectChain::setEffectSuspendedAll_l(bool suspend)
2849 {
2850 sp<SuspendedEffectDesc> desc;
2851
2852 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2853 if (suspend) {
2854 if (index >= 0) {
2855 desc = mSuspendedEffects.valueAt(index);
2856 } else {
2857 desc = new SuspendedEffectDesc();
2858 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2859 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2860 }
2861 if (desc->mRefCount++ == 0) {
2862 Vector< sp<IAfEffectModule> > effects;
2863 getSuspendEligibleEffects(effects);
2864 for (size_t i = 0; i < effects.size(); i++) {
2865 setEffectSuspended_l(&effects[i]->desc().type, true);
2866 }
2867 }
2868 } else {
2869 if (index < 0) {
2870 return;
2871 }
2872 desc = mSuspendedEffects.valueAt(index);
2873 if (desc->mRefCount <= 0) {
2874 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2875 desc->mRefCount = 1;
2876 }
2877 if (--desc->mRefCount == 0) {
2878 Vector<const effect_uuid_t *> types;
2879 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2880 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2881 continue;
2882 }
2883 types.add(&mSuspendedEffects.valueAt(i)->mType);
2884 }
2885 for (size_t i = 0; i < types.size(); i++) {
2886 setEffectSuspended_l(types[i], false);
2887 }
2888 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2889 mSuspendedEffects.keyAt(index));
2890 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2891 }
2892 }
2893 }
2894
2895
2896 // The volume effect is used for automated tests only
2897 #ifndef OPENSL_ES_H_
2898 static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2899 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2900 const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2901 #endif //OPENSL_ES_H_
2902
2903 /* static */
isEffectEligibleForBtNrecSuspend_l(const effect_uuid_t * type)2904 bool EffectChain::isEffectEligibleForBtNrecSuspend_l(const effect_uuid_t* type) {
2905 // Only NS and AEC are suspended when BtNRec is off
2906 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2907 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2908 return true;
2909 }
2910 return false;
2911 }
2912
isEffectEligibleForSuspend(const effect_descriptor_t & desc)2913 bool EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2914 {
2915 // auxiliary effects and visualizer are never suspended on output mix
2916 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2917 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2918 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
2919 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) ||
2920 (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) {
2921 return false;
2922 }
2923 return true;
2924 }
2925
getSuspendEligibleEffects(Vector<sp<IAfEffectModule>> & effects)2926 void EffectChain::getSuspendEligibleEffects(
2927 Vector< sp<IAfEffectModule> > &effects)
2928 {
2929 effects.clear();
2930 audio_utils::lock_guard _l(mutex());
2931 for (size_t i = 0; i < mEffects.size(); i++) {
2932 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2933 effects.add(mEffects[i]);
2934 }
2935 }
2936 }
2937
getEffectIfEnabled_l(const effect_uuid_t * type)2938 sp<IAfEffectModule> EffectChain::getEffectIfEnabled_l(const effect_uuid_t *type)
2939 {
2940 sp<IAfEffectModule> effect = getEffectFromType_l(type);
2941 return effect != 0 && effect->isEnabled() ? effect : 0;
2942 }
2943
checkSuspendOnEffectEnabled_l(const sp<IAfEffectModule> & effect,bool enabled)2944 void EffectChain::checkSuspendOnEffectEnabled_l(const sp<IAfEffectModule>& effect, bool enabled) {
2945 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2946 if (enabled) {
2947 if (index < 0) {
2948 // if the effect is not suspend check if all effects are suspended
2949 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2950 if (index < 0) {
2951 return;
2952 }
2953 if (!isEffectEligibleForSuspend(effect->desc())) {
2954 return;
2955 }
2956 setEffectSuspended_l(&effect->desc().type, enabled);
2957 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2958 if (index < 0) {
2959 ALOGW("%s Fx should be suspended here!", __func__);
2960 return;
2961 }
2962 }
2963 ALOGV("%s enable suspending fx %08x", __func__, effect->desc().type.timeLow);
2964 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2965 // if effect is requested to suspended but was not yet enabled, suspend it now.
2966 if (desc->mEffect == 0) {
2967 desc->mEffect = effect;
2968 effect->setEnabled(false, false /*fromHandle*/);
2969 effect->setSuspended(true);
2970 }
2971 } else {
2972 if (index < 0) {
2973 return;
2974 }
2975 ALOGV("%s disable restoring fx %08x", __func__, effect->desc().type.timeLow);
2976 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2977 desc->mEffect.clear();
2978 effect->setSuspended(false);
2979 }
2980 }
2981
isNonOffloadableEnabled() const2982 bool EffectChain::isNonOffloadableEnabled() const
2983 {
2984 audio_utils::lock_guard _l(mutex());
2985 return isNonOffloadableEnabled_l();
2986 }
2987
isNonOffloadableEnabled_l() const2988 bool EffectChain::isNonOffloadableEnabled_l() const
2989 {
2990 size_t size = mEffects.size();
2991 for (size_t i = 0; i < size; i++) {
2992 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
2993 return true;
2994 }
2995 }
2996 return false;
2997 }
2998
setThread(const sp<IAfThreadBase> & thread)2999 void EffectChain::setThread(const sp<IAfThreadBase>& thread)
3000 {
3001 if (thread != nullptr) {
3002 mStrategy = thread->getStrategyForStream(AUDIO_STREAM_MUSIC);
3003 mMaxTailBuffers =
3004 ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
3005 thread->frameCount();
3006 }
3007 audio_utils::lock_guard _l(mutex());
3008 mEffectCallback->setThread(thread);
3009 }
3010
checkOutputFlagCompatibility(audio_output_flags_t * flags) const3011 void EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
3012 {
3013 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
3014 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
3015 }
3016 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
3017 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
3018 }
3019 if ((*flags & AUDIO_OUTPUT_FLAG_BIT_PERFECT) != 0 && !isBitPerfectCompatible()) {
3020 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_BIT_PERFECT);
3021 }
3022 }
3023
checkInputFlagCompatibility(audio_input_flags_t * flags) const3024 void EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
3025 {
3026 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
3027 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
3028 }
3029 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
3030 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
3031 }
3032 }
3033
isRawCompatible() const3034 bool EffectChain::isRawCompatible() const
3035 {
3036 audio_utils::lock_guard _l(mutex());
3037 for (const auto &effect : mEffects) {
3038 if (effect->isProcessImplemented()) {
3039 return false;
3040 }
3041 }
3042 // Allow effects without processing.
3043 return true;
3044 }
3045
isFastCompatible() const3046 bool EffectChain::isFastCompatible() const
3047 {
3048 audio_utils::lock_guard _l(mutex());
3049 for (const auto &effect : mEffects) {
3050 if (effect->isProcessImplemented()
3051 && effect->isImplementationSoftware()) {
3052 return false;
3053 }
3054 }
3055 // Allow effects without processing or hw accelerated effects.
3056 return true;
3057 }
3058
isBitPerfectCompatible() const3059 bool EffectChain::isBitPerfectCompatible() const {
3060 audio_utils::lock_guard _l(mutex());
3061 for (const auto &effect : mEffects) {
3062 if (effect->isProcessImplemented()
3063 && effect->isImplementationSoftware()) {
3064 return false;
3065 }
3066 }
3067 // Allow effects without processing or hw accelerated effects.
3068 return true;
3069 }
3070
3071 // isCompatibleWithThread_l() must be called with thread->mutex() held
isCompatibleWithThread_l(const sp<IAfThreadBase> & thread) const3072 bool EffectChain::isCompatibleWithThread_l(const sp<IAfThreadBase>& thread) const
3073 {
3074 audio_utils::lock_guard _l(mutex());
3075 for (size_t i = 0; i < mEffects.size(); i++) {
3076 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
3077 return false;
3078 }
3079 }
3080 return true;
3081 }
3082
3083 // sendMetadata_l() must be called with thread->mutex() held
sendMetadata_l(const std::vector<playback_track_metadata_v7_t> & allMetadata,const std::optional<const std::vector<playback_track_metadata_v7_t>> spatializedMetadata)3084 void EffectChain::sendMetadata_l(const std::vector<playback_track_metadata_v7_t>& allMetadata,
3085 const std::optional<const std::vector<playback_track_metadata_v7_t>> spatializedMetadata) {
3086 audio_utils::lock_guard _l(mutex());
3087 for (const auto& effect : mEffects) {
3088 if (spatializedMetadata.has_value()
3089 && IAfEffectModule::isSpatializer(&effect->desc().type)) {
3090 effect->sendMetadata_ll(spatializedMetadata.value());
3091 } else {
3092 effect->sendMetadata_ll(allMetadata);
3093 }
3094 }
3095 }
3096
3097 // EffectCallbackInterface implementation
createEffectHal(const effect_uuid_t * pEffectUuid,int32_t sessionId,int32_t deviceId,sp<EffectHalInterface> * effect)3098 status_t EffectChain::EffectCallback::createEffectHal(
3099 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3100 sp<EffectHalInterface> *effect) {
3101 status_t status = NO_INIT;
3102 const sp<EffectsFactoryHalInterface> effectsFactory =
3103 EffectConfiguration::getEffectsFactoryHal();
3104 if (effectsFactory != 0) {
3105 status = effectsFactory->createEffect(pEffectUuid, sessionId, io(), deviceId, effect);
3106 }
3107 return status;
3108 }
3109
updateOrphanEffectChains(const sp<IAfEffectBase> & effect)3110 bool EffectChain::EffectCallback::updateOrphanEffectChains(
3111 const sp<IAfEffectBase>& effect) {
3112 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3113 return mAfThreadCallback->updateOrphanEffectChains(effect->asEffectModule());
3114 }
3115
allocateHalBuffer(size_t size,sp<EffectBufferHalInterface> * buffer)3116 status_t EffectChain::EffectCallback::allocateHalBuffer(
3117 size_t size, sp<EffectBufferHalInterface>* buffer) {
3118 return mAfThreadCallback->getEffectsFactoryHal()->allocateBuffer(size, buffer);
3119 }
3120
addEffectToHal(const sp<EffectHalInterface> & effect)3121 status_t EffectChain::EffectCallback::addEffectToHal(
3122 const sp<EffectHalInterface>& effect) {
3123 status_t result = NO_INIT;
3124 const sp<IAfThreadBase> t = thread().promote();
3125 if (t == nullptr) {
3126 return result;
3127 }
3128 sp <StreamHalInterface> st = t->stream();
3129 if (st == nullptr) {
3130 return result;
3131 }
3132 result = st->addEffect(effect);
3133 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
3134 return result;
3135 }
3136
removeEffectFromHal(const sp<EffectHalInterface> & effect)3137 status_t EffectChain::EffectCallback::removeEffectFromHal(
3138 const sp<EffectHalInterface>& effect) {
3139 status_t result = NO_INIT;
3140 const sp<IAfThreadBase> t = thread().promote();
3141 if (t == nullptr) {
3142 return result;
3143 }
3144 sp <StreamHalInterface> st = t->stream();
3145 if (st == nullptr) {
3146 return result;
3147 }
3148 result = st->removeEffect(effect);
3149 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
3150 return result;
3151 }
3152
io() const3153 audio_io_handle_t EffectChain::EffectCallback::io() const {
3154 const sp<IAfThreadBase> t = thread().promote();
3155 if (t == nullptr) {
3156 return AUDIO_IO_HANDLE_NONE;
3157 }
3158 return t->id();
3159 }
3160
isOutput() const3161 bool EffectChain::EffectCallback::isOutput() const {
3162 const sp<IAfThreadBase> t = thread().promote();
3163 if (t == nullptr) {
3164 return true;
3165 }
3166 return t->isOutput();
3167 }
3168
isOffload() const3169 bool EffectChain::EffectCallback::isOffload() const {
3170 return mThreadType == IAfThreadBase::OFFLOAD;
3171 }
3172
isOffloadOrDirect() const3173 bool EffectChain::EffectCallback::isOffloadOrDirect() const {
3174 return mThreadType == IAfThreadBase::OFFLOAD
3175 || mThreadType == IAfThreadBase::DIRECT;
3176 }
3177
isOffloadOrMmap() const3178 bool EffectChain::EffectCallback::isOffloadOrMmap() const {
3179 switch (mThreadType) {
3180 case IAfThreadBase::OFFLOAD:
3181 case IAfThreadBase::MMAP_PLAYBACK:
3182 case IAfThreadBase::MMAP_CAPTURE:
3183 return true;
3184 default:
3185 return false;
3186 }
3187 }
3188
isSpatializer() const3189 bool EffectChain::EffectCallback::isSpatializer() const {
3190 return mThreadType == IAfThreadBase::SPATIALIZER;
3191 }
3192
sampleRate() const3193 uint32_t EffectChain::EffectCallback::sampleRate() const {
3194 const sp<IAfThreadBase> t = thread().promote();
3195 if (t == nullptr) {
3196 return DEFAULT_OUTPUT_SAMPLE_RATE;
3197 }
3198 return t->sampleRate();
3199 }
3200
inChannelMask(int id) const3201 audio_channel_mask_t EffectChain::EffectCallback::inChannelMask(int id) const
3202 NO_THREAD_SAFETY_ANALYSIS
3203 // calling function 'hasAudioSession_l' requires holding mutex 'ThreadBase_Mutex' exclusively
3204 // calling function 'isFirstEffect_l' requires holding mutex 'EffectChain_Mutex' exclusively
3205 {
3206 const sp<IAfThreadBase> t = thread().promote();
3207 if (t == nullptr) {
3208 return AUDIO_CHANNEL_OUT_STEREO;
3209 }
3210 sp<IAfEffectChain> c = chain().promote();
3211 if (c == nullptr) {
3212 return AUDIO_CHANNEL_OUT_STEREO;
3213 }
3214
3215 if (mThreadType == IAfThreadBase::SPATIALIZER) {
3216 if (c->sessionId() == AUDIO_SESSION_OUTPUT_STAGE) {
3217 if (c->isFirstEffect_l(id)) {
3218 return t->mixerChannelMask();
3219 } else {
3220 return t->channelMask();
3221 }
3222 } else if (!audio_is_global_session(c->sessionId())) {
3223 if ((t->hasAudioSession_l(c->sessionId())
3224 & IAfThreadBase::SPATIALIZED_SESSION) != 0) {
3225 return t->mixerChannelMask();
3226 } else {
3227 return t->channelMask();
3228 }
3229 } else {
3230 return t->channelMask();
3231 }
3232 } else {
3233 return t->channelMask();
3234 }
3235 }
3236
inChannelCount(int id) const3237 uint32_t EffectChain::EffectCallback::inChannelCount(int id) const {
3238 return audio_channel_count_from_out_mask(inChannelMask(id));
3239 }
3240
outChannelMask() const3241 audio_channel_mask_t EffectChain::EffectCallback::outChannelMask() const
3242 NO_THREAD_SAFETY_ANALYSIS
3243 // calling function 'hasAudioSession_l' requires holding mutex 'ThreadBase_Mutex' exclusively
3244 {
3245 const sp<IAfThreadBase> t = thread().promote();
3246 if (t == nullptr) {
3247 return AUDIO_CHANNEL_OUT_STEREO;
3248 }
3249 sp<IAfEffectChain> c = chain().promote();
3250 if (c == nullptr) {
3251 return AUDIO_CHANNEL_OUT_STEREO;
3252 }
3253
3254 if (mThreadType == IAfThreadBase::SPATIALIZER) {
3255 if (!audio_is_global_session(c->sessionId())) {
3256 if ((t->hasAudioSession_l(c->sessionId())
3257 & IAfThreadBase::SPATIALIZED_SESSION) != 0) {
3258 return t->mixerChannelMask();
3259 } else {
3260 return t->channelMask();
3261 }
3262 } else {
3263 return t->channelMask();
3264 }
3265 } else {
3266 return t->channelMask();
3267 }
3268 }
3269
outChannelCount() const3270 uint32_t EffectChain::EffectCallback::outChannelCount() const {
3271 return audio_channel_count_from_out_mask(outChannelMask());
3272 }
3273
hapticChannelMask() const3274 audio_channel_mask_t EffectChain::EffectCallback::hapticChannelMask() const {
3275 const sp<IAfThreadBase> t = thread().promote();
3276 if (t == nullptr) {
3277 return AUDIO_CHANNEL_NONE;
3278 }
3279 return t->hapticChannelMask();
3280 }
3281
frameCount() const3282 size_t EffectChain::EffectCallback::frameCount() const {
3283 const sp<IAfThreadBase> t = thread().promote();
3284 if (t == nullptr) {
3285 // frameCount cannot be zero.
3286 return 1;
3287 }
3288 return t->frameCount();
3289 }
3290
latency() const3291 uint32_t EffectChain::EffectCallback::latency() const
3292 NO_THREAD_SAFETY_ANALYSIS // latency_l() access
3293 {
3294 const sp<IAfThreadBase> t = thread().promote();
3295 if (t == nullptr) {
3296 return 0;
3297 }
3298 // TODO(b/275956781) - this requires the thread lock.
3299 return t->latency_l();
3300 }
3301
setVolumeForOutput(float left,float right) const3302 void EffectChain::EffectCallback::setVolumeForOutput(float left, float right) const
3303 NO_THREAD_SAFETY_ANALYSIS // setVolumeForOutput_l() access
3304 {
3305 const sp<IAfThreadBase> t = thread().promote();
3306 if (t == nullptr) {
3307 return;
3308 }
3309 t->setVolumeForOutput_l(left, right);
3310 }
3311
checkSuspendOnEffectEnabled(const sp<IAfEffectBase> & effect,bool enabled,bool threadLocked)3312 void EffectChain::EffectCallback::checkSuspendOnEffectEnabled(const sp<IAfEffectBase>& effect,
3313 bool enabled, bool threadLocked)
3314 NO_THREAD_SAFETY_ANALYSIS {
3315 const sp<IAfThreadBase> t = thread().promote();
3316 if (t == nullptr) {
3317 return;
3318 }
3319 t->checkSuspendOnEffectEnabled(enabled, effect->sessionId(), threadLocked);
3320
3321 sp<IAfEffectChain> c = chain().promote();
3322 if (c == nullptr) {
3323 return;
3324 }
3325 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3326 c->checkSuspendOnEffectEnabled_l(effect->asEffectModule(), enabled);
3327 }
3328
onEffectEnable(const sp<IAfEffectBase> & effect)3329 void EffectChain::EffectCallback::onEffectEnable(const sp<IAfEffectBase>& effect) {
3330 const sp<IAfThreadBase> t = thread().promote();
3331 if (t == nullptr) {
3332 return;
3333 }
3334 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3335 t->onEffectEnable(effect->asEffectModule());
3336 }
3337
onEffectDisable(const sp<IAfEffectBase> & effect)3338 void EffectChain::EffectCallback::onEffectDisable(const sp<IAfEffectBase>& effect) {
3339 checkSuspendOnEffectEnabled(effect, false, false /*threadLocked*/);
3340
3341 const sp<IAfThreadBase> t = thread().promote();
3342 if (t == nullptr) {
3343 return;
3344 }
3345 t->onEffectDisable();
3346 }
3347
disconnectEffectHandle(IAfEffectHandle * handle,bool unpinIfLast)3348 bool EffectChain::EffectCallback::disconnectEffectHandle(IAfEffectHandle *handle,
3349 bool unpinIfLast) {
3350 const sp<IAfThreadBase> t = thread().promote();
3351 if (t == nullptr) {
3352 return false;
3353 }
3354 t->disconnectEffectHandle(handle, unpinIfLast);
3355 return true;
3356 }
3357
resetVolume_l()3358 void EffectChain::EffectCallback::resetVolume_l() {
3359 sp<IAfEffectChain> c = chain().promote();
3360 if (c == nullptr) {
3361 return;
3362 }
3363 c->resetVolume_l();
3364
3365 }
3366
strategy() const3367 product_strategy_t EffectChain::EffectCallback::strategy() const {
3368 sp<IAfEffectChain> c = chain().promote();
3369 if (c == nullptr) {
3370 return PRODUCT_STRATEGY_NONE;
3371 }
3372 return c->strategy();
3373 }
3374
activeTrackCnt() const3375 int32_t EffectChain::EffectCallback::activeTrackCnt() const {
3376 sp<IAfEffectChain> c = chain().promote();
3377 if (c == nullptr) {
3378 return 0;
3379 }
3380 return c->activeTrackCnt();
3381 }
3382
3383
3384 #undef LOG_TAG
3385 #define LOG_TAG "DeviceEffectProxy"
3386
3387 /* static */
create(const AudioDeviceTypeAddr & device,const sp<DeviceEffectManagerCallback> & callback,effect_descriptor_t * desc,int id,bool notifyFramesProcessed)3388 sp<IAfDeviceEffectProxy> IAfDeviceEffectProxy::create(
3389 const AudioDeviceTypeAddr& device,
3390 const sp<DeviceEffectManagerCallback>& callback,
3391 effect_descriptor_t *desc, int id, bool notifyFramesProcessed)
3392 {
3393 return sp<DeviceEffectProxy>::make(device,
3394 callback,
3395 desc, id, notifyFramesProcessed);
3396 }
3397
setEnabled(bool enabled,bool fromHandle)3398 status_t DeviceEffectProxy::setEnabled(bool enabled, bool fromHandle)
3399 {
3400 status_t status = EffectBase::setEnabled(enabled, fromHandle);
3401 audio_utils::lock_guard _l(proxyMutex());
3402 if (status == NO_ERROR) {
3403 for (auto& handle : mEffectHandles) {
3404 Status bs;
3405 if (enabled) {
3406 bs = handle.second->asIEffect()->enable(&status);
3407 } else {
3408 bs = handle.second->asIEffect()->disable(&status);
3409 }
3410 if (!bs.isOk()) {
3411 status = statusTFromBinderStatus(bs);
3412 }
3413 }
3414 }
3415 ALOGV("%s enable %d status %d", __func__, enabled, status);
3416 return status;
3417 }
3418
init_l(const std::map<audio_patch_handle_t,IAfPatchPanel::Patch> & patches)3419 status_t DeviceEffectProxy::init_l(
3420 const std::map <audio_patch_handle_t, IAfPatchPanel::Patch>& patches) {
3421 //For all audio patches
3422 //If src or sink device match
3423 //If the effect is HW accelerated
3424 // if no corresponding effect module
3425 // Create EffectModule: mHalEffect
3426 //Create and attach EffectHandle
3427 //If the effect is not HW accelerated and the patch sink or src is a mixer port
3428 // Create Effect on patch input or output thread on session -1
3429 //Add EffectHandle to EffectHandle map of Effect Proxy:
3430 ALOGV("%s device type %d address %s", __func__, mDevice.mType, mDevice.getAddress());
3431 status_t status = NO_ERROR;
3432 for (auto &patch : patches) {
3433 status = onCreatePatch(patch.first, patch.second);
3434 ALOGV("%s onCreatePatch status %d", __func__, status);
3435 if (status == BAD_VALUE) {
3436 return status;
3437 }
3438 }
3439 return status;
3440 }
3441
onUpdatePatch(audio_patch_handle_t oldPatchHandle,audio_patch_handle_t newPatchHandle,const IAfPatchPanel::Patch &)3442 status_t DeviceEffectProxy::onUpdatePatch(audio_patch_handle_t oldPatchHandle,
3443 audio_patch_handle_t newPatchHandle,
3444 const IAfPatchPanel::Patch& /* patch */) {
3445 status_t status = NAME_NOT_FOUND;
3446 ALOGV("%s", __func__);
3447 audio_utils::lock_guard _l(proxyMutex());
3448 if (mEffectHandles.find(oldPatchHandle) != mEffectHandles.end()) {
3449 ALOGV("%s replacing effect from handle %d to handle %d", __func__, oldPatchHandle,
3450 newPatchHandle);
3451 sp<IAfEffectHandle> effect = mEffectHandles.at(oldPatchHandle);
3452 mEffectHandles.erase(oldPatchHandle);
3453 mEffectHandles.emplace(newPatchHandle, effect);
3454 status = NO_ERROR;
3455 }
3456 return status;
3457 }
3458
onCreatePatch(audio_patch_handle_t patchHandle,const IAfPatchPanel::Patch & patch)3459 status_t DeviceEffectProxy::onCreatePatch(
3460 audio_patch_handle_t patchHandle, const IAfPatchPanel::Patch& patch) {
3461 status_t status = NAME_NOT_FOUND;
3462 sp<IAfEffectHandle> handle;
3463 // only consider source[0] as this is the only "true" source of a patch
3464 status = checkPort(patch, &patch.mAudioPatch.sources[0], &handle);
3465 ALOGV("%s source checkPort status %d", __func__, status);
3466 for (uint32_t i = 0; i < patch.mAudioPatch.num_sinks && status == NAME_NOT_FOUND; i++) {
3467 status = checkPort(patch, &patch.mAudioPatch.sinks[i], &handle);
3468 ALOGV("%s sink %d checkPort status %d", __func__, i, status);
3469 }
3470 if (status == NO_ERROR || status == ALREADY_EXISTS) {
3471 audio_utils::lock_guard _l(proxyMutex());
3472 size_t erasedHandle = mEffectHandles.erase(patchHandle);
3473 ALOGV("%s %s effecthandle %p for patch %d",
3474 __func__, (erasedHandle == 0 ? "adding" : "replacing"), handle.get(), patchHandle);
3475 mEffectHandles.emplace(patchHandle, handle);
3476 }
3477 ALOGW_IF(status == BAD_VALUE,
3478 "%s cannot attach effect %s on patch %d", __func__, mDescriptor.name, patchHandle);
3479
3480 return status;
3481 }
3482
checkPort(const IAfPatchPanel::Patch & patch,const struct audio_port_config * port,sp<IAfEffectHandle> * handle)3483 status_t DeviceEffectProxy::checkPort(const IAfPatchPanel::Patch& patch,
3484 const struct audio_port_config *port, sp<IAfEffectHandle> *handle)
3485 NO_THREAD_SAFETY_ANALYSIS
3486 // calling function 'createEffect_l' requires holding mutex 'AudioFlinger_Mutex' exclusively
3487 {
3488
3489 ALOGV("%s type %d device type %d address %s device ID %d patch.isSoftware() %d",
3490 __func__, port->type, port->ext.device.type,
3491 port->ext.device.address, port->id, patch.isSoftware());
3492 if (port->type != AUDIO_PORT_TYPE_DEVICE || port->ext.device.type != mDevice.mType ||
3493 port->ext.device.address != mDevice.address()) {
3494 return NAME_NOT_FOUND;
3495 }
3496 if (((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) &&
3497 (audio_port_config_has_input_direction(port))) {
3498 ALOGI("%s don't create postprocessing effect on record port", __func__);
3499 return NAME_NOT_FOUND;
3500 }
3501 if (((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC) &&
3502 (!audio_port_config_has_input_direction(port))) {
3503 ALOGI("%s don't create preprocessing effect on playback port", __func__);
3504 return NAME_NOT_FOUND;
3505 }
3506 status_t status = NAME_NOT_FOUND;
3507
3508 if (mDescriptor.flags & EFFECT_FLAG_HW_ACC_TUNNEL) {
3509 audio_utils::lock_guard _l(proxyMutex());
3510 if (mHalEffect != nullptr && mDevicePort.id == port->id) {
3511 ALOGV("%s reusing HAL effect", __func__);
3512 } else {
3513 mDevicePort = *port;
3514 mHalEffect = new EffectModule(mMyCallback,
3515 const_cast<effect_descriptor_t *>(&mDescriptor),
3516 mMyCallback->newEffectId(), AUDIO_SESSION_DEVICE,
3517 false /* pinned */, port->id);
3518 if (audio_is_input_device(mDevice.mType)) {
3519 mHalEffect->setInputDevice(mDevice);
3520 } else {
3521 mHalEffect->setDevices({mDevice});
3522 }
3523 mHalEffect->configure_l();
3524 }
3525 *handle = new EffectHandle(mHalEffect, nullptr, nullptr, 0 /*priority*/,
3526 mNotifyFramesProcessed);
3527 status = (*handle)->initCheck();
3528 if (status == OK) {
3529 status = mHalEffect->addHandle((*handle).get());
3530 } else {
3531 mHalEffect.clear();
3532 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3533 }
3534 } else if (patch.isSoftware() || patch.thread().promote() != nullptr) {
3535 sp<IAfThreadBase> thread;
3536 if (audio_port_config_has_input_direction(port)) {
3537 if (patch.isSoftware()) {
3538 thread = patch.mRecord.thread();
3539 } else {
3540 thread = patch.thread().promote();
3541 }
3542 } else {
3543 if (patch.isSoftware()) {
3544 thread = patch.mPlayback.thread();
3545 } else {
3546 thread = patch.thread().promote();
3547 }
3548 }
3549 int enabled;
3550 *handle = thread->createEffect_l(nullptr, nullptr, 0, AUDIO_SESSION_DEVICE,
3551 const_cast<effect_descriptor_t *>(&mDescriptor),
3552 &enabled, &status, false, false /*probe*/,
3553 mNotifyFramesProcessed);
3554 ALOGV("%s thread->createEffect_l status %d", __func__, status);
3555 } else {
3556 status = BAD_VALUE;
3557 }
3558
3559 if (status == NO_ERROR || status == ALREADY_EXISTS) {
3560 Status bs;
3561 if (isEnabled()) {
3562 bs = (*handle)->asIEffect()->enable(&status);
3563 } else {
3564 bs = (*handle)->asIEffect()->disable(&status);
3565 }
3566 if (!bs.isOk()) {
3567 status = statusTFromBinderStatus(bs);
3568 }
3569 }
3570 return status;
3571 }
3572
onReleasePatch(audio_patch_handle_t patchHandle)3573 void DeviceEffectProxy::onReleasePatch(audio_patch_handle_t patchHandle) {
3574 sp<IAfEffectHandle> effect;
3575 {
3576 audio_utils::lock_guard _l(proxyMutex());
3577 if (mEffectHandles.find(patchHandle) != mEffectHandles.end()) {
3578 effect = mEffectHandles.at(patchHandle);
3579 mEffectHandles.erase(patchHandle);
3580 }
3581 }
3582 }
3583
3584
removeEffect(const sp<IAfEffectModule> & effect)3585 size_t DeviceEffectProxy::removeEffect(const sp<IAfEffectModule>& effect)
3586 {
3587 audio_utils::lock_guard _l(proxyMutex());
3588 if (effect == mHalEffect) {
3589 mHalEffect->release_l("DeviceEffectProxy::removeEffect");
3590 mHalEffect.clear();
3591 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3592 }
3593 return mHalEffect == nullptr ? 0 : 1;
3594 }
3595
addEffectToHal(const sp<EffectHalInterface> & effect)3596 status_t DeviceEffectProxy::addEffectToHal(
3597 const sp<EffectHalInterface>& effect) {
3598 if (mHalEffect == nullptr) {
3599 return NO_INIT;
3600 }
3601 return mManagerCallback->addEffectToHal(&mDevicePort, effect);
3602 }
3603
removeEffectFromHal(const sp<EffectHalInterface> & effect)3604 status_t DeviceEffectProxy::removeEffectFromHal(
3605 const sp<EffectHalInterface>& effect) {
3606 if (mHalEffect == nullptr) {
3607 return NO_INIT;
3608 }
3609 return mManagerCallback->removeEffectFromHal(&mDevicePort, effect);
3610 }
3611
command(int32_t cmdCode,const std::vector<uint8_t> & cmdData,int32_t maxReplySize,std::vector<uint8_t> * reply)3612 status_t DeviceEffectProxy::command(
3613 int32_t cmdCode, const std::vector<uint8_t>& cmdData, int32_t maxReplySize,
3614 std::vector<uint8_t>* reply) {
3615 audio_utils::lock_guard _l(proxyMutex());
3616 status_t status = EffectBase::command(cmdCode, cmdData, maxReplySize, reply);
3617 if (status == NO_ERROR) {
3618 for (auto& handle : mEffectHandles) {
3619 sp<IAfEffectBase> effect = handle.second->effect().promote();
3620 if (effect != nullptr) {
3621 status = effect->command(cmdCode, cmdData, maxReplySize, reply);
3622 }
3623 }
3624 }
3625 ALOGV("%s status %d", __func__, status);
3626 return status;
3627 }
3628
isOutput() const3629 bool DeviceEffectProxy::isOutput() const {
3630 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE) {
3631 return mDevicePort.role == AUDIO_PORT_ROLE_SINK;
3632 }
3633 return true;
3634 }
3635
sampleRate() const3636 uint32_t DeviceEffectProxy::sampleRate() const {
3637 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3638 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) != 0) {
3639 return mDevicePort.sample_rate;
3640 }
3641 return DEFAULT_OUTPUT_SAMPLE_RATE;
3642 }
3643
channelMask() const3644 audio_channel_mask_t DeviceEffectProxy::channelMask() const {
3645 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3646 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) != 0) {
3647 return mDevicePort.channel_mask;
3648 }
3649 return AUDIO_CHANNEL_OUT_STEREO;
3650 }
3651
channelCount() const3652 uint32_t DeviceEffectProxy::channelCount() const {
3653 if (isOutput()) {
3654 return audio_channel_count_from_out_mask(channelMask());
3655 }
3656 return audio_channel_count_from_in_mask(channelMask());
3657 }
3658
dump2(int fd,int spaces) const3659 void DeviceEffectProxy::dump2(int fd, int spaces) const
3660 NO_THREAD_SAFETY_ANALYSIS // conditional try lock
3661 {
3662 const Vector<String16> args;
3663 EffectBase::dump(fd, args);
3664
3665 const bool locked = afutils::dumpTryLock(proxyMutex());
3666 if (!locked) {
3667 String8 result("DeviceEffectProxy may be deadlocked\n");
3668 write(fd, result.c_str(), result.size());
3669 }
3670
3671 String8 outStr;
3672 if (mHalEffect != nullptr) {
3673 outStr.appendFormat("%*sHAL Effect Id: %d\n", spaces, "", mHalEffect->id());
3674 } else {
3675 outStr.appendFormat("%*sNO HAL Effect\n", spaces, "");
3676 }
3677 write(fd, outStr.c_str(), outStr.size());
3678 outStr.clear();
3679
3680 outStr.appendFormat("%*sSub Effects:\n", spaces, "");
3681 write(fd, outStr.c_str(), outStr.size());
3682 outStr.clear();
3683
3684 for (const auto& iter : mEffectHandles) {
3685 outStr.appendFormat("%*sEffect for patch handle %d:\n", spaces + 2, "", iter.first);
3686 write(fd, outStr.c_str(), outStr.size());
3687 outStr.clear();
3688 sp<IAfEffectBase> effect = iter.second->effect().promote();
3689 if (effect != nullptr) {
3690 effect->dump(fd, args);
3691 }
3692 }
3693
3694 if (locked) {
3695 proxyMutex().unlock();
3696 }
3697 }
3698
3699 #undef LOG_TAG
3700 #define LOG_TAG "DeviceEffectProxy::ProxyCallback"
3701
newEffectId()3702 int DeviceEffectProxy::ProxyCallback::newEffectId() {
3703 return mManagerCallback->newEffectId();
3704 }
3705
3706
disconnectEffectHandle(IAfEffectHandle * handle,bool unpinIfLast)3707 bool DeviceEffectProxy::ProxyCallback::disconnectEffectHandle(
3708 IAfEffectHandle *handle, bool unpinIfLast) {
3709 sp<IAfEffectBase> effectBase = handle->effect().promote();
3710 if (effectBase == nullptr) {
3711 return false;
3712 }
3713
3714 sp<IAfEffectModule> effect = effectBase->asEffectModule();
3715 if (effect == nullptr) {
3716 return false;
3717 }
3718
3719 // restore suspended effects if the disconnected handle was enabled and the last one.
3720 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
3721 if (remove) {
3722 sp<DeviceEffectProxy> proxy = mProxy.promote();
3723 if (proxy != nullptr) {
3724 proxy->removeEffect(effect);
3725 }
3726 if (handle->enabled()) {
3727 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
3728 }
3729 }
3730 return true;
3731 }
3732
createEffectHal(const effect_uuid_t * pEffectUuid,int32_t sessionId,int32_t deviceId,sp<EffectHalInterface> * effect)3733 status_t DeviceEffectProxy::ProxyCallback::createEffectHal(
3734 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3735 sp<EffectHalInterface> *effect) {
3736 return mManagerCallback->createEffectHal(pEffectUuid, sessionId, deviceId, effect);
3737 }
3738
addEffectToHal(const sp<EffectHalInterface> & effect)3739 status_t DeviceEffectProxy::ProxyCallback::addEffectToHal(
3740 const sp<EffectHalInterface>& effect) {
3741 sp<DeviceEffectProxy> proxy = mProxy.promote();
3742 if (proxy == nullptr) {
3743 return NO_INIT;
3744 }
3745 return proxy->addEffectToHal(effect);
3746 }
3747
removeEffectFromHal(const sp<EffectHalInterface> & effect)3748 status_t DeviceEffectProxy::ProxyCallback::removeEffectFromHal(
3749 const sp<EffectHalInterface>& effect) {
3750 sp<DeviceEffectProxy> proxy = mProxy.promote();
3751 if (proxy == nullptr) {
3752 return NO_INIT;
3753 }
3754 return proxy->removeEffectFromHal(effect);
3755 }
3756
isOutput() const3757 bool DeviceEffectProxy::ProxyCallback::isOutput() const {
3758 sp<DeviceEffectProxy> proxy = mProxy.promote();
3759 if (proxy == nullptr) {
3760 return true;
3761 }
3762 return proxy->isOutput();
3763 }
3764
sampleRate() const3765 uint32_t DeviceEffectProxy::ProxyCallback::sampleRate() const {
3766 sp<DeviceEffectProxy> proxy = mProxy.promote();
3767 if (proxy == nullptr) {
3768 return DEFAULT_OUTPUT_SAMPLE_RATE;
3769 }
3770 return proxy->sampleRate();
3771 }
3772
inChannelMask(int id __unused) const3773 audio_channel_mask_t DeviceEffectProxy::ProxyCallback::inChannelMask(
3774 int id __unused) const {
3775 sp<DeviceEffectProxy> proxy = mProxy.promote();
3776 if (proxy == nullptr) {
3777 return AUDIO_CHANNEL_OUT_STEREO;
3778 }
3779 return proxy->channelMask();
3780 }
3781
inChannelCount(int id __unused) const3782 uint32_t DeviceEffectProxy::ProxyCallback::inChannelCount(int id __unused) const {
3783 sp<DeviceEffectProxy> proxy = mProxy.promote();
3784 if (proxy == nullptr) {
3785 return 2;
3786 }
3787 return proxy->channelCount();
3788 }
3789
outChannelMask() const3790 audio_channel_mask_t DeviceEffectProxy::ProxyCallback::outChannelMask() const {
3791 sp<DeviceEffectProxy> proxy = mProxy.promote();
3792 if (proxy == nullptr) {
3793 return AUDIO_CHANNEL_OUT_STEREO;
3794 }
3795 return proxy->channelMask();
3796 }
3797
outChannelCount() const3798 uint32_t DeviceEffectProxy::ProxyCallback::outChannelCount() const {
3799 sp<DeviceEffectProxy> proxy = mProxy.promote();
3800 if (proxy == nullptr) {
3801 return 2;
3802 }
3803 return proxy->channelCount();
3804 }
3805
onEffectEnable(const sp<IAfEffectBase> & effectBase)3806 void DeviceEffectProxy::ProxyCallback::onEffectEnable(
3807 const sp<IAfEffectBase>& effectBase) {
3808 sp<IAfEffectModule> effect = effectBase->asEffectModule();
3809 if (effect == nullptr) {
3810 return;
3811 }
3812 effect->start_l();
3813 }
3814
onEffectDisable(const sp<IAfEffectBase> & effectBase)3815 void DeviceEffectProxy::ProxyCallback::onEffectDisable(
3816 const sp<IAfEffectBase>& effectBase) {
3817 sp<IAfEffectModule> effect = effectBase->asEffectModule();
3818 if (effect == nullptr) {
3819 return;
3820 }
3821 effect->stop_l();
3822 }
3823
3824 } // namespace android
3825