Android源码分析--AudioEngine的初始化(04)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/WAN_EXE/article/details/79155529

    前面的文章中讲到Engine设备的选择,那么这个类是怎么被初始化的呢?可以看到在AudioPolicyManager的构造器中,

AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
{
    // Once policy config has been parsed, retrieve an instance of the engine and initialize it.
    audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance();
    if (!engineInstance) {
        ALOGE("%s:  Could not get an instance of policy engine", __FUNCTION__);
        return;
    }
    // Retrieve the Policy Manager Interface
    mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>();
    if (mEngine == NULL) {
        ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__);
        return;
    }
    mEngine->setObserver(this);
    status_t status = mEngine->initCheck();
    (void) status;
    ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status);
}
有对Engine进行初始化,这里有个EngineInstance类,这个类保证只有只能构造一个Engine实例,Engine只有在其它的参数被初始化之后才能对它进行初始化。

/frameworks/av/services/audiopolicy/enginedefault/include/AudioPolicyEngineInstance.h

#pragma once

class AudioPolicyManagerInterface;

namespace android
{
namespace audio_policy
{

class Engine;

class EngineInstance
{
protected:
    EngineInstance();

public:
    virtual ~EngineInstance();

    /**
     * Get Audio Policy Engine instance.
     *
     * @return pointer to Route Manager Instance object.
     */
    static EngineInstance *getInstance();

    /**
     * Interface query.
     * The first client of an interface of the policy engine will start the singleton.
     *
     * @tparam RequestedInterface: interface that the client is wishing to retrieve.
     *
     * @return interface handle.
     */
    template <class RequestedInterface>
    RequestedInterface *queryInterface() const;

protected:
    /**
     * Get Audio Policy Engine instance.
     *
     * @return Audio Policy Engine singleton.
     */
    Engine *getEngine() const;

private:
    /* Copy facilities are put private to disable copy. */
    EngineInstance(const EngineInstance &object);
    EngineInstance &operator=(const EngineInstance &object);
};

/**
 * Limit template instantation to supported type interfaces.
 * Compile time error will claim if invalid interface is requested.
 */
template <>
AudioPolicyManagerInterface *EngineInstance::queryInterface() const;

} // namespace audio_policy
}
最后面的一段话限制这个泛型类型只能传递AudioPolicyManagerInterface的类。

实现在

/frameworks/av/services/audiopolicy/enginedefault/src/EngineInstance.cpp

#include <AudioPolicyManagerInterface.h>
#include "AudioPolicyEngineInstance.h"
#include "Engine.h"

namespace android
{
namespace audio_policy
{

EngineInstance::EngineInstance()
{
}

EngineInstance *EngineInstance::getInstance()
{
    static EngineInstance instance;
    return &instance;
}

EngineInstance::~EngineInstance()
{
}

Engine *EngineInstance::getEngine() const
{
    static Engine engine;
    return &engine;
}

template <>
AudioPolicyManagerInterface *EngineInstance::queryInterface() const
{
    return getEngine()->queryInterface<AudioPolicyManagerInterface>();
}

} // namespace audio_policy
}

到这里就算Engine初始化完成,对比其它代码算是比较容易理解的。

总结:

        之前Engine构造器中传递this指针给内部类ManagerInterfaceImpl初始化它的变量Engine *mPolicyEngine,C++的写法还是比较绕的,但是要从面向对象的角度去理解。然后AudioPolicyManager通过头文件中定义的

        // Audio Policy Engine Interface.
        AudioPolicyManagerInterface *mEngine;

可以直接调用AudioPolicyManagerInterface中的方法。

下篇:

        完成Engine的分析如果直接深入到AudioPolicyManager这个模块仍然会感觉到很庞大,所以下篇文章将从AudioPolicyManagerObserver这个类入手,因为它包含很多抽象的集合,从这个类中可以获取很多音频系统的信息。

附件:

C++泛型讲解

未完成





猜你喜欢

转载自blog.csdn.net/WAN_EXE/article/details/79155529