Android Codec2 CCodec(二)服务启动

本篇文章我们会先简单了解Android Framework提供的Codec2软件编解码服务(SW Codec Service)的启动过程,然后分析C2Store的设计结构。

1、Codec2 SW Codec Service

Codec2 SW服务启动文件名为main_swcodecservice.cpp,它和OpenMAX服务的启动文件放在同一个文件夹。

int main(int argc __unused, char** argv)
{
    LOG(INFO) << "media swcodec service starting";
    signal(SIGPIPE, SIG_IGN);
    SetUpMinijail(kSystemSeccompPolicyPath, kVendorSeccompPolicyPath);
    strcpy(argv[0], "media.swcodec");

    ::android::hardware::configureRpcThreadpool(64, false);

    RegisterCodecServices();

    ::android::hardware::joinRpcThreadpool();
}

从代码中可以看到,Codec2 SW服务名为media.swcodec,而我们之前学习的OpenMAX服务名为media.codec。

$ ps -A | grep codec
mediacodec     489     1      67712   2144 binder_thread_read  0 S media.codec
mediacodec     600     1     102996   2012 binder_thread_read  0 S media.swcodec

media.swcodec是一个HIDL Service,所以它会有一个Manifest文件,比较奇怪的是media.swcodec的清单文件位于frameworks/av/media/mediaserver目录下:

<manifest version="1.0" type="framework">
    <hal>
        <name>android.hardware.media.c2</name>
        <transport>hwbinder</transport>
        <version>1.2</version>
        <interface>
            <name>IComponentStore</name>
            <instance>software</instance>
        </interface>
    </hal>
</manifest>

回过头来看main,主要是调用RegisterCodecServices创建ComponentStore实例。简化后的代码如下:

extern "C" void RegisterCodecServices() {
    // 创建C2ComponentStore实例
    std::shared_ptr<C2ComponentStore> store =
        android::GetCodec2PlatformComponentStore();

    using namespace ::android::hardware::media::c2;

    int platformVersion = android_get_device_api_level();
    // 创建IComponentStore/ComponentStore实例
    if (platformVersion >= __ANDROID_API_S__) {
        android::sp<V1_2::IComponentStore> storeV1_2 =
            new V1_2::utils::ComponentStore(store);
        if (storeV1_2->registerAsService("software") != android::OK) {
            LOG(ERROR) << "Cannot register software Codec2 v1.2 service.";
            return;
        }
    } else if (platformVersion == __ANDROID_API_R__) {
        android::sp<V1_1::IComponentStore> storeV1_1 =
            new V1_1::utils::ComponentStore(store);
        if (storeV1_1->registerAsService("software") != android::OK) {
            LOG(ERROR) << "Cannot register software Codec2 v1.1 service.";
            return;
        }
    } else if (platformVersion == __ANDROID_API_Q__) {
        android::sp<V1_0::IComponentStore> storeV1_0 =
            new V1_0::utils::ComponentStore(store);
        if (storeV1_0->registerAsService("software") != android::OK) {
            LOG(ERROR) << "Cannot register software Codec2 v1.0 service.";
            return;
        }
    } else {  // platformVersion < __ANDROID_API_Q__
        LOG(ERROR) << "The platform version " << platformVersion <<
                      " is not supported.";
        return;
    }
}

RegisterCodecServices做了如下两件事:

  1. 调用GetCodec2PlatformComponentStore创建了一个C2ComponentStore对象,从函数名中的Platform可以看出,这个ComponentStore是由Android Framework提供;
  2. 根据Android版本,将C2ComponentStore对象封装到不同版本的IComponentStore中,并将创建的IComponentStore注册到hw binder中。

2、C2ComponentStore

std::shared_ptr<C2ComponentStore> GetCodec2PlatformComponentStore() {
    static std::mutex mutex;
    static std::weak_ptr<C2ComponentStore> platformStore;
    std::lock_guard<std::mutex> lock(mutex);
    std::shared_ptr<C2ComponentStore> store = platformStore.lock();
    if (store == nullptr) {
        store = std::make_shared<C2PlatformComponentStore>();
        platformStore = store;
    }
    return store;
}

GetCodec2PlatformComponentStore最终创建的是一个C2PlatformComponentStore对象,该类型实现了C2ComponentStore的接口。另外,从上述方法可以看出,C2ComponentStore使用了单例,每个进程只有一个store实例。

C2PlatformComponentStore::C2PlatformComponentStore()
    : mVisited(false),
      mReflector(std::make_shared<C2ReflectorHelper>()),
      mInterface(mReflector) {

    auto emplace = [this](const char *libPath) {
        mComponents.emplace(libPath, libPath);
    };

    emplace("libcodec2_soft_h263dec.so");
    emplace("libcodec2_soft_h263enc.so");
    emplace("libcodec2_soft_hevcdec.so");
    emplace("libcodec2_soft_hevcenc.so");
    // ...
}

C2PlatformComponentStore在构造函数中做了三件事:

  1. 实例化C2ReflectorHelper;
  2. 实例化C2PlatformComponentStore::Interface;
  3. 将Android提供的Codec2 SW encoder/decoder Component添加到store中。

添加组件的步骤是不是似曾相识呢?和OMXStore是差不多的。

std::map<C2String, ComponentLoader> mComponents;

mComponents是一个键值列表,key是组件lib名,value是ComponentLoader对象,在给列表添加条目时使用了隐式转换。

ComponentLoader(std::string libPath)
    : mLibPath(libPath) {}

3、ComponentStore

ComponentStore是HDIL接口实现,创建ComponentStore实例需要传入先前创建的C2ComponentStore对象。

struct ComponentStore : public IComponentStore {

protected:
    sp<CachedConfigurable> mConfigurable;
    std::shared_ptr<C2ComponentStore> mStore;
    std::shared_ptr<StoreParameterCache> mParameterCache;
}

ComponentStore::ComponentStore(const std::shared_ptr<C2ComponentStore>& store)
      : mConfigurable{new CachedConfigurable(std::make_unique<StoreIntf>(store))},
        mParameterCache{std::make_shared<StoreParameterCache>(this)},
        mStore{store} {

    std::shared_ptr<C2ComponentStore> platformStore = android::GetCodec2PlatformComponentStore();
    SetPreferredCodec2ComponentStore(store);

    mParamReflector = mStore->getParamReflector();

    using namespace std::placeholders;
    mInit = mConfigurable->init(mParameterCache);
}
  1. ComponentStore的构造函数会实例化CachedConfigurable成员以及StoreParameterCache成员;
  2. 调用SetPreferredCodec2ComponentStore;
  3. 调用C2ComponentStore的getParamReflector;
  4. 调用CachedConfigurable的init方法。

在Codec2框架中,HIDL实现位于frameworks/av/media/codec2/hal/hidl/1.2/utils,服务最终调用的是Codec2 Core定义的接口。下图是ComponentStore相关的简单UML类图:

请添加图片描述

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青山渺渺

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值