/*
* OMX_GetComponentsOfRole()
*
* Description: This method will query the component for its supported roles
*
* Parameters:
* @param[in] role     The role name to query for
* @param[in] pNumComps     The number of components supporting the given role
* @param[in] compNames      The names of the components supporting the given role
*
* Returns:    OMX_NOERROR          Successful
*
* Note
*
*/
OMX_API OMX_ERRORTYPE OMX_GetComponentsOfRole(OMX_IN OMX_STRING role,
                                              OMX_INOUT OMX_U32 *pNumComps, OMX_INOUT OMX_U8 * *compNames)
{
    OMX_ERRORTYPE    eError = OMX_ErrorNone;
    OMX_U32          i = 0;
    OMX_U32          j = 0;
    OMX_U32          k = 0;

    CORE_require(role != NULL, OMX_ErrorBadParameter, NULL);
    CORE_require(pNumComps != NULL, OMX_ErrorBadParameter, NULL);
    CORE_require(count > 0, OMX_ErrorUndefined, "OMX_GetHandle called without calling OMX_Init first");

    /* This implies that the componentTable is not filled */
    CORE_assert(componentTable[i].pRoleArray[j] != NULL, OMX_ErrorBadParameter, NULL);

    for( i = 0; i < (OMX_U32)tableCount; i++ ) {
        for( j = 0; j < componentTable[i].nRoles; j++ ) {
            if( strcmp(componentTable[i].pRoleArray[j], role) == 0 ) {
                /* the first call to this function should only count the number
                of roles so that for the second call compNames can be allocated
                with the proper size for that number of roles */
                if( compNames != NULL ) {
                    strncpy((OMX_STRING) (compNames[k]),
                            (OMX_STRING) componentTable[i].name, MAXNAMESIZE);
                }
                k++;
            }
        }
    *pNumComps = k;
    }

EXIT:
    return (eError);
}
/*
* OMX_Init()
*
* Description:This method will initialize the OMX Core.  It is the
* responsibility of the application to call OMX_Init to ensure the proper
* set up of core resources.
*
* Returns:    OMX_NOERROR          Successful
*
* Note
*/
OMX_ERRORTYPE OMX_Init()
{
    OMX_ERRORTYPE eError = OMX_ErrorNone;
    OSAL_ERROR  eOsalError = OSAL_ErrNone;

    eOsalError = OSAL_ObtainMutex(pCoreInitMutex, OSAL_SUSPEND);
    CORE_assert(eOsalError == OSAL_ErrNone, OMX_ErrorInsufficientResources, "Mutex lock failed");
    count++;
    if( count == 1 ) {
        pthread_mutex_init(&mutex, NULL);
        eError = OMX_BuildComponentTable();
    }

    eOsalError = OSAL_ReleaseMutex(pCoreInitMutex);
    CORE_assert(eOsalError == OSAL_ErrNone, OMX_ErrorInsufficientResources, "Mutex release failed");
EXIT:
    return (eError);
}
/******************************Public*Routine******************************\
* OMX_Init()
*
* Description:This method will initialize the OMX Core.  It is the
* responsibility of the application to call OMX_Init to ensure the proper
* set up of core resources.
*
* Returns:    OMX_NOERROR          Successful
*
* Note
*
\**************************************************************************/
OMX_ERRORTYPE OMX_Init()
{
	OMX_ERRORTYPE eError = OMX_ErrorNone;
	TIMM_OSAL_ERRORTYPE eOsalError = TIMM_OSAL_ERR_NONE;

	eOsalError = TIMM_OSAL_MutexObtain(pCoreInitMutex, TIMM_OSAL_SUSPEND);
	CORE_assert(eOsalError == TIMM_OSAL_ERR_NONE,
	    OMX_ErrorInsufficientResources, "Mutex lock failed");

	count++;

	if (count == 1)
	{
		pthread_mutex_init(&mutex, NULL);
		eError = OMX_BuildComponentTable();
	}

	eOsalError = TIMM_OSAL_MutexRelease(pCoreInitMutex);
	CORE_assert(eOsalError == TIMM_OSAL_ERR_NONE,
	    OMX_ErrorInsufficientResources, "Mutex release failed");
      EXIT:
	return eError;
}
/******************************Public*Routine******************************\
* OMX_FreeHandle()
*
* Description:This method will unload the OMX component pointed by
* OMX_HANDLETYPE. It is the responsibility of the calling method to ensure that
* the Deinit method of the component has been called prior to unloading component
*
* Parameters:
* @param[in] hComponent the component to unload
*
* Returns:    OMX_NOERROR          Successful
*
* Note
*
\**************************************************************************/
OMX_ERRORTYPE OMX_FreeHandle(OMX_HANDLETYPE hComponent)
{

	OMX_ERRORTYPE eError = OMX_ErrorUndefined;
	OMX_COMPONENTTYPE *pHandle = (OMX_COMPONENTTYPE *) hComponent;
	int i;

	if (pthread_mutex_lock(&mutex) != 0)
	{
		TIMM_OSAL_Error("Core: Error in Mutex lock");
	}

	CORE_require(pHandle != NULL, OMX_ErrorBadParameter, NULL);
	CORE_require(count > 0, OMX_ErrorUndefined,
	    "OMX_FreeHandle called without calling OMX_Init first");

	/* Locate the component handle in the array of handles */
	for (i = 0; i < COUNTOF(pModules); i++)
	{
		if (pComponents[i] == hComponent)
			break;
	}

	CORE_assert(i != COUNTOF(pModules), OMX_ErrorBadParameter, NULL);

	eError = pHandle->ComponentDeInit(hComponent);
	if (eError != OMX_ErrorNone)
	{
		TIMM_OSAL_Error("Error From ComponentDeInit..");
	}

	/* release the component and the component handle */
	dlclose(pModules[i]);
	pModules[i] = NULL;
	free(pComponents[i]);

	pComponents[i] = NULL;
	eError = OMX_ErrorNone;

      EXIT:
	/* The unload is now complete, so set the error code to pass and exit */
	if (pthread_mutex_unlock(&mutex) != 0)
	{
		TIMM_OSAL_Error("Core: Error in Mutex unlock");
	}

	return eError;
}
/******************************Public*Routine******************************\
* OMX_DeInit()
*
* Description:This method will release the resources of the OMX Core.  It is the
* responsibility of the application to call OMX_DeInit to ensure the clean up of these
* resources.
*
* Returns:    OMX_NOERROR          Successful
*
* Note
*
\**************************************************************************/
OMX_ERRORTYPE OMX_Deinit()
{
	OMX_ERRORTYPE eError = OMX_ErrorNone;
	TIMM_OSAL_ERRORTYPE eOsalError = TIMM_OSAL_ERR_NONE;

	eOsalError = TIMM_OSAL_MutexObtain(pCoreInitMutex, TIMM_OSAL_SUSPEND);
	if (eOsalError != TIMM_OSAL_ERR_NONE)
	{
		TIMM_OSAL_Error("Mutex lock failed");
	}
	/*Returning error none because of OMX spec limitation on error codes that
	   can be returned by OMX_Deinit */
	CORE_assert(count > 0, OMX_ErrorNone,
	    "OMX_Deinit being called without a corresponding OMX_Init");
	count--;

	if (pthread_mutex_lock(&mutex) != 0)
		TIMM_OSAL_Error("Core: Error in Mutex lock");

	if (count == 0)
	{
		if (pthread_mutex_unlock(&mutex) != 0)
			TIMM_OSAL_Error("Core: Error in Mutex unlock");
		if (pthread_mutex_destroy(&mutex) != 0)
		{
			/*printf("%d :: Core: Error in Mutex destroy\n",__LINE__); */
		}
	} else
	{
		if (pthread_mutex_unlock(&mutex) != 0)
			TIMM_OSAL_Error("Core: Error in Mutex unlock");
	}

      EXIT:
	eOsalError = TIMM_OSAL_MutexRelease(pCoreInitMutex);
	if (eOsalError != TIMM_OSAL_ERR_NONE)
	{
		TIMM_OSAL_Error("Mutex release failed");
	}
	return eError;
}
/*
* OMX_DeInit()
*
* Description:This method will release the resources of the OMX Core.  It is the
* responsibility of the application to call OMX_DeInit to ensure the clean up of these
* resources.
*
* Returns:    OMX_NOERROR          Successful
*
* Note
*
*/
OMX_ERRORTYPE OMX_Deinit()
{
    OMX_ERRORTYPE   eError = OMX_ErrorNone;
    OSAL_ERROR    eOsalError = OSAL_ErrNone;

    eOsalError = OSAL_ObtainMutex(pCoreInitMutex, OSAL_SUSPEND);
    if( eOsalError != OSAL_ErrNone ) {
        OSAL_ErrorTrace("Mutex lock failed");
    }
    /*Returning error none because of OMX spec limitation on error codes that
    can be returned by OMX_Deinit */
    CORE_assert(count > 0, OMX_ErrorNone, "OMX_Deinit being called without a corresponding OMX_Init");
    count--;

    if( pthread_mutex_lock(&mutex) != 0 ) {
        OSAL_ErrorTrace("Core: Error in Mutex lock");
    }

    if( count == 0 ) {
        if( pthread_mutex_unlock(&mutex) != 0 ) {
            OSAL_ErrorTrace("Core: Error in Mutex unlock");
        }
        if( pthread_mutex_destroy(&mutex) != 0 ) {
            OSAL_ErrorTrace("%d :: Core: Error in Mutex destroy\n" ,__LINE__);
        }
    } else {
        if( pthread_mutex_unlock(&mutex) != 0 ) {
            OSAL_ErrorTrace("Core: Error in Mutex unlock");
        }
    }

EXIT:
    eOsalError = OSAL_ReleaseMutex(pCoreInitMutex);
    if( eOsalError != OSAL_ErrNone) {
        OSAL_ErrorTrace("Mutex release failed");
    }
    return (eError);
}
OMX_ERRORTYPE OMX_BuildComponentTable()
{
	OMX_ERRORTYPE eError = OMX_ErrorNone;
	OMX_CALLBACKTYPE sCallbacks;
#ifndef STATIC_TABLE
	OMX_HANDLETYPE hComp = 0;
	OMX_U8 cRole[MAXNAMESIZE];
	OMX_STRING tempName = NULL;
	OMX_STRING temp = NULL;
	static OMX_STRING namePrefix = "OMX";
	static OMX_STRING filePrefix = "libOMX.";
	static OMX_STRING suffix = ".so";
#endif
	int j = 0;
	int numFiles = 0;
	int i, k;
	int componentfound = 0;

	/* set up dummy call backs */
	sCallbacks.EventHandler = ComponentTable_EventHandler;
	sCallbacks.EmptyBufferDone = ComponentTable_EmptyBufferDone;
	sCallbacks.FillBufferDone = ComponentTable_FillBufferDone;

#ifndef STATIC_TABLE
	/* allocate the name table */
	/*
	   compName = (OMX_STRING *) malloc(MAX_TABLE_SIZE * sizeof(OMX_STRING));
	   sRoleArray = (OMX_STRING**) malloc(MAX_TABLE_SIZE * sizeof(OMX_STRING));
	 */

	/* scan the target/lib directory and create a list of files in the directory */
	numFiles = scandir(libdir, &namelist, 0, 0);
	tableCount = 0;
	while (numFiles--)
	{
		/*  check if the file is an OMX component */
		if (strncmp(namelist[numFiles]->d_name, filePrefix,
			strlen(filePrefix)) == 0)
		{

			/* if the file is an OMX component, trim the prefix and suffix */
			tempName = (OMX_STRING) malloc(sizeof(namelist[numFiles]->d_name) + 1);	/* adding one ensures */
			memset(tempName, 0x00, sizeof(namelist[numFiles]->d_name) + 1);	/*  that a null terminator will */
			/*  always be present */
			/* copy only the name without the suffix */
			strncpy(tempName, namelist[numFiles]->d_name,
			    strlen(namelist[numFiles]->d_name) -
			    strlen(suffix));
			/* set a pointer to be after the lib prefix, i.e the beginning of the component name */
			temp = strstr(tempName, namePrefix);

			/* then copy the component name to the table */
			/*
			   compName[tableCount]= (OMX_STRING) malloc(MAXNAMESIZE);
			 */
			strncpy(compName[tableCount], temp, strlen(temp) + 1);
			componentTable[tableCount].name =
			    compName[tableCount];

			/* get the handle for the component and query for the roles of each component */
			eError =
			    OMX_GetHandle(&hComp,
			    componentTable[tableCount].name, 0x0,
			    &sCallbacks);
			if (eError == OMX_ErrorNone)
			{
				j = 0;
				while (eError != OMX_ErrorNoMore)
				{
					eError =
					    ((OMX_COMPONENTTYPE *) hComp)->
					    ComponentRoleEnum(hComp, cRole,
					    j++);
					if (eError == OMX_ErrorNotImplemented)
					{
						j = 1;
						break;
					}
				}
				nRoles = j - 1;
				componentTable[tableCount].nRoles = nRoles;
				/* sRoleArray[tableCount] = (OMX_STRING *) malloc(nRoles * sizeof(OMX_STRING)); */
				if (nRoles > 0)
				{
					/* sRoleArray[tableCount] = (OMX_STRING *) malloc(nRoles * sizeof(OMX_STRING)); */
					for (j = 0; j < nRoles; j++)
					{
						sRoleArray[tableCount][j] =
						    (OMX_STRING)
						    malloc(sizeof(OMX_U8) *
						    MAXNAMESIZE);
						((OMX_COMPONENTTYPE *)
						    hComp)->
						    ComponentRoleEnum(hComp,
						    (OMX_U8 *)
						    sRoleArray[tableCount][j],
						    j);
						componentTable[tableCount].
						    pRoleArray[j] =
						    sRoleArray[tableCount][j];
					}
				} else
				{
					/* sRoleArray[tableCount] = (OMX_STRING *) malloc(sizeof(OMX_STRING));    */
					sRoleArray[tableCount][j] =
					    (OMX_STRING) malloc(sizeof(OMX_U8)
					    * MAXNAMESIZE);
					strcpy(sRoleArray[tableCount][j],
					    EMPTY_STRING);
					componentTable[tableCount].
					    pRoleArray[j] =
					    sRoleArray[tableCount][j];
				}
			}
			if (hComp)
			{
				/* free the component handle */
				eError = OMX_FreeHandle(hComp);
				if (eError != OMX_ErrorNone)
				{
					goto EXIT;
				}
			}
			/* increment the table counter index only if above was successful */
			tableCount++;
			if (tempName != NULL)
			{
				free(tempName);
			}

		}
	}

#endif

	for (i = 0, numFiles = 0; i < MAXCOMP; i++)
	{
		if (tComponentName[i][0] == NULL)
		{
			break;
		}

		for (j = 0; j < numFiles; j++)
		{
			if (!strcmp(componentTable[j].name,
				tComponentName[i][0]))
			{
				componentfound = 1;
				break;
			}
		}
		if (componentfound == 1)
		{
			continue;
		}

		if (j == numFiles)
		{		/* new component */
			k = 1;
			while (tComponentName[i][k] != NULL)
			{
				componentTable[numFiles].pRoleArray[k - 1] =
				    tComponentName[i][k];
				k++;
			}
			componentTable[numFiles].nRoles = k - 1;
			strcpy(compName[numFiles], tComponentName[i][0]);
			componentTable[numFiles].name = compName[numFiles];
			numFiles++;
		}
	}
	tableCount = numFiles;

	CORE_assert(eError == OMX_ErrorNone, eError,
	    "Could not build Component Table");
      EXIT:
	return eError;
}
OMX_ERRORTYPE OMX_GetHandle(OMX_HANDLETYPE * pHandle,
    OMX_STRING cComponentName, OMX_PTR pAppData,
    OMX_CALLBACKTYPE * pCallBacks)
{
	static const char prefix[] = "lib";
	static const char postfix[] = ".so";
	OMX_ERRORTYPE(*pComponentInit) (OMX_HANDLETYPE *);
	OMX_ERRORTYPE eError = OMX_ErrorNone;
	OMX_COMPONENTTYPE *componentType;
	int i;
	char buf[sizeof(prefix) + MAXNAMESIZE + sizeof(postfix)];
	const char *pErr = dlerror();
	char *dlError = NULL;
#ifdef CHECK_SECURE_STATE
        int secure_misc_drv_fd,ret;
        OMX_U8 mode, enable=1;
#endif
	if (pthread_mutex_lock(&mutex) != 0)
	{
		TIMM_OSAL_Error("Core: Error in Mutex lock");
	}

	CORE_require(NULL != cComponentName, OMX_ErrorBadParameter, NULL);
	CORE_require(NULL != pHandle, OMX_ErrorBadParameter, NULL);
	CORE_require(NULL != pCallBacks, OMX_ErrorBadParameter, NULL);
	CORE_require(count > 0, OMX_ErrorUndefined,
	    "OMX_GetHandle called without calling OMX_Init first");

	/* Verify that the name is not too long and could cause a crash.  Notice
	 * that the comparison is a greater than or equals.  This is to make
	 * sure that there is room for the terminating NULL at the end of the
	 * name. */
	CORE_require(strlen(cComponentName) < MAXNAMESIZE,
	    OMX_ErrorInvalidComponentName, NULL);

	/* Locate the first empty slot for a component.  If no slots
	 * are available, error out */
	for (i = 0; i < COUNTOF(pModules); i++)
	{
		if (pModules[i] == NULL)
			break;
	}
	CORE_assert(i != COUNTOF(pModules), OMX_ErrorInsufficientResources,
	    NULL);

	/* load the component and check for an error.  If filename is not an
	 * absolute path (i.e., it does not  begin with a "/"), then the
	 * file is searched for in the following locations:
	 *
	 *     The LD_LIBRARY_PATH environment variable locations
	 *     The library cache, /etc/ld.so.cache.
	 *     /lib
	 *     /usr/lib
	 *
	 * If there is an error, we can't go on, so set the error code and exit */
	strcpy(buf, prefix);	/* the lengths are defined herein or have been */
	strcat(buf, cComponentName);	/* checked already, so strcpy and strcat are  */
	strcat(buf, postfix);	/* are safe to use in this context. */

#ifdef CHECK_SECURE_STATE
        //Dont return errors from misc driver to the user if any.
        //Since this affects all usecases, secure and non-secure.
        //Do log the errors though.
        secure_misc_drv_fd = open("/dev/rproc_user", O_SYNC | O_RDONLY);
	if (secure_misc_drv_fd < 0)
	{
		TIMM_OSAL_Error("Can't open misc driver device 0x%x\n", errno);
	}

	ret = read(secure_misc_drv_fd, &mode, sizeof(mode));
	if (ret < 0)
	{
		TIMM_OSAL_Error("Can't read from the misc driver");
	}
        if(mode == enable && strstr(cComponentName,"secure") == NULL)
	{
		TIMM_OSAL_Error("non-secure component not supported in secure mode");
		eError = OMX_ErrorComponentNotFound;
	}
	ret = close(secure_misc_drv_fd);
	if (ret < 0)
	{
		TIMM_OSAL_Error("Can't close the misc driver");
	}
        //Dont allow non-secure usecases if we are in secure state.
        //Else some of the memory regions will be unexpected firewalled.
        //This provides a clean exit in case we are in secure mode.
        if(eError == OMX_ErrorComponentNotFound)
        {
                goto EXIT;
        }
#endif

//#if 0
	pModules[i] = dlopen(buf, RTLD_LAZY | RTLD_GLOBAL);
	if (pModules[i] == NULL)
	{
		dlError = dlerror();
		TIMM_OSAL_Error("Failed because %s", dlError);
		eError = OMX_ErrorComponentNotFound;
		goto EXIT;
	}

	/* Get a function pointer to the "OMX_ComponentInit" function.  If
	 * there is an error, we can't go on, so set the error code and exit */
	pComponentInit = dlsym(pModules[i], "OMX_ComponentInit");
	pErr = dlerror();
	CORE_assert(((pErr == NULL) && (pComponentInit != NULL)),
	    OMX_ErrorInvalidComponent, NULL);
//#endif

	/* We now can access the dll.  So, we need to call the "OMX_ComponentInit"
	 * method to load up the "handle" (which is just a list of functions to
	 * call) and we should be all set.*/
	*pHandle = malloc(sizeof(OMX_COMPONENTTYPE));
	CORE_assert((*pHandle != NULL), OMX_ErrorInsufficientResources,
	    "Malloc of pHandle* failed");

	pComponents[i] = *pHandle;
	componentType = (OMX_COMPONENTTYPE *) * pHandle;
	componentType->nSize = sizeof(OMX_COMPONENTTYPE);

	componentType->nVersion.s.nVersionMajor = 1;
	componentType->nVersion.s.nVersionMinor = 1;
	componentType->nVersion.s.nRevision = 0;
	componentType->nVersion.s.nStep = 0;

	eError = (*pComponentInit) (*pHandle);
//eError = OMX_ComponentInit(*pHandle);
	if (OMX_ErrorNone == eError)
	{
		eError =
		    (componentType->SetCallbacks) (*pHandle, pCallBacks,
		    pAppData);
		CORE_assert(eError == OMX_ErrorNone, eError,
		    "Core: Error returned from component SetCallBack");
	} else
	{
		/* when the component fails to initialize, release the
		   component handle structure */
		free(*pHandle);
		/* mark the component handle as NULL to prevent the caller from
		   actually trying to access the component with it, should they
		   ignore the return code */
		*pHandle = NULL;
		pComponents[i] = NULL;
		dlclose(pModules[i]);
		goto EXIT;
	}
	eError = OMX_ErrorNone;
      EXIT:
	if (pthread_mutex_unlock(&mutex) != 0)
	{
		TIMM_OSAL_Error("Core: Error in Mutex unlock");
	}
	return (eError);
}
OMX_ERRORTYPE OMX_GetHandle(OMX_HANDLETYPE *pHandle,
                            OMX_STRING cComponentName, OMX_PTR pAppData,
                            OMX_CALLBACKTYPE *pCallBacks)
{
    static const char    prefix[] = "lib";
    static const char    postfix[] = ".so";

    OMX_ERRORTYPE        (*pComponentInit)(OMX_HANDLETYPE *);
    OMX_ERRORTYPE        eError = OMX_ErrorNone;
    OMX_COMPONENTTYPE   *componentType;
    int                  i;
    char                 buf[sizeof(prefix) + MAXNAMESIZE + sizeof(postfix)];
    const char          *pErr = dlerror();
    char                *dlError = NULL;
    if( pthread_mutex_lock(&mutex) != 0 ) {
        OSAL_ErrorTrace("Core: Error in Mutex lock");
    }

    CORE_require(NULL != cComponentName, OMX_ErrorBadParameter, NULL);
    CORE_require(NULL != pHandle, OMX_ErrorBadParameter, NULL);
    CORE_require(NULL != pCallBacks, OMX_ErrorBadParameter, NULL);
    CORE_require(count > 0, OMX_ErrorUndefined, "OMX_GetHandle called without calling OMX_Init first");

    /* Verify that the name is not too long and could cause a crash.  Notice
    * that the comparison is a greater than or equals.  This is to make
    * sure that there is room for the terminating NULL at the end of the
    * name. */
    CORE_require(strlen(cComponentName) < MAXNAMESIZE,
    OMX_ErrorInvalidComponentName, NULL);

    /* Locate the first empty slot for a component.  If no slots
    * are available, error out */
    for( i = 0; i < (int)COUNTOF(pModules); i++ ) {
        if( pModules[i] == NULL ) {
            break;
        }
    }

    CORE_assert(i != COUNTOF(pModules), OMX_ErrorInsufficientResources, NULL);

    /* load the component and check for an error.  If filename is not an
    * absolute path (i.e., it does not  begin with a "/" ), then the
    * file is searched for in the following locations:
    *
    *     The LD_LIBRARY_PATH environment variable locations
    *     The library cache, /etc/ld.so.cache.
    *     /lib
    *     /usr/lib
    *
    * If there is an error, we can't go on, so set the error code and exit */
    strcpy(buf, prefix);    /* the lengths are defined herein or have been */
    strcat(buf, cComponentName);    /* checked already, so strcpy and strcat are  */
    strcat(buf, postfix);   /* are safe to use in this context. */

    pModules[i] = dlopen(buf, RTLD_LAZY | RTLD_GLOBAL);
    if( pModules[i] == NULL ) {
        dlError = (char *)dlerror();
        OSAL_ErrorTrace("Failed because %s", dlError);
        eError = OMX_ErrorComponentNotFound;
        goto EXIT;
    }

    /* Get a function pointer to the "OMX_ComponentInit" function.  If
    * there is an error, we can't go on, so set the error code and exit */
    pComponentInit = dlsym(pModules[i], "OMX_ComponentInit");
    pErr = dlerror();
    CORE_assert(((pErr == NULL) && (pComponentInit != NULL)), OMX_ErrorInvalidComponent, NULL);

    /* We now can access the dll.  So, we need to call the "OMX_ComponentInit"
    * method to load up the "handle" (which is just a list of functions to
    * call) and we should be all set.
    */
    *pHandle = malloc(sizeof(OMX_COMPONENTTYPE));
    CORE_assert((*pHandle != NULL), OMX_ErrorInsufficientResources, "Malloc of pHandle* failed");

    pComponents[i] = *pHandle;
    componentType = (OMX_COMPONENTTYPE *) *pHandle;
    componentType->nSize = sizeof(OMX_COMPONENTTYPE);

    componentType->nVersion.s.nVersionMajor = 1;
    componentType->nVersion.s.nVersionMinor = 1;
    componentType->nVersion.s.nRevision = 0;
    componentType->nVersion.s.nStep = 0;

    eError = (*pComponentInit)(*pHandle);
    if( OMX_ErrorNone == eError ) {
        eError = (componentType->SetCallbacks)(*pHandle, pCallBacks, pAppData);
        CORE_assert(eError == OMX_ErrorNone, eError, "Core: Error returned from component SetCallBack");
    } else {
        /* when the component fails to initialize, release the
        component handle structure */
        free(*pHandle);
        /* mark the component handle as NULL to prevent the caller from
        actually trying to access the component with it, should they
        ignore the return code */
        *pHandle = NULL;
        pComponents[i] = NULL;
        dlclose(pModules[i]);
        goto EXIT;
    }
    eError = OMX_ErrorNone;
EXIT:
    if( pthread_mutex_unlock(&mutex) != 0 ) {
        OSAL_ErrorTrace("Core: Error in Mutex unlock");
    }
    return (eError);
}