OMX_ERRORTYPE PortCommTest_TransitionWait(OMX_STATETYPE eToState,
            PortCommTestCtxt* pContext)
    {
        OMX_ERRORTYPE eError = OMX_ErrorNone;
        OMX_BOOL bTimeout = OMX_FALSE;

        OMX_OSAL_EventReset(pContext->hStateSetEvent);
        eError = OMX_SendCommand(pContext->hWComp, OMX_CommandStateSet, eToState, 0);
        OMX_CONF_BAIL_IF_ERROR(eError);

        if (eToState == OMX_StateIdle && (pContext->eState == OMX_StateLoaded))
        {
            OMX_CONF_BAIL_IF_ERROR(PortCommTest_OperateOnPorts(pContext, AllocBuf));
        }
        else if (eToState == OMX_StateLoaded && pContext->eState == OMX_StateIdle)
        {
            OMX_CONF_BAIL_IF_ERROR(PortCommTest_DeInitBuffer(pContext));
        }

        OMX_OSAL_EventWait(pContext->hStateSetEvent, OMX_CONF_TIMEOUT_EXPECTING_SUCCESS, &bTimeout);

        if (bTimeout == OMX_TRUE)
            OMX_CONF_SET_ERROR_BAIL("Transition timed out\n", OMX_ErrorUndefined);

        if (pContext->eState != eToState)
            OMX_CONF_SET_ERROR_BAIL("Incorrect transition\n", OMX_ErrorUndefined);

OMX_CONF_TEST_BAIL:
        return eError;
    }
    OMX_ERRORTYPE ResourceExhaustionTest_AllocateAllBuffers(ResourceExhaustionTestContext *pCtxt)
    {
        OMX_ERRORTYPE eError = OMX_ErrorNone;
        OMX_PARAM_PORTDEFINITIONTYPE sPortDef;
        OMX_U32 i, j, k;
        OMX_BUFFERHEADERTYPE *pBufferHdr = NULL;
        BufferList *pBufferListObj;
        BufferList *pTemp;

        /* for all ports */
        for (j = 0; j < NUM_DOMAINS; j++)
        {
            for (i = pCtxt->sPortParam[j].nStartPortNumber;
                    i < pCtxt->sPortParam[j].nStartPortNumber +
                    pCtxt->sPortParam[j].nPorts; i++)
            {

                OMX_CONF_INIT_STRUCT(sPortDef, OMX_PARAM_PORTDEFINITIONTYPE);
                sPortDef.nPortIndex = i;
                OMX_CONF_BAIL_IF_ERROR(OMX_GetParameter(pCtxt->hWComp[pCtxt->nInst],
                                                        OMX_IndexParamPortDefinition,
                                                        (OMX_PTR)&sPortDef));
                if (sPortDef.nBufferCountActual == 0x0 ||
                        sPortDef.nBufferCountActual < sPortDef.nBufferCountMin)
                    OMX_CONF_SET_ERROR_BAIL("Incorrect nBufferCountActual\n", OMX_ErrorUndefined);

                for (k = 0x0; k < sPortDef.nBufferCountActual; k++)
                {
                    pBufferListObj = (BufferList *)OMX_OSAL_Malloc(sizeof(BufferList));
                    if (!pBufferListObj)
                    {
                        OMX_CONF_SET_ERROR_BAIL("Malloc internal list failed\n", OMX_ErrorInsufficientResources);
                    }
                    OMX_CONF_BAIL_IF_ERROR(OMX_AllocateBuffer(pCtxt->hWComp[pCtxt->nInst], &pBufferHdr,
                                           sPortDef.nPortIndex, 0, sPortDef.nBufferSize));
                    pBufferListObj->pNextBuf = NULL;
                    pBufferListObj->pBufHdr = pBufferHdr;

                    if (sPortDef.eDir == OMX_DirInput)
                        pBufferListObj->pBufHdr->nOutputPortIndex = OMX_NOPORT;
                    else
                        pBufferListObj->pBufHdr->nInputPortIndex = OMX_NOPORT;

                    if (pCtxt->pBufferList[pCtxt->nInst] == NULL)
                        pCtxt->pBufferList[pCtxt->nInst] = pBufferListObj;
                    else
                    {
                        pTemp = pCtxt->pBufferList[pCtxt->nInst];
                        while (pTemp->pNextBuf)
                        {
                            pTemp = pTemp->pNextBuf;
                        }
                        pTemp->pNextBuf = pBufferListObj;
                    }
                }
            }
        }
OMX_CONF_TEST_BAIL:
        return eError;
    }
    OMX_ERRORTYPE PortCommTest_DeInitBuffer(PortCommTestCtxt* pContext)
    {
        OMX_U8 *pBuffer;
        BufferList *pBufferListObject;
        BufferList *pTemp;
        OMX_ERRORTYPE eError = OMX_ErrorNone;

        OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "Free all buffers\n");
        pTemp = pContext->pInBufferList;
        while (pTemp)
        {
            pBufferListObject = (BufferList *)pTemp;
            pBuffer = (OMX_U8 *)pTemp->pOrigBufHdr->pBuffer;
            if (pContext->bClientAllocBuf)
            {
                OMX_OSAL_Free(pBuffer);
                pTemp->pOrigBufHdr->pBuffer = NULL;
            }
            eError = OMX_FreeBuffer(pContext->hWComp, pTemp->pOrigBufHdr->nInputPortIndex, pTemp->pOrigBufHdr);
            if (eError == OMX_ErrorInvalidState)
                eError = OMX_ErrorNone;
            OMX_CONF_BAIL_IF_ERROR(eError);
            pTemp = pTemp->pNextBuf;
            if (pBufferListObject)
                OMX_OSAL_Free(pBufferListObject);
            pContext->nInBuf--;
        }
        pContext->pInBufferList = NULL;

        pTemp = pContext->pOutBufferList;
        while (pTemp)
        {
            pBufferListObject = (BufferList *)pTemp;
            pBuffer = (OMX_U8 *)pTemp->pOrigBufHdr->pBuffer;
            if (pContext->bClientAllocBuf)
            {
                OMX_OSAL_Free(pBuffer);
                pTemp->pOrigBufHdr->pBuffer = NULL;
            }
            eError = OMX_FreeBuffer(pContext->hWComp, pTemp->pOrigBufHdr->nOutputPortIndex, pTemp->pOrigBufHdr);
            if (eError == OMX_ErrorInvalidState)
                eError = OMX_ErrorNone;
            OMX_CONF_BAIL_IF_ERROR(eError);
            pTemp = pTemp->pNextBuf;
            if (pBufferListObject)
                OMX_OSAL_Free(pBufferListObject);
            pContext->nOutBuf--;
        }
        pContext->pOutBufferList = NULL;
OMX_CONF_TEST_BAIL:
        return eError;
    }
    OMX_ERRORTYPE ResourceExhaustionTest_DeInitBuffer(ResourceExhaustionTestContext *pCtxt)
    {
        BufferList *pBufferListObject;
        BufferList *pTemp;
        OMX_ERRORTYPE eError = OMX_ErrorNone;

        pTemp = pCtxt->pBufferList[pCtxt->nInst];
        while (pTemp)
        {
            pBufferListObject = (BufferList *)pTemp;
            if (pTemp->pBufHdr->nInputPortIndex != OMX_NOPORT)
                eError = OMX_FreeBuffer(pCtxt->hWComp[pCtxt->nInst],
                                        pTemp->pBufHdr->nInputPortIndex, pTemp->pBufHdr);
            else
                eError = OMX_FreeBuffer(pCtxt->hWComp[pCtxt->nInst],
                                        pTemp->pBufHdr->nOutputPortIndex, pTemp->pBufHdr);

            if (eError == OMX_ErrorInvalidState || eError == OMX_ErrorIncorrectStateOperation)
                eError = OMX_ErrorNone;
            OMX_CONF_BAIL_IF_ERROR(eError);

            pTemp = pTemp->pNextBuf;
            if (pBufferListObject)
                OMX_OSAL_Free(pBufferListObject);
        }
        pCtxt->pBufferList[pCtxt->nInst] = NULL;

OMX_CONF_TEST_BAIL:
        return eError;
    }
    OMX_ERRORTYPE PortCommTest_WriteInBuffers(PortCommTestCtxt* pContext)
    {
        BufferList * pList;
        OMX_BUFFERHEADERTYPE *pBufHeader;
        OMX_ERRORTYPE eError = OMX_ErrorNone;
        OMX_U32 nPayload;

        pList = pContext->pInBufferList;
        while (pList && pList->pBufHdr)
        {
            pContext->nInBufBusy++;
            LIST_CLEAR_ENTRY(pList, pBufHeader);
            nPayload = (pContext->nBufFactor) ? pBufHeader->nAllocLen / pContext->nBufFactor : 1;
            pBufHeader->nFilledLen = OMX_OSAL_ReadFromInputFile(pBufHeader->pBuffer, nPayload,
                                     pBufHeader->nInputPortIndex);
            if (pBufHeader->nFilledLen != nPayload)
            {
                pBufHeader->nFlags |= OMX_BUFFERFLAG_EOS;
            }
            OMX_CONF_BAIL_IF_ERROR(OMX_EmptyThisBuffer(pContext->hWComp, pBufHeader));
        }

OMX_CONF_TEST_BAIL:
        return eError;
    }
    OMX_ERRORTYPE ResourceExhaustionTest_UNLOAD(ResourceExhaustionTestContext *pCtxt)
    {
        OMX_ERRORTYPE eError = OMX_ErrorNone;
        if (pCtxt->hComp[pCtxt->nInst])
        {
            eError = OMX_FreeHandle(pCtxt->hComp[pCtxt->nInst]);
            OMX_CONF_BAIL_IF_ERROR(eError);
        }
        if (pCtxt->hWComp[pCtxt->nInst])
        {
            eError = OMX_CONF_ComponentTracerDestroy(pCtxt->hWComp[pCtxt->nInst]);
            OMX_CONF_BAIL_IF_ERROR(eError);
        }
        pCtxt->hComp[pCtxt->nInst] = NULL;
        pCtxt->hWComp[pCtxt->nInst] = NULL;

OMX_CONF_TEST_BAIL:
        return eError;
    }
    OMX_ERRORTYPE ResourceExhaustionTest_LOAD(ResourceExhaustionTestContext *pCtxt,
            OMX_STRING cComponentName,
            OMX_PTR pWAppData,
            OMX_PTR pWCallbacks)
    {
        OMX_ERRORTYPE eError = OMX_ErrorNone;
        OMX_STATETYPE eState;
        eError = OMX_GetHandle(&pCtxt->hComp[pCtxt->nInst], cComponentName, pWAppData, pWCallbacks);
        OMX_CONF_BAIL_IF_ERROR(eError);
        eError = OMX_CONF_ComponentTracerCreate(pCtxt->hComp[pCtxt->nInst], cComponentName,
                                                &pCtxt->hWComp[pCtxt->nInst]);
        OMX_CONF_BAIL_IF_ERROR(eError);
        eError = OMX_GetState(pCtxt->hWComp[pCtxt->nInst], &eState);
        OMX_CONF_BAIL_IF_ERROR(eError);
        if (eState != OMX_StateLoaded)
            OMX_CONF_SET_ERROR_BAIL("Not in loaded state\n", OMX_ErrorUndefined);
OMX_CONF_TEST_BAIL:
        return eError;
    }
    OMX_ERRORTYPE PortCommTest_TransmitTest(PortCommTestCtxt* pContext,
                                            OMX_BOOL bInit, OMX_BOOL bClose)
    {
        OMX_ERRORTYPE eError = OMX_ErrorNone;
        OMX_BOOL bTimeout;

        if (bInit)
        {
            OMX_CONF_BAIL_IF_ERROR(PortCommTest_OperateOnPorts(pContext, OpenFile));
            OMX_CONF_BAIL_IF_ERROR(PortCommTest_TransitionWait(OMX_StateIdle, pContext));
            OMX_CONF_BAIL_IF_ERROR(PortCommTest_TransitionWait(OMX_StateExecuting, pContext));
        }
        /* process buffers */
        while (pContext->nBufDoneCalls < OMX_CONF_BUFFERS_OF_TRAFFIC)
        {
            if (pContext->nOutBuf)
                OMX_CONF_BAIL_IF_ERROR(PortCommTest_ReadOutBuffers(pContext));
            if (pContext->nInBuf)
                OMX_CONF_BAIL_IF_ERROR(PortCommTest_WriteInBuffers(pContext));

            /* if no empty input or output buffer, wait for bufferdone call */
            if (pContext->nOutBuf == pContext->nOutBufBusy && pContext->nInBuf == pContext->nInBufBusy)
            {
                OMX_OSAL_EventReset(pContext->hBufDoneEvent);
                OMX_OSAL_EventWait(pContext->hBufDoneEvent, OMX_CONF_TIMEOUT_BUFFER_TRAFFIC, &bTimeout);
                if (OMX_TRUE == bTimeout)
                {
                    OMX_CONF_SET_ERROR_BAIL("Component is not processing any buffers\n", OMX_ErrorUndefined);
                }
            }
        }
        OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "Processed %d buffers\n", pContext->nBufDoneCalls);
        if (bClose)
        {
            OMX_CONF_BAIL_IF_ERROR(PortCommTest_OperateOnPorts(pContext, CloseFile));
            OMX_CONF_BAIL_IF_ERROR(PortCommTest_TransitionWait(OMX_StateIdle, pContext));
            while (pContext->nOutBufBusy || pContext->nInBufBusy)
            {
                OMX_OSAL_EventReset(pContext->hBufDoneEvent);
                OMX_OSAL_EventWait(pContext->hBufDoneEvent, OMX_CONF_TIMEOUT_BUFFER_TRAFFIC, &bTimeout);
                if (OMX_TRUE == bTimeout)
                {
                    OMX_CONF_SET_ERROR_BAIL("All buffers not returned\n", OMX_ErrorUndefined);
                }
            }
            OMX_CONF_BAIL_IF_ERROR(PortCommTest_TransitionWait(OMX_StateLoaded, pContext));
        }
        pContext->nBufDoneCalls = 0;
OMX_CONF_TEST_BAIL:
        return eError;
    }
    OMX_ERRORTYPE PortCommTest_RepackTest(PortCommTestCtxt* pContext)
    {
        OMX_ERRORTYPE eError = OMX_ErrorNone;
        OMX_BOOL bTimeout;

        OMX_CONF_BAIL_IF_ERROR(PortCommTest_OperateOnPorts(pContext, OpenFile));
        OMX_CONF_BAIL_IF_ERROR(PortCommTest_TransitionWait(OMX_StateIdle, pContext));

        if (pContext->nInBuf == 0)
        {
            OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "Source component, Repack Test not applicable\n");
            OMX_CONF_BAIL_IF_ERROR(PortCommTest_OperateOnPorts(pContext, CloseFile));
            OMX_CONF_BAIL_IF_ERROR(PortCommTest_TransitionWait(OMX_StateLoaded, pContext));
            goto OMX_CONF_TEST_BAIL;
        }
        OMX_CONF_BAIL_IF_ERROR(PortCommTest_TransitionWait(OMX_StateExecuting, pContext));

        OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "Sending single byte payloads\n");
        pContext->nBufFactor = 0;
        OMX_OSAL_EventReset(pContext->hEmptyBufDoneEvent);

        if (pContext->nOutBuf)
            OMX_CONF_BAIL_IF_ERROR(PortCommTest_ReadOutBuffers(pContext));
        if (pContext->nInBuf)
            OMX_CONF_BAIL_IF_ERROR(PortCommTest_WriteInBuffers(pContext));

        OMX_OSAL_EventWait(pContext->hEmptyBufDoneEvent, OMX_CONF_TIMEOUT_BUFFER_TRAFFIC, &bTimeout);
        if (pContext->nOutBuf && OMX_TRUE == bTimeout)
        {
            OMX_CONF_SET_ERROR_BAIL("Component is not repacking\n", OMX_ErrorUndefined);
        }
        if (pContext->nInBuf && !pContext->pInBufferList->pBufHdr)
        {
            OMX_CONF_SET_ERROR_BAIL("first buffer has to be returned\n", OMX_ErrorUndefined);
        }
        OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "Sending full payloads\n");
        pContext->nBufFactor = 1;
        OMX_CONF_BAIL_IF_ERROR(PortCommTest_TransmitTest(pContext, OMX_FALSE, OMX_TRUE));

OMX_CONF_TEST_BAIL:
        return eError;
    }
    OMX_ERRORTYPE PortCommTest_ReadOutBuffers(PortCommTestCtxt* pContext)
    {
        BufferList * pList;
        OMX_BUFFERHEADERTYPE *pBufHeader;
        OMX_ERRORTYPE eError = OMX_ErrorNone;

        pList = pContext->pOutBufferList;
        while (pList && pList->pBufHdr)
        {
            pContext->nOutBufBusy++;
            LIST_CLEAR_ENTRY(pList, pBufHeader);
            OMX_CONF_BAIL_IF_ERROR(OMX_FillThisBuffer(pContext->hWComp, pBufHeader));
            if (pBufHeader->nFlags == OMX_BUFFERFLAG_EOS)
                pContext->nBufDoneCalls = OMX_CONF_BUFFERS_OF_TRAFFIC;
        }
OMX_CONF_TEST_BAIL:
        return eError;
    }
    OMX_ERRORTYPE ResourceExhaustionTest_TransitionWait(ResourceExhaustionTestContext* pCtxt,
            OMX_STATETYPE eToState)
    {
        OMX_ERRORTYPE eError = OMX_ErrorNone;
        OMX_BOOL bTimeout = OMX_FALSE;
        OMX_STATETYPE eState;

        OMX_OSAL_EventReset(pCtxt->hStateSetEvent);
        pCtxt->eLastError = OMX_ErrorNone;
        OMX_CONF_BAIL_IF_ERROR(OMX_GetState(pCtxt->hWComp[pCtxt->nInst], &eState));

        eError = OMX_SendCommand(pCtxt->hWComp[pCtxt->nInst], OMX_CommandStateSet, eToState, 0);
        OMX_CONF_BAIL_IF_ERROR(eError);

        if (eToState == OMX_StateIdle && (eState == OMX_StateLoaded))
        {
            OMX_CONF_BAIL_IF_ERROR(ResourceExhaustionTest_AllocateAllBuffers(pCtxt));
        }
        else if (eToState == OMX_StateLoaded && eState == OMX_StateIdle)
        {
            OMX_CONF_BAIL_IF_ERROR(ResourceExhaustionTest_DeInitBuffer(pCtxt));
        }

        OMX_OSAL_EventWait(pCtxt->hStateSetEvent, OMX_CONF_TIMEOUT_EXPECTING_SUCCESS, &bTimeout);
        OMX_CONF_BAIL_IF_ERROR(pCtxt->eLastError);

        if (bTimeout == OMX_TRUE)
            OMX_CONF_SET_ERROR_BAIL("Timeout on state transition\n", OMX_ErrorUndefined);

        OMX_CONF_BAIL_IF_ERROR(OMX_GetState(pCtxt->hWComp[pCtxt->nInst], &eState));
        if (eState != eToState)
            OMX_CONF_SET_ERROR_BAIL("Incorrect transition\n", OMX_ErrorUndefined);

OMX_CONF_TEST_BAIL:
        if (OMX_ErrorNone != eError && eToState == OMX_StateIdle && (eState == OMX_StateLoaded))
        {

            /* Add some delay before cleaning up */
            OMX_OSAL_EventWait(pCtxt->hStateSetEvent, OMX_CONF_TIMEOUT_EXPECTING_FAILURE, &bTimeout);

            /* Disable all ports to free buffers*/
            OMX_SendCommand(pCtxt->hWComp[pCtxt->nInst], OMX_CommandPortDisable, OMX_ALL, 0x0);

            /* Cleanup last instance */
            ResourceExhaustionTest_DeInitBuffer(pCtxt);
        }
        return eError;
    }
    OMX_ERRORTYPE PortCommTest_PauseResume(PortCommTestCtxt* pContext)
    {
        OMX_ERRORTYPE eError = OMX_ErrorNone;
        OMX_BOOL bTimeout;

        OMX_CONF_BAIL_IF_ERROR(PortCommTest_TransmitTest(pContext, OMX_TRUE, OMX_FALSE));
        OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "Pause the component\n");
        OMX_CONF_BAIL_IF_ERROR(PortCommTest_TransitionWait(OMX_StatePause, pContext));
        OMX_OSAL_EventReset(pContext->hBufDoneEvent);
        OMX_OSAL_EventWait(pContext->hBufDoneEvent, OMX_CONF_TIMEOUT_EXPECTING_FAILURE, &bTimeout);
        if (OMX_FALSE == bTimeout)
        {
            OMX_CONF_SET_ERROR_BAIL("bufferdone callbacks made after pause cmdcomplete\n", OMX_ErrorUndefined);
        }
        pContext->nBufDoneCalls = 0;

        OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "Attempting to process buffers while paused\n");
        OMX_OSAL_EventReset(pContext->hBufDoneEvent);

        if (pContext->nOutBuf)
            OMX_CONF_BAIL_IF_ERROR(PortCommTest_ReadOutBuffers(pContext));
        if (pContext->nInBuf)
            OMX_CONF_BAIL_IF_ERROR(PortCommTest_WriteInBuffers(pContext));

        OMX_OSAL_EventWait(pContext->hBufDoneEvent, OMX_CONF_TIMEOUT_EXPECTING_FAILURE, &bTimeout);
        if (OMX_FALSE == bTimeout)
        {
            OMX_CONF_SET_ERROR_BAIL("bufferdone callbacks made during paused state\n", OMX_ErrorUndefined);
        }
        OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "Processed %d buffers\n", pContext->nBufDoneCalls);
        OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "Return to executing\n");
        OMX_CONF_BAIL_IF_ERROR(PortCommTest_TransitionWait(OMX_StateExecuting, pContext));
        OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "Process buffers after resuming\n");
        OMX_CONF_BAIL_IF_ERROR(PortCommTest_TransmitTest(pContext, OMX_FALSE, OMX_TRUE));

OMX_CONF_TEST_BAIL:
        return eError;
    }
    OMX_ERRORTYPE OMX_CONF_PortCommunicationTest(OMX_IN OMX_STRING cComponentName)
    {
        OMX_ERRORTYPE eError = OMX_ErrorNone;
        OMX_HANDLETYPE hComp  = 0;
        OMX_CALLBACKTYPE sCallbacks;
        PortCommTestCtxt sContext;
        OMX_HANDLETYPE hWrappedComp = 0;
        OMX_CALLBACKTYPE *pWrappedCallbacks;
        OMX_PTR pWrappedAppData;
        PortCommTestCtxt *pCtxt;
        pCtxt = &sContext;
        memset(pCtxt, 0x0, sizeof(PortCommTestCtxt));

        sCallbacks.EventHandler    =  PortCommTest_EventHandler;
        sCallbacks.EmptyBufferDone =  PortCommTest_EmptyBufferDone;
        sCallbacks.FillBufferDone  =  PortCommTest_FillBufferDone;

        eError = OMX_CONF_CallbackTracerCreate(&sCallbacks, (OMX_PTR)pCtxt, cComponentName,
                                               &pWrappedCallbacks, &pWrappedAppData);

        /* initialize events to track callbacks */
        OMX_OSAL_EventCreate(&pCtxt->hStateSetEvent);
        OMX_OSAL_EventReset(pCtxt->hStateSetEvent);
        OMX_OSAL_EventCreate(&pCtxt->hPortDisableEvent);
        OMX_OSAL_EventReset(pCtxt->hPortDisableEvent);
        OMX_OSAL_EventCreate(&pCtxt->hPortEnableEvent);
        OMX_OSAL_EventReset(pCtxt->hPortEnableEvent);
        OMX_OSAL_EventCreate(&pCtxt->hBufDoneEvent);
        OMX_OSAL_EventReset(pCtxt->hBufDoneEvent);
        OMX_OSAL_EventCreate(&pCtxt->hEmptyBufDoneEvent);
        OMX_OSAL_EventReset(pCtxt->hEmptyBufDoneEvent);

        /* Initialize OpenMax */
        eError = OMX_Init();
        OMX_CONF_BAIL_IF_ERROR(eError);

        OMX_CONF_BAIL_IF_ERROR(OMX_GetHandle(&hComp, cComponentName, pWrappedAppData,
                                             pWrappedCallbacks));
        OMX_CONF_BAIL_IF_ERROR(OMX_CONF_ComponentTracerCreate(hComp, cComponentName,
                               &hWrappedComp));
        pCtxt->hWComp = hWrappedComp;

        /* Verify start in Loaded state */
        OMX_CONF_BAIL_IF_ERROR(OMX_GetState(pCtxt->hWComp, &pCtxt->eState));
        if (pCtxt->eState != OMX_StateLoaded)
            OMX_CONF_SET_ERROR_BAIL("Component not in loaded state at init\n", OMX_ErrorUndefined);

        /* detect all audio ports on the component */
        OMX_CONF_INIT_STRUCT(pCtxt->sPortParam[0], OMX_PORT_PARAM_TYPE);
        eError = OMX_GetParameter(pCtxt->hWComp, OMX_IndexParamAudioInit,
                                  (OMX_PTR) & pCtxt->sPortParam[0]);
        if (OMX_ErrorUnsupportedIndex == eError)
            eError = OMX_ErrorNone;
        OMX_CONF_BAIL_IF_ERROR(eError);
        OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "detected %i audio ports starting at %i \n",
                       pCtxt->sPortParam[0].nPorts, pCtxt->sPortParam[0].nStartPortNumber);

        /* detect all video ports on the component */
        OMX_CONF_INIT_STRUCT(pCtxt->sPortParam[1], OMX_PORT_PARAM_TYPE);
        eError = OMX_GetParameter(pCtxt->hWComp, OMX_IndexParamVideoInit,
                                  (OMX_PTR) & pCtxt->sPortParam[1]);
        if (OMX_ErrorUnsupportedIndex == eError)
            eError = OMX_ErrorNone;
        OMX_CONF_BAIL_IF_ERROR(eError);
        OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "detected %i video ports starting at %i \n",
                       pCtxt->sPortParam[1].nPorts, pCtxt->sPortParam[1].nStartPortNumber);

        /* detect all image ports on the component */
        OMX_CONF_INIT_STRUCT(pCtxt->sPortParam[2], OMX_PORT_PARAM_TYPE);
        eError = OMX_GetParameter(pCtxt->hWComp, OMX_IndexParamImageInit,
                                  (OMX_PTR) & pCtxt->sPortParam[2]);
        if (OMX_ErrorUnsupportedIndex == eError)
            eError = OMX_ErrorNone;
        OMX_CONF_BAIL_IF_ERROR(eError);
        OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "detected %i image ports starting at %i \n",
                       pCtxt->sPortParam[2].nPorts, pCtxt->sPortParam[2].nStartPortNumber);

        /* detect all other ports on the component */
        OMX_CONF_INIT_STRUCT(pCtxt->sPortParam[3], OMX_PORT_PARAM_TYPE);
        eError = OMX_GetParameter(pCtxt->hWComp, OMX_IndexParamOtherInit,
                                  (OMX_PTR) & pCtxt->sPortParam[3]);
        if (OMX_ErrorUnsupportedIndex == eError)
            eError = OMX_ErrorNone;
        OMX_CONF_BAIL_IF_ERROR(eError);
        OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "detected %i other ports starting at %i \n",
                       pCtxt->sPortParam[3].nPorts, pCtxt->sPortParam[3].nStartPortNumber);

        pCtxt->nPorts = pCtxt->sPortParam[0].nPorts + pCtxt->sPortParam[1].nPorts +
                        pCtxt->sPortParam[2].nPorts + pCtxt->sPortParam[3].nPorts;

        /* 3.4.1. Non-Tunneling Setup Test */
        /* TODO: reference core does not take null as port */
        OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "Non-Tunneling Setup Test\n");
        OMX_CONF_BAIL_IF_ERROR(PortCommTest_OperateOnPorts(pCtxt, NonTunnelTest));

        /* 3.4.2. Transmit Buffers Between Input and Output Ports and Application */
        OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "Transmit Test - Client allocates - Full payload\n");
        pCtxt->nBufFactor = 1;
        pCtxt->bClientAllocBuf = OMX_TRUE;
        OMX_CONF_BAIL_IF_ERROR(PortCommTest_TransmitTest(pCtxt, OMX_TRUE, OMX_TRUE));

        OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "Transmit Test - Client allocates - Half payload\n");
        pCtxt->nBufFactor = 2;
        OMX_CONF_BAIL_IF_ERROR(PortCommTest_TransmitTest(pCtxt, OMX_TRUE, OMX_TRUE));

        OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "Transmit Test - Client allocates - Quarter payload\n");
        pCtxt->nBufFactor = 4;
        OMX_CONF_BAIL_IF_ERROR(PortCommTest_TransmitTest(pCtxt, OMX_TRUE, OMX_TRUE));

        OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "Transmit Test - Component allocates - Full payload\n");
        pCtxt->bClientAllocBuf = OMX_FALSE;
        pCtxt->nBufFactor = 1;
        OMX_CONF_BAIL_IF_ERROR(PortCommTest_TransmitTest(pCtxt, OMX_TRUE, OMX_TRUE));

        /* 3.4.3. Port Repack Test */
        OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "Repack Test\n");
        pCtxt->bClientAllocBuf = OMX_TRUE;
        OMX_CONF_BAIL_IF_ERROR(PortCommTest_RepackTest(pCtxt));

        /* 3.4.4. Port Stop and Restart Test */
        OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "Port Stop and Restart Test\n");
        pCtxt->nBufFactor = 1;
        OMX_CONF_BAIL_IF_ERROR(PortCommTest_StopRestartTest(pCtxt));

        /* 3.4.5. Component Pause and Resume Test*/
        OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "Component Pause and Resume Test\n");
        OMX_CONF_BAIL_IF_ERROR(PortCommTest_PauseResume(pCtxt));

OMX_CONF_TEST_BAIL:
        /* cleanup: return function errors rather than closing errors if appropriate */
        OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "Cleanup\n");

        if (OMX_ErrorNone == eError)
        {
            if (hWrappedComp)
            {
                eError = OMX_CONF_ComponentTracerDestroy(hWrappedComp);
            }
            if (hComp)
            {
                eError = OMX_FreeHandle(hComp);
            }
            eError = OMX_CONF_CallbackTracerDestroy(pWrappedCallbacks, pWrappedAppData);

            eError = OMX_Deinit();
        }
        else
        {
            PortCommTest_TransitionWait(OMX_StateInvalid, pCtxt);
            PortCommTest_DeInitBuffer(pCtxt);

            if (hWrappedComp)
            {
                OMX_CONF_ComponentTracerDestroy(hWrappedComp);
            }
            if (hComp)
            {
                OMX_FreeHandle(hComp);
            }
            OMX_CONF_CallbackTracerDestroy(pWrappedCallbacks, pWrappedAppData);
            OMX_Deinit();
        }

        OMX_OSAL_EventDestroy(pCtxt->hStateSetEvent);
        OMX_OSAL_EventDestroy(pCtxt->hPortDisableEvent);
        OMX_OSAL_EventDestroy(pCtxt->hPortEnableEvent);
        OMX_OSAL_EventDestroy(pCtxt->hBufDoneEvent);
        OMX_OSAL_EventDestroy(pCtxt->hEmptyBufDoneEvent);

        return eError;
    }
    OMX_ERRORTYPE PortCommTest_OperateOnPorts(PortCommTestCtxt* pContext, PortOpType eOp)
    {
        OMX_PARAM_PORTDEFINITIONTYPE sPortDef;
        OMX_ERRORTYPE eError = OMX_ErrorNone;
        OMX_U32 i, j;
        OMX_BUFFERHEADERTYPE sBufHdr;

        for (j = 0; j < NUM_DOMAINS; j++)
        {
            for (i = pContext->sPortParam[j].nStartPortNumber;
                    i < pContext->sPortParam[j].nStartPortNumber + pContext->sPortParam[j].nPorts; i++)
            {

                OMX_CONF_INIT_STRUCT(sPortDef, OMX_PARAM_PORTDEFINITIONTYPE);
                sPortDef.nPortIndex = i;
                OMX_CONF_BAIL_IF_ERROR(OMX_GetParameter(pContext->hWComp, OMX_IndexParamPortDefinition ,
                                                        (OMX_PTR)&sPortDef));
                switch (eOp)
                {
                    case AllocBuf:
                        if (0x0 == sPortDef.nBufferCountMin || sPortDef.nBufferCountMin > sPortDef.nBufferCountActual)
                        {
                            OMX_CONF_SET_ERROR_BAIL("PortDefinition nBufferCount incorrect\n", OMX_ErrorUndefined);
                        }
                        OMX_CONF_BAIL_IF_ERROR(PortCommTest_AllocateBuffers(pContext, &sPortDef));
                        break;

                    case NonTunnelTest:
                        if (sPortDef.eDir == OMX_DirInput)
                        {
                            eError = OMX_SetupTunnel(NULL, 0, pContext->hWComp, sPortDef.nPortIndex);
                            OMX_CONF_ErrorToString(eError, szDesc);
                            OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "Setup tunnel reported error code %s\n", szDesc);
                        }
                        else
                        {
                            eError = OMX_SetupTunnel(pContext->hWComp, sPortDef.nPortIndex, NULL, 0);
                            OMX_CONF_ErrorToString(eError, szDesc);
                            OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "Setup tunnel reported error code %s\n", szDesc);
                        }
                        eError = OMX_ErrorNone;
                        break;

                    case OpenFile:
                        /* set buffercount actual */
                        if (0x0 == sPortDef.nBufferCountMin || sPortDef.nBufferCountMin > sPortDef.nBufferCountActual)
                        {
                            OMX_CONF_SET_ERROR_BAIL("PortDefinition nBufferCount incorrect\n", OMX_ErrorUndefined);
                        }
                        sPortDef.nBufferCountActual = sPortDef.nBufferCountMin + 1;
                        OMX_CONF_BAIL_IF_ERROR(OMX_SetParameter(pContext->hWComp, OMX_IndexParamPortDefinition,
                                                                (OMX_PTR)&sPortDef));
                        /* open files for input ports */
                        if (sPortDef.eDir == OMX_DirInput)
                        {
                            OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "Opened input file for port %d\n", i);
                            OMX_CONF_BAIL_IF_ERROR(OMX_OSAL_OpenInputFile(i));
                        }
                        break;

                    case CloseFile:
                        if (sPortDef.eDir == OMX_DirInput)
                        {
                            OMX_CONF_BAIL_IF_ERROR(OMX_OSAL_CloseInputFile(i));
                        }
                        break;

                    case EmptyFill:
                        OMX_CONF_INIT_STRUCT(sBufHdr, OMX_BUFFERHEADERTYPE);
                        if (sPortDef.eDir == OMX_DirInput)
                        {
                            sBufHdr.nInputPortIndex = i;
                            eError = OMX_EmptyThisBuffer(pContext->hWComp, &sBufHdr);
                        }
                        else
                        {
                            sBufHdr.nOutputPortIndex = i;
                            eError = OMX_FillThisBuffer(pContext->hWComp, &sBufHdr);
                        }
                        if (eError != OMX_ErrorIncorrectStateOperation)
                            OMX_CONF_SET_ERROR_BAIL("Buffer handling while stopped\n", OMX_ErrorUndefined);
                        eError = OMX_ErrorNone;
                        break;

                    default:
                        eError = OMX_ErrorBadParameter;
                }
            }
        }
OMX_CONF_TEST_BAIL:
        return eError;
    }
    OMX_ERRORTYPE PortCommTest_StopRestartTest(PortCommTestCtxt* pContext)
    {
        OMX_ERRORTYPE eError = OMX_ErrorNone;
        OMX_BOOL bTimeout;

        /* process max frames */
        OMX_CONF_BAIL_IF_ERROR(PortCommTest_TransmitTest(pContext, OMX_TRUE, OMX_FALSE));

        OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "Stopping all ports\n");
        OMX_OSAL_EventReset(pContext->hPortDisableEvent);
        OMX_CONF_BAIL_IF_ERROR(OMX_SendCommand(pContext->hWComp, OMX_CommandPortDisable, OMX_ALL, 0x0));

        OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "Wait for all buffers to be returned\n");
        while (pContext->nOutBufBusy || pContext->nInBufBusy)
        {
            OMX_OSAL_EventReset(pContext->hBufDoneEvent);
            OMX_OSAL_EventWait(pContext->hBufDoneEvent, OMX_CONF_TIMEOUT_BUFFER_TRAFFIC, &bTimeout);
            if (OMX_TRUE == bTimeout)
            {
                OMX_CONF_SET_ERROR_BAIL("All buffers not returned\n", OMX_ErrorUndefined);
            }
        }
        /* check if command completed before de-allocation*/
        OMX_OSAL_EventWait(pContext->hPortDisableEvent, OMX_CONF_TIMEOUT_EXPECTING_FAILURE, &bTimeout);
        if (OMX_FALSE == bTimeout)
        {
            OMX_CONF_SET_ERROR_BAIL("Ports stopped unexpectedly\n", OMX_ErrorUndefined);
        }
        OMX_OSAL_EventReset(pContext->hPortDisableEvent);

        /* free all buffers */
        OMX_CONF_BAIL_IF_ERROR(PortCommTest_DeInitBuffer(pContext));

        /* wait for cmd complete */
        OMX_OSAL_EventWait(pContext->hPortDisableEvent, OMX_CONF_TIMEOUT_BUFFER_TRAFFIC, &bTimeout);
        if (OMX_TRUE == bTimeout)
        {
            OMX_CONF_SET_ERROR_BAIL("All ports not stopped\n", OMX_ErrorUndefined);
        }

        /* ensure no more buffer done callbacks are made */
        OMX_OSAL_EventReset(pContext->hBufDoneEvent);
        OMX_OSAL_EventWait(pContext->hBufDoneEvent, OMX_CONF_TIMEOUT_EXPECTING_FAILURE, &bTimeout);
        if (OMX_FALSE == bTimeout)
        {
            OMX_CONF_SET_ERROR_BAIL("callbacks made after cmdcomplete\n", OMX_ErrorUndefined);
        }
        pContext->nBufDoneCalls = 0;

        OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "Attempting to process buffers while stopped\n");
        OMX_OSAL_EventReset(pContext->hBufDoneEvent);
        OMX_CONF_BAIL_IF_ERROR(PortCommTest_OperateOnPorts(pContext, EmptyFill));
        OMX_OSAL_EventWait(pContext->hBufDoneEvent, OMX_CONF_TIMEOUT_EXPECTING_FAILURE, &bTimeout);
        if (OMX_FALSE == bTimeout)
        {
            OMX_CONF_SET_ERROR_BAIL("callbacks made while ports disabled\n", OMX_ErrorUndefined);
        }

        OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "Processed %d buffers\n", pContext->nBufDoneCalls);
        OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "Restarting all ports\n");
        OMX_OSAL_EventReset(pContext->hPortEnableEvent);

        /* send restart command */
        OMX_CONF_BAIL_IF_ERROR(OMX_SendCommand(pContext->hWComp, OMX_CommandPortEnable, OMX_ALL, 0x0));

        /* check if command completed before allocation*/
        OMX_OSAL_EventWait(pContext->hPortEnableEvent, OMX_CONF_TIMEOUT_EXPECTING_FAILURE, &bTimeout);
        if (OMX_FALSE == bTimeout)
        {
            OMX_CONF_SET_ERROR_BAIL("Ports restarted unexpectedly\n", OMX_ErrorUndefined);
        }
        OMX_OSAL_EventReset(pContext->hPortEnableEvent);

        /* allocate buffers */
        OMX_CONF_BAIL_IF_ERROR(PortCommTest_OperateOnPorts(pContext, AllocBuf));

        /* wait for command complete */
        OMX_OSAL_EventWait(pContext->hPortEnableEvent, OMX_CONF_TIMEOUT_BUFFER_TRAFFIC, &bTimeout);
        if (OMX_TRUE == bTimeout)
        {
            OMX_CONF_SET_ERROR_BAIL("All ports not restarted\n", OMX_ErrorUndefined);
        }
        OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "Process buffers after restarting\n");
        OMX_CONF_BAIL_IF_ERROR(PortCommTest_TransmitTest(pContext, OMX_FALSE, OMX_TRUE));

OMX_CONF_TEST_BAIL:
        return eError;
    }
    OMX_ERRORTYPE OMX_CONF_ResourceExhaustionTest(OMX_IN OMX_STRING cComponentName)
    {
        OMX_ERRORTYPE eError = OMX_ErrorNone;
        OMX_CALLBACKTYPE oCallbacks;
        ResourceExhaustionTestContext oAppData;
        OMX_CALLBACKTYPE *pWrapCallbacks;
        OMX_PTR pWrapAppData;
        ResourceExhaustionTestContext *pCtxt;
        OMX_U32 i;

        pCtxt = &oAppData;
        memset(pCtxt, 0x0, sizeof(ResourceExhaustionTestContext));

        oCallbacks.EventHandler    =  ResourceExhaustionTest_EventHandler;
        oCallbacks.EmptyBufferDone =  StubbedEmptyBufferDone;
        oCallbacks.FillBufferDone  =  StubbedFillBufferDone;

        eError = OMX_CONF_CallbackTracerCreate(&oCallbacks, (OMX_PTR)pCtxt, cComponentName,
                                               &pWrapCallbacks, &pWrapAppData);

        OMX_OSAL_EventCreate(&pCtxt->hStateSetEvent);
        OMX_OSAL_EventReset(pCtxt->hStateSetEvent);

        /* Initialize OpenMax */
        eError = OMX_Init();
        OMX_CONF_BAIL_IF_ERROR(eError);

        eError = ResourceExhaustionTest_LOAD(pCtxt, cComponentName, pWrapAppData, pWrapCallbacks);
        OMX_CONF_BAIL_IF_ERROR(eError);

        /* detect all audio ports on the component */
        OMX_CONF_INIT_STRUCT(pCtxt->sPortParam[0], OMX_PORT_PARAM_TYPE);
        eError = OMX_GetParameter(pCtxt->hWComp[pCtxt->nInst], OMX_IndexParamAudioInit,
                                  (OMX_PTR) & pCtxt->sPortParam[0]);
        if (OMX_ErrorUnsupportedIndex == eError)
            eError = OMX_ErrorNone;
        OMX_CONF_BAIL_IF_ERROR(eError);
        OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "detected %i audio ports starting at %i \n",
                       pCtxt->sPortParam[0].nPorts, pCtxt->sPortParam[0].nStartPortNumber);

        /* detect all video ports on the component */
        OMX_CONF_INIT_STRUCT(pCtxt->sPortParam[1], OMX_PORT_PARAM_TYPE);
        eError = OMX_GetParameter(pCtxt->hWComp[pCtxt->nInst], OMX_IndexParamVideoInit,
                                  (OMX_PTR) & pCtxt->sPortParam[1]);
        if (OMX_ErrorUnsupportedIndex == eError)
            eError = OMX_ErrorNone;
        OMX_CONF_BAIL_IF_ERROR(eError);
        OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "detected %i video ports starting at %i \n",
                       pCtxt->sPortParam[1].nPorts, pCtxt->sPortParam[1].nStartPortNumber);

        /* detect all image ports on the component */
        OMX_CONF_INIT_STRUCT(pCtxt->sPortParam[2], OMX_PORT_PARAM_TYPE);
        eError = OMX_GetParameter(pCtxt->hWComp[pCtxt->nInst], OMX_IndexParamImageInit,
                                  (OMX_PTR) & pCtxt->sPortParam[2]);
        if (OMX_ErrorUnsupportedIndex == eError)
            eError = OMX_ErrorNone;
        OMX_CONF_BAIL_IF_ERROR(eError);
        OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "detected %i image ports starting at %i \n",
                       pCtxt->sPortParam[2].nPorts, pCtxt->sPortParam[2].nStartPortNumber);

        /* detect all other ports on the component */
        OMX_CONF_INIT_STRUCT(pCtxt->sPortParam[3], OMX_PORT_PARAM_TYPE);
        eError = OMX_GetParameter(pCtxt->hWComp[pCtxt->nInst], OMX_IndexParamOtherInit,
                                  (OMX_PTR) & pCtxt->sPortParam[3]);
        if (OMX_ErrorUnsupportedIndex == eError)
            eError = OMX_ErrorNone;
        OMX_CONF_BAIL_IF_ERROR(eError);
        OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "detected %i other ports starting at %i \n",
                       pCtxt->sPortParam[3].nPorts, pCtxt->sPortParam[3].nStartPortNumber);

        OMX_CONF_BAIL_IF_ERROR(ResourceExhaustionTest_TransitionWait(pCtxt, OMX_StateIdle));

        OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "Repeatedly load and idle component\n");
        /* JVW-NVIDIA: set bound to MAX_INSTANCE-1 http://cvs.khronos.org/bugzilla/show_bug.cgi?id=219 */
        while (pCtxt->nInst++ < MAX_INSTANCE - 1)
        {
            eError = ResourceExhaustionTest_LOAD(pCtxt, cComponentName, pWrapAppData, pWrapCallbacks);
            if (eError != OMX_ErrorNone)
                break;

            eError = ResourceExhaustionTest_TransitionWait(pCtxt, OMX_StateIdle);
            if (eError != OMX_ErrorNone)
                break;
        }
        if (eError == OMX_ErrorInsufficientResources)
            eError = OMX_ErrorNone;

        if (pCtxt->nInst == MAX_INSTANCE)
        {
            OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "Resources not exhausted - increase max instance\n");
            eError = OMX_ErrorUndefined;
        }
        OMX_CONF_BAIL_IF_ERROR(eError);

        OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "Resources exhausted by instance %d\n", pCtxt->nInst);
        OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "Alternatively de-allocate/allocate last 2 instances\n");

        for (i = 0x0; i < MAX_ITERATIONS; i++)
        {
            /* Unload last successfully idled instance */
            pCtxt->nInst--;
            OMX_CONF_BAIL_IF_ERROR(ResourceExhaustionTest_TransitionWait(pCtxt, OMX_StateLoaded));
            OMX_CONF_BAIL_IF_ERROR(ResourceExhaustionTest_UNLOAD(pCtxt));

            /* Load last failing instance */
            pCtxt->nInst++;
            OMX_CONF_BAIL_IF_ERROR(ResourceExhaustionTest_UNLOAD(pCtxt));
            OMX_CONF_BAIL_IF_ERROR(ResourceExhaustionTest_LOAD(pCtxt, cComponentName, pWrapAppData, pWrapCallbacks));
            OMX_CONF_BAIL_IF_ERROR(ResourceExhaustionTest_TransitionWait(pCtxt, OMX_StateIdle));

            /* Unload last successfully idled instance */
            OMX_CONF_BAIL_IF_ERROR(ResourceExhaustionTest_TransitionWait(pCtxt, OMX_StateLoaded));
            OMX_CONF_BAIL_IF_ERROR(ResourceExhaustionTest_UNLOAD(pCtxt));

            /* Load last failing instance */
            pCtxt->nInst--;
            OMX_CONF_BAIL_IF_ERROR(ResourceExhaustionTest_LOAD(pCtxt, cComponentName, pWrapAppData, pWrapCallbacks));
            OMX_CONF_BAIL_IF_ERROR(ResourceExhaustionTest_TransitionWait(pCtxt, OMX_StateIdle));

            pCtxt->nInst++;
        }

        OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "De-allocate all instances\n");
        while (pCtxt->nInst--)
        {
            OMX_CONF_BAIL_IF_ERROR(ResourceExhaustionTest_TransitionWait(pCtxt, OMX_StateLoaded));
            OMX_CONF_BAIL_IF_ERROR(ResourceExhaustionTest_UNLOAD(pCtxt));
        }

OMX_CONF_TEST_BAIL:
        /* cleanup: return function errors rather than closing errors if appropriate */
        OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "Cleanup\n");
        if (OMX_ErrorNone == eError)
        {
            eError = OMX_CONF_CallbackTracerDestroy(pWrapCallbacks, pWrapAppData);
            eError = OMX_Deinit();
        }
        else
        {
            do
            {
                if (pCtxt->hWComp[pCtxt->nInst])
                {
                    ResourceExhaustionTest_TransitionWait(pCtxt, OMX_StateInvalid);
                    ResourceExhaustionTest_DeInitBuffer(pCtxt);
                    ResourceExhaustionTest_UNLOAD(pCtxt);
                }
            }
            while (pCtxt->nInst--);

            OMX_CONF_CallbackTracerDestroy(pWrapCallbacks, pWrapAppData);
            OMX_Deinit();
        }

        OMX_OSAL_EventDestroy(pCtxt->hStateSetEvent);

        return eError;
    }
    OMX_ERRORTYPE PortCommTest_AllocateBuffers(PortCommTestCtxt *pContext,
            OMX_PARAM_PORTDEFINITIONTYPE *pPortDef)
    {
        OMX_BUFFERHEADERTYPE *pBufferHdr = NULL;
        OMX_U8 *pBuffer = NULL;
        BufferList *pBufferList;
        BufferList *pTemp;
        OMX_ERRORTYPE eError = OMX_ErrorNone;
        OMX_U32 i;

        OMX_OSAL_Trace(OMX_OSAL_TRACE_INFO, "Allocate buffers for port %d\n", pPortDef->nPortIndex);
        for (i = 0x0; i < pPortDef->nBufferCountActual; i++)
        {
            pBufferList = (BufferList *)OMX_OSAL_Malloc(sizeof(BufferList));
            if (!pBufferList)
                OMX_CONF_SET_ERROR_BAIL("malloc failed\n", OMX_ErrorInsufficientResources);

            if (pContext->bClientAllocBuf)
            {
                pBuffer = (OMX_U8*)OMX_OSAL_Malloc(pPortDef->nBufferSize);
                if (!pBuffer)
                    OMX_CONF_SET_ERROR_BAIL("malloc failed\n", OMX_ErrorInsufficientResources);
                OMX_CONF_BAIL_IF_ERROR(OMX_UseBuffer(pContext->hWComp, &pBufferHdr, pPortDef->nPortIndex,
                                                     0, pPortDef->nBufferSize, pBuffer));
            }
            else
            {
                OMX_CONF_BAIL_IF_ERROR(OMX_AllocateBuffer(pContext->hWComp, &pBufferHdr, pPortDef->nPortIndex,
                                       0, pPortDef->nBufferSize));
            }
            pBufferList->pNextBuf = NULL;
            pBufferList->pBufHdr = pBufferHdr;
            pBufferList->pOrigBufHdr = pBufferHdr;

            if (pPortDef->eDir == OMX_DirInput)
            {
                /* check if buffer header is as expected */
                if (pBufferHdr->nAllocLen != pPortDef->nBufferSize || !pBufferHdr->pBuffer ||
                        pBufferHdr->nInputPortIndex != pPortDef->nPortIndex)
                    OMX_CONF_SET_ERROR_BAIL("Buffer header incorrect\n", OMX_ErrorUndefined);

                pBufferHdr->nOutputPortIndex = OMX_NOPORT;

                /* put it in internal list */
                if (pContext->pInBufferList == NULL)
                    pContext->pInBufferList = pBufferList;
                else
                {
                    pTemp = pContext->pInBufferList;
                    while (pTemp->pNextBuf)
                        pTemp = pTemp->pNextBuf;
                    pTemp->pNextBuf = pBufferList;
                }
                pContext->nInBuf++;
            }
            else
            {
                /* check if buffer header is as expected */
                if (pBufferHdr->nAllocLen != pPortDef->nBufferSize || !pBufferHdr->pBuffer ||
                        pBufferHdr->nOutputPortIndex != pPortDef->nPortIndex)
                    OMX_CONF_SET_ERROR_BAIL("Buffer header incorrect\n", OMX_ErrorUndefined);

                pBufferHdr->nInputPortIndex = OMX_NOPORT;

                /* put it in internal list */
                if (pContext->pOutBufferList  == NULL)
                    pContext->pOutBufferList = pBufferList;
                else
                {
                    pTemp = pContext->pOutBufferList;
                    while (pTemp->pNextBuf)
                        pTemp = pTemp->pNextBuf;
                    pTemp->pNextBuf = pBufferList;
                }
                pContext->nOutBuf++;
            }
        }

OMX_CONF_TEST_BAIL:
        return eError;
    }