android OMX相关2: allocateNode流程详解

转载请注明:https://www.jianshu.com/p/0cb16c896c36

以RK3399为例

相关数据结构:


中心主题.png

OMX::allocateNode -> new OMXNodeInstance
-> OMXMaster::makeComponentInstance -> 从名称插件map中查到名字对应的插件 OMXPluginBase *plugin = mPluginByComponentName.valueAt(index) -> plugin->makeComponentInstance
-> RKOMXPlugin::makeComponentInstance  如果用到了插件libstagefrighthw.so
    ->对Vector mCores循环  mCores存着编码器和解码器各一
    -> 调用core->mGetHandle句柄
    -> Rockchip_OMX_Core: RKOMX_GetHandle(pHandle, cComponentName, pAppData, pCallBacks)
        创建ROCKCHIP_OMX_COMPONENT *loadComponent 对象, 
        loadComponent->libName = "libomxvpu_enc.so", loadComponent->componentName = "rk.omx_enc"
    -> Rockchip_OMX_Component_Register: Rockchip_OMX_ComponentLoad(loadComponent)
    -> dlopen(libomxvpu_enc.so) -> dlsym("Rockchip_OMX_ComponentConstructor")  对应代码位于hardware_rockchip\omx_il\component\video\enc
        -> Rkvpu_OMX_Venc: Rockchip_OMX_ComponentConstructor
            -> Rockchip_OMX_Basecomponent: Rockchip_OMX_BaseComponent_Constructor(OMX_HANDLETYPE hComponent)
                ->构建变量ROCKCHIP_OMX_BASECOMPONENT *pRockchipComponent,  pOMXComponent->pComponentPrivate=pRockchipComponent
                ->创建互斥量:Rockchip_OSAL_SemaphoreCreate(&pRockchipComponent->msgSemaphoreHandle),
                    Rockchip_OSAL_MutexCreate(&pRockchipComponent->compMutex),
                    Rockchip_OSAL_SignalCreate(&pRockchipComponent->abendStateEvent)
                ->Rockchip_OSAL_QueueCreate(&pRockchipComponent->messageQ, MAX_QUEUE_ELEMENTS);创建消息队列
                -> Rockchip_OSAL_ThreadCreate(&pRockchipComponent->hMessageHandler, Rockchip_OMX_MessageHandlerThread, pOMXComponent);  创建消息处理线程Rockchip_OMX_MessageHandlerThread
                -> 设置几个函数指针
                        pOMXComponent->GetComponentVersion = &Rockchip_OMX_GetComponentVersion;
                        pOMXComponent->SendCommand         = &Rockchip_OMX_SendCommand;   //用于给消息队列发送指令
                        pOMXComponent->GetState            = &Rockchip_OMX_GetState;
                        pOMXComponent->SetCallbacks        = &Rockchip_OMX_SetCallbacks;
                        pOMXComponent->UseEGLImage         = &Rockchip_OMX_UseEGLImage;
            ->>>>>>Rockchip_OMX_Basecomponent: Rockchip_OMX_BaseComponent_Constructor end            
            ->Rockchip_OMX_Baseport: Rockchip_OMX_Port_Constructor(pOMXComponent)
                -> pRockchipComponent = (ROCKCHIP_OMX_BASECOMPONENT *)pOMXComponent->pComponentPrivate
                -> 设置版本号,port数量为ALL_PORT_NUM = 2, INPUT_PORT_INDEX = 1
                    INIT_SET_SIZE_VERSION(&pRockchipComponent->portParam, OMX_PORT_PARAM_TYPE);
                    pRockchipComponent->portParam.nPorts = ALL_PORT_NUM;
                    pRockchipComponent->portParam.nStartPortNumber = INPUT_PORT_INDEX;
                    pRockchipPort = Rockchip_OSAL_Malloc(sizeof(ROCKCHIP_OMX_BASEPORT) * ALL_PORT_NUM);
                ->  构建input port: pRockchipInputPort
                    pRockchipComponent->pRockchipPort = pRockchipPort; 利用中间变量创建两个队列
                    pRockchipInputPort = &pRockchipPort[INPUT_PORT_INDEX];
                    Rockchip_OSAL_QueueCreate(&pRockchipInputPort->bufferQ, MAX_QUEUE_ELEMENTS);
                    Rockchip_OSAL_QueueCreate(&pRockchipInputPort->securebufferQ, MAX_QUEUE_ELEMENTS);
                    pRockchipInputPort->extendBufferHeader = Rockchip_OSAL_Malloc(sizeof(ROCKCHIP_OMX_BUFFERHEADERTYPE) * MAX_BUFFER_NUM);
                    ...
                ->  构建output buffer : pRockchipOutputPort
                -> 给返回参数设置函数指针
                    pOMXComponent->EmptyThisBuffer = &Rockchip_OMX_EmptyThisBuffer;
                    pOMXComponent->FillThisBuffer  = &Rockchip_OMX_FillThisBuffer;
            ->>>>>>  Rockchip_OMX_Baseport: Rockchip_OMX_Port_Constructor end
            ->构建对象 RKVPU_OMX_VIDEOENC_COMPONENT *pVideoEnc
                ->pVideoEnc->hSharedMemory 这个字段存着"/dev/ion" 这个文件的句柄
            ->设置 pRockchipComponent 的各种版本信息
                pRockchipComponent->hComponentHandle = pVideoEnc(把上一步的共享内存句柄赋值到这里)
                pRockchipComponent->bMultiThreadProcess = OMX_TRUE;
                pRockchipComponent->codecType = HW_VIDEO_ENC_CODEC;
            -> 设置pVideoEnc的信息
                pVideoEnc->bFirstFrame = OMX_TRUE;
                pVideoEnc->bFirstInput = OMX_TRUE;
                pVideoEnc->bFirstOutput = OMX_TRUE;
                pVideoEnc->configChange = OMX_FALSE;
                pVideoEnc->bStoreMetaData = OMX_FALSE;
                pVideoEnc->bPrependSpsPpsToIdr = OMX_FALSE;
                pVideoEnc->bRkWFD = OMX_FALSE;
                pVideoEnc->quantization.nQpI = 4; // I frame quantization parameter
                pVideoEnc->quantization.nQpP = 5; // P frame quantization parameter
                pVideoEnc->quantization.nQpB = 5; // B frame quantization parameter
                //add by xlm for use mpp or vpuapi
                pVideoEnc->bIsUseMpp = OMX_FALSE;
                pVideoEnc->bIsNewVpu = OMX_TRUE;        
            -> 初始化input port: pRockchipComponent->pRockchipPort[INPUT_PORT_INDEX]
                一堆赋值操作
            -> 初始化output port: pRockchipComponent->pRockchipPort[OUTPUT_PORT_INDEX]
                一堆赋值操作
            -> pOMXComponent 设置一堆函数指针
                pOMXComponent->UseBuffer              = &Rkvpu_OMX_UseBuffer;
                pOMXComponent->AllocateBuffer         = &Rkvpu_OMX_AllocateBuffer;
                pOMXComponent->FreeBuffer             = &Rkvpu_OMX_FreeBuffer;
                pOMXComponent->ComponentTunnelRequest = &Rkvpu_OMX_ComponentTunnelRequest;
                pOMXComponent->GetParameter           = &Rkvpu_OMX_GetParameter;
                pOMXComponent->SetParameter           = &Rkvpu_OMX_SetParameter;
                pOMXComponent->GetConfig              = &Rkvpu_OMX_GetConfig;
                pOMXComponent->SetConfig              = &Rkvpu_OMX_SetConfig;
                pOMXComponent->GetExtensionIndex      = &Rkvpu_OMX_GetExtensionIndex;
                pOMXComponent->ComponentRoleEnum      = &Rkvpu_OMX_ComponentRoleEnum;
                pOMXComponent->ComponentDeInit        = &Rockchip_OMX_ComponentDeInit;
            -> pRockchipComponent设置一堆函数指针
                pRockchipComponent->rockchip_codec_componentInit      = &Rkvpu_Enc_ComponentInit;
                pRockchipComponent->rockchip_codec_componentTerminate = &Rkvpu_Enc_Terminate;
                pRockchipComponent->rockchip_AllocateTunnelBuffer = &Rkvpu_OMX_AllocateTunnelBuffer;
                pRockchipComponent->rockchip_FreeTunnelBuffer     = &Rkvpu_OMX_FreeTunnelBuffer;
                pRockchipComponent->rockchip_BufferProcessCreate    = &Rkvpu_OMX_BufferProcess_Create;  //注意这个函数指针,会启动两个线程
                pRockchipComponent->rockchip_BufferProcessTerminate = &Rkvpu_OMX_BufferProcess_Terminate;
                pRockchipComponent->rockchip_BufferFlush          = &Rkvpu_OMX_BufferFlush;
            -> 为pVideoEnc中的AVCComponent数组设置参数,????这里比较可疑,没有生效
                INIT_SET_SIZE_VERSION(&pVideoEnc->AVCComponent[i], OMX_VIDEO_PARAM_AVCTYPE);
                pVideoEnc->AVCComponent[i].nPortIndex = i;
                pVideoEnc->AVCComponent[i].eProfile   = OMX_VIDEO_AVCProfileBaseline;
                pVideoEnc->AVCComponent[i].eLevel     = OMX_VIDEO_AVCLevel31;
                pVideoEnc->AVCComponent[i].nPFrames = 20;
                pVideoEnc->codecId = OMX_VIDEO_CodingAVC;
                pRockchipPort->portDefinition.format.video.eCompressionFormat = OMX_VIDEO_CodingAVC;
            ->设置状态机:     pRockchipComponent->currentState = OMX_StateLoaded;
            ->>>>>>Rkvpu_OMX_Venc: Rockchip_OMX_ComponentConstructor end
    ->>>>>>Rockchip_OMX_Component_Register: Rockchip_OMX_ComponentLoad(loadComponent)  end
        rockchip_component 就是调用者传进的参数loadComponent
        rockchip_component->libHandle = libHandle;
        rockchip_component->pOMXComponent = pOMXComponent;
        rockchip_component->rkversion = OMX_COMPILE_INFO;
    -> loadComponent->pOMXComponent->SetCallbacks(loadComponent->pOMXComponent, pCallBacks, pAppData)
        这里会设置一个回调,回调函数是从OMX中创建的 OMXNodeInstance 对象的 kCallbacks
    ->>>>>> Rockchip_OMX_Core: RKOMX_GetHandle(pHandle, cComponentName, pAppData, pCallBacks)  end
        返回给调用者的 pHandle = loadComponent->pOMXComponent
    -> 构建 RKOMXComponent comp
        comp.mComponent = *component //这里的component就是上一步RKOMX_GetHandle的参数pHandle
        comp.mCore = mCores[i];
    -> mComponents.push_back(comp); 把组件加入队列
->>>> 循环结束
->>>> RKOMXPlugin::makeComponentInstance结束
-> instance->setHandle(handle) // instance是OMXNodeInstance对象,
    这里把上面创建的OMX_COMPONENTTYPE *handle给赋值到OMXNodeInstance:mHandle
->>>> OMX::allocateNode结束

你可能感兴趣的:(android OMX相关2: allocateNode流程详解)