美文网首页
Android多媒体框架--21:ACodec加载OMX

Android多媒体框架--21:ACodec加载OMX

作者: DarcyZhou | 来源:发表于2023-05-05 08:17 被阅读0次

"本文转载自:[VNanyesheshou]的Android MultiMedia框架——ACodec加载OMX"

1.概述

  这篇主要分析一下ACodec状态机模式,及ACodec和OMX建立连接,进行交互。

2.状态机

  ACodec继承于状态机类AHierarchicalStateMachine,内部包含九种状态和一个基础状态BaseState,这九种状态都继承于BaseState,用来管理Component的各种状态。

  • ACodec.h
struct BaseState;
struct UninitializedState;
struct LoadedState;
struct LoadedToIdleState;
struct IdleToExecutingState;
struct ExecutingState;
struct OutputPortSettingsChangedState;
struct ExecutingToIdleState;
struct IdleToLoadedState;
struct FlushingState;

其状态机状态变化会先执行原先状态的 stateExited函数,然后执行新状态的stateEntered函数。这几个状态转换逻辑如下图:

01.jpg

3.ACodec加载OMX

  ACodec运行于app进程中,而OMX运行在hw/android.hardware.media.omx@1.0-service进程,它们之间通过hidl,跨进程通信。

  先回顾下ACodec未初始化状态 调用onAllocateComponent。

bool ACodec::UninitializedState::onAllocateComponent(const sp<AMessage> &msg) {
    //.......
    sp<IOMX> omx;
    sp<IOMXNode> omxNode;

    OMXClient client;
    //(1)与media.codec服务建立连接
    if (client.connect(owner.c_str()) != OK) {
        mCodec->signalError(OMX_ErrorUndefined, NO_INIT);
        return false;
    }
    //(2)获取句柄IOMX
    omx = client.interface();
    //(3)分配Node
    err = omx->allocateNode(componentName.c_str(), observer, &omxNode);
        //.......
}

(1)OMXClient connect与media.codec服务建立连接,连接server;
(2)获取句柄IOMX;
(3)分配Node。

   先看下client.connect,ACodec连接Server:

status_t OMXClient::connect(const char* name) {
    using namespace ::android::hardware::media::omx::V1_0;
    if (name == nullptr) {
        name = "default";
    }
    sp<IOmx> tOmx = IOmx::getService(name);
    if (tOmx.get() == nullptr) {
        return NO_INIT;
    }
    if (!tOmx->isRemote()) {
        return NO_INIT;
    }
    mOMX = new utils::LWOmx(tOmx);
    return OK;
}

(1)建立ACodec与hw/android.hardware.media.omx@1.0-service进程连接;
(2)IOmx包装一层,LWOmx类型。

  client.interface() 返回包装后的LWOmx对象:

sp<IOMX> OMXClient::interface() {
    return mOMX;
}

  接着看omx->allocateNode, 跨进程调用到server中:

  • frameworks/av/media/libstagefright/omx/1.0/Omx.cpp
Return<void> Omx::allocateNode(const hidl_string& name, const sp<IOmxObserver>& observer,
        allocateNode_cb _hidl_cb) {
    using ::android::IOMXNode;
    using ::android::IOMXObserver;

    sp<OMXNodeInstance> instance;
    {
       //.....
        instance = new OMXNodeInstance(
                this, new LWOmxObserver(observer), name.c_str());

        OMX_ERRORTYPE err = mMaster->makeComponentInstance(
                name.c_str(), &OMXNodeInstance::kCallbacks,
                instance.get(), &handle);

        //.....
    return Void();
}

(1)创建OMXNodeInstance对象,OMXNodeInstance构造器主要初始化了一些变量;
(2)makeComponentInstance。

  • frameworks/av/media/libstagefright/omx/OMXMaster.cpp
OMX_ERRORTYPE OMXMaster::makeComponentInstance(
        const char *name,
        const OMX_CALLBACKTYPE *callbacks,
        OMX_PTR appData,
        OMX_COMPONENTTYPE **component) {
    Mutex::Autolock autoLock(mLock);
    *component = NULL;
    ssize_t index = mPluginByComponentName.indexOfKey(String8(name));

    if (index < 0) {
        return OMX_ErrorInvalidComponentName;
    }

    OMXPluginBase *plugin = mPluginByComponentName.valueAt(index);
    OMX_ERRORTYPE err =
        plugin->makeComponentInstance(name, callbacks, appData, component);

    if (err != OMX_ErrorNone) {
        return err;
    }

    mPluginByInstance.add(*component, plugin);

    return err;
}

从mPluginByComponentName向量中获取对应的OMXPluginBase, OMXPluginBase主要分为SoftOMXPlugin(表示软编码)和其他XXXOMXPlugin(表示硬编码)。

  这里看下SoftOMXPlugin对应makeComponentInstance函数:

OMX_ERRORTYPE SoftOMXPlugin::makeComponentInstance(
        const char *name,
        const OMX_CALLBACKTYPE *callbacks,
        OMX_PTR appData,
        OMX_COMPONENTTYPE **component) {
    for (size_t i = 0; i < kNumComponents; ++i) {
        if (strcmp(name, kComponents[i].mName)) {
            continue;
        }
        AString libName = "libstagefright_soft_";
        libName.append(kComponents[i].mLibNameSuffix);
        libName.append(".so");

        void *libHandle = dlopen(libName.c_str(), RTLD_NOW|RTLD_NODELETE);

        typedef SoftOMXComponent *(*CreateSoftOMXComponentFunc)(
                const char *, const OMX_CALLBACKTYPE *,
                OMX_PTR, OMX_COMPONENTTYPE **);

        CreateSoftOMXComponentFunc createSoftOMXComponent =
            (CreateSoftOMXComponentFunc)dlsym(
                    libHandle,
                    "_Z22createSoftOMXComponentPKcPK16OMX_CALLBACKTYPE"
                    "PvPP17OMX_COMPONENTTYPE");

                //........
    }

    return OMX_ErrorInvalidComponentName;
}

(1)通过name查找对应的component信息,组成对应的lib库名称,如:libstagefright_soft_aacenc.so;
(2)加载编解码库so;
(3)调用createSoftOMXComponent,创建对应的编解码组件SoftOMXComponent,这里对应SoftAACEncoder2。

4.总结

  ACodec 通过状态机模式的切换,确保状态正确运行,及和OMX service交互,加载对应的编解码器,进行相应编解码。内部值得继续研究研究。

相关文章

网友评论

      本文标题:Android多媒体框架--21:ACodec加载OMX

      本文链接:https://www.haomeiwen.com/subject/aayqjdtx.html