Android原生回音消除API分析(三)音效启用分析

接下来我们开始分析setEnabled方法。
基于我们之前的分析,我们可以直接定位到/frameworks/av/media/libmedia/AudioEffect.cpp中的源码。
代码精简后我们可以得出:

status_t AudioEffect::setEnabled(bool enabled)
{
...
        if (enabled) {
            status = mIEffect->enable();
        } else {
            status = mIEffect->disable();
        }
...
    return status;
}

由代码可以看出依据传入的布尔值进行状态控制,由于我们目前需要跟踪的是回音消除开启情况,我们以enabled==true的条件进行跟踪。通过AudioEffect示例化的分析,我们可以知道存在以下关系。

...
iEffect = audioFlinger->createEffect((effect_descriptor_t *)&mDescriptor,mIEffectClient, priority, io, mSessionId, mOpPackageName, &mStatus, &mId, &enabled);
...
mIEffect = iEffect;

所有mIEffect->enable()会调用到
/frameworks/av/media/libmedia/IEffect.cpp

    status_t enable()
    {
        Parcel data, reply;
        data.writeInterfaceToken(IEffect::getInterfaceDescriptor());
        remote()->transact(ENABLE, data, &reply);
        return reply.readInt32();
    }

    status_t disable()
    {
        Parcel data, reply;
        data.writeInterfaceToken(IEffect::getInterfaceDescriptor());
        remote()->transact(DISABLE, data, &reply);
        return reply.readInt32();
    }

AudioFlinger Service再来执行外部函数实现会调用到
/frameworks/av/services/audioflinger/Effects.cpp

    status_t AudioFlinger::EffectHandle::onTransact(
    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
{
    return BnEffect::onTransact(code, data, reply, flags);
}

最终调用到了
/frameworks/av/media/libmedia/IEffect.cpp

status_t BnEffect::onTransact(
    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
{
    switch (code) {
    case ENABLE: {
            CHECK_INTERFACE(IEffect, data, reply);
            reply->writeInt32(enable());
            return NO_ERROR;
        } break;

    case DISABLE: {
            CHECK_INTERFACE(IEffect, data, reply);
            reply->writeInt32(disable());
            return NO_ERROR;
        } break;
    }
    ....
}

然后调用到了
frameworks/av/services/audioflinger/Effects.cpp

status_t AudioFlinger::EffectHandle::enable()
{
...
sp<EffectModule> effect = mEffect.promote();
...
    status_t status = effect->setEnabled(true);
...
    return status;
}

此时我们回到Effects.cpp的构造函数查看mEffect是什么来头
frameworks/av/services/audioflinger/Effects.cpp

AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,const sp<AudioFlinger::Client>& client,const sp<IEffectClient>& effectClient,int32_t priority)
: BnEffect(),
mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
{
...
...
}

EffectModule的类的外部实现位于
frameworks/av/services/audioflinger/Effects.cpp

status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
{
    return setEnabled_l(enabled);
}

status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
{
    status_t status = AudioSystem::setEffectEnabled(mId, enabled);
}

我们再来跟进AudioSystem::setEffectEnabled
frameworks/av/media/libmedia/AudioSystem.cpp

status_t AudioSystem::setEffectEnabled(int id, bool enabled)
{
    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
    if (aps == 0) return PERMISSION_DENIED;
    return aps->setEffectEnabled(id, enabled);
}

frameworks/av/media/libmedia/IAudioPolicyService.cpp

    virtual status_t setEffectEnabled(int id, bool enabled)
    {
        Parcel data, reply;
        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
        data.writeInt32(id);
        data.writeInt32(enabled);
        remote()->transact(SET_EFFECT_ENABLED, data, &reply);
        return static_cast <status_t> (reply.readInt32());
    }

frameworks/av/media/libmedia/IAudioPolicyService.cpp

status_t BnAudioPolicyService::onTransact(
    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
{
...
        case SET_EFFECT_ENABLED: {
            int id = data.readInt32();
            bool enabled = static_cast <bool>(data.readInt32());
            reply->writeInt32(static_cast <int32_t>(setEffectEnabled(id, enabled)));
            return NO_ERROR;
        } break;
...
}

frameworks/av/services/audiopolicy/service/AudioPolicyInterfaceImpl.cpp

status_t AudioPolicyService::setEffectEnabled(int id, bool enabled)
{
    ...
    return mAudioPolicyManager->setEffectEnabled(id, enabled);
}

frameworks/av/services/audiopolicy/managerdefault/AudioPolicyManager.h

 EffectDescriptorCollection mEffects;
      virtual status_t setEffectEnabled(int id, bool enabled)
        {
            return mEffects.setEffectEnabled(id, enabled);
        }

frameworks/av/services/audiopolicy/common/managerdefinitions/src/EffectDescriptor.cpp

status_t EffectDescriptorCollection::setEffectEnabled(int id, bool enabled)
{
    ssize_t index = indexOfKey(id);
    ...
    return setEffectEnabled(valueAt(index), enabled);
}

status_t EffectDescriptorCollection::setEffectEnabled(const sp<EffectDescriptor> &effectDesc,
                                                      bool enabled)
{
   ...
    effectDesc->mEnabled = enabled;
    return NO_ERROR;
}

从代码中可以看出来应该是从一个列表中获取到EffectDescriptor对象并且将mEnabled属性进行设置,因此setEffectEnabled的终点应该就在此处了,我们剩下来的事情就是研究音效库的加载了。

发表评论

CAPTCHAis initialing...