SfxDecoder.cpp 2.77 KB
#include "SfxDecoder.h"

#include <thread>
#include <chrono>

using namespace std;

SfxDecoder::SfxDecoder( const DxDecoderConfig * cfg )
{
	memset( &m_video, 0, sizeof( DxVideoConfig ) );
	m_handler = sfx_null;
	m_bRun = sfx_true;

	SfxStreamHandlerConfig* sfx_cfg = new SfxStreamHandlerConfig();
	sfx_cfg->userPtr = this;
	sfx_cfg->escb = SfxStreamCallback;
	sfx_cfg->log = SfxLogDefaultHandler;
	sfx_cfg->flags.outFrameIndex = 1;
	sfx_cfg->flags.preposeFrameCount = 1;
	sfx_cfg->flags.useFastDecode = 0;
	sfx_cfg->flags.accelId = cfg->cfg.devId;
	

	m_handler = SfxStreamHandlerInterface::SfxGetInstance(sfx_cfg);
	memcpy( &m_cfg, cfg, sizeof( DxDecoderConfig ) );

	printf( "alloc SfxDecoder..\n" );

	return;
}


SfxDecoder::~SfxDecoder()
{
	printf( "free SfxDecoder..\n" );

	if ( sfx_null != m_handler )
	{
		SfxStreamHandlerInterface::SfxReleaseInstance( m_handler );
		m_handler = sfx_null;
	}

	return;
}


int SfxDecoder::OpenDecoder( const char * uri )
{
	return m_handler->SfxOpenStreamHandler( uri );
}


int SfxDecoder::CloseDecoder()
{
	m_handler->SfxCloseStreamHandler();
	while (m_bRun)
	{
		//sfxSleepMs(250); //220729
		std::this_thread::sleep_for(std::chrono::milliseconds(250));
	}

	return 0;
}

int SfxDecoder::GetFrameCount(){
	return m_handler->SfxGetFrameCount();
}


sfx_32 SfxDecoder::SfxStreamCallback( 
									 const sfx_void * userPtr,
									 SfxFrameType type,
									 const sfx_u8 * buf, 
									 sfx_u32 segLen, 
									 sfx_u32 len, 
									 sfx_u32 timestamp 
									 )
{
	SfxDecoder * pThis = sfx_null;

	pThis = ( SfxDecoder * )userPtr;
	if (!pThis->m_bRun)
	{
		return 0;
	}

	if ( SFX_FRAME_TYPE_CONFIG == type )
	{
		pThis->m_video.height = ( ( SfxMediaConfig * )buf )->v.height;
		pThis->m_video.width = ( ( SfxMediaConfig * )buf )->v.width;
		
		pThis->m_cfg.escbk( pThis->m_cfg.userPtr, &pThis->m_video, 0, 0 );

		return 0;
	}
	
	pThis->m_cfg.escbk( pThis->m_cfg.userPtr, ( void * )buf, segLen, timestamp );

	return 0;
}
	
sfx_32 SfxDecoder::SfxLogDefaultHandler(
										const sfx_void * userPtr,
										SfxLogLevel level,
										const sfx_8 * log,
										sfx_u32 logLen
										)
{
	SfxDecoder * pThis = sfx_null;

	pThis = ( SfxDecoder * )userPtr;

	if ( SFX_LOG_LEVEL_IMPORTANT == level) //modify by zsh 220602
	{
		
		pThis->m_cfg.logcbk(
							pThis->m_cfg.userPtr,
							DX_LOG_LEVEL_IMPORTANT, log,
							0
							);

		return 0;	
	}


	if ( SFX_LOG_LEVEL_CLEANUP == level )
	{
		pThis->m_bRun = sfx_false;
		if ( pThis->m_cfg.logcbk )
		{
			pThis->m_cfg.logcbk(
								pThis->m_cfg.userPtr,
								DX_LOG_LEVEL_CLEANUP,
								"DxDecoder[CUDA] module exited..",
								0
								);
		}
	}
	
	return 0;
}