Effects.cpp revision fed6292af65a0b97b583ecbd3c232b3811a3f37b
10b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin/*
20b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin**
30b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin** Copyright 2012, The Android Open Source Project
40b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin**
50b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin** Licensed under the Apache License, Version 2.0 (the "License");
60b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin** you may not use this file except in compliance with the License.
70b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin** You may obtain a copy of the License at
80b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin**
90b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin**     http://www.apache.org/licenses/LICENSE-2.0
100b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin**
110b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin** Unless required by applicable law or agreed to in writing, software
120b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin** distributed under the License is distributed on an "AS IS" BASIS,
130b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
140b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin** See the License for the specific language governing permissions and
150b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin** limitations under the License.
160b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin*/
170b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin
180b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin
190b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin#define LOG_TAG "AudioFlinger"
200b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin//#define LOG_NDEBUG 0
210b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin
220b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin#include "Configuration.h"
230b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin#include <utils/Log.h>
240b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin#include <audio_effects/effect_visualizer.h>
250b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin#include <audio_utils/primitives.h>
260b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin#include <private/media/AudioEffectShared.h>
270b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin#include <media/EffectsFactoryApi.h>
280b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin
290b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin#include "AudioFlinger.h"
300b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin#include "ServiceUtilities.h"
310b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin
320b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin// ----------------------------------------------------------------------------
330b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin
340b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin// Note: the following macro is used for extremely verbose logging message.  In
350b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
360b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin// 0; but one side effect of this is to turn all LOGV's as well.  Some messages
370b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin// are so verbose that we want to suppress them even when we have ALOG_ASSERT
380b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin// turned on.  Do not uncomment the #def below unless you really know what you
390b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin// are doing and want to see all of the extremely verbose messages.
400b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin//#define VERY_VERY_VERBOSE_LOGGING
410b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin#ifdef VERY_VERY_VERBOSE_LOGGING
420b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin#define ALOGVV ALOGV
430b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin#else
440b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin#define ALOGVV(a...) do { } while(0)
450b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin#endif
460b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin
470b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkinnamespace android {
480b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin
490b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin// ----------------------------------------------------------------------------
500b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin//  EffectModule implementation
510b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin// ----------------------------------------------------------------------------
520b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin
530b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin#undef LOG_TAG
540b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin#define LOG_TAG "AudioFlinger::EffectModule"
550b27d3453d5e257594792e9177c5fedb1bc6f9e9Igor Murashkin
56AudioFlinger::EffectModule::EffectModule(ThreadBase *thread,
57                                        const wp<AudioFlinger::EffectChain>& chain,
58                                        effect_descriptor_t *desc,
59                                        int id,
60                                        int sessionId)
61    : mPinned(sessionId > AUDIO_SESSION_OUTPUT_MIX),
62      mThread(thread), mChain(chain), mId(id), mSessionId(sessionId),
63      mDescriptor(*desc),
64      // mConfig is set by configure() and not used before then
65      mEffectInterface(NULL),
66      mStatus(NO_INIT), mState(IDLE),
67      // mMaxDisableWaitCnt is set by configure() and not used before then
68      // mDisableWaitCnt is set by process() and updateState() and not used before then
69      mSuspended(false)
70{
71    ALOGV("Constructor %p", this);
72    int lStatus;
73
74    // create effect engine from effect factory
75    mStatus = EffectCreate(&desc->uuid, sessionId, thread->id(), &mEffectInterface);
76
77    if (mStatus != NO_ERROR) {
78        return;
79    }
80    lStatus = init();
81    if (lStatus < 0) {
82        mStatus = lStatus;
83        goto Error;
84    }
85
86    ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface);
87    return;
88Error:
89    EffectRelease(mEffectInterface);
90    mEffectInterface = NULL;
91    ALOGV("Constructor Error %d", mStatus);
92}
93
94AudioFlinger::EffectModule::~EffectModule()
95{
96    ALOGV("Destructor %p", this);
97    if (mEffectInterface != NULL) {
98        remove_effect_from_hal_l();
99        // release effect engine
100        EffectRelease(mEffectInterface);
101    }
102}
103
104status_t AudioFlinger::EffectModule::addHandle(EffectHandle *handle)
105{
106    status_t status;
107
108    Mutex::Autolock _l(mLock);
109    int priority = handle->priority();
110    size_t size = mHandles.size();
111    EffectHandle *controlHandle = NULL;
112    size_t i;
113    for (i = 0; i < size; i++) {
114        EffectHandle *h = mHandles[i];
115        if (h == NULL || h->destroyed_l()) {
116            continue;
117        }
118        // first non destroyed handle is considered in control
119        if (controlHandle == NULL)
120            controlHandle = h;
121        if (h->priority() <= priority) {
122            break;
123        }
124    }
125    // if inserted in first place, move effect control from previous owner to this handle
126    if (i == 0) {
127        bool enabled = false;
128        if (controlHandle != NULL) {
129            enabled = controlHandle->enabled();
130            controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
131        }
132        handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
133        status = NO_ERROR;
134    } else {
135        status = ALREADY_EXISTS;
136    }
137    ALOGV("addHandle() %p added handle %p in position %d", this, handle, i);
138    mHandles.insertAt(handle, i);
139    return status;
140}
141
142size_t AudioFlinger::EffectModule::removeHandle(EffectHandle *handle)
143{
144    Mutex::Autolock _l(mLock);
145    size_t size = mHandles.size();
146    size_t i;
147    for (i = 0; i < size; i++) {
148        if (mHandles[i] == handle) {
149            break;
150        }
151    }
152    if (i == size) {
153        return size;
154    }
155    ALOGV("removeHandle() %p removed handle %p in position %d", this, handle, i);
156
157    mHandles.removeAt(i);
158    // if removed from first place, move effect control from this handle to next in line
159    if (i == 0) {
160        EffectHandle *h = controlHandle_l();
161        if (h != NULL) {
162            h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
163        }
164    }
165
166    // Prevent calls to process() and other functions on effect interface from now on.
167    // The effect engine will be released by the destructor when the last strong reference on
168    // this object is released which can happen after next process is called.
169    if (mHandles.size() == 0 && !mPinned) {
170        mState = DESTROYED;
171    }
172
173    return mHandles.size();
174}
175
176// must be called with EffectModule::mLock held
177AudioFlinger::EffectHandle *AudioFlinger::EffectModule::controlHandle_l()
178{
179    // the first valid handle in the list has control over the module
180    for (size_t i = 0; i < mHandles.size(); i++) {
181        EffectHandle *h = mHandles[i];
182        if (h != NULL && !h->destroyed_l()) {
183            return h;
184        }
185    }
186
187    return NULL;
188}
189
190size_t AudioFlinger::EffectModule::disconnect(EffectHandle *handle, bool unpinIfLast)
191{
192    ALOGV("disconnect() %p handle %p", this, handle);
193    // keep a strong reference on this EffectModule to avoid calling the
194    // destructor before we exit
195    sp<EffectModule> keep(this);
196    {
197        sp<ThreadBase> thread = mThread.promote();
198        if (thread != 0) {
199            thread->disconnectEffect(keep, handle, unpinIfLast);
200        }
201    }
202    return mHandles.size();
203}
204
205void AudioFlinger::EffectModule::updateState() {
206    Mutex::Autolock _l(mLock);
207
208    switch (mState) {
209    case RESTART:
210        reset_l();
211        // FALL THROUGH
212
213    case STARTING:
214        // clear auxiliary effect input buffer for next accumulation
215        if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
216            memset(mConfig.inputCfg.buffer.raw,
217                   0,
218                   mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
219        }
220        if (start_l() == NO_ERROR) {
221            mState = ACTIVE;
222        } else {
223            mState = IDLE;
224        }
225        break;
226    case STOPPING:
227        if (stop_l() == NO_ERROR) {
228            mDisableWaitCnt = mMaxDisableWaitCnt;
229        } else {
230            mDisableWaitCnt = 1; // will cause immediate transition to IDLE
231        }
232        mState = STOPPED;
233        break;
234    case STOPPED:
235        // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
236        // turn off sequence.
237        if (--mDisableWaitCnt == 0) {
238            reset_l();
239            mState = IDLE;
240        }
241        break;
242    default: //IDLE , ACTIVE, DESTROYED
243        break;
244    }
245}
246
247void AudioFlinger::EffectModule::process()
248{
249    Mutex::Autolock _l(mLock);
250
251    if (mState == DESTROYED || mEffectInterface == NULL ||
252            mConfig.inputCfg.buffer.raw == NULL ||
253            mConfig.outputCfg.buffer.raw == NULL) {
254        return;
255    }
256
257    if (isProcessEnabled()) {
258        // do 32 bit to 16 bit conversion for auxiliary effect input buffer
259        if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
260            ditherAndClamp(mConfig.inputCfg.buffer.s32,
261                                        mConfig.inputCfg.buffer.s32,
262                                        mConfig.inputCfg.buffer.frameCount/2);
263        }
264
265        // do the actual processing in the effect engine
266        int ret = (*mEffectInterface)->process(mEffectInterface,
267                                               &mConfig.inputCfg.buffer,
268                                               &mConfig.outputCfg.buffer);
269
270        // force transition to IDLE state when engine is ready
271        if (mState == STOPPED && ret == -ENODATA) {
272            mDisableWaitCnt = 1;
273        }
274
275        // clear auxiliary effect input buffer for next accumulation
276        if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
277            memset(mConfig.inputCfg.buffer.raw, 0,
278                   mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
279        }
280    } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
281                mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
282        // If an insert effect is idle and input buffer is different from output buffer,
283        // accumulate input onto output
284        sp<EffectChain> chain = mChain.promote();
285        if (chain != 0 && chain->activeTrackCnt() != 0) {
286            size_t frameCnt = mConfig.inputCfg.buffer.frameCount * 2;  //always stereo here
287            int16_t *in = mConfig.inputCfg.buffer.s16;
288            int16_t *out = mConfig.outputCfg.buffer.s16;
289            for (size_t i = 0; i < frameCnt; i++) {
290                out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]);
291            }
292        }
293    }
294}
295
296void AudioFlinger::EffectModule::reset_l()
297{
298    if (mStatus != NO_ERROR || mEffectInterface == NULL) {
299        return;
300    }
301    (*mEffectInterface)->command(mEffectInterface, EFFECT_CMD_RESET, 0, NULL, 0, NULL);
302}
303
304status_t AudioFlinger::EffectModule::configure()
305{
306    status_t status;
307    sp<ThreadBase> thread;
308    uint32_t size;
309    audio_channel_mask_t channelMask;
310
311    if (mEffectInterface == NULL) {
312        status = NO_INIT;
313        goto exit;
314    }
315
316    thread = mThread.promote();
317    if (thread == 0) {
318        status = DEAD_OBJECT;
319        goto exit;
320    }
321
322    // TODO: handle configuration of effects replacing track process
323    channelMask = thread->channelMask();
324
325    if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
326        mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
327    } else {
328        mConfig.inputCfg.channels = channelMask;
329    }
330    mConfig.outputCfg.channels = channelMask;
331    mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
332    mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
333    mConfig.inputCfg.samplingRate = thread->sampleRate();
334    mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
335    mConfig.inputCfg.bufferProvider.cookie = NULL;
336    mConfig.inputCfg.bufferProvider.getBuffer = NULL;
337    mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
338    mConfig.outputCfg.bufferProvider.cookie = NULL;
339    mConfig.outputCfg.bufferProvider.getBuffer = NULL;
340    mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
341    mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
342    // Insert effect:
343    // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
344    // always overwrites output buffer: input buffer == output buffer
345    // - in other sessions:
346    //      last effect in the chain accumulates in output buffer: input buffer != output buffer
347    //      other effect: overwrites output buffer: input buffer == output buffer
348    // Auxiliary effect:
349    //      accumulates in output buffer: input buffer != output buffer
350    // Therefore: accumulate <=> input buffer != output buffer
351    if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
352        mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
353    } else {
354        mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
355    }
356    mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
357    mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
358    mConfig.inputCfg.buffer.frameCount = thread->frameCount();
359    mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
360
361    ALOGV("configure() %p thread %p buffer %p framecount %d",
362            this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
363
364    status_t cmdStatus;
365    size = sizeof(int);
366    status = (*mEffectInterface)->command(mEffectInterface,
367                                                   EFFECT_CMD_SET_CONFIG,
368                                                   sizeof(effect_config_t),
369                                                   &mConfig,
370                                                   &size,
371                                                   &cmdStatus);
372    if (status == 0) {
373        status = cmdStatus;
374    }
375
376    if (status == 0 &&
377            (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0)) {
378        uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
379        effect_param_t *p = (effect_param_t *)buf32;
380
381        p->psize = sizeof(uint32_t);
382        p->vsize = sizeof(uint32_t);
383        size = sizeof(int);
384        *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
385
386        uint32_t latency = 0;
387        PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId);
388        if (pbt != NULL) {
389            latency = pbt->latency_l();
390        }
391
392        *((int32_t *)p->data + 1)= latency;
393        (*mEffectInterface)->command(mEffectInterface,
394                                     EFFECT_CMD_SET_PARAM,
395                                     sizeof(effect_param_t) + 8,
396                                     &buf32,
397                                     &size,
398                                     &cmdStatus);
399    }
400
401    mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) /
402            (1000 * mConfig.outputCfg.buffer.frameCount);
403
404exit:
405    mStatus = status;
406    return status;
407}
408
409status_t AudioFlinger::EffectModule::init()
410{
411    Mutex::Autolock _l(mLock);
412    if (mEffectInterface == NULL) {
413        return NO_INIT;
414    }
415    status_t cmdStatus;
416    uint32_t size = sizeof(status_t);
417    status_t status = (*mEffectInterface)->command(mEffectInterface,
418                                                   EFFECT_CMD_INIT,
419                                                   0,
420                                                   NULL,
421                                                   &size,
422                                                   &cmdStatus);
423    if (status == 0) {
424        status = cmdStatus;
425    }
426    return status;
427}
428
429status_t AudioFlinger::EffectModule::start()
430{
431    Mutex::Autolock _l(mLock);
432    return start_l();
433}
434
435status_t AudioFlinger::EffectModule::start_l()
436{
437    if (mEffectInterface == NULL) {
438        return NO_INIT;
439    }
440    if (mStatus != NO_ERROR) {
441        return mStatus;
442    }
443    status_t cmdStatus;
444    uint32_t size = sizeof(status_t);
445    status_t status = (*mEffectInterface)->command(mEffectInterface,
446                                                   EFFECT_CMD_ENABLE,
447                                                   0,
448                                                   NULL,
449                                                   &size,
450                                                   &cmdStatus);
451    if (status == 0) {
452        status = cmdStatus;
453    }
454    if (status == 0 &&
455            ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
456             (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC)) {
457        sp<ThreadBase> thread = mThread.promote();
458        if (thread != 0) {
459            audio_stream_t *stream = thread->stream();
460            if (stream != NULL) {
461                stream->add_audio_effect(stream, mEffectInterface);
462            }
463        }
464    }
465    return status;
466}
467
468status_t AudioFlinger::EffectModule::stop()
469{
470    Mutex::Autolock _l(mLock);
471    return stop_l();
472}
473
474status_t AudioFlinger::EffectModule::stop_l()
475{
476    if (mEffectInterface == NULL) {
477        return NO_INIT;
478    }
479    if (mStatus != NO_ERROR) {
480        return mStatus;
481    }
482    status_t cmdStatus = NO_ERROR;
483    uint32_t size = sizeof(status_t);
484    status_t status = (*mEffectInterface)->command(mEffectInterface,
485                                                   EFFECT_CMD_DISABLE,
486                                                   0,
487                                                   NULL,
488                                                   &size,
489                                                   &cmdStatus);
490    if (status == NO_ERROR) {
491        status = cmdStatus;
492    }
493    if (status == NO_ERROR) {
494        status = remove_effect_from_hal_l();
495    }
496    return status;
497}
498
499status_t AudioFlinger::EffectModule::remove_effect_from_hal_l()
500{
501    if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
502             (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
503        sp<ThreadBase> thread = mThread.promote();
504        if (thread != 0) {
505            audio_stream_t *stream = thread->stream();
506            if (stream != NULL) {
507                stream->remove_audio_effect(stream, mEffectInterface);
508            }
509        }
510    }
511    return NO_ERROR;
512}
513
514status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
515                                             uint32_t cmdSize,
516                                             void *pCmdData,
517                                             uint32_t *replySize,
518                                             void *pReplyData)
519{
520    Mutex::Autolock _l(mLock);
521    ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface);
522
523    if (mState == DESTROYED || mEffectInterface == NULL) {
524        return NO_INIT;
525    }
526    if (mStatus != NO_ERROR) {
527        return mStatus;
528    }
529    status_t status = (*mEffectInterface)->command(mEffectInterface,
530                                                   cmdCode,
531                                                   cmdSize,
532                                                   pCmdData,
533                                                   replySize,
534                                                   pReplyData);
535    if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
536        uint32_t size = (replySize == NULL) ? 0 : *replySize;
537        for (size_t i = 1; i < mHandles.size(); i++) {
538            EffectHandle *h = mHandles[i];
539            if (h != NULL && !h->destroyed_l()) {
540                h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
541            }
542        }
543    }
544    return status;
545}
546
547status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
548{
549    Mutex::Autolock _l(mLock);
550    return setEnabled_l(enabled);
551}
552
553// must be called with EffectModule::mLock held
554status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
555{
556
557    ALOGV("setEnabled %p enabled %d", this, enabled);
558
559    if (enabled != isEnabled()) {
560        status_t status = AudioSystem::setEffectEnabled(mId, enabled);
561        if (enabled && status != NO_ERROR) {
562            return status;
563        }
564
565        switch (mState) {
566        // going from disabled to enabled
567        case IDLE:
568            mState = STARTING;
569            break;
570        case STOPPED:
571            mState = RESTART;
572            break;
573        case STOPPING:
574            mState = ACTIVE;
575            break;
576
577        // going from enabled to disabled
578        case RESTART:
579            mState = STOPPED;
580            break;
581        case STARTING:
582            mState = IDLE;
583            break;
584        case ACTIVE:
585            mState = STOPPING;
586            break;
587        case DESTROYED:
588            return NO_ERROR; // simply ignore as we are being destroyed
589        }
590        for (size_t i = 1; i < mHandles.size(); i++) {
591            EffectHandle *h = mHandles[i];
592            if (h != NULL && !h->destroyed_l()) {
593                h->setEnabled(enabled);
594            }
595        }
596    }
597    return NO_ERROR;
598}
599
600bool AudioFlinger::EffectModule::isEnabled() const
601{
602    switch (mState) {
603    case RESTART:
604    case STARTING:
605    case ACTIVE:
606        return true;
607    case IDLE:
608    case STOPPING:
609    case STOPPED:
610    case DESTROYED:
611    default:
612        return false;
613    }
614}
615
616bool AudioFlinger::EffectModule::isProcessEnabled() const
617{
618    if (mStatus != NO_ERROR) {
619        return false;
620    }
621
622    switch (mState) {
623    case RESTART:
624    case ACTIVE:
625    case STOPPING:
626    case STOPPED:
627        return true;
628    case IDLE:
629    case STARTING:
630    case DESTROYED:
631    default:
632        return false;
633    }
634}
635
636status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
637{
638    Mutex::Autolock _l(mLock);
639    if (mStatus != NO_ERROR) {
640        return mStatus;
641    }
642    status_t status = NO_ERROR;
643    // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
644    // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
645    if (isProcessEnabled() &&
646            ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
647            (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
648        status_t cmdStatus;
649        uint32_t volume[2];
650        uint32_t *pVolume = NULL;
651        uint32_t size = sizeof(volume);
652        volume[0] = *left;
653        volume[1] = *right;
654        if (controller) {
655            pVolume = volume;
656        }
657        status = (*mEffectInterface)->command(mEffectInterface,
658                                              EFFECT_CMD_SET_VOLUME,
659                                              size,
660                                              volume,
661                                              &size,
662                                              pVolume);
663        if (controller && status == NO_ERROR && size == sizeof(volume)) {
664            *left = volume[0];
665            *right = volume[1];
666        }
667    }
668    return status;
669}
670
671status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
672{
673    if (device == AUDIO_DEVICE_NONE) {
674        return NO_ERROR;
675    }
676
677    Mutex::Autolock _l(mLock);
678    if (mStatus != NO_ERROR) {
679        return mStatus;
680    }
681    status_t status = NO_ERROR;
682    if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
683        status_t cmdStatus;
684        uint32_t size = sizeof(status_t);
685        uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
686                            EFFECT_CMD_SET_INPUT_DEVICE;
687        status = (*mEffectInterface)->command(mEffectInterface,
688                                              cmd,
689                                              sizeof(uint32_t),
690                                              &device,
691                                              &size,
692                                              &cmdStatus);
693    }
694    return status;
695}
696
697status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
698{
699    Mutex::Autolock _l(mLock);
700    if (mStatus != NO_ERROR) {
701        return mStatus;
702    }
703    status_t status = NO_ERROR;
704    if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
705        status_t cmdStatus;
706        uint32_t size = sizeof(status_t);
707        status = (*mEffectInterface)->command(mEffectInterface,
708                                              EFFECT_CMD_SET_AUDIO_MODE,
709                                              sizeof(audio_mode_t),
710                                              &mode,
711                                              &size,
712                                              &cmdStatus);
713        if (status == NO_ERROR) {
714            status = cmdStatus;
715        }
716    }
717    return status;
718}
719
720status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
721{
722    Mutex::Autolock _l(mLock);
723    if (mStatus != NO_ERROR) {
724        return mStatus;
725    }
726    status_t status = NO_ERROR;
727    if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
728        uint32_t size = 0;
729        status = (*mEffectInterface)->command(mEffectInterface,
730                                              EFFECT_CMD_SET_AUDIO_SOURCE,
731                                              sizeof(audio_source_t),
732                                              &source,
733                                              &size,
734                                              NULL);
735    }
736    return status;
737}
738
739void AudioFlinger::EffectModule::setSuspended(bool suspended)
740{
741    Mutex::Autolock _l(mLock);
742    mSuspended = suspended;
743}
744
745bool AudioFlinger::EffectModule::suspended() const
746{
747    Mutex::Autolock _l(mLock);
748    return mSuspended;
749}
750
751bool AudioFlinger::EffectModule::purgeHandles()
752{
753    bool enabled = false;
754    Mutex::Autolock _l(mLock);
755    for (size_t i = 0; i < mHandles.size(); i++) {
756        EffectHandle *handle = mHandles[i];
757        if (handle != NULL && !handle->destroyed_l()) {
758            handle->effect().clear();
759            if (handle->hasControl()) {
760                enabled = handle->enabled();
761            }
762        }
763    }
764    return enabled;
765}
766
767status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
768{
769    Mutex::Autolock _l(mLock);
770    if (mStatus != NO_ERROR) {
771        return mStatus;
772    }
773    status_t status = NO_ERROR;
774    if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
775        status_t cmdStatus;
776        uint32_t size = sizeof(status_t);
777        effect_offload_param_t cmd;
778
779        cmd.isOffload = offloaded;
780        cmd.ioHandle = io;
781        status = (*mEffectInterface)->command(mEffectInterface,
782                                              EFFECT_CMD_OFFLOAD,
783                                              sizeof(effect_offload_param_t),
784                                              &cmd,
785                                              &size,
786                                              &cmdStatus);
787        if (status == NO_ERROR) {
788            status = cmdStatus;
789        }
790        mOffloaded = (status == NO_ERROR) ? offloaded : false;
791    } else {
792        if (offloaded) {
793            status = INVALID_OPERATION;
794        }
795        mOffloaded = false;
796    }
797    ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
798    return status;
799}
800
801bool AudioFlinger::EffectModule::isOffloaded() const
802{
803    Mutex::Autolock _l(mLock);
804    return mOffloaded;
805}
806
807void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args)
808{
809    const size_t SIZE = 256;
810    char buffer[SIZE];
811    String8 result;
812
813    snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId);
814    result.append(buffer);
815
816    bool locked = AudioFlinger::dumpTryLock(mLock);
817    // failed to lock - AudioFlinger is probably deadlocked
818    if (!locked) {
819        result.append("\t\tCould not lock Fx mutex:\n");
820    }
821
822    result.append("\t\tSession Status State Engine:\n");
823    snprintf(buffer, SIZE, "\t\t%05d   %03d    %03d   0x%08x\n",
824            mSessionId, mStatus, mState, (uint32_t)mEffectInterface);
825    result.append(buffer);
826
827    result.append("\t\tDescriptor:\n");
828    snprintf(buffer, SIZE, "\t\t- UUID: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
829            mDescriptor.uuid.timeLow, mDescriptor.uuid.timeMid, mDescriptor.uuid.timeHiAndVersion,
830            mDescriptor.uuid.clockSeq, mDescriptor.uuid.node[0], mDescriptor.uuid.node[1],
831                    mDescriptor.uuid.node[2],
832            mDescriptor.uuid.node[3],mDescriptor.uuid.node[4],mDescriptor.uuid.node[5]);
833    result.append(buffer);
834    snprintf(buffer, SIZE, "\t\t- TYPE: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
835                mDescriptor.type.timeLow, mDescriptor.type.timeMid,
836                    mDescriptor.type.timeHiAndVersion,
837                mDescriptor.type.clockSeq, mDescriptor.type.node[0], mDescriptor.type.node[1],
838                    mDescriptor.type.node[2],
839                mDescriptor.type.node[3],mDescriptor.type.node[4],mDescriptor.type.node[5]);
840    result.append(buffer);
841    snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X\n",
842            mDescriptor.apiVersion,
843            mDescriptor.flags);
844    result.append(buffer);
845    snprintf(buffer, SIZE, "\t\t- name: %s\n",
846            mDescriptor.name);
847    result.append(buffer);
848    snprintf(buffer, SIZE, "\t\t- implementor: %s\n",
849            mDescriptor.implementor);
850    result.append(buffer);
851
852    result.append("\t\t- Input configuration:\n");
853    result.append("\t\t\tBuffer     Frames  Smp rate Channels Format\n");
854    snprintf(buffer, SIZE, "\t\t\t0x%08x %05d   %05d    %08x %d\n",
855            (uint32_t)mConfig.inputCfg.buffer.raw,
856            mConfig.inputCfg.buffer.frameCount,
857            mConfig.inputCfg.samplingRate,
858            mConfig.inputCfg.channels,
859            mConfig.inputCfg.format);
860    result.append(buffer);
861
862    result.append("\t\t- Output configuration:\n");
863    result.append("\t\t\tBuffer     Frames  Smp rate Channels Format\n");
864    snprintf(buffer, SIZE, "\t\t\t0x%08x %05d   %05d    %08x %d\n",
865            (uint32_t)mConfig.outputCfg.buffer.raw,
866            mConfig.outputCfg.buffer.frameCount,
867            mConfig.outputCfg.samplingRate,
868            mConfig.outputCfg.channels,
869            mConfig.outputCfg.format);
870    result.append(buffer);
871
872    snprintf(buffer, SIZE, "\t\t%d Clients:\n", mHandles.size());
873    result.append(buffer);
874    result.append("\t\t\tPid   Priority Ctrl Locked client server\n");
875    for (size_t i = 0; i < mHandles.size(); ++i) {
876        EffectHandle *handle = mHandles[i];
877        if (handle != NULL && !handle->destroyed_l()) {
878            handle->dump(buffer, SIZE);
879            result.append(buffer);
880        }
881    }
882
883    result.append("\n");
884
885    write(fd, result.string(), result.length());
886
887    if (locked) {
888        mLock.unlock();
889    }
890}
891
892// ----------------------------------------------------------------------------
893//  EffectHandle implementation
894// ----------------------------------------------------------------------------
895
896#undef LOG_TAG
897#define LOG_TAG "AudioFlinger::EffectHandle"
898
899AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
900                                        const sp<AudioFlinger::Client>& client,
901                                        const sp<IEffectClient>& effectClient,
902                                        int32_t priority)
903    : BnEffect(),
904    mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
905    mPriority(priority), mHasControl(false), mEnabled(false), mDestroyed(false)
906{
907    ALOGV("constructor %p", this);
908
909    if (client == 0) {
910        return;
911    }
912    int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
913    mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
914    if (mCblkMemory != 0) {
915        mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer());
916
917        if (mCblk != NULL) {
918            new(mCblk) effect_param_cblk_t();
919            mBuffer = (uint8_t *)mCblk + bufOffset;
920        }
921    } else {
922        ALOGE("not enough memory for Effect size=%u", EFFECT_PARAM_BUFFER_SIZE +
923                sizeof(effect_param_cblk_t));
924        return;
925    }
926}
927
928AudioFlinger::EffectHandle::~EffectHandle()
929{
930    ALOGV("Destructor %p", this);
931
932    if (mEffect == 0) {
933        mDestroyed = true;
934        return;
935    }
936    mEffect->lock();
937    mDestroyed = true;
938    mEffect->unlock();
939    disconnect(false);
940}
941
942status_t AudioFlinger::EffectHandle::enable()
943{
944    ALOGV("enable %p", this);
945    if (!mHasControl) {
946        return INVALID_OPERATION;
947    }
948    if (mEffect == 0) {
949        return DEAD_OBJECT;
950    }
951
952    if (mEnabled) {
953        return NO_ERROR;
954    }
955
956    mEnabled = true;
957
958    sp<ThreadBase> thread = mEffect->thread().promote();
959    if (thread != 0) {
960        thread->checkSuspendOnEffectEnabled(mEffect, true, mEffect->sessionId());
961    }
962
963    // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
964    if (mEffect->suspended()) {
965        return NO_ERROR;
966    }
967
968    status_t status = mEffect->setEnabled(true);
969    if (status != NO_ERROR) {
970        if (thread != 0) {
971            thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
972        }
973        mEnabled = false;
974    } else {
975        if (thread != 0 && !mEffect->isOffloadable()) {
976            if ((thread->type() == ThreadBase::OFFLOAD)) {
977                PlaybackThread *t = (PlaybackThread *)thread.get();
978                t->invalidateTracks(AUDIO_STREAM_MUSIC);
979            }
980            if (mEffect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
981                thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
982            }
983        }
984    }
985    return status;
986}
987
988status_t AudioFlinger::EffectHandle::disable()
989{
990    ALOGV("disable %p", this);
991    if (!mHasControl) {
992        return INVALID_OPERATION;
993    }
994    if (mEffect == 0) {
995        return DEAD_OBJECT;
996    }
997
998    if (!mEnabled) {
999        return NO_ERROR;
1000    }
1001    mEnabled = false;
1002
1003    if (mEffect->suspended()) {
1004        return NO_ERROR;
1005    }
1006
1007    status_t status = mEffect->setEnabled(false);
1008
1009    sp<ThreadBase> thread = mEffect->thread().promote();
1010    if (thread != 0) {
1011        thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
1012    }
1013
1014    return status;
1015}
1016
1017void AudioFlinger::EffectHandle::disconnect()
1018{
1019    disconnect(true);
1020}
1021
1022void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1023{
1024    ALOGV("disconnect(%s)", unpinIfLast ? "true" : "false");
1025    if (mEffect == 0) {
1026        return;
1027    }
1028    // restore suspended effects if the disconnected handle was enabled and the last one.
1029    if ((mEffect->disconnect(this, unpinIfLast) == 0) && mEnabled) {
1030        sp<ThreadBase> thread = mEffect->thread().promote();
1031        if (thread != 0) {
1032            thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
1033        }
1034    }
1035
1036    // release sp on module => module destructor can be called now
1037    mEffect.clear();
1038    if (mClient != 0) {
1039        if (mCblk != NULL) {
1040            // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1041            mCblk->~effect_param_cblk_t();   // destroy our shared-structure.
1042        }
1043        mCblkMemory.clear();    // free the shared memory before releasing the heap it belongs to
1044        // Client destructor must run with AudioFlinger mutex locked
1045        Mutex::Autolock _l(mClient->audioFlinger()->mLock);
1046        mClient.clear();
1047    }
1048}
1049
1050status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1051                                             uint32_t cmdSize,
1052                                             void *pCmdData,
1053                                             uint32_t *replySize,
1054                                             void *pReplyData)
1055{
1056    ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
1057            cmdCode, mHasControl, (mEffect == 0) ? 0 : mEffect.get());
1058
1059    // only get parameter command is permitted for applications not controlling the effect
1060    if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1061        return INVALID_OPERATION;
1062    }
1063    if (mEffect == 0) {
1064        return DEAD_OBJECT;
1065    }
1066    if (mClient == 0) {
1067        return INVALID_OPERATION;
1068    }
1069
1070    // handle commands that are not forwarded transparently to effect engine
1071    if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
1072        // No need to trylock() here as this function is executed in the binder thread serving a
1073        // particular client process:  no risk to block the whole media server process or mixer
1074        // threads if we are stuck here
1075        Mutex::Autolock _l(mCblk->lock);
1076        if (mCblk->clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1077            mCblk->serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
1078            mCblk->serverIndex = 0;
1079            mCblk->clientIndex = 0;
1080            return BAD_VALUE;
1081        }
1082        status_t status = NO_ERROR;
1083        while (mCblk->serverIndex < mCblk->clientIndex) {
1084            int reply;
1085            uint32_t rsize = sizeof(int);
1086            int *p = (int *)(mBuffer + mCblk->serverIndex);
1087            int size = *p++;
1088            if (((uint8_t *)p + size) > mBuffer + mCblk->clientIndex) {
1089                ALOGW("command(): invalid parameter block size");
1090                break;
1091            }
1092            effect_param_t *param = (effect_param_t *)p;
1093            if (param->psize == 0 || param->vsize == 0) {
1094                ALOGW("command(): null parameter or value size");
1095                mCblk->serverIndex += size;
1096                continue;
1097            }
1098            uint32_t psize = sizeof(effect_param_t) +
1099                             ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) +
1100                             param->vsize;
1101            status_t ret = mEffect->command(EFFECT_CMD_SET_PARAM,
1102                                            psize,
1103                                            p,
1104                                            &rsize,
1105                                            &reply);
1106            // stop at first error encountered
1107            if (ret != NO_ERROR) {
1108                status = ret;
1109                *(int *)pReplyData = reply;
1110                break;
1111            } else if (reply != NO_ERROR) {
1112                *(int *)pReplyData = reply;
1113                break;
1114            }
1115            mCblk->serverIndex += size;
1116        }
1117        mCblk->serverIndex = 0;
1118        mCblk->clientIndex = 0;
1119        return status;
1120    } else if (cmdCode == EFFECT_CMD_ENABLE) {
1121        *(int *)pReplyData = NO_ERROR;
1122        return enable();
1123    } else if (cmdCode == EFFECT_CMD_DISABLE) {
1124        *(int *)pReplyData = NO_ERROR;
1125        return disable();
1126    }
1127
1128    return mEffect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1129}
1130
1131void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1132{
1133    ALOGV("setControl %p control %d", this, hasControl);
1134
1135    mHasControl = hasControl;
1136    mEnabled = enabled;
1137
1138    if (signal && mEffectClient != 0) {
1139        mEffectClient->controlStatusChanged(hasControl);
1140    }
1141}
1142
1143void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1144                                                 uint32_t cmdSize,
1145                                                 void *pCmdData,
1146                                                 uint32_t replySize,
1147                                                 void *pReplyData)
1148{
1149    if (mEffectClient != 0) {
1150        mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1151    }
1152}
1153
1154
1155
1156void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1157{
1158    if (mEffectClient != 0) {
1159        mEffectClient->enableStatusChanged(enabled);
1160    }
1161}
1162
1163status_t AudioFlinger::EffectHandle::onTransact(
1164    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1165{
1166    return BnEffect::onTransact(code, data, reply, flags);
1167}
1168
1169
1170void AudioFlinger::EffectHandle::dump(char* buffer, size_t size)
1171{
1172    bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1173
1174    snprintf(buffer, size, "\t\t\t%05d %05d    %01u    %01u      %05u  %05u\n",
1175            (mClient == 0) ? getpid_cached : mClient->pid(),
1176            mPriority,
1177            mHasControl,
1178            !locked,
1179            mCblk ? mCblk->clientIndex : 0,
1180            mCblk ? mCblk->serverIndex : 0
1181            );
1182
1183    if (locked) {
1184        mCblk->lock.unlock();
1185    }
1186}
1187
1188#undef LOG_TAG
1189#define LOG_TAG "AudioFlinger::EffectChain"
1190
1191AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
1192                                        int sessionId)
1193    : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
1194      mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
1195      mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
1196{
1197    mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1198    if (thread == NULL) {
1199        return;
1200    }
1201    mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1202                                    thread->frameCount();
1203}
1204
1205AudioFlinger::EffectChain::~EffectChain()
1206{
1207    if (mOwnInBuffer) {
1208        delete mInBuffer;
1209    }
1210
1211}
1212
1213// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1214sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1215        effect_descriptor_t *descriptor)
1216{
1217    size_t size = mEffects.size();
1218
1219    for (size_t i = 0; i < size; i++) {
1220        if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1221            return mEffects[i];
1222        }
1223    }
1224    return 0;
1225}
1226
1227// getEffectFromId_l() must be called with ThreadBase::mLock held
1228sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1229{
1230    size_t size = mEffects.size();
1231
1232    for (size_t i = 0; i < size; i++) {
1233        // by convention, return first effect if id provided is 0 (0 is never a valid id)
1234        if (id == 0 || mEffects[i]->id() == id) {
1235            return mEffects[i];
1236        }
1237    }
1238    return 0;
1239}
1240
1241// getEffectFromType_l() must be called with ThreadBase::mLock held
1242sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1243        const effect_uuid_t *type)
1244{
1245    size_t size = mEffects.size();
1246
1247    for (size_t i = 0; i < size; i++) {
1248        if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1249            return mEffects[i];
1250        }
1251    }
1252    return 0;
1253}
1254
1255void AudioFlinger::EffectChain::clearInputBuffer()
1256{
1257    Mutex::Autolock _l(mLock);
1258    sp<ThreadBase> thread = mThread.promote();
1259    if (thread == 0) {
1260        ALOGW("clearInputBuffer(): cannot promote mixer thread");
1261        return;
1262    }
1263    clearInputBuffer_l(thread);
1264}
1265
1266// Must be called with EffectChain::mLock locked
1267void AudioFlinger::EffectChain::clearInputBuffer_l(sp<ThreadBase> thread)
1268{
1269    memset(mInBuffer, 0, thread->frameCount() * thread->frameSize());
1270}
1271
1272// Must be called with EffectChain::mLock locked
1273void AudioFlinger::EffectChain::process_l()
1274{
1275    sp<ThreadBase> thread = mThread.promote();
1276    if (thread == 0) {
1277        ALOGW("process_l(): cannot promote mixer thread");
1278        return;
1279    }
1280    bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1281            (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
1282    // never process effects when:
1283    // - on an OFFLOAD thread
1284    // - no more tracks are on the session and the effect tail has been rendered
1285    bool doProcess = (thread->type() != ThreadBase::OFFLOAD);
1286    if (!isGlobalSession) {
1287        bool tracksOnSession = (trackCnt() != 0);
1288
1289        if (!tracksOnSession && mTailBufferCount == 0) {
1290            doProcess = false;
1291        }
1292
1293        if (activeTrackCnt() == 0) {
1294            // if no track is active and the effect tail has not been rendered,
1295            // the input buffer must be cleared here as the mixer process will not do it
1296            if (tracksOnSession || mTailBufferCount > 0) {
1297                clearInputBuffer_l(thread);
1298                if (mTailBufferCount > 0) {
1299                    mTailBufferCount--;
1300                }
1301            }
1302        }
1303    }
1304
1305    size_t size = mEffects.size();
1306    if (doProcess) {
1307        for (size_t i = 0; i < size; i++) {
1308            mEffects[i]->process();
1309        }
1310    }
1311    for (size_t i = 0; i < size; i++) {
1312        mEffects[i]->updateState();
1313    }
1314}
1315
1316// addEffect_l() must be called with PlaybackThread::mLock held
1317status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1318{
1319    effect_descriptor_t desc = effect->desc();
1320    uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1321
1322    Mutex::Autolock _l(mLock);
1323    effect->setChain(this);
1324    sp<ThreadBase> thread = mThread.promote();
1325    if (thread == 0) {
1326        return NO_INIT;
1327    }
1328    effect->setThread(thread);
1329
1330    if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1331        // Auxiliary effects are inserted at the beginning of mEffects vector as
1332        // they are processed first and accumulated in chain input buffer
1333        mEffects.insertAt(effect, 0);
1334
1335        // the input buffer for auxiliary effect contains mono samples in
1336        // 32 bit format. This is to avoid saturation in AudoMixer
1337        // accumulation stage. Saturation is done in EffectModule::process() before
1338        // calling the process in effect engine
1339        size_t numSamples = thread->frameCount();
1340        int32_t *buffer = new int32_t[numSamples];
1341        memset(buffer, 0, numSamples * sizeof(int32_t));
1342        effect->setInBuffer((int16_t *)buffer);
1343        // auxiliary effects output samples to chain input buffer for further processing
1344        // by insert effects
1345        effect->setOutBuffer(mInBuffer);
1346    } else {
1347        // Insert effects are inserted at the end of mEffects vector as they are processed
1348        //  after track and auxiliary effects.
1349        // Insert effect order as a function of indicated preference:
1350        //  if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1351        //  another effect is present
1352        //  else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1353        //  last effect claiming first position
1354        //  else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1355        //  first effect claiming last position
1356        //  else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1357        // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1358        // already present
1359
1360        size_t size = mEffects.size();
1361        size_t idx_insert = size;
1362        ssize_t idx_insert_first = -1;
1363        ssize_t idx_insert_last = -1;
1364
1365        for (size_t i = 0; i < size; i++) {
1366            effect_descriptor_t d = mEffects[i]->desc();
1367            uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1368            uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1369            if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1370                // check invalid effect chaining combinations
1371                if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1372                    iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1373                    ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1374                            desc.name, d.name);
1375                    return INVALID_OPERATION;
1376                }
1377                // remember position of first insert effect and by default
1378                // select this as insert position for new effect
1379                if (idx_insert == size) {
1380                    idx_insert = i;
1381                }
1382                // remember position of last insert effect claiming
1383                // first position
1384                if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1385                    idx_insert_first = i;
1386                }
1387                // remember position of first insert effect claiming
1388                // last position
1389                if (iPref == EFFECT_FLAG_INSERT_LAST &&
1390                    idx_insert_last == -1) {
1391                    idx_insert_last = i;
1392                }
1393            }
1394        }
1395
1396        // modify idx_insert from first position if needed
1397        if (insertPref == EFFECT_FLAG_INSERT_LAST) {
1398            if (idx_insert_last != -1) {
1399                idx_insert = idx_insert_last;
1400            } else {
1401                idx_insert = size;
1402            }
1403        } else {
1404            if (idx_insert_first != -1) {
1405                idx_insert = idx_insert_first + 1;
1406            }
1407        }
1408
1409        // always read samples from chain input buffer
1410        effect->setInBuffer(mInBuffer);
1411
1412        // if last effect in the chain, output samples to chain
1413        // output buffer, otherwise to chain input buffer
1414        if (idx_insert == size) {
1415            if (idx_insert != 0) {
1416                mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
1417                mEffects[idx_insert-1]->configure();
1418            }
1419            effect->setOutBuffer(mOutBuffer);
1420        } else {
1421            effect->setOutBuffer(mInBuffer);
1422        }
1423        mEffects.insertAt(effect, idx_insert);
1424
1425        ALOGV("addEffect_l() effect %p, added in chain %p at rank %d", effect.get(), this,
1426                idx_insert);
1427    }
1428    effect->configure();
1429    return NO_ERROR;
1430}
1431
1432// removeEffect_l() must be called with PlaybackThread::mLock held
1433size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect)
1434{
1435    Mutex::Autolock _l(mLock);
1436    size_t size = mEffects.size();
1437    uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
1438
1439    for (size_t i = 0; i < size; i++) {
1440        if (effect == mEffects[i]) {
1441            // calling stop here will remove pre-processing effect from the audio HAL.
1442            // This is safe as we hold the EffectChain mutex which guarantees that we are not in
1443            // the middle of a read from audio HAL
1444            if (mEffects[i]->state() == EffectModule::ACTIVE ||
1445                    mEffects[i]->state() == EffectModule::STOPPING) {
1446                mEffects[i]->stop();
1447            }
1448            if (type == EFFECT_FLAG_TYPE_AUXILIARY) {
1449                delete[] effect->inBuffer();
1450            } else {
1451                if (i == size - 1 && i != 0) {
1452                    mEffects[i - 1]->setOutBuffer(mOutBuffer);
1453                    mEffects[i - 1]->configure();
1454                }
1455            }
1456            mEffects.removeAt(i);
1457            ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %d", effect.get(),
1458                    this, i);
1459            break;
1460        }
1461    }
1462
1463    return mEffects.size();
1464}
1465
1466// setDevice_l() must be called with PlaybackThread::mLock held
1467void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
1468{
1469    size_t size = mEffects.size();
1470    for (size_t i = 0; i < size; i++) {
1471        mEffects[i]->setDevice(device);
1472    }
1473}
1474
1475// setMode_l() must be called with PlaybackThread::mLock held
1476void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
1477{
1478    size_t size = mEffects.size();
1479    for (size_t i = 0; i < size; i++) {
1480        mEffects[i]->setMode(mode);
1481    }
1482}
1483
1484// setAudioSource_l() must be called with PlaybackThread::mLock held
1485void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
1486{
1487    size_t size = mEffects.size();
1488    for (size_t i = 0; i < size; i++) {
1489        mEffects[i]->setAudioSource(source);
1490    }
1491}
1492
1493// setVolume_l() must be called with PlaybackThread::mLock held
1494bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right)
1495{
1496    uint32_t newLeft = *left;
1497    uint32_t newRight = *right;
1498    bool hasControl = false;
1499    int ctrlIdx = -1;
1500    size_t size = mEffects.size();
1501
1502    // first update volume controller
1503    for (size_t i = size; i > 0; i--) {
1504        if (mEffects[i - 1]->isProcessEnabled() &&
1505            (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
1506            ctrlIdx = i - 1;
1507            hasControl = true;
1508            break;
1509        }
1510    }
1511
1512    if (ctrlIdx == mVolumeCtrlIdx && *left == mLeftVolume && *right == mRightVolume) {
1513        if (hasControl) {
1514            *left = mNewLeftVolume;
1515            *right = mNewRightVolume;
1516        }
1517        return hasControl;
1518    }
1519
1520    mVolumeCtrlIdx = ctrlIdx;
1521    mLeftVolume = newLeft;
1522    mRightVolume = newRight;
1523
1524    // second get volume update from volume controller
1525    if (ctrlIdx >= 0) {
1526        mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
1527        mNewLeftVolume = newLeft;
1528        mNewRightVolume = newRight;
1529    }
1530    // then indicate volume to all other effects in chain.
1531    // Pass altered volume to effects before volume controller
1532    // and requested volume to effects after controller
1533    uint32_t lVol = newLeft;
1534    uint32_t rVol = newRight;
1535
1536    for (size_t i = 0; i < size; i++) {
1537        if ((int)i == ctrlIdx) {
1538            continue;
1539        }
1540        // this also works for ctrlIdx == -1 when there is no volume controller
1541        if ((int)i > ctrlIdx) {
1542            lVol = *left;
1543            rVol = *right;
1544        }
1545        mEffects[i]->setVolume(&lVol, &rVol, false);
1546    }
1547    *left = newLeft;
1548    *right = newRight;
1549
1550    return hasControl;
1551}
1552
1553void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
1554{
1555    const size_t SIZE = 256;
1556    char buffer[SIZE];
1557    String8 result;
1558
1559    snprintf(buffer, SIZE, "Effects for session %d:\n", mSessionId);
1560    result.append(buffer);
1561
1562    bool locked = AudioFlinger::dumpTryLock(mLock);
1563    // failed to lock - AudioFlinger is probably deadlocked
1564    if (!locked) {
1565        result.append("\tCould not lock mutex:\n");
1566    }
1567
1568    result.append("\tNum fx In buffer   Out buffer   Active tracks:\n");
1569    snprintf(buffer, SIZE, "\t%02d     0x%08x  0x%08x   %d\n",
1570            mEffects.size(),
1571            (uint32_t)mInBuffer,
1572            (uint32_t)mOutBuffer,
1573            mActiveTrackCnt);
1574    result.append(buffer);
1575    write(fd, result.string(), result.size());
1576
1577    for (size_t i = 0; i < mEffects.size(); ++i) {
1578        sp<EffectModule> effect = mEffects[i];
1579        if (effect != 0) {
1580            effect->dump(fd, args);
1581        }
1582    }
1583
1584    if (locked) {
1585        mLock.unlock();
1586    }
1587}
1588
1589// must be called with ThreadBase::mLock held
1590void AudioFlinger::EffectChain::setEffectSuspended_l(
1591        const effect_uuid_t *type, bool suspend)
1592{
1593    sp<SuspendedEffectDesc> desc;
1594    // use effect type UUID timelow as key as there is no real risk of identical
1595    // timeLow fields among effect type UUIDs.
1596    ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
1597    if (suspend) {
1598        if (index >= 0) {
1599            desc = mSuspendedEffects.valueAt(index);
1600        } else {
1601            desc = new SuspendedEffectDesc();
1602            desc->mType = *type;
1603            mSuspendedEffects.add(type->timeLow, desc);
1604            ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
1605        }
1606        if (desc->mRefCount++ == 0) {
1607            sp<EffectModule> effect = getEffectIfEnabled(type);
1608            if (effect != 0) {
1609                desc->mEffect = effect;
1610                effect->setSuspended(true);
1611                effect->setEnabled(false);
1612            }
1613        }
1614    } else {
1615        if (index < 0) {
1616            return;
1617        }
1618        desc = mSuspendedEffects.valueAt(index);
1619        if (desc->mRefCount <= 0) {
1620            ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
1621            desc->mRefCount = 1;
1622        }
1623        if (--desc->mRefCount == 0) {
1624            ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
1625            if (desc->mEffect != 0) {
1626                sp<EffectModule> effect = desc->mEffect.promote();
1627                if (effect != 0) {
1628                    effect->setSuspended(false);
1629                    effect->lock();
1630                    EffectHandle *handle = effect->controlHandle_l();
1631                    if (handle != NULL && !handle->destroyed_l()) {
1632                        effect->setEnabled_l(handle->enabled());
1633                    }
1634                    effect->unlock();
1635                }
1636                desc->mEffect.clear();
1637            }
1638            mSuspendedEffects.removeItemsAt(index);
1639        }
1640    }
1641}
1642
1643// must be called with ThreadBase::mLock held
1644void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
1645{
1646    sp<SuspendedEffectDesc> desc;
1647
1648    ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
1649    if (suspend) {
1650        if (index >= 0) {
1651            desc = mSuspendedEffects.valueAt(index);
1652        } else {
1653            desc = new SuspendedEffectDesc();
1654            mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
1655            ALOGV("setEffectSuspendedAll_l() add entry for 0");
1656        }
1657        if (desc->mRefCount++ == 0) {
1658            Vector< sp<EffectModule> > effects;
1659            getSuspendEligibleEffects(effects);
1660            for (size_t i = 0; i < effects.size(); i++) {
1661                setEffectSuspended_l(&effects[i]->desc().type, true);
1662            }
1663        }
1664    } else {
1665        if (index < 0) {
1666            return;
1667        }
1668        desc = mSuspendedEffects.valueAt(index);
1669        if (desc->mRefCount <= 0) {
1670            ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
1671            desc->mRefCount = 1;
1672        }
1673        if (--desc->mRefCount == 0) {
1674            Vector<const effect_uuid_t *> types;
1675            for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
1676                if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
1677                    continue;
1678                }
1679                types.add(&mSuspendedEffects.valueAt(i)->mType);
1680            }
1681            for (size_t i = 0; i < types.size(); i++) {
1682                setEffectSuspended_l(types[i], false);
1683            }
1684            ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
1685                    mSuspendedEffects.keyAt(index));
1686            mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
1687        }
1688    }
1689}
1690
1691
1692// The volume effect is used for automated tests only
1693#ifndef OPENSL_ES_H_
1694static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
1695                                            { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
1696const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
1697#endif //OPENSL_ES_H_
1698
1699bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
1700{
1701    // auxiliary effects and visualizer are never suspended on output mix
1702    if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
1703        (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
1704         (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
1705         (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
1706        return false;
1707    }
1708    return true;
1709}
1710
1711void AudioFlinger::EffectChain::getSuspendEligibleEffects(
1712        Vector< sp<AudioFlinger::EffectModule> > &effects)
1713{
1714    effects.clear();
1715    for (size_t i = 0; i < mEffects.size(); i++) {
1716        if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
1717            effects.add(mEffects[i]);
1718        }
1719    }
1720}
1721
1722sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
1723                                                            const effect_uuid_t *type)
1724{
1725    sp<EffectModule> effect = getEffectFromType_l(type);
1726    return effect != 0 && effect->isEnabled() ? effect : 0;
1727}
1728
1729void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
1730                                                            bool enabled)
1731{
1732    ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
1733    if (enabled) {
1734        if (index < 0) {
1735            // if the effect is not suspend check if all effects are suspended
1736            index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
1737            if (index < 0) {
1738                return;
1739            }
1740            if (!isEffectEligibleForSuspend(effect->desc())) {
1741                return;
1742            }
1743            setEffectSuspended_l(&effect->desc().type, enabled);
1744            index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
1745            if (index < 0) {
1746                ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
1747                return;
1748            }
1749        }
1750        ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
1751            effect->desc().type.timeLow);
1752        sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
1753        // if effect is requested to suspended but was not yet enabled, supend it now.
1754        if (desc->mEffect == 0) {
1755            desc->mEffect = effect;
1756            effect->setEnabled(false);
1757            effect->setSuspended(true);
1758        }
1759    } else {
1760        if (index < 0) {
1761            return;
1762        }
1763        ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
1764            effect->desc().type.timeLow);
1765        sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
1766        desc->mEffect.clear();
1767        effect->setSuspended(false);
1768    }
1769}
1770
1771bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
1772{
1773    Mutex::Autolock _l(mLock);
1774    size_t size = mEffects.size();
1775    for (size_t i = 0; i < size; i++) {
1776        if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
1777            return true;
1778        }
1779    }
1780    return false;
1781}
1782
1783}; // namespace android
1784