• 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 "Configuration.h"
23 #include <utils/Log.h>
24 #include <system/audio_effects/effect_aec.h>
25 #include <system/audio_effects/effect_ns.h>
26 #include <system/audio_effects/effect_visualizer.h>
27 #include <audio_utils/primitives.h>
28 #include <media/AudioEffect.h>
29 #include <media/audiohal/EffectHalInterface.h>
30 #include <media/audiohal/EffectsFactoryHalInterface.h>
31 
32 #include "AudioFlinger.h"
33 #include "ServiceUtilities.h"
34 
35 // ----------------------------------------------------------------------------
36 
37 // Note: the following macro is used for extremely verbose logging message.  In
38 // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
39 // 0; but one side effect of this is to turn all LOGV's as well.  Some messages
40 // are so verbose that we want to suppress them even when we have ALOG_ASSERT
41 // turned on.  Do not uncomment the #def below unless you really know what you
42 // are doing and want to see all of the extremely verbose messages.
43 //#define VERY_VERY_VERBOSE_LOGGING
44 #ifdef VERY_VERY_VERBOSE_LOGGING
45 #define ALOGVV ALOGV
46 #else
47 #define ALOGVV(a...) do { } while(0)
48 #endif
49 
50 #define min(a, b) ((a) < (b) ? (a) : (b))
51 
52 namespace android {
53 
54 // ----------------------------------------------------------------------------
55 //  EffectModule implementation
56 // ----------------------------------------------------------------------------
57 
58 #undef LOG_TAG
59 #define LOG_TAG "AudioFlinger::EffectModule"
60 
EffectModule(ThreadBase * thread,const wp<AudioFlinger::EffectChain> & chain,effect_descriptor_t * desc,int id,audio_session_t sessionId,bool pinned)61 AudioFlinger::EffectModule::EffectModule(ThreadBase *thread,
62                                         const wp<AudioFlinger::EffectChain>& chain,
63                                         effect_descriptor_t *desc,
64                                         int id,
65                                         audio_session_t sessionId,
66                                         bool pinned)
67     : mPinned(pinned),
68       mThread(thread), mChain(chain), mId(id), mSessionId(sessionId),
69       mDescriptor(*desc),
70       // mConfig is set by configure() and not used before then
71       mStatus(NO_INIT), mState(IDLE),
72       // mMaxDisableWaitCnt is set by configure() and not used before then
73       // mDisableWaitCnt is set by process() and updateState() and not used before then
74       mSuspended(false),
75       mAudioFlinger(thread->mAudioFlinger)
76 {
77     ALOGV("Constructor %p pinned %d", this, pinned);
78     int lStatus;
79 
80     // create effect engine from effect factory
81     mStatus = -ENODEV;
82     sp<AudioFlinger> audioFlinger = mAudioFlinger.promote();
83     if (audioFlinger != 0) {
84         sp<EffectsFactoryHalInterface> effectsFactory = audioFlinger->getEffectsFactory();
85         if (effectsFactory != 0) {
86             mStatus = effectsFactory->createEffect(
87                     &desc->uuid, sessionId, thread->id(), &mEffectInterface);
88         }
89     }
90 
91     if (mStatus != NO_ERROR) {
92         return;
93     }
94     lStatus = init();
95     if (lStatus < 0) {
96         mStatus = lStatus;
97         goto Error;
98     }
99 
100     setOffloaded(thread->type() == ThreadBase::OFFLOAD, thread->id());
101     ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
102 
103     return;
104 Error:
105     mEffectInterface.clear();
106     ALOGV("Constructor Error %d", mStatus);
107 }
108 
~EffectModule()109 AudioFlinger::EffectModule::~EffectModule()
110 {
111     ALOGV("Destructor %p", this);
112     if (mEffectInterface != 0) {
113         char uuidStr[64];
114         AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
115         ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
116                 this, uuidStr);
117         release_l();
118     }
119 
120 }
121 
addHandle(EffectHandle * handle)122 status_t AudioFlinger::EffectModule::addHandle(EffectHandle *handle)
123 {
124     status_t status;
125 
126     Mutex::Autolock _l(mLock);
127     int priority = handle->priority();
128     size_t size = mHandles.size();
129     EffectHandle *controlHandle = NULL;
130     size_t i;
131     for (i = 0; i < size; i++) {
132         EffectHandle *h = mHandles[i];
133         if (h == NULL || h->disconnected()) {
134             continue;
135         }
136         // first non destroyed handle is considered in control
137         if (controlHandle == NULL) {
138             controlHandle = h;
139         }
140         if (h->priority() <= priority) {
141             break;
142         }
143     }
144     // if inserted in first place, move effect control from previous owner to this handle
145     if (i == 0) {
146         bool enabled = false;
147         if (controlHandle != NULL) {
148             enabled = controlHandle->enabled();
149             controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
150         }
151         handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
152         status = NO_ERROR;
153     } else {
154         status = ALREADY_EXISTS;
155     }
156     ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
157     mHandles.insertAt(handle, i);
158     return status;
159 }
160 
removeHandle(EffectHandle * handle)161 ssize_t AudioFlinger::EffectModule::removeHandle(EffectHandle *handle)
162 {
163     Mutex::Autolock _l(mLock);
164     return removeHandle_l(handle);
165 }
166 
removeHandle_l(EffectHandle * handle)167 ssize_t AudioFlinger::EffectModule::removeHandle_l(EffectHandle *handle)
168 {
169     size_t size = mHandles.size();
170     size_t i;
171     for (i = 0; i < size; i++) {
172         if (mHandles[i] == handle) {
173             break;
174         }
175     }
176     if (i == size) {
177         ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
178         return BAD_VALUE;
179     }
180     ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
181 
182     mHandles.removeAt(i);
183     // if removed from first place, move effect control from this handle to next in line
184     if (i == 0) {
185         EffectHandle *h = controlHandle_l();
186         if (h != NULL) {
187             h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
188         }
189     }
190 
191     // Prevent calls to process() and other functions on effect interface from now on.
192     // The effect engine will be released by the destructor when the last strong reference on
193     // this object is released which can happen after next process is called.
194     if (mHandles.size() == 0 && !mPinned) {
195         mState = DESTROYED;
196         mEffectInterface->close();
197     }
198 
199     return mHandles.size();
200 }
201 
202 // must be called with EffectModule::mLock held
controlHandle_l()203 AudioFlinger::EffectHandle *AudioFlinger::EffectModule::controlHandle_l()
204 {
205     // the first valid handle in the list has control over the module
206     for (size_t i = 0; i < mHandles.size(); i++) {
207         EffectHandle *h = mHandles[i];
208         if (h != NULL && !h->disconnected()) {
209             return h;
210         }
211     }
212 
213     return NULL;
214 }
215 
216 // unsafe method called when the effect parent thread has been destroyed
disconnectHandle(EffectHandle * handle,bool unpinIfLast)217 ssize_t AudioFlinger::EffectModule::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
218 {
219     ALOGV("disconnect() %p handle %p", this, handle);
220     Mutex::Autolock _l(mLock);
221     ssize_t numHandles = removeHandle_l(handle);
222     if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
223         AudioSystem::unregisterEffect(mId);
224         sp<AudioFlinger> af = mAudioFlinger.promote();
225         if (af != 0) {
226             mLock.unlock();
227             af->updateOrphanEffectChains(this);
228             mLock.lock();
229         }
230     }
231     return numHandles;
232 }
233 
updateState()234 bool AudioFlinger::EffectModule::updateState() {
235     Mutex::Autolock _l(mLock);
236 
237     bool started = false;
238     switch (mState) {
239     case RESTART:
240         reset_l();
241         // FALL THROUGH
242 
243     case STARTING:
244         // clear auxiliary effect input buffer for next accumulation
245         if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
246             memset(mConfig.inputCfg.buffer.raw,
247                    0,
248                    mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
249         }
250         if (start_l() == NO_ERROR) {
251             mState = ACTIVE;
252             started = true;
253         } else {
254             mState = IDLE;
255         }
256         break;
257     case STOPPING:
258         if (stop_l() == NO_ERROR) {
259             mDisableWaitCnt = mMaxDisableWaitCnt;
260         } else {
261             mDisableWaitCnt = 1; // will cause immediate transition to IDLE
262         }
263         mState = STOPPED;
264         break;
265     case STOPPED:
266         // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
267         // turn off sequence.
268         if (--mDisableWaitCnt == 0) {
269             reset_l();
270             mState = IDLE;
271         }
272         break;
273     default: //IDLE , ACTIVE, DESTROYED
274         break;
275     }
276 
277     return started;
278 }
279 
process()280 void AudioFlinger::EffectModule::process()
281 {
282     Mutex::Autolock _l(mLock);
283 
284     if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
285         return;
286     }
287 
288     if (isProcessEnabled()) {
289         // do 32 bit to 16 bit conversion for auxiliary effect input buffer
290         if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
291             ditherAndClamp(mConfig.inputCfg.buffer.s32,
292                                         mConfig.inputCfg.buffer.s32,
293                                         mConfig.inputCfg.buffer.frameCount/2);
294         }
295         int ret;
296         if (isProcessImplemented()) {
297             // do the actual processing in the effect engine
298             ret = mEffectInterface->process();
299         } else {
300             if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
301                 size_t frameCnt = mConfig.inputCfg.buffer.frameCount * FCC_2;  //always stereo here
302                 int16_t *in = mConfig.inputCfg.buffer.s16;
303                 int16_t *out = mConfig.outputCfg.buffer.s16;
304 
305                 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
306                     for (size_t i = 0; i < frameCnt; i++) {
307                         out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]);
308                     }
309                 } else {
310                     memcpy(mConfig.outputCfg.buffer.raw, mConfig.inputCfg.buffer.raw,
311                            frameCnt * sizeof(int16_t));
312                 }
313             }
314             ret = -ENODATA;
315         }
316         // force transition to IDLE state when engine is ready
317         if (mState == STOPPED && ret == -ENODATA) {
318             mDisableWaitCnt = 1;
319         }
320 
321         // clear auxiliary effect input buffer for next accumulation
322         if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
323             memset(mConfig.inputCfg.buffer.raw, 0,
324                    mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
325         }
326     } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
327                 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
328         // If an insert effect is idle and input buffer is different from output buffer,
329         // accumulate input onto output
330         sp<EffectChain> chain = mChain.promote();
331         if (chain != 0 && chain->activeTrackCnt() != 0) {
332             size_t frameCnt = mConfig.inputCfg.buffer.frameCount * FCC_2;  //always stereo here
333             int16_t *in = mConfig.inputCfg.buffer.s16;
334             int16_t *out = mConfig.outputCfg.buffer.s16;
335             for (size_t i = 0; i < frameCnt; i++) {
336                 out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]);
337             }
338         }
339     }
340 }
341 
reset_l()342 void AudioFlinger::EffectModule::reset_l()
343 {
344     if (mStatus != NO_ERROR || mEffectInterface == 0) {
345         return;
346     }
347     mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
348 }
349 
configure()350 status_t AudioFlinger::EffectModule::configure()
351 {
352     status_t status;
353     sp<ThreadBase> thread;
354     uint32_t size;
355     audio_channel_mask_t channelMask;
356 
357     if (mEffectInterface == 0) {
358         status = NO_INIT;
359         goto exit;
360     }
361 
362     thread = mThread.promote();
363     if (thread == 0) {
364         status = DEAD_OBJECT;
365         goto exit;
366     }
367 
368     // TODO: handle configuration of effects replacing track process
369     channelMask = thread->channelMask();
370     mConfig.outputCfg.channels = channelMask;
371 
372     if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
373         mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
374         mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
375         ALOGV("Overriding auxiliary effect input as MONO and output as STEREO");
376     } else {
377         mConfig.inputCfg.channels = channelMask;
378         // TODO: Update this logic when multichannel effects are implemented.
379         // For offloaded tracks consider mono output as stereo for proper effect initialization
380         if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
381             mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
382             mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
383             ALOGV("Overriding effect input and output as STEREO");
384         }
385     }
386 
387     mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
388     mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
389     mConfig.inputCfg.samplingRate = thread->sampleRate();
390     mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
391     mConfig.inputCfg.bufferProvider.cookie = NULL;
392     mConfig.inputCfg.bufferProvider.getBuffer = NULL;
393     mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
394     mConfig.outputCfg.bufferProvider.cookie = NULL;
395     mConfig.outputCfg.bufferProvider.getBuffer = NULL;
396     mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
397     mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
398     // Insert effect:
399     // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
400     // always overwrites output buffer: input buffer == output buffer
401     // - in other sessions:
402     //      last effect in the chain accumulates in output buffer: input buffer != output buffer
403     //      other effect: overwrites output buffer: input buffer == output buffer
404     // Auxiliary effect:
405     //      accumulates in output buffer: input buffer != output buffer
406     // Therefore: accumulate <=> input buffer != output buffer
407     if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
408         mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
409     } else {
410         mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
411     }
412     mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
413     mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
414     mConfig.inputCfg.buffer.frameCount = thread->frameCount();
415     mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
416     if (mInBuffer != 0) {
417         mInBuffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
418     }
419     if (mOutBuffer != 0) {
420         mOutBuffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
421     }
422 
423     ALOGV("configure() %p thread %p buffer %p framecount %zu",
424             this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
425 
426     status_t cmdStatus;
427     size = sizeof(int);
428     status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
429                                        sizeof(effect_config_t),
430                                        &mConfig,
431                                        &size,
432                                        &cmdStatus);
433     if (status == 0) {
434         status = cmdStatus;
435     }
436 
437     if (status == 0 &&
438             (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0)) {
439         uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
440         effect_param_t *p = (effect_param_t *)buf32;
441 
442         p->psize = sizeof(uint32_t);
443         p->vsize = sizeof(uint32_t);
444         size = sizeof(int);
445         *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
446 
447         uint32_t latency = 0;
448         PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId);
449         if (pbt != NULL) {
450             latency = pbt->latency_l();
451         }
452 
453         *((int32_t *)p->data + 1)= latency;
454         mEffectInterface->command(EFFECT_CMD_SET_PARAM,
455                                   sizeof(effect_param_t) + 8,
456                                   &buf32,
457                                   &size,
458                                   &cmdStatus);
459     }
460 
461     mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) /
462             (1000 * mConfig.outputCfg.buffer.frameCount);
463 
464 exit:
465     mStatus = status;
466     return status;
467 }
468 
init()469 status_t AudioFlinger::EffectModule::init()
470 {
471     Mutex::Autolock _l(mLock);
472     if (mEffectInterface == 0) {
473         return NO_INIT;
474     }
475     status_t cmdStatus;
476     uint32_t size = sizeof(status_t);
477     status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
478                                                 0,
479                                                 NULL,
480                                                 &size,
481                                                 &cmdStatus);
482     if (status == 0) {
483         status = cmdStatus;
484     }
485     return status;
486 }
487 
addEffectToHal_l()488 void AudioFlinger::EffectModule::addEffectToHal_l()
489 {
490     if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
491          (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
492         sp<ThreadBase> thread = mThread.promote();
493         if (thread != 0) {
494             sp<StreamHalInterface> stream = thread->stream();
495             if (stream != 0) {
496                 status_t result = stream->addEffect(mEffectInterface);
497                 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
498             }
499         }
500     }
501 }
502 
503 // start() must be called with PlaybackThread::mLock or EffectChain::mLock held
start()504 status_t AudioFlinger::EffectModule::start()
505 {
506     sp<EffectChain> chain;
507     status_t status;
508     {
509         Mutex::Autolock _l(mLock);
510         status = start_l();
511         if (status == NO_ERROR) {
512             chain = mChain.promote();
513         }
514     }
515     if (chain != 0) {
516         chain->resetVolume_l();
517     }
518     return status;
519 }
520 
start_l()521 status_t AudioFlinger::EffectModule::start_l()
522 {
523     if (mEffectInterface == 0) {
524         return NO_INIT;
525     }
526     if (mStatus != NO_ERROR) {
527         return mStatus;
528     }
529     status_t cmdStatus;
530     uint32_t size = sizeof(status_t);
531     status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
532                                                 0,
533                                                 NULL,
534                                                 &size,
535                                                 &cmdStatus);
536     if (status == 0) {
537         status = cmdStatus;
538     }
539     if (status == 0) {
540         addEffectToHal_l();
541     }
542     return status;
543 }
544 
stop()545 status_t AudioFlinger::EffectModule::stop()
546 {
547     Mutex::Autolock _l(mLock);
548     return stop_l();
549 }
550 
stop_l()551 status_t AudioFlinger::EffectModule::stop_l()
552 {
553     if (mEffectInterface == 0) {
554         return NO_INIT;
555     }
556     if (mStatus != NO_ERROR) {
557         return mStatus;
558     }
559     status_t cmdStatus = NO_ERROR;
560     uint32_t size = sizeof(status_t);
561     status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
562                                                 0,
563                                                 NULL,
564                                                 &size,
565                                                 &cmdStatus);
566     if (status == NO_ERROR) {
567         status = cmdStatus;
568     }
569     if (status == NO_ERROR) {
570         status = remove_effect_from_hal_l();
571     }
572     return status;
573 }
574 
575 // must be called with EffectChain::mLock held
release_l()576 void AudioFlinger::EffectModule::release_l()
577 {
578     if (mEffectInterface != 0) {
579         remove_effect_from_hal_l();
580         // release effect engine
581         mEffectInterface->close();
582         mEffectInterface.clear();
583     }
584 }
585 
remove_effect_from_hal_l()586 status_t AudioFlinger::EffectModule::remove_effect_from_hal_l()
587 {
588     if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
589              (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
590         sp<ThreadBase> thread = mThread.promote();
591         if (thread != 0) {
592             sp<StreamHalInterface> stream = thread->stream();
593             if (stream != 0) {
594                 status_t result = stream->removeEffect(mEffectInterface);
595                 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
596             }
597         }
598     }
599     return NO_ERROR;
600 }
601 
602 // round up delta valid if value and divisor are positive.
603 template <typename T>
roundUpDelta(const T & value,const T & divisor)604 static T roundUpDelta(const T &value, const T &divisor) {
605     T remainder = value % divisor;
606     return remainder == 0 ? 0 : divisor - remainder;
607 }
608 
command(uint32_t cmdCode,uint32_t cmdSize,void * pCmdData,uint32_t * replySize,void * pReplyData)609 status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
610                                              uint32_t cmdSize,
611                                              void *pCmdData,
612                                              uint32_t *replySize,
613                                              void *pReplyData)
614 {
615     Mutex::Autolock _l(mLock);
616     ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
617 
618     if (mState == DESTROYED || mEffectInterface == 0) {
619         return NO_INIT;
620     }
621     if (mStatus != NO_ERROR) {
622         return mStatus;
623     }
624     if (cmdCode == EFFECT_CMD_GET_PARAM &&
625             (sizeof(effect_param_t) > cmdSize ||
626                     ((effect_param_t *)pCmdData)->psize > cmdSize
627                                                           - sizeof(effect_param_t))) {
628         android_errorWriteLog(0x534e4554, "32438594");
629         android_errorWriteLog(0x534e4554, "33003822");
630         return -EINVAL;
631     }
632     if (cmdCode == EFFECT_CMD_GET_PARAM &&
633             (*replySize < sizeof(effect_param_t) ||
634                     ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
635         android_errorWriteLog(0x534e4554, "29251553");
636         return -EINVAL;
637     }
638     if (cmdCode == EFFECT_CMD_GET_PARAM &&
639         (sizeof(effect_param_t) > *replySize
640           || ((effect_param_t *)pCmdData)->psize > *replySize
641                                                    - sizeof(effect_param_t)
642           || ((effect_param_t *)pCmdData)->vsize > *replySize
643                                                    - sizeof(effect_param_t)
644                                                    - ((effect_param_t *)pCmdData)->psize
645           || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
646                                                    *replySize
647                                                    - sizeof(effect_param_t)
648                                                    - ((effect_param_t *)pCmdData)->psize
649                                                    - ((effect_param_t *)pCmdData)->vsize)) {
650         ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
651                      android_errorWriteLog(0x534e4554, "32705438");
652         return -EINVAL;
653     }
654     if ((cmdCode == EFFECT_CMD_SET_PARAM
655             || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) &&  // DEFERRED not generally used
656         (sizeof(effect_param_t) > cmdSize
657             || ((effect_param_t *)pCmdData)->psize > cmdSize
658                                                      - sizeof(effect_param_t)
659             || ((effect_param_t *)pCmdData)->vsize > cmdSize
660                                                      - sizeof(effect_param_t)
661                                                      - ((effect_param_t *)pCmdData)->psize
662             || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
663                                                      cmdSize
664                                                      - sizeof(effect_param_t)
665                                                      - ((effect_param_t *)pCmdData)->psize
666                                                      - ((effect_param_t *)pCmdData)->vsize)) {
667         android_errorWriteLog(0x534e4554, "30204301");
668         return -EINVAL;
669     }
670     status_t status = mEffectInterface->command(cmdCode,
671                                                 cmdSize,
672                                                 pCmdData,
673                                                 replySize,
674                                                 pReplyData);
675     if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
676         uint32_t size = (replySize == NULL) ? 0 : *replySize;
677         for (size_t i = 1; i < mHandles.size(); i++) {
678             EffectHandle *h = mHandles[i];
679             if (h != NULL && !h->disconnected()) {
680                 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
681             }
682         }
683     }
684     return status;
685 }
686 
setEnabled(bool enabled)687 status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
688 {
689     Mutex::Autolock _l(mLock);
690     return setEnabled_l(enabled);
691 }
692 
693 // must be called with EffectModule::mLock held
setEnabled_l(bool enabled)694 status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
695 {
696 
697     ALOGV("setEnabled %p enabled %d", this, enabled);
698 
699     if (enabled != isEnabled()) {
700         status_t status = AudioSystem::setEffectEnabled(mId, enabled);
701         if (enabled && status != NO_ERROR) {
702             return status;
703         }
704 
705         switch (mState) {
706         // going from disabled to enabled
707         case IDLE:
708             mState = STARTING;
709             break;
710         case STOPPED:
711             mState = RESTART;
712             break;
713         case STOPPING:
714             mState = ACTIVE;
715             break;
716 
717         // going from enabled to disabled
718         case RESTART:
719             mState = STOPPED;
720             break;
721         case STARTING:
722             mState = IDLE;
723             break;
724         case ACTIVE:
725             mState = STOPPING;
726             break;
727         case DESTROYED:
728             return NO_ERROR; // simply ignore as we are being destroyed
729         }
730         for (size_t i = 1; i < mHandles.size(); i++) {
731             EffectHandle *h = mHandles[i];
732             if (h != NULL && !h->disconnected()) {
733                 h->setEnabled(enabled);
734             }
735         }
736     }
737     return NO_ERROR;
738 }
739 
isEnabled() const740 bool AudioFlinger::EffectModule::isEnabled() const
741 {
742     switch (mState) {
743     case RESTART:
744     case STARTING:
745     case ACTIVE:
746         return true;
747     case IDLE:
748     case STOPPING:
749     case STOPPED:
750     case DESTROYED:
751     default:
752         return false;
753     }
754 }
755 
isProcessEnabled() const756 bool AudioFlinger::EffectModule::isProcessEnabled() const
757 {
758     if (mStatus != NO_ERROR) {
759         return false;
760     }
761 
762     switch (mState) {
763     case RESTART:
764     case ACTIVE:
765     case STOPPING:
766     case STOPPED:
767         return true;
768     case IDLE:
769     case STARTING:
770     case DESTROYED:
771     default:
772         return false;
773     }
774 }
775 
setInBuffer(const sp<EffectBufferHalInterface> & buffer)776 void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
777     if (buffer != 0) {
778         mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
779         buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
780     } else {
781         mConfig.inputCfg.buffer.raw = NULL;
782     }
783     mInBuffer = buffer;
784     mEffectInterface->setInBuffer(buffer);
785 }
786 
setOutBuffer(const sp<EffectBufferHalInterface> & buffer)787 void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
788     if (buffer != 0) {
789         mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
790         buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
791     } else {
792         mConfig.outputCfg.buffer.raw = NULL;
793     }
794     mOutBuffer = buffer;
795     mEffectInterface->setOutBuffer(buffer);
796 }
797 
setVolume(uint32_t * left,uint32_t * right,bool controller)798 status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
799 {
800     Mutex::Autolock _l(mLock);
801     if (mStatus != NO_ERROR) {
802         return mStatus;
803     }
804     status_t status = NO_ERROR;
805     // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
806     // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
807     if (isProcessEnabled() &&
808             ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
809             (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
810         uint32_t volume[2];
811         uint32_t *pVolume = NULL;
812         uint32_t size = sizeof(volume);
813         volume[0] = *left;
814         volume[1] = *right;
815         if (controller) {
816             pVolume = volume;
817         }
818         status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
819                                            size,
820                                            volume,
821                                            &size,
822                                            pVolume);
823         if (controller && status == NO_ERROR && size == sizeof(volume)) {
824             *left = volume[0];
825             *right = volume[1];
826         }
827     }
828     return status;
829 }
830 
setDevice(audio_devices_t device)831 status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
832 {
833     if (device == AUDIO_DEVICE_NONE) {
834         return NO_ERROR;
835     }
836 
837     Mutex::Autolock _l(mLock);
838     if (mStatus != NO_ERROR) {
839         return mStatus;
840     }
841     status_t status = NO_ERROR;
842     if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
843         status_t cmdStatus;
844         uint32_t size = sizeof(status_t);
845         uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
846                             EFFECT_CMD_SET_INPUT_DEVICE;
847         status = mEffectInterface->command(cmd,
848                                            sizeof(uint32_t),
849                                            &device,
850                                            &size,
851                                            &cmdStatus);
852     }
853     return status;
854 }
855 
setMode(audio_mode_t mode)856 status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
857 {
858     Mutex::Autolock _l(mLock);
859     if (mStatus != NO_ERROR) {
860         return mStatus;
861     }
862     status_t status = NO_ERROR;
863     if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
864         status_t cmdStatus;
865         uint32_t size = sizeof(status_t);
866         status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
867                                            sizeof(audio_mode_t),
868                                            &mode,
869                                            &size,
870                                            &cmdStatus);
871         if (status == NO_ERROR) {
872             status = cmdStatus;
873         }
874     }
875     return status;
876 }
877 
setAudioSource(audio_source_t source)878 status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
879 {
880     Mutex::Autolock _l(mLock);
881     if (mStatus != NO_ERROR) {
882         return mStatus;
883     }
884     status_t status = NO_ERROR;
885     if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
886         uint32_t size = 0;
887         status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
888                                            sizeof(audio_source_t),
889                                            &source,
890                                            &size,
891                                            NULL);
892     }
893     return status;
894 }
895 
setSuspended(bool suspended)896 void AudioFlinger::EffectModule::setSuspended(bool suspended)
897 {
898     Mutex::Autolock _l(mLock);
899     mSuspended = suspended;
900 }
901 
suspended() const902 bool AudioFlinger::EffectModule::suspended() const
903 {
904     Mutex::Autolock _l(mLock);
905     return mSuspended;
906 }
907 
purgeHandles()908 bool AudioFlinger::EffectModule::purgeHandles()
909 {
910     bool enabled = false;
911     Mutex::Autolock _l(mLock);
912     for (size_t i = 0; i < mHandles.size(); i++) {
913         EffectHandle *handle = mHandles[i];
914         if (handle != NULL && !handle->disconnected()) {
915             if (handle->hasControl()) {
916                 enabled = handle->enabled();
917             }
918         }
919     }
920     return enabled;
921 }
922 
setOffloaded(bool offloaded,audio_io_handle_t io)923 status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
924 {
925     Mutex::Autolock _l(mLock);
926     if (mStatus != NO_ERROR) {
927         return mStatus;
928     }
929     status_t status = NO_ERROR;
930     if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
931         status_t cmdStatus;
932         uint32_t size = sizeof(status_t);
933         effect_offload_param_t cmd;
934 
935         cmd.isOffload = offloaded;
936         cmd.ioHandle = io;
937         status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
938                                            sizeof(effect_offload_param_t),
939                                            &cmd,
940                                            &size,
941                                            &cmdStatus);
942         if (status == NO_ERROR) {
943             status = cmdStatus;
944         }
945         mOffloaded = (status == NO_ERROR) ? offloaded : false;
946     } else {
947         if (offloaded) {
948             status = INVALID_OPERATION;
949         }
950         mOffloaded = false;
951     }
952     ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
953     return status;
954 }
955 
isOffloaded() const956 bool AudioFlinger::EffectModule::isOffloaded() const
957 {
958     Mutex::Autolock _l(mLock);
959     return mOffloaded;
960 }
961 
effectFlagsToString(uint32_t flags)962 String8 effectFlagsToString(uint32_t flags) {
963     String8 s;
964 
965     s.append("conn. mode: ");
966     switch (flags & EFFECT_FLAG_TYPE_MASK) {
967     case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
968     case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
969     case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
970     case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
971     case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
972     default: s.append("unknown/reserved"); break;
973     }
974     s.append(", ");
975 
976     s.append("insert pref: ");
977     switch (flags & EFFECT_FLAG_INSERT_MASK) {
978     case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
979     case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
980     case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
981     case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
982     default: s.append("unknown/reserved"); break;
983     }
984     s.append(", ");
985 
986     s.append("volume mgmt: ");
987     switch (flags & EFFECT_FLAG_VOLUME_MASK) {
988     case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
989     case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
990     case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
991     default: s.append("unknown/reserved"); break;
992     }
993     s.append(", ");
994 
995     uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
996     if (devind) {
997         s.append("device indication: ");
998         switch (devind) {
999         case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
1000         default: s.append("unknown/reserved"); break;
1001         }
1002         s.append(", ");
1003     }
1004 
1005     s.append("input mode: ");
1006     switch (flags & EFFECT_FLAG_INPUT_MASK) {
1007     case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
1008     case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
1009     case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
1010     default: s.append("not set"); break;
1011     }
1012     s.append(", ");
1013 
1014     s.append("output mode: ");
1015     switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
1016     case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
1017     case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
1018     case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
1019     default: s.append("not set"); break;
1020     }
1021     s.append(", ");
1022 
1023     uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
1024     if (accel) {
1025         s.append("hardware acceleration: ");
1026         switch (accel) {
1027         case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
1028         case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
1029         default: s.append("unknown/reserved"); break;
1030         }
1031         s.append(", ");
1032     }
1033 
1034     uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
1035     if (modeind) {
1036         s.append("mode indication: ");
1037         switch (modeind) {
1038         case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
1039         default: s.append("unknown/reserved"); break;
1040         }
1041         s.append(", ");
1042     }
1043 
1044     uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
1045     if (srcind) {
1046         s.append("source indication: ");
1047         switch (srcind) {
1048         case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
1049         default: s.append("unknown/reserved"); break;
1050         }
1051         s.append(", ");
1052     }
1053 
1054     if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
1055         s.append("offloadable, ");
1056     }
1057 
1058     int len = s.length();
1059     if (s.length() > 2) {
1060         (void) s.lockBuffer(len);
1061         s.unlockBuffer(len - 2);
1062     }
1063     return s;
1064 }
1065 
1066 
dump(int fd,const Vector<String16> & args __unused)1067 void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused)
1068 {
1069     const size_t SIZE = 256;
1070     char buffer[SIZE];
1071     String8 result;
1072 
1073     snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId);
1074     result.append(buffer);
1075 
1076     bool locked = AudioFlinger::dumpTryLock(mLock);
1077     // failed to lock - AudioFlinger is probably deadlocked
1078     if (!locked) {
1079         result.append("\t\tCould not lock Fx mutex:\n");
1080     }
1081 
1082     result.append("\t\tSession Status State Engine:\n");
1083     snprintf(buffer, SIZE, "\t\t%05d   %03d    %03d   %p\n",
1084             mSessionId, mStatus, mState, mEffectInterface.get());
1085     result.append(buffer);
1086 
1087     result.append("\t\tDescriptor:\n");
1088     char uuidStr[64];
1089     AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
1090     snprintf(buffer, SIZE, "\t\t- UUID: %s\n", uuidStr);
1091     result.append(buffer);
1092     AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
1093     snprintf(buffer, SIZE, "\t\t- TYPE: %s\n", uuidStr);
1094     result.append(buffer);
1095     snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
1096             mDescriptor.apiVersion,
1097             mDescriptor.flags,
1098             effectFlagsToString(mDescriptor.flags).string());
1099     result.append(buffer);
1100     snprintf(buffer, SIZE, "\t\t- name: %s\n",
1101             mDescriptor.name);
1102     result.append(buffer);
1103     snprintf(buffer, SIZE, "\t\t- implementor: %s\n",
1104             mDescriptor.implementor);
1105     result.append(buffer);
1106 
1107     result.append("\t\t- Input configuration:\n");
1108     result.append("\t\t\tFrames  Smp rate Channels Format Buffer\n");
1109     snprintf(buffer, SIZE, "\t\t\t%05zu   %05d    %08x %6d (%s) %p\n",
1110             mConfig.inputCfg.buffer.frameCount,
1111             mConfig.inputCfg.samplingRate,
1112             mConfig.inputCfg.channels,
1113             mConfig.inputCfg.format,
1114             formatToString((audio_format_t)mConfig.inputCfg.format).c_str(),
1115             mConfig.inputCfg.buffer.raw);
1116     result.append(buffer);
1117 
1118     result.append("\t\t- Output configuration:\n");
1119     result.append("\t\t\tBuffer     Frames  Smp rate Channels Format\n");
1120     snprintf(buffer, SIZE, "\t\t\t%p %05zu   %05d    %08x %d (%s)\n",
1121             mConfig.outputCfg.buffer.raw,
1122             mConfig.outputCfg.buffer.frameCount,
1123             mConfig.outputCfg.samplingRate,
1124             mConfig.outputCfg.channels,
1125             mConfig.outputCfg.format,
1126             formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
1127     result.append(buffer);
1128 
1129     snprintf(buffer, SIZE, "\t\t%zu Clients:\n", mHandles.size());
1130     result.append(buffer);
1131     result.append("\t\t\t  Pid Priority Ctrl Locked client server\n");
1132     for (size_t i = 0; i < mHandles.size(); ++i) {
1133         EffectHandle *handle = mHandles[i];
1134         if (handle != NULL && !handle->disconnected()) {
1135             handle->dumpToBuffer(buffer, SIZE);
1136             result.append(buffer);
1137         }
1138     }
1139 
1140     write(fd, result.string(), result.length());
1141 
1142     if (locked) {
1143         mLock.unlock();
1144     }
1145 }
1146 
1147 // ----------------------------------------------------------------------------
1148 //  EffectHandle implementation
1149 // ----------------------------------------------------------------------------
1150 
1151 #undef LOG_TAG
1152 #define LOG_TAG "AudioFlinger::EffectHandle"
1153 
EffectHandle(const sp<EffectModule> & effect,const sp<AudioFlinger::Client> & client,const sp<IEffectClient> & effectClient,int32_t priority)1154 AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1155                                         const sp<AudioFlinger::Client>& client,
1156                                         const sp<IEffectClient>& effectClient,
1157                                         int32_t priority)
1158     : BnEffect(),
1159     mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
1160     mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
1161 {
1162     ALOGV("constructor %p", this);
1163 
1164     if (client == 0) {
1165         return;
1166     }
1167     int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1168     mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
1169     if (mCblkMemory == 0 ||
1170             (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
1171         ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
1172                 sizeof(effect_param_cblk_t));
1173         mCblkMemory.clear();
1174         return;
1175     }
1176     new(mCblk) effect_param_cblk_t();
1177     mBuffer = (uint8_t *)mCblk + bufOffset;
1178 }
1179 
~EffectHandle()1180 AudioFlinger::EffectHandle::~EffectHandle()
1181 {
1182     ALOGV("Destructor %p", this);
1183     disconnect(false);
1184 }
1185 
initCheck()1186 status_t AudioFlinger::EffectHandle::initCheck()
1187 {
1188     return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1189 }
1190 
enable()1191 status_t AudioFlinger::EffectHandle::enable()
1192 {
1193     AutoMutex _l(mLock);
1194     ALOGV("enable %p", this);
1195     sp<EffectModule> effect = mEffect.promote();
1196     if (effect == 0 || mDisconnected) {
1197         return DEAD_OBJECT;
1198     }
1199     if (!mHasControl) {
1200         return INVALID_OPERATION;
1201     }
1202 
1203     if (mEnabled) {
1204         return NO_ERROR;
1205     }
1206 
1207     mEnabled = true;
1208 
1209     sp<ThreadBase> thread = effect->thread().promote();
1210     if (thread != 0) {
1211         thread->checkSuspendOnEffectEnabled(effect, true, effect->sessionId());
1212     }
1213 
1214     // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
1215     if (effect->suspended()) {
1216         return NO_ERROR;
1217     }
1218 
1219     status_t status = effect->setEnabled(true);
1220     if (status != NO_ERROR) {
1221         if (thread != 0) {
1222             thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
1223         }
1224         mEnabled = false;
1225     } else {
1226         if (thread != 0) {
1227             if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1228                 Mutex::Autolock _l(thread->mLock);
1229                 thread->broadcast_l();
1230             }
1231             if (!effect->isOffloadable()) {
1232                 if (thread->type() == ThreadBase::OFFLOAD) {
1233                     PlaybackThread *t = (PlaybackThread *)thread.get();
1234                     t->invalidateTracks(AUDIO_STREAM_MUSIC);
1235                 }
1236                 if (effect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
1237                     thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1238                 }
1239             }
1240         }
1241     }
1242     return status;
1243 }
1244 
disable()1245 status_t AudioFlinger::EffectHandle::disable()
1246 {
1247     ALOGV("disable %p", this);
1248     AutoMutex _l(mLock);
1249     sp<EffectModule> effect = mEffect.promote();
1250     if (effect == 0 || mDisconnected) {
1251         return DEAD_OBJECT;
1252     }
1253     if (!mHasControl) {
1254         return INVALID_OPERATION;
1255     }
1256 
1257     if (!mEnabled) {
1258         return NO_ERROR;
1259     }
1260     mEnabled = false;
1261 
1262     if (effect->suspended()) {
1263         return NO_ERROR;
1264     }
1265 
1266     status_t status = effect->setEnabled(false);
1267 
1268     sp<ThreadBase> thread = effect->thread().promote();
1269     if (thread != 0) {
1270         thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
1271         if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1272             Mutex::Autolock _l(thread->mLock);
1273             thread->broadcast_l();
1274         }
1275     }
1276 
1277     return status;
1278 }
1279 
disconnect()1280 void AudioFlinger::EffectHandle::disconnect()
1281 {
1282     ALOGV("%s %p", __FUNCTION__, this);
1283     disconnect(true);
1284 }
1285 
disconnect(bool unpinIfLast)1286 void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1287 {
1288     AutoMutex _l(mLock);
1289     ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1290     if (mDisconnected) {
1291         if (unpinIfLast) {
1292             android_errorWriteLog(0x534e4554, "32707507");
1293         }
1294         return;
1295     }
1296     mDisconnected = true;
1297     sp<ThreadBase> thread;
1298     {
1299         sp<EffectModule> effect = mEffect.promote();
1300         if (effect != 0) {
1301             thread = effect->thread().promote();
1302         }
1303     }
1304     if (thread != 0) {
1305         thread->disconnectEffectHandle(this, unpinIfLast);
1306     } else {
1307         // try to cleanup as much as we can
1308         sp<EffectModule> effect = mEffect.promote();
1309         if (effect != 0 && effect->disconnectHandle(this, unpinIfLast) > 0) {
1310             ALOGW("%s Effect handle %p disconnected after thread destruction", __FUNCTION__, this);
1311         }
1312     }
1313 
1314     if (mClient != 0) {
1315         if (mCblk != NULL) {
1316             // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1317             mCblk->~effect_param_cblk_t();   // destroy our shared-structure.
1318         }
1319         mCblkMemory.clear();    // free the shared memory before releasing the heap it belongs to
1320         // Client destructor must run with AudioFlinger client mutex locked
1321         Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
1322         mClient.clear();
1323     }
1324 }
1325 
command(uint32_t cmdCode,uint32_t cmdSize,void * pCmdData,uint32_t * replySize,void * pReplyData)1326 status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1327                                              uint32_t cmdSize,
1328                                              void *pCmdData,
1329                                              uint32_t *replySize,
1330                                              void *pReplyData)
1331 {
1332     ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
1333             cmdCode, mHasControl, mEffect.unsafe_get());
1334 
1335     // reject commands reserved for internal use by audio framework if coming from outside
1336     // of audioserver
1337     switch(cmdCode) {
1338         case EFFECT_CMD_ENABLE:
1339         case EFFECT_CMD_DISABLE:
1340         case EFFECT_CMD_SET_PARAM:
1341         case EFFECT_CMD_SET_PARAM_DEFERRED:
1342         case EFFECT_CMD_SET_PARAM_COMMIT:
1343         case EFFECT_CMD_GET_PARAM:
1344             break;
1345         default:
1346             if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1347                 break;
1348             }
1349             android_errorWriteLog(0x534e4554, "62019992");
1350             return BAD_VALUE;
1351     }
1352 
1353     if (cmdCode == EFFECT_CMD_ENABLE) {
1354         if (*replySize < sizeof(int)) {
1355             android_errorWriteLog(0x534e4554, "32095713");
1356             return BAD_VALUE;
1357         }
1358         *(int *)pReplyData = NO_ERROR;
1359         *replySize = sizeof(int);
1360         return enable();
1361     } else if (cmdCode == EFFECT_CMD_DISABLE) {
1362         if (*replySize < sizeof(int)) {
1363             android_errorWriteLog(0x534e4554, "32095713");
1364             return BAD_VALUE;
1365         }
1366         *(int *)pReplyData = NO_ERROR;
1367         *replySize = sizeof(int);
1368         return disable();
1369     }
1370 
1371     AutoMutex _l(mLock);
1372     sp<EffectModule> effect = mEffect.promote();
1373     if (effect == 0 || mDisconnected) {
1374         return DEAD_OBJECT;
1375     }
1376     // only get parameter command is permitted for applications not controlling the effect
1377     if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1378         return INVALID_OPERATION;
1379     }
1380     if (mClient == 0) {
1381         return INVALID_OPERATION;
1382     }
1383 
1384     // handle commands that are not forwarded transparently to effect engine
1385     if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
1386         if (*replySize < sizeof(int)) {
1387             android_errorWriteLog(0x534e4554, "32095713");
1388             return BAD_VALUE;
1389         }
1390         *(int *)pReplyData = NO_ERROR;
1391         *replySize = sizeof(int);
1392 
1393         // No need to trylock() here as this function is executed in the binder thread serving a
1394         // particular client process:  no risk to block the whole media server process or mixer
1395         // threads if we are stuck here
1396         Mutex::Autolock _l(mCblk->lock);
1397         // keep local copy of index in case of client corruption b/32220769
1398         const uint32_t clientIndex = mCblk->clientIndex;
1399         const uint32_t serverIndex = mCblk->serverIndex;
1400         if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1401             serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
1402             mCblk->serverIndex = 0;
1403             mCblk->clientIndex = 0;
1404             return BAD_VALUE;
1405         }
1406         status_t status = NO_ERROR;
1407         effect_param_t *param = NULL;
1408         for (uint32_t index = serverIndex; index < clientIndex;) {
1409             int *p = (int *)(mBuffer + index);
1410             const int size = *p++;
1411             if (size < 0
1412                     || size > EFFECT_PARAM_BUFFER_SIZE
1413                     || ((uint8_t *)p + size) > mBuffer + clientIndex) {
1414                 ALOGW("command(): invalid parameter block size");
1415                 status = BAD_VALUE;
1416                 break;
1417             }
1418 
1419             // copy to local memory in case of client corruption b/32220769
1420             param = (effect_param_t *)realloc(param, size);
1421             if (param == NULL) {
1422                 ALOGW("command(): out of memory");
1423                 status = NO_MEMORY;
1424                 break;
1425             }
1426             memcpy(param, p, size);
1427 
1428             int reply = 0;
1429             uint32_t rsize = sizeof(reply);
1430             status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
1431                                             size,
1432                                             param,
1433                                             &rsize,
1434                                             &reply);
1435 
1436             // verify shared memory: server index shouldn't change; client index can't go back.
1437             if (serverIndex != mCblk->serverIndex
1438                     || clientIndex > mCblk->clientIndex) {
1439                 android_errorWriteLog(0x534e4554, "32220769");
1440                 status = BAD_VALUE;
1441                 break;
1442             }
1443 
1444             // stop at first error encountered
1445             if (ret != NO_ERROR) {
1446                 status = ret;
1447                 *(int *)pReplyData = reply;
1448                 break;
1449             } else if (reply != NO_ERROR) {
1450                 *(int *)pReplyData = reply;
1451                 break;
1452             }
1453             index += size;
1454         }
1455         free(param);
1456         mCblk->serverIndex = 0;
1457         mCblk->clientIndex = 0;
1458         return status;
1459     }
1460 
1461     return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1462 }
1463 
setControl(bool hasControl,bool signal,bool enabled)1464 void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1465 {
1466     ALOGV("setControl %p control %d", this, hasControl);
1467 
1468     mHasControl = hasControl;
1469     mEnabled = enabled;
1470 
1471     if (signal && mEffectClient != 0) {
1472         mEffectClient->controlStatusChanged(hasControl);
1473     }
1474 }
1475 
commandExecuted(uint32_t cmdCode,uint32_t cmdSize,void * pCmdData,uint32_t replySize,void * pReplyData)1476 void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1477                                                  uint32_t cmdSize,
1478                                                  void *pCmdData,
1479                                                  uint32_t replySize,
1480                                                  void *pReplyData)
1481 {
1482     if (mEffectClient != 0) {
1483         mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1484     }
1485 }
1486 
1487 
1488 
setEnabled(bool enabled)1489 void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1490 {
1491     if (mEffectClient != 0) {
1492         mEffectClient->enableStatusChanged(enabled);
1493     }
1494 }
1495 
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)1496 status_t AudioFlinger::EffectHandle::onTransact(
1497     uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1498 {
1499     return BnEffect::onTransact(code, data, reply, flags);
1500 }
1501 
1502 
dumpToBuffer(char * buffer,size_t size)1503 void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
1504 {
1505     bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1506 
1507     snprintf(buffer, size, "\t\t\t%5d    %5d  %3s    %3s  %5u  %5u\n",
1508             (mClient == 0) ? getpid_cached : mClient->pid(),
1509             mPriority,
1510             mHasControl ? "yes" : "no",
1511             locked ? "yes" : "no",
1512             mCblk ? mCblk->clientIndex : 0,
1513             mCblk ? mCblk->serverIndex : 0
1514             );
1515 
1516     if (locked) {
1517         mCblk->lock.unlock();
1518     }
1519 }
1520 
1521 #undef LOG_TAG
1522 #define LOG_TAG "AudioFlinger::EffectChain"
1523 
EffectChain(ThreadBase * thread,audio_session_t sessionId)1524 AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
1525                                         audio_session_t sessionId)
1526     : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
1527       mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
1528       mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
1529 {
1530     mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1531     if (thread == NULL) {
1532         return;
1533     }
1534     mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1535                                     thread->frameCount();
1536 }
1537 
~EffectChain()1538 AudioFlinger::EffectChain::~EffectChain()
1539 {
1540 }
1541 
1542 // getEffectFromDesc_l() must be called with ThreadBase::mLock held
getEffectFromDesc_l(effect_descriptor_t * descriptor)1543 sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1544         effect_descriptor_t *descriptor)
1545 {
1546     size_t size = mEffects.size();
1547 
1548     for (size_t i = 0; i < size; i++) {
1549         if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1550             return mEffects[i];
1551         }
1552     }
1553     return 0;
1554 }
1555 
1556 // getEffectFromId_l() must be called with ThreadBase::mLock held
getEffectFromId_l(int id)1557 sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1558 {
1559     size_t size = mEffects.size();
1560 
1561     for (size_t i = 0; i < size; i++) {
1562         // by convention, return first effect if id provided is 0 (0 is never a valid id)
1563         if (id == 0 || mEffects[i]->id() == id) {
1564             return mEffects[i];
1565         }
1566     }
1567     return 0;
1568 }
1569 
1570 // getEffectFromType_l() must be called with ThreadBase::mLock held
getEffectFromType_l(const effect_uuid_t * type)1571 sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1572         const effect_uuid_t *type)
1573 {
1574     size_t size = mEffects.size();
1575 
1576     for (size_t i = 0; i < size; i++) {
1577         if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1578             return mEffects[i];
1579         }
1580     }
1581     return 0;
1582 }
1583 
clearInputBuffer()1584 void AudioFlinger::EffectChain::clearInputBuffer()
1585 {
1586     Mutex::Autolock _l(mLock);
1587     sp<ThreadBase> thread = mThread.promote();
1588     if (thread == 0) {
1589         ALOGW("clearInputBuffer(): cannot promote mixer thread");
1590         return;
1591     }
1592     clearInputBuffer_l(thread);
1593 }
1594 
1595 // Must be called with EffectChain::mLock locked
clearInputBuffer_l(const sp<ThreadBase> & thread)1596 void AudioFlinger::EffectChain::clearInputBuffer_l(const sp<ThreadBase>& thread)
1597 {
1598     if (mInBuffer == NULL) {
1599         return;
1600     }
1601     // TODO: This will change in the future, depending on multichannel
1602     // and sample format changes for effects.
1603     // Currently effects processing is only available for stereo, AUDIO_FORMAT_PCM_16_BIT
1604     // (4 bytes frame size)
1605     const size_t frameSize =
1606             audio_bytes_per_sample(AUDIO_FORMAT_PCM_16_BIT) * min(FCC_2, thread->channelCount());
1607     memset(mInBuffer->audioBuffer()->raw, 0, thread->frameCount() * frameSize);
1608     mInBuffer->commit();
1609 }
1610 
1611 // Must be called with EffectChain::mLock locked
process_l()1612 void AudioFlinger::EffectChain::process_l()
1613 {
1614     sp<ThreadBase> thread = mThread.promote();
1615     if (thread == 0) {
1616         ALOGW("process_l(): cannot promote mixer thread");
1617         return;
1618     }
1619     bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1620             (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
1621     // never process effects when:
1622     // - on an OFFLOAD thread
1623     // - no more tracks are on the session and the effect tail has been rendered
1624     bool doProcess = (thread->type() != ThreadBase::OFFLOAD)
1625                   && (thread->type() != ThreadBase::MMAP);
1626     if (!isGlobalSession) {
1627         bool tracksOnSession = (trackCnt() != 0);
1628 
1629         if (!tracksOnSession && mTailBufferCount == 0) {
1630             doProcess = false;
1631         }
1632 
1633         if (activeTrackCnt() == 0) {
1634             // if no track is active and the effect tail has not been rendered,
1635             // the input buffer must be cleared here as the mixer process will not do it
1636             if (tracksOnSession || mTailBufferCount > 0) {
1637                 clearInputBuffer_l(thread);
1638                 if (mTailBufferCount > 0) {
1639                     mTailBufferCount--;
1640                 }
1641             }
1642         }
1643     }
1644 
1645     size_t size = mEffects.size();
1646     if (doProcess) {
1647         // Only the input and output buffers of the chain can be external,
1648         // and 'update' / 'commit' do nothing for allocated buffers, thus
1649         // it's not needed to consider any other buffers here.
1650         mInBuffer->update();
1651         if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
1652             mOutBuffer->update();
1653         }
1654         for (size_t i = 0; i < size; i++) {
1655             mEffects[i]->process();
1656         }
1657         mInBuffer->commit();
1658         if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
1659             mOutBuffer->commit();
1660         }
1661     }
1662     bool doResetVolume = false;
1663     for (size_t i = 0; i < size; i++) {
1664         doResetVolume = mEffects[i]->updateState() || doResetVolume;
1665     }
1666     if (doResetVolume) {
1667         resetVolume_l();
1668     }
1669 }
1670 
1671 // createEffect_l() must be called with ThreadBase::mLock held
createEffect_l(sp<EffectModule> & effect,ThreadBase * thread,effect_descriptor_t * desc,int id,audio_session_t sessionId,bool pinned)1672 status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
1673                                                    ThreadBase *thread,
1674                                                    effect_descriptor_t *desc,
1675                                                    int id,
1676                                                    audio_session_t sessionId,
1677                                                    bool pinned)
1678 {
1679     Mutex::Autolock _l(mLock);
1680     effect = new EffectModule(thread, this, desc, id, sessionId, pinned);
1681     status_t lStatus = effect->status();
1682     if (lStatus == NO_ERROR) {
1683         lStatus = addEffect_ll(effect);
1684     }
1685     if (lStatus != NO_ERROR) {
1686         effect.clear();
1687     }
1688     return lStatus;
1689 }
1690 
1691 // addEffect_l() must be called with ThreadBase::mLock held
addEffect_l(const sp<EffectModule> & effect)1692 status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1693 {
1694     Mutex::Autolock _l(mLock);
1695     return addEffect_ll(effect);
1696 }
1697 // addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
addEffect_ll(const sp<EffectModule> & effect)1698 status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
1699 {
1700     effect_descriptor_t desc = effect->desc();
1701     uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1702 
1703     effect->setChain(this);
1704     sp<ThreadBase> thread = mThread.promote();
1705     if (thread == 0) {
1706         return NO_INIT;
1707     }
1708     effect->setThread(thread);
1709 
1710     if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1711         // Auxiliary effects are inserted at the beginning of mEffects vector as
1712         // they are processed first and accumulated in chain input buffer
1713         mEffects.insertAt(effect, 0);
1714 
1715         // the input buffer for auxiliary effect contains mono samples in
1716         // 32 bit format. This is to avoid saturation in AudoMixer
1717         // accumulation stage. Saturation is done in EffectModule::process() before
1718         // calling the process in effect engine
1719         size_t numSamples = thread->frameCount();
1720         sp<EffectBufferHalInterface> halBuffer;
1721         status_t result = EffectBufferHalInterface::allocate(
1722                 numSamples * sizeof(int32_t), &halBuffer);
1723         if (result != OK) return result;
1724         effect->setInBuffer(halBuffer);
1725         // auxiliary effects output samples to chain input buffer for further processing
1726         // by insert effects
1727         effect->setOutBuffer(mInBuffer);
1728     } else {
1729         // Insert effects are inserted at the end of mEffects vector as they are processed
1730         //  after track and auxiliary effects.
1731         // Insert effect order as a function of indicated preference:
1732         //  if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1733         //  another effect is present
1734         //  else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1735         //  last effect claiming first position
1736         //  else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1737         //  first effect claiming last position
1738         //  else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1739         // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1740         // already present
1741 
1742         size_t size = mEffects.size();
1743         size_t idx_insert = size;
1744         ssize_t idx_insert_first = -1;
1745         ssize_t idx_insert_last = -1;
1746 
1747         for (size_t i = 0; i < size; i++) {
1748             effect_descriptor_t d = mEffects[i]->desc();
1749             uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1750             uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1751             if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1752                 // check invalid effect chaining combinations
1753                 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1754                     iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1755                     ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1756                             desc.name, d.name);
1757                     return INVALID_OPERATION;
1758                 }
1759                 // remember position of first insert effect and by default
1760                 // select this as insert position for new effect
1761                 if (idx_insert == size) {
1762                     idx_insert = i;
1763                 }
1764                 // remember position of last insert effect claiming
1765                 // first position
1766                 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1767                     idx_insert_first = i;
1768                 }
1769                 // remember position of first insert effect claiming
1770                 // last position
1771                 if (iPref == EFFECT_FLAG_INSERT_LAST &&
1772                     idx_insert_last == -1) {
1773                     idx_insert_last = i;
1774                 }
1775             }
1776         }
1777 
1778         // modify idx_insert from first position if needed
1779         if (insertPref == EFFECT_FLAG_INSERT_LAST) {
1780             if (idx_insert_last != -1) {
1781                 idx_insert = idx_insert_last;
1782             } else {
1783                 idx_insert = size;
1784             }
1785         } else {
1786             if (idx_insert_first != -1) {
1787                 idx_insert = idx_insert_first + 1;
1788             }
1789         }
1790 
1791         // always read samples from chain input buffer
1792         effect->setInBuffer(mInBuffer);
1793 
1794         // if last effect in the chain, output samples to chain
1795         // output buffer, otherwise to chain input buffer
1796         if (idx_insert == size) {
1797             if (idx_insert != 0) {
1798                 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
1799                 mEffects[idx_insert-1]->configure();
1800             }
1801             effect->setOutBuffer(mOutBuffer);
1802         } else {
1803             effect->setOutBuffer(mInBuffer);
1804         }
1805         mEffects.insertAt(effect, idx_insert);
1806 
1807         ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
1808                 idx_insert);
1809     }
1810     effect->configure();
1811 
1812     return NO_ERROR;
1813 }
1814 
1815 // removeEffect_l() must be called with ThreadBase::mLock held
removeEffect_l(const sp<EffectModule> & effect,bool release)1816 size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
1817                                                  bool release)
1818 {
1819     Mutex::Autolock _l(mLock);
1820     size_t size = mEffects.size();
1821     uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
1822 
1823     for (size_t i = 0; i < size; i++) {
1824         if (effect == mEffects[i]) {
1825             // calling stop here will remove pre-processing effect from the audio HAL.
1826             // This is safe as we hold the EffectChain mutex which guarantees that we are not in
1827             // the middle of a read from audio HAL
1828             if (mEffects[i]->state() == EffectModule::ACTIVE ||
1829                     mEffects[i]->state() == EffectModule::STOPPING) {
1830                 mEffects[i]->stop();
1831             }
1832             if (release) {
1833                 mEffects[i]->release_l();
1834             }
1835 
1836             if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
1837                 if (i == size - 1 && i != 0) {
1838                     mEffects[i - 1]->setOutBuffer(mOutBuffer);
1839                     mEffects[i - 1]->configure();
1840                 }
1841             }
1842             mEffects.removeAt(i);
1843             ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
1844                     this, i);
1845 
1846             break;
1847         }
1848     }
1849 
1850     return mEffects.size();
1851 }
1852 
1853 // setDevice_l() must be called with ThreadBase::mLock held
setDevice_l(audio_devices_t device)1854 void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
1855 {
1856     size_t size = mEffects.size();
1857     for (size_t i = 0; i < size; i++) {
1858         mEffects[i]->setDevice(device);
1859     }
1860 }
1861 
1862 // setMode_l() must be called with ThreadBase::mLock held
setMode_l(audio_mode_t mode)1863 void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
1864 {
1865     size_t size = mEffects.size();
1866     for (size_t i = 0; i < size; i++) {
1867         mEffects[i]->setMode(mode);
1868     }
1869 }
1870 
1871 // setAudioSource_l() must be called with ThreadBase::mLock held
setAudioSource_l(audio_source_t source)1872 void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
1873 {
1874     size_t size = mEffects.size();
1875     for (size_t i = 0; i < size; i++) {
1876         mEffects[i]->setAudioSource(source);
1877     }
1878 }
1879 
1880 // setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
setVolume_l(uint32_t * left,uint32_t * right,bool force)1881 bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
1882 {
1883     uint32_t newLeft = *left;
1884     uint32_t newRight = *right;
1885     bool hasControl = false;
1886     int ctrlIdx = -1;
1887     size_t size = mEffects.size();
1888 
1889     // first update volume controller
1890     for (size_t i = size; i > 0; i--) {
1891         if (mEffects[i - 1]->isProcessEnabled() &&
1892             (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
1893             ctrlIdx = i - 1;
1894             hasControl = true;
1895             break;
1896         }
1897     }
1898 
1899     if (!force && ctrlIdx == mVolumeCtrlIdx &&
1900             *left == mLeftVolume && *right == mRightVolume) {
1901         if (hasControl) {
1902             *left = mNewLeftVolume;
1903             *right = mNewRightVolume;
1904         }
1905         return hasControl;
1906     }
1907 
1908     mVolumeCtrlIdx = ctrlIdx;
1909     mLeftVolume = newLeft;
1910     mRightVolume = newRight;
1911 
1912     // second get volume update from volume controller
1913     if (ctrlIdx >= 0) {
1914         mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
1915         mNewLeftVolume = newLeft;
1916         mNewRightVolume = newRight;
1917     }
1918     // then indicate volume to all other effects in chain.
1919     // Pass altered volume to effects before volume controller
1920     // and requested volume to effects after controller
1921     uint32_t lVol = newLeft;
1922     uint32_t rVol = newRight;
1923 
1924     for (size_t i = 0; i < size; i++) {
1925         if ((int)i == ctrlIdx) {
1926             continue;
1927         }
1928         // this also works for ctrlIdx == -1 when there is no volume controller
1929         if ((int)i > ctrlIdx) {
1930             lVol = *left;
1931             rVol = *right;
1932         }
1933         mEffects[i]->setVolume(&lVol, &rVol, false);
1934     }
1935     *left = newLeft;
1936     *right = newRight;
1937 
1938     return hasControl;
1939 }
1940 
1941 // resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
resetVolume_l()1942 void AudioFlinger::EffectChain::resetVolume_l()
1943 {
1944     if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
1945         uint32_t left = mLeftVolume;
1946         uint32_t right = mRightVolume;
1947         (void)setVolume_l(&left, &right, true);
1948     }
1949 }
1950 
syncHalEffectsState()1951 void AudioFlinger::EffectChain::syncHalEffectsState()
1952 {
1953     Mutex::Autolock _l(mLock);
1954     for (size_t i = 0; i < mEffects.size(); i++) {
1955         if (mEffects[i]->state() == EffectModule::ACTIVE ||
1956                 mEffects[i]->state() == EffectModule::STOPPING) {
1957             mEffects[i]->addEffectToHal_l();
1958         }
1959     }
1960 }
1961 
dumpInOutBuffer(char * dump,size_t dumpSize,bool isInput,EffectBufferHalInterface * buffer)1962 static void dumpInOutBuffer(
1963         char *dump, size_t dumpSize, bool isInput, EffectBufferHalInterface *buffer) {
1964     if (buffer == nullptr) {
1965         snprintf(dump, dumpSize, "%p", buffer);
1966     } else if (buffer->externalData() != nullptr) {
1967         snprintf(dump, dumpSize, "%p -> %p",
1968                 isInput ? buffer->externalData() : buffer->audioBuffer()->raw,
1969                 isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1970     } else {
1971         snprintf(dump, dumpSize, "%p", buffer->audioBuffer()->raw);
1972     }
1973 }
1974 
dump(int fd,const Vector<String16> & args)1975 void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
1976 {
1977     const size_t SIZE = 256;
1978     char buffer[SIZE];
1979     String8 result;
1980 
1981     size_t numEffects = mEffects.size();
1982     snprintf(buffer, SIZE, "    %zu effects for session %d\n", numEffects, mSessionId);
1983     result.append(buffer);
1984 
1985     if (numEffects) {
1986         bool locked = AudioFlinger::dumpTryLock(mLock);
1987         // failed to lock - AudioFlinger is probably deadlocked
1988         if (!locked) {
1989             result.append("\tCould not lock mutex:\n");
1990         }
1991 
1992         char inBufferStr[64], outBufferStr[64];
1993         dumpInOutBuffer(inBufferStr, sizeof(inBufferStr), true, mInBuffer.get());
1994         dumpInOutBuffer(outBufferStr, sizeof(outBufferStr), false, mOutBuffer.get());
1995         snprintf(buffer, SIZE, "\t%-*s%-*s   Active tracks:\n",
1996                 (int)strlen(inBufferStr), "In buffer    ",
1997                 (int)strlen(outBufferStr), "Out buffer      ");
1998         result.append(buffer);
1999         snprintf(buffer, SIZE, "\t%s   %s   %d\n", inBufferStr, outBufferStr, mActiveTrackCnt);
2000         result.append(buffer);
2001         write(fd, result.string(), result.size());
2002 
2003         for (size_t i = 0; i < numEffects; ++i) {
2004             sp<EffectModule> effect = mEffects[i];
2005             if (effect != 0) {
2006                 effect->dump(fd, args);
2007             }
2008         }
2009 
2010         if (locked) {
2011             mLock.unlock();
2012         }
2013     }
2014 }
2015 
2016 // must be called with ThreadBase::mLock held
setEffectSuspended_l(const effect_uuid_t * type,bool suspend)2017 void AudioFlinger::EffectChain::setEffectSuspended_l(
2018         const effect_uuid_t *type, bool suspend)
2019 {
2020     sp<SuspendedEffectDesc> desc;
2021     // use effect type UUID timelow as key as there is no real risk of identical
2022     // timeLow fields among effect type UUIDs.
2023     ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2024     if (suspend) {
2025         if (index >= 0) {
2026             desc = mSuspendedEffects.valueAt(index);
2027         } else {
2028             desc = new SuspendedEffectDesc();
2029             desc->mType = *type;
2030             mSuspendedEffects.add(type->timeLow, desc);
2031             ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2032         }
2033 
2034         if (desc->mRefCount++ == 0) {
2035             sp<EffectModule> effect = getEffectIfEnabled(type);
2036             if (effect != 0) {
2037                 desc->mEffect = effect;
2038                 effect->setSuspended(true);
2039                 effect->setEnabled(false);
2040             }
2041         }
2042     } else {
2043         if (index < 0) {
2044             return;
2045         }
2046         desc = mSuspendedEffects.valueAt(index);
2047         if (desc->mRefCount <= 0) {
2048             ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
2049             desc->mRefCount = 0;
2050             return;
2051         }
2052         if (--desc->mRefCount == 0) {
2053             ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2054             if (desc->mEffect != 0) {
2055                 sp<EffectModule> effect = desc->mEffect.promote();
2056                 if (effect != 0) {
2057                     effect->setSuspended(false);
2058                     effect->lock();
2059                     EffectHandle *handle = effect->controlHandle_l();
2060                     if (handle != NULL && !handle->disconnected()) {
2061                         effect->setEnabled_l(handle->enabled());
2062                     }
2063                     effect->unlock();
2064                 }
2065                 desc->mEffect.clear();
2066             }
2067             mSuspendedEffects.removeItemsAt(index);
2068         }
2069     }
2070 }
2071 
2072 // must be called with ThreadBase::mLock held
setEffectSuspendedAll_l(bool suspend)2073 void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2074 {
2075     sp<SuspendedEffectDesc> desc;
2076 
2077     ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2078     if (suspend) {
2079         if (index >= 0) {
2080             desc = mSuspendedEffects.valueAt(index);
2081         } else {
2082             desc = new SuspendedEffectDesc();
2083             mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2084             ALOGV("setEffectSuspendedAll_l() add entry for 0");
2085         }
2086         if (desc->mRefCount++ == 0) {
2087             Vector< sp<EffectModule> > effects;
2088             getSuspendEligibleEffects(effects);
2089             for (size_t i = 0; i < effects.size(); i++) {
2090                 setEffectSuspended_l(&effects[i]->desc().type, true);
2091             }
2092         }
2093     } else {
2094         if (index < 0) {
2095             return;
2096         }
2097         desc = mSuspendedEffects.valueAt(index);
2098         if (desc->mRefCount <= 0) {
2099             ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2100             desc->mRefCount = 1;
2101         }
2102         if (--desc->mRefCount == 0) {
2103             Vector<const effect_uuid_t *> types;
2104             for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2105                 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2106                     continue;
2107                 }
2108                 types.add(&mSuspendedEffects.valueAt(i)->mType);
2109             }
2110             for (size_t i = 0; i < types.size(); i++) {
2111                 setEffectSuspended_l(types[i], false);
2112             }
2113             ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2114                     mSuspendedEffects.keyAt(index));
2115             mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2116         }
2117     }
2118 }
2119 
2120 
2121 // The volume effect is used for automated tests only
2122 #ifndef OPENSL_ES_H_
2123 static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2124                                             { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2125 const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2126 #endif //OPENSL_ES_H_
2127 
2128 /* static */
isEffectEligibleForBtNrecSuspend(const effect_uuid_t * type)2129 bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2130 {
2131     // Only NS and AEC are suspended when BtNRec is off
2132     if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2133         (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2134         return true;
2135     }
2136     return false;
2137 }
2138 
isEffectEligibleForSuspend(const effect_descriptor_t & desc)2139 bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2140 {
2141     // auxiliary effects and visualizer are never suspended on output mix
2142     if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2143         (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2144          (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
2145          (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
2146         return false;
2147     }
2148     return true;
2149 }
2150 
getSuspendEligibleEffects(Vector<sp<AudioFlinger::EffectModule>> & effects)2151 void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2152         Vector< sp<AudioFlinger::EffectModule> > &effects)
2153 {
2154     effects.clear();
2155     for (size_t i = 0; i < mEffects.size(); i++) {
2156         if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2157             effects.add(mEffects[i]);
2158         }
2159     }
2160 }
2161 
getEffectIfEnabled(const effect_uuid_t * type)2162 sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2163                                                             const effect_uuid_t *type)
2164 {
2165     sp<EffectModule> effect = getEffectFromType_l(type);
2166     return effect != 0 && effect->isEnabled() ? effect : 0;
2167 }
2168 
checkSuspendOnEffectEnabled(const sp<EffectModule> & effect,bool enabled)2169 void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2170                                                             bool enabled)
2171 {
2172     ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2173     if (enabled) {
2174         if (index < 0) {
2175             // if the effect is not suspend check if all effects are suspended
2176             index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2177             if (index < 0) {
2178                 return;
2179             }
2180             if (!isEffectEligibleForSuspend(effect->desc())) {
2181                 return;
2182             }
2183             setEffectSuspended_l(&effect->desc().type, enabled);
2184             index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2185             if (index < 0) {
2186                 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2187                 return;
2188             }
2189         }
2190         ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2191             effect->desc().type.timeLow);
2192         sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2193         // if effect is requested to suspended but was not yet enabled, suspend it now.
2194         if (desc->mEffect == 0) {
2195             desc->mEffect = effect;
2196             effect->setEnabled(false);
2197             effect->setSuspended(true);
2198         }
2199     } else {
2200         if (index < 0) {
2201             return;
2202         }
2203         ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2204             effect->desc().type.timeLow);
2205         sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2206         desc->mEffect.clear();
2207         effect->setSuspended(false);
2208     }
2209 }
2210 
isNonOffloadableEnabled()2211 bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
2212 {
2213     Mutex::Autolock _l(mLock);
2214     size_t size = mEffects.size();
2215     for (size_t i = 0; i < size; i++) {
2216         if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
2217             return true;
2218         }
2219     }
2220     return false;
2221 }
2222 
setThread(const sp<ThreadBase> & thread)2223 void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2224 {
2225     Mutex::Autolock _l(mLock);
2226     mThread = thread;
2227     for (size_t i = 0; i < mEffects.size(); i++) {
2228         mEffects[i]->setThread(thread);
2229     }
2230 }
2231 
checkOutputFlagCompatibility(audio_output_flags_t * flags) const2232 void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2233 {
2234     if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2235         *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2236     }
2237     if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2238         *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2239     }
2240 }
2241 
checkInputFlagCompatibility(audio_input_flags_t * flags) const2242 void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2243 {
2244     if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2245         *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2246     }
2247     if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2248         *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2249     }
2250 }
2251 
isRawCompatible() const2252 bool AudioFlinger::EffectChain::isRawCompatible() const
2253 {
2254     Mutex::Autolock _l(mLock);
2255     for (const auto &effect : mEffects) {
2256         if (effect->isProcessImplemented()) {
2257             return false;
2258         }
2259     }
2260     // Allow effects without processing.
2261     return true;
2262 }
2263 
isFastCompatible() const2264 bool AudioFlinger::EffectChain::isFastCompatible() const
2265 {
2266     Mutex::Autolock _l(mLock);
2267     for (const auto &effect : mEffects) {
2268         if (effect->isProcessImplemented()
2269                 && effect->isImplementationSoftware()) {
2270             return false;
2271         }
2272     }
2273     // Allow effects without processing or hw accelerated effects.
2274     return true;
2275 }
2276 
2277 // isCompatibleWithThread_l() must be called with thread->mLock held
isCompatibleWithThread_l(const sp<ThreadBase> & thread) const2278 bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2279 {
2280     Mutex::Autolock _l(mLock);
2281     for (size_t i = 0; i < mEffects.size(); i++) {
2282         if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2283             return false;
2284         }
2285     }
2286     return true;
2287 }
2288 
2289 } // namespace android
2290