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