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