Android Codec2 CCodec(二)服务启动

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

1、Codec2 SW Codec Service

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

cpp 复制代码
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。

cpp 复制代码
$ 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目录下:

xml 复制代码
<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实例。简化后的代码如下:

cpp 复制代码
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

cpp 复制代码
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实例。

cpp 复制代码
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是差不多的。

cpp 复制代码
std::map<C2String, ComponentLoader> mComponents;

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

cpp 复制代码
ComponentLoader(std::string libPath)
    : mLibPath(libPath) {}

3、ComponentStore

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

cpp 复制代码
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类图:

相关推荐
STCNXPARM8 小时前
Android camera之硬件架构
android·硬件架构·camera
2501_944525549 小时前
Flutter for OpenHarmony 个人理财管理App实战 - 支出分析页面
android·开发语言·前端·javascript·flutter
松☆11 小时前
Dart 核心语法精讲:从空安全到流程控制(3)
android·java·开发语言
_李小白12 小时前
【Android 美颜相机】第二十三天:GPUImageDarkenBlendFilter(变暗混合滤镜)
android·数码相机
colicode15 小时前
语音消息接口应用开发:如何通过API发送个性化音频或TTS语音内容
音视频
小天源15 小时前
银河麒麟 V10(x86_64)离线安装 MySQL 8.0
android·mysql·adb·麒麟v10
2501_9159214315 小时前
傻瓜式 HTTPS 抓包,简单抓取iOS设备数据
android·网络协议·ios·小程序·https·uni-app·iphone
美狐美颜sdk16 小时前
直播美颜sdk与智能美妆技术解析:实时人脸算法如何驱动新一代互动体验
人工智能·音视频·美颜sdk·视频美颜sdk·美狐美颜sdk
csj5016 小时前
安卓基础之《(20)—高级控件(2)列表类视图》
android
JMchen12316 小时前
Android计算摄影实战:多帧合成、HDR+与夜景算法深度剖析
android·经验分享·数码相机·算法·移动开发·android-studio