/**
@brief Configure the processing component by sending all relevant info 
*/
WRAPPER_OPENMAX_API OMX_ERRORTYPE OpenMax_Proxy::deInstantiate()
//*************************************************************************************************************
{
	IN0("\n");
	OMX_ERRORTYPE OmxError=OMX_ErrorNone;
	t_nmf_error nmf_err = NMF_OK;
	OMX_HANDLETYPE handle = mENSComponent.getOMXHandle();

	nmf_err = m_pNmfProcWrp->unbindFromUser("sendcommand");
	if (nmf_err != NMF_OK)
	{
		MSG1("Error: Imageproc unbindfromUser sendcommand 0x%x\n", nmf_err);
		OmxError=OMX_ErrorHardware;
	}
	nmf_err = m_pNmfProcWrp->unbindFromUser("fsminit");
	if (nmf_err != NMF_OK)
	{
		MSG1("Error: Imageproc unbindfromUser fsminit 0x%x\n", nmf_err);
		OmxError=OMX_ErrorHardware;
	}
#if (SYNCHRONOUS ==0)
	nmf_err |= m_pNmfProcWrp->unbindFromUser("Config");
	if (nmf_err != NMF_OK)
	{
		MSG1("Error: Imageproc unbindfromUser Config 0x%x\n", nmf_err);
		OmxError=OMX_ErrorHardware;
	}

	nmf_err = m_pNmfProcWrp->unbindFromUser("Param");
	if (nmf_err != NMF_OK)
	{
		MSG1("Error: Imageproc unbindfromUser Param 0x%x\n", nmf_err);
		OmxError=OMX_ErrorHardware;
	}
#endif
	nmf_err |= EnsWrapper_unbindToUser(handle, m_pNmfProcWrp, "proxy");
	if (nmf_err != NMF_OK)
	{
		MSG1("Error: m_pNmfProcWrp unbindToUser proxy 0x%x\n", nmf_err);
		OmxError=OMX_ErrorHardware;
	}

	nmf_err |= EnsWrapper_unbindToUser(handle, m_pNmfProcWrp, /* "get_config" */ "ToOMXComponent");
	if (nmf_err != NMF_OK)
	{
		MSG1("Error: m_pNmfProcWrp unbindToUser ToOMXComponent 0x%x\n", nmf_err);
		OmxError=OMX_ErrorHardware;
	}

	char name[20]; //reserve some char for formatting callback name
	PortVideo* pPort;
	for (unsigned int Index=0; Index< mENSComponent.getPortCount(); ++Index)
	{
		pPort = (PortVideo*)mENSComponent.getPort(Index);
		switch(pPort->getDirection())
		{
		case OMX_DirInput: //bind emptythisbuffer, emptybufferdone for each input port
			sprintf(name, "emptythisbuffer[%u]", Index);
			nmf_err |= m_pNmfProcWrp->unbindFromUser(name);
			sprintf(name, "emptybufferdone[%u]", Index);
			nmf_err |= EnsWrapper_unbindToUser(handle, m_pNmfProcWrp, name);
			break;
		case OMX_DirOutput: //bind emptythisbuffer, emptybufferdone for each input port
			sprintf(name, "fillthisbuffer[%u]", Index);
			nmf_err |= m_pNmfProcWrp->unbindFromUser(name);
			sprintf(name, "fillbufferdone[%u]", Index);
			nmf_err |= EnsWrapper_unbindToUser(handle, m_pNmfProcWrp, name);
			break;
		default:
			WARNING(0);
			break;
		}
	}
	if (nmf_err != NMF_OK)
	{
		OmxError=OMX_ErrorHardware;
	}

	m_pNmfProcWrp->destroy(); 
	openmax_processor_wrpDestroy(m_pNmfProcWrp);
	m_pProcessor=NULL; // for releasing the 'processor'
	OUTR(" ", (OmxError));
	return OmxError;
}
/** 
brief Instantiate the nmf component and bind interfaces
Called during Loaded to Idle transition
*/
WRAPPER_OPENMAX_API OMX_ERRORTYPE OpenMax_Proxy::instantiate()
//*************************************************************************************************************
{ //binding all interfaces
	OMX_ERRORTYPE omx_error=OMX_ErrorNone;
	IN0("\n");
	// Init Components
	// Bind of arm nmf components
	MSG1("OpenMax_Proxy(%s) Instantiating arm nmf component\n", GetComponentName());
	openmax_processor *pProcessor=NULL;
	if (m_pProcessor != NULL)
		pProcessor = m_pProcessor;
	else if (m_fnCreateNmfComponent !=NULL)
	{ 
		m_pProcessor= m_fnCreateNmfComponent();
		pProcessor  = m_pProcessor;
	}

	m_pNmfProcWrp = openmax_processor_wrpCreate(pProcessor);

	m_pNmfProcWrp->priority = m_nmfPriority;

	if (m_pNmfProcWrp->construct() != NMF_OK)
		NMF_PANIC("PANIC - Construct Error\n") ;

	t_nmf_error error ;
	// bindFromUser --> Asynchronous
	// getInterface --> Synchronous

	error = m_pNmfProcWrp->bindFromUser("sendcommand", 2, &mIsendCommand) ;
	if (error != NMF_OK)
		NMF_PANIC("PANIC - bindFromUser sendCommand\n") ;
#if (SYNCHRONOUS ==0)
	error = m_pNmfProcWrp->bindFromUser("Param", 2*mENSComponent.getPortCount()+1, &m_IParam) ; // One setparam per port + 1
	if (error != NMF_OK)
		NMF_PANIC("PANIC - bindFromUser Param\n") ;

	error = m_pNmfProcWrp->bindFromUser("Config", 2*mENSComponent.getPortCount()+1, &m_IConfig) ; // One setparam per port + 1
	if (error != NMF_OK)
		NMF_PANIC("PANIC - bindFromUser Config\n") ;
#else //Bind synchronous interfaces
	//interfaces are binded in a synchronous way
	error = m_pNmfProcWrp->getInterface("Param", &m_IParam);
	if (error != NMF_OK)
		NMF_PANIC("PANIC - getInterface Param\n") ;

	error = m_pNmfProcWrp->getInterface("Config", &m_IConfig);
	if (error != NMF_OK)
		NMF_PANIC("PANIC - getInterface Config\n") ;
#endif
	error = m_pNmfProcWrp->bindFromUser("fsminit", 1 , &mIfsmInit);
	if (error != NMF_OK)
		NMF_PANIC("Error: unable to bind fsminit!...\n");

	error = EnsWrapper_bindToUser(mENSComponent.getOMXHandle(), m_pNmfProcWrp, "proxy", getEventHandlerCB(), 8);
	if (error != NMF_OK)
		NMF_PANIC("Error: unable to bind proxy!...\n");

	error = EnsWrapper_bindToUser(mENSComponent.getOMXHandle(), m_pNmfProcWrp, "ToOMXComponent", mConfigCB, 1);
	if (error != NMF_OK)
		NMF_PANIC("Error: unable to bind ToOMXComponent!...\n");

	char name[20]; //reserve some char for formatting callback name
	size_t iNbInput =0;
	size_t iNbOutput=0;
	PortVideo* pPort;
	for (unsigned int Index=0; Index< mENSComponent.getPortCount(); ++Index)
	{
		pPort = (PortVideo*)mENSComponent.getPort(Index);
		MSG3("\n %s Port %d : direction =%d\n", GetComponentName(), Index, pPort->getDirection());
		//reset port info at this point 
		//NO it's too late!
#if 0
		if (pPort->getBufferSupplier() !=OMX_BufferSupplyUnspecified)
		{
			printf("wwwwwwwwwwwwwwwwwwwwwwwwwwww    %s.port[%d] reset state(%d) to OMX_BufferSupplyUnspecified\n", GetComponentName(), Index, pPort->getBufferSupplier());
			pPort->setBufferSupplier(OMX_BufferSupplyUnspecified);
		}
#endif
		switch(pPort->getDirection())
		{
		case OMX_DirInput: //bind emptythisbuffer, emptybufferdone for each input port
			if (iNbInput < MAX_PORTS_COUNT)
			{
				sprintf(name, "emptythisbuffer[%d]", Index);
				error = m_pNmfProcWrp->bindFromUser(name, pPort->getBufferCountActual(), &mIemptyThisBuffer[Index]);
				if (error != NMF_OK)
					NMF_PANIC("PANIC %s->bindFromUser %s\n", GetComponentName(), name) ;

				sprintf(name, "emptybufferdone[%d]", Index);
				error = EnsWrapper_bindToUser(mENSComponent.getOMXHandle(),
					m_pNmfProcWrp,
					name ,
					this->getEmptyBufferDoneCB(),
					pPort->getBufferCountActual());

				if (error != NMF_OK)
					NMF_PANIC("PANIC %s->bindToUser %s\n", GetComponentName(), name) ;

				++iNbInput;
			}
			else
			{
				// ReportError(
			}
			break;
		case OMX_DirOutput://bind fillthisbuffer, fillbufferdone for each output port
			if (iNbOutput < MAX_PORTS_COUNT)
			{
				sprintf(name, "fillthisbuffer[%d]", Index);
				error = m_pNmfProcWrp->bindFromUser(name, pPort->getBufferCountActual(), &mIfillThisBuffer[Index]);
				if (error != NMF_OK)
					NMF_PANIC("PANIC %s->bindFromUser %s\n", GetComponentName(), name) ;

				sprintf(name, "fillbufferdone[%d]", Index);
				error = EnsWrapper_bindToUser(mENSComponent.getOMXHandle(),
					m_pNmfProcWrp,
					name,
					this->getFillBufferDoneCB(),
					pPort->getBufferCountActual());
				if (error != NMF_OK)
					NMF_PANIC("PANIC %s->bindToUser %s\n", GetComponentName(), name) ;
				++iNbOutput;
			}
			else
			{
				// ReportError(
			}

			break;
		default:
			break;
		}
	}
	// Configure the number of ports
	m_IConfig.setTargetComponent(mENSComponent.getPortCount() , &mENSComponent);
	OUTR(" ", (omx_error));
	return omx_error;
}