stagefright and OMXIL plugin porting

来自个人维基
跳转至: 导航搜索

Ref:https://blog.csdn.net/yanbixing123/article/details/88925903#comments

1. Create libstagefrighthw project in AOSP/hardware/<company>/media/chagall/libstagefrighthw/
Android.mk

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
 
LOCAL_SRC_FILES := \
    ChagallOMXPlugin.cpp
 
LOCAL_CFLAGS := $(PV_CFLAGS_MINUS_VISIBILITY)
 
LOCAL_C_INCLUDES:= \
        $(TOP)/frameworks/native/include/media/openmax \
        $(TOP)/frameworks/native/include/media/hardware \
 
LOCAL_SHARED_LIBRARIES :=       \
        libbinder               \
        libutils                \
        libcutils               \
        libui                   \
        libdl                   \
        liblog
 
LOCAL_MODULE := libstagefrighthw
 
LOCAL_MODULE_TAGS := optional
 
include $(BUILD_SHARED_LIBRARY)

ChagallOMXPlugin.cpp

#include <utils/Log.h>
#undef LOG_TAG
#define LOG_TAG "ChagallOMXPlugin"
 
#include <dlfcn.h>
#include <HardwareAPI.h>
 
namespace android {
 
extern OMXPluginBase *createOMXPlugin() {
    return new ChagallOMXPlugin;
}
 
extern void destroyOMXPlugin(OMXPluginBase *plugin) {
    delete plugin;
}
 
ChagallOMXPlugin::ChagallOMXPlugin()
    : mLibHandle(dlopen("libchagallOmxCore.so", RTLD_NOW)),
        mInit(NULL),
        mDeinit(NULL),
        mComponentNameEnum(NULL),
        mGetHandle(NULL),
        mFreeHandle(NULL),
        mGetRolesOfComponentHandle(NULL) {
 
    if (mLibHandle != NULL) {
        mInit = (InitFunc)dlsym(mLibHandle, "OMX_Init");
        mDeinit = (DeinitFunc)dlsym(mLibHandle, "OMX_DeInit");
 
        mComponentNameEnum =
            (ComponentNameEnumFunc)dlsym(mLibHandle, "OMX_ComponentNameEnum");
 
        mGetHandle = (GetHandleFunc)dlsym(mLibHandle, "OMX_GetHandle");
        mFreeHandle = (FreeHandleFunc)dlsym(mLibHandle, "OMX_FreeHandle");
 
        mGetRolesOfComponentHandle =
            (GetRolesOfComponentFunc)dlsym(
                    mLibHandle, "OMX_GetRolesOfComponent");
 
        (*mInit)();
    }
    else
        ALOGE("Failed to load %s", LIBOMXCORE);
}
 
ChagallOMXPlugin::~ChagallOMXPlugin() {
    if (mLibHandle != NULL) {
        (*mDeinit)();
 
        dlclose(mLibHandle);
        mLibHandle = NULL;
    }
}
 
OMX_ERRORTYPE ChagallOMXPlugin::makeComponentInstance(
        const char *name,
        const OMX_CALLBACKTYPE *callbacks,
        OMX_PTR appData,
        OMX_COMPONENTTYPE **component) {
    if (mLibHandle == NULL) {
        ALOGE("makeComponentInstance: mLibHandle = NULL");
        return OMX_ErrorUndefined;
    }
 
    return (*mGetHandle)(
            reinterpret_cast<OMX_HANDLETYPE *>(component),
            const_cast<char *>(name),
            appData, const_cast<OMX_CALLBACKTYPE *>(callbacks));
}
 
OMX_ERRORTYPE ChagallOMXPlugin::destroyComponentInstance(
        OMX_COMPONENTTYPE *component) {
    if (mLibHandle == NULL) {
        ALOGE("destroyComponentInstance: mLibHandle = NULL");
        return OMX_ErrorUndefined;
    }
 
    return (*mFreeHandle)(reinterpret_cast<OMX_HANDLETYPE *>(component));
}
 
OMX_ERRORTYPE ChagallOMXPlugin::enumerateComponents(
        OMX_STRING name,
        size_t size,
        OMX_U32 index) {
    if (mLibHandle == NULL) {
        ALOGE("enumerateComponents: mLibHandle = NULL");
        return OMX_ErrorUndefined;
    }
 
    return (*mComponentNameEnum)(name, size, index);
}
 
OMX_ERRORTYPE ChagallOMXPlugin::getRolesOfComponent(
        const char *name,
        Vector<String8> *roles) {
    roles->clear();
 
    if (mLibHandle == NULL) {
        ALOGE("getRolesOfComponent: mLibHandle = NULL");
        return OMX_ErrorUndefined;
    }
 
    OMX_U32 numRoles;
    OMX_ERRORTYPE err = (*mGetRolesOfComponentHandle)(
            const_cast<OMX_STRING>(name), &numRoles, NULL);
    ALOGD("Name %s, roles %d", name, (int)numRoles);
    if (err != OMX_ErrorNone) {
        ALOGE("mGetRolesOfComponentHandle: err %d", err);
        return err;
    }
 
    if (numRoles > 0) {
        OMX_U8 **array = new OMX_U8 *[numRoles];
        for (OMX_U32 i = 0; i < numRoles; ++i) {
            array[i] = new OMX_U8[OMX_MAX_STRINGNAME_SIZE];
        }
 
        OMX_U32 numRoles2;
        err = (*mGetRolesOfComponentHandle)(
                const_cast<OMX_STRING>(name), &numRoles2, array);
 
        if (err == OMX_ErrorNone && numRoles != numRoles2) {
            ALOGE("mGetRolesOfComponentHandle: numRoles %d != numRoles2 %d", (int)numRoles, (int)numRoles2);
            err = OMX_ErrorUndefined;
        }
 
        for (OMX_U32 i = 0; i < numRoles; ++i) {
            if (err == OMX_ErrorNone) {
                String8 s((const char *)array[i]);
                roles->push(s);
            }
 
            delete[] array[i];
            array[i] = NULL;
        }
 
        delete[] array;
        array = NULL;
    }
 
    return err;
}
 
}  // namespace android

ChagallOMXPlugin.h

#ifndef CHAGALL_OMX_PLUGIN_H_
#define CHAGALL_OMX_PLUGIN_H_
 
#include <OMXPluginBase.h>
 
namespace android {
 
struct ChagallOMXPlugin : public OMXPluginBase {
    ChagallOMXPlugin();
    virtual ~ChagallOMXPlugin();
 
    virtual OMX_ERRORTYPE makeComponentInstance(
            const char *name,
            const OMX_CALLBACKTYPE *callbacks,
            OMX_PTR appData,
            OMX_COMPONENTTYPE **component);
 
    virtual OMX_ERRORTYPE destroyComponentInstance(
            OMX_COMPONENTTYPE *component);
 
    virtual OMX_ERRORTYPE enumerateComponents(
            OMX_STRING name,
            size_t size,
            OMX_U32 index);
 
    virtual OMX_ERRORTYPE getRolesOfComponent(
            const char *name,
            Vector<String8> *roles);
 
private:
    void *mLibHandle;
 
    typedef OMX_ERRORTYPE (*InitFunc)();
    typedef OMX_ERRORTYPE (*DeinitFunc)();
    typedef OMX_ERRORTYPE (*ComponentNameEnumFunc)(
            OMX_STRING, OMX_U32, OMX_U32);
 
    typedef OMX_ERRORTYPE (*GetHandleFunc)(
            OMX_HANDLETYPE *, OMX_STRING, OMX_PTR, OMX_CALLBACKTYPE *);
 
    typedef OMX_ERRORTYPE (*FreeHandleFunc)(OMX_HANDLETYPE *);
 
    typedef OMX_ERRORTYPE (*GetRolesOfComponentFunc)(
            OMX_STRING, OMX_U32 *, OMX_U8 **);
 
    InitFunc mInit;
    DeinitFunc mDeinit;
    ComponentNameEnumFunc mComponentNameEnum;
    GetHandleFunc mGetHandle;
    FreeHandleFunc mFreeHandle;
    GetRolesOfComponentFunc mGetRolesOfComponentHandle;
 
    ChagallOMXPlugin(const ChagallOMXPlugin &);
    ChagallOMXPlugin &operator=(const ChagallOMXPlugin &);
};
 
}  // namespace android
 
#endif  // CHAGALL_OMX_PLUGIN_H_