📄 codec.cpp
字号:
/****************************************************/
/** MAIN Function */
int main(int argc,char *argv[])
{
// ConfigInfo setting;
// generic counter variable
int i;
int debug_level=Debug_MSG_NONE;
BOOL user_stop=FALSE;
VW2010 vw2010hw; // actual hardware implementation
VW2010 *chip_interface = &vw2010hw; // virtual interface to the hardware
char opt_filename[MAX_FILENAME_LEN];
char strline[1024];
char time_str[256];
int rtval;
//vars for indicate witch thread need to be restarted since params had been updated
bool b_needRestart = FALSE;
bool b_restart_capture=FALSE, b_restart_netread=FALSE, b_restart_decode=FALSE,b_restart_active=FALSE;
bool b_reconfig_capture = FALSE;
bool b_reconfig_decode = FALSE;
ConfigSetting m_Config;
int m_fwStatus;
drv_fd=open("/dev/iodrv", O_RDWR); //打开IO驱动文件
showVersion( ); /*显示版本*/
//vars for share-memory opration-- read,parse etc.
int start_pos, r ;
string line,name,val;
//init shared_mmory
CMyshm myshm(SERVER_MODE, 4000, 0);
char shmdata[1024];
int rtnval = myshm.getState(); /*取得codec的信息*/
if (rtnval == -1)
{
printf("[ CODEC application ] : Bad sharemem : state = %d ! Application aborting...\n",rtnval);
return FAILURE;
}
// get the filenames
if (argc < 2)
{
strcpy(opt_filename, CONFIG_FILE_NAME);
}
else strcpy(opt_filename, argv[1]); /*写配置信息*/
//Check the configuration file
FILE *file = fopen( opt_filename , "r");
if (file==NULL)
{
printf("[ CODEC application ] : Open config_file \"%s\" failed! Please check the filename is correct. Application aborting...\n", opt_filename);
return FAILURE;
}
else fclose( file );
// open board 0
const int board_num = 0;
// start the device
chip_interface->setDebugLevel((DebugLevel)debug_level); /*进入vw2010的调试*/
if (SUCCESS != chip_interface->openDevice(board_num) )
{
printf("[ CODEC application ] : Open VW2010 Device failed! Please check the driver is installed\n");
return FAILURE;
}
// get Chip ID
if(chip_interface->getChipID()==CHIP_ID_VW2010)
{ /*检测Vw2010*/
if(chip_interface->getChipSubID()==0)
{
printf("[ CODEC application ] : VWeb 2010 detected.\n");
}
else
{
printf("[ CODEC application ] : VWeb 2002 detected.\n");
}
}
else
{
printf("[ CODEC application ] : VWeb 2005 detected.\n");
}
// set clock
chip_interface->changeClockFreq(CHIP_FREQ_162Mhz); /*设置vw2010的频率*/
// reset the chip
chip_interface->resetOn(board_num,NULL); /*复位芯片*/
chip_interface->resetHandles(board_num);
// get the PCI driver version
printf("[ CODEC application ] : VW2010 PCI Driver Version = 0x%08x\n", chip_interface->getDriverVersion() );
// get the API version
chip_interface->getAPIVersion( strline );
printf("[ CODEC application ] : VW2010 API Version = %s\n", strline );
if( FAILURE == configGetStreamType_XML( opt_filename, &m_Config)) /*取得流类型格式*/
return FAILURE;
if( FAILURE == configGetControlMode_XML( opt_filename, &m_Config)) /*取得控制模式*/
return FAILURE;
chip_interface->Setting_Init(m_Config.m_FirmwareType, m_Config.m_StreamType); /*设置*/
// Parse configuration file
if( FAILURE == configLoadFile_XML( opt_filename, chip_interface, &m_Config))
{ /*装载配置文件*/
printf("[ CODEC application ] : Config File Setting Error. Aborting...\n");
return FAILURE;
}
// download (and verify ) the firmware files (//inside config file//)
if( FAILURE == chip_interface->downloadFirmware( m_Config.m_FirmwareType, FALSE))
{ /*装载固件*/
printf("[ CODEC application ] : => Download Firmware failure! Aborting...\n");
return FAILURE;
}
m_fwStatus = chip_interface->getFirmwareStatus(); // if firmware is Alive don't download
dprintf("[ CODEC application ] : => Firmware is %s\n", (m_fwStatus==FW_RUNNING)? "Running!" : "Dead!");
if ( FW_RUNNING != m_fwStatus )
{
dprintf("[ CODEC application ] : => Please re-run program and restart again. Aborting...\n");
return FAILURE;
}
//*
//Active Detection
ThreadStruct active_thread_struct;
ThreadParam active_param;
memset(&active_thread_struct, 0, sizeof(ThreadStruct));
memset(&active_param, 0, sizeof(ThreadParam));
//active_param.hHandle = handle;
active_param.Port = m_Config.active_port;
init_active_handle(active_thread_struct.hActive);
// Start the active check thread
dprintf("[ CODEC application ] : starting active_check_thread.....\n");
signal_quit_active = FALSE;
start_thread(active_thread_struct, &active_param, &active_thread);
set_thread_priority(active_thread_struct, THREAD_PRIORITY_BELOW_NORMAL);
//*/
// At this point, we can get the version number, since we
// 1) parsed the config file to determine what Firmware this is
// 2) downloaded the FW
DWORD dwFWVersion = chip_interface->getFWVersion();
dprintf("[ CODEC application ] : Firmware version # = %d [0x%08x]\n",dwFWVersion,dwFWVersion);
if (dwFWVersion == 0xdeadbeef)
{
printf("[ CODEC application ] : Unable to get firmware version, aborting...\n");
return FAILURE;
}
//Since we have 2 light on board to signal encode and decode working states, now let vw2010 to known them.
chip_interface->Set_IO_Light(drv_fd, IO_ENCODE_LIGHT , IO_DECODE_LIGHT);
dprintf("[ CODEC application ] : starting ENCODE.....\n");
chip_interface->StartMux( TRUE, m_Config.doEncode, m_Config.doSplitAV,
m_Config.enc_Video_Addr,
m_Config.enc_Video_Port,
m_Config.enc_Audio_Addr,
m_Config.enc_Audio_Port);
dprintf("[ CODEC application ] : starting DECODE.....\n");
chip_interface->StartDeMux( TRUE,
m_Config.dec_Stream_Addr,
m_Config.dec_Stream_Port);
// loop until die :P
user_stop = FALSE;
//The main loop.
while(!user_stop)
{
if(myshm.readShareMem(shmdata) == TRUE )
{ //share memory has been updated, need to parse commands in shareMem.
b_needRestart = FALSE;
b_reconfig_capture = FALSE;
b_reconfig_decode = FALSE;
b_restart_capture = FALSE;
b_restart_decode = FALSE;
b_restart_netread = FALSE;
b_restart_active = FALSE;
start_pos = 0;
dprintf("[ CODEC application ] : command data = %s\n",shmdata);
for(i=0;i<1024;i++) strline[i]='\0';
start_pos = 0;
r = getaline(shmdata, strline, &start_pos);
while( r != -1)
{ //parse it line by line
dprintf("[ CODEC application ] : command line : %s .\n", strline);
line.erase();
line.insert(0, strline);
if (line.find_first_not_of(" \t\n\r")==string::npos) continue; // skip ahead if line is empty
if (line[0]=='#') continue; // skip ahead if the line is a comment
if (line[0]=='<' && line[1]=='!') continue; //comment line in xml file format.
//analize the line.
if (configParseLine_XML(line, name, val) == -1)
{
dprintf("[ CODEC application ] : Bad command line: %s\n", line.c_str());
continue; //no useful line, skip
}
//CAUTION : If user changed Stream_Type or Device_Type, CODEC application should be restart,
// since vweb needs to download new firmware.
if ( name =="Stream_Type" || name=="Device_Type" )
{
user_stop = TRUE;
break;
}
{ //otherwise , changes need to restart vw2010 encode or decode working thread.
b_needRestart = TRUE;
if( name=="Video_Stream_Address" ||name=="Video_Stream_Port"
||name == "SplitAV_enc" || name == "Open_Encode_Channel"
|| name=="Audio_Stream_Address" ||name=="Audio_Stream_Port" )
{
//just need to stop and restart the Mux
b_restart_capture = TRUE;
configProcess(name, val, chip_interface, &m_Config);
}
else if(name=="dec_Stream_Address" ||name=="dec_Stream_Port" )
{
b_restart_decode = TRUE;
configProcess(name, val, chip_interface, &m_Config);
}else if( name=="active_channel_port" )
{
b_restart_active = TRUE;
configProcess(name, val, chip_interface, &m_Config);
}
else
{//The other changes will need to reconfig & restart Mux or Demux thread!!
if( name == "dec_Video_Format" || name=="dec_Mpeg_Coding_Standard"
||name=="dec_Audio_Mute" ){
if( -1 != configProcess(name, val, chip_interface, &m_Config)){
b_reconfig_decode = TRUE;
b_restart_decode = TRUE;
}
else{
dprintf("[ CODEC application ] : Bad command line: %s.\n",line.c_str());
}
}else{
if( -1 != configProcess(name, val, chip_interface, &m_Config)){
b_reconfig_capture = TRUE;
b_restart_capture = TRUE;
}
else{
dprintf("[ CODEC application ] : Bad command line: %s.\n",line.c_str());
}
}
}
}
r = getaline(shmdata, strline, &start_pos);
}
dprintf("[ CODEC application ] : finished command parse.\n");
//now, stop & restart(reconfig) Active / Mux or Demux thread
if( b_needRestart == TRUE )
{
b_needRestart = FALSE;
if(b_restart_active == TRUE)
{
if(active_thread_done == FALSE)
{
signal_quit_active = TRUE;
dprintf("[ CODEC application ] : try to stop ACTIVE()\n");
thread_join(active_thread_struct.hThread);
}
}
if(b_restart_decode == TRUE || b_reconfig_decode == TRUE )
{
dprintf("[ CODEC application ] : try to stop DECODE()\n");
if( chip_interface->GetDemuxState() == TRUE )
{
if(chip_interface->StopDeMux() != SUCCESS)
{
dprintf("[ CODEC application ] : Stop Demux failed.\n");
}
nap(10);
}
else
{
dprintf("[ CODEC application ] : Demux not start yet.\n");
}
}
if(b_restart_capture == TRUE || b_reconfig_capture==TRUE )
{
if( chip_interface->GetMuxState() == TRUE )
{
dprintf("[ CODEC application ] : try to stop CAPTURE()\n");
if(chip_interface->StopMux() != SUCCESS)
{
dprintf("[ CODEC application ] : Stop Mux failed.\n");
}
nap(10);
}else{
dprintf("[ CODEC application ] : Mux not start yet.\n");
}
}
///////////
if(b_restart_capture == TRUE ){
b_restart_capture = FALSE;
dprintf("[ CODEC application ] : starting ENCODE.....\n");
chip_interface->StartMux( b_reconfig_capture,
m_Config.doEncode,
m_Config.doSplitAV,
m_Config.enc_Video_Addr,
m_Config.enc_Video_Port,
m_Config.enc_Audio_Addr,
m_Config.enc_Audio_Port);
}
if ( b_restart_decode == TRUE ){
b_restart_decode = FALSE;
dprintf("[ CODEC application ] : starting DECODE.....\n");
chip_interface->StartDeMux( b_reconfig_decode,
m_Config.dec_Stream_Addr,
m_Config.dec_Stream_Port);
}
if(b_restart_active == TRUE){
b_restart_active = FALSE;
signal_quit_active = FALSE;
active_param.Port = m_Config.active_port;
// Start the active check thread
dprintf("[ CODEC application ] : starting active_check_thread.....\n");
start_thread(active_thread_struct, &active_param, &active_thread);
set_thread_priority(active_thread_struct, THREAD_PRIORITY_BELOW_NORMAL);
}
}
}
nap(5);
}
CLEANUP:
// close device
chip_interface->closeDevice();
dprintf("[ CODEC application ] : closeDevice() finished.\n");
if(active_thread_done == FALSE)
{
signal_quit_active = TRUE;
dprintf("try to stop ACTIVE()\n");
thread_join(active_thread_struct.hThread);
}
return SUCCESS;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -