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