⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 codec.cpp

📁 VW2010芯片的控制程序和库
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/*This is a small Transmite-Receive(over Network) application for VWeb2010 board.

Date		: 2005/06/22
This program is doing the same thing like the netcodec.cpp, but this is more sample.

//*/
#include <sys/socket.h>
#include <sys/times.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <string>

#include "codec.h"
#include "sharemem.h"

using namespace std;

#define	SOFTWARE_Version	_T("Ver 2.0.0")

////////////////////////////////

#define 	IO_ENCODE_LIGHT	1
#define	IO_DECODE_LIGHT	15


/**************debug switch*****************/
#if 0
#define dprintf printf
#else
#define dprintf(format, args...)
#endif

#if 0
#define cprintf printf
#else
#define cprintf(format, args...)
#endif

#if 0
#define ddprintf printf
#else
#define ddprintf(format, args...)
#endif


/*****************************************/

#define CONFIG_FILE_NAME         _T("/var/etc/config.xml")		// config filename 
//#define CONFIG_FILE_NAME         _T("config.xml")            			// config filename

const int MAX_FILENAME_LEN  	= 512;
const int MBYTE       			= 1024*1024;        // 1MB

//Globle vars:
int 		signal_quit_active 	= FALSE;		//signal for quit active_thread
int 		active_thread_done 	= FALSE;		//status of active_thread
int 		video_input_device 	= 0xc40242;	//Composite Video input . 0xc80242 for S-Video input
int 		video_format       		= PAL;		//encode Video format

//these vars for DO light control
#define 	IO_ENCODE_LIGHT	1
#define 	IO_DECODE_LIGHT	15
int		drv_fd;						//IO FD

/****************************************************/




/****************************************************/
/*
Active_Thread() : for device activity detection over network.
*/
THREAD_RETURN THREAD_API active_thread(LPVOID p_threadParam) {
    ThreadParam *p_param = (ThreadParam*) p_threadParam;
    char time_str[256];

	int	flag,n;
	fd_set	rset,errset,allset;
	struct timeval tv;
	int	i,maxi,maxfd,listenfd,sockfd,connfd;
	int	nready,client[FD_SETSIZE];
	socklen_t		clilen;
	struct sockaddr_in cliaddr,servaddr;
	char		buf[500+1];
	
	// initialize network receiver
	int port = p_param->Port;
	sockaddr_in sin;
	int sock_opt = 1;

	retrieve_time(time_str);
	active_thread_done = FALSE;
	dprintf("[%s] Active_check_thread() created!\n", time_str);
	ddprintf("active_check_thread() : port = %d\n",port);

	if(-1==(listenfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP))){
		dprintf("active_check_thread() :Unable to Create ACTIVE_CHANNEL network.\n");
		active_thread_done = TRUE;
		return 0;
	}
	if ((setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (void *) &sock_opt,
					sizeof(sock_opt))) == -1)
		{
			dprintf( "set socketopt  SO_REUSEADDR error\n" );
			//fflush(0);
			exit ( 1 );
		}
	bzero(&sin,sizeof(sin));
//	memset((char *)&sin, 0 , sizeof(sin));
	sin.sin_family = AF_INET;
	sin.sin_addr.s_addr = htonl(INADDR_ANY);
	sin.sin_port = htons(port);

//Set to NoBlock socket
	if( (flag = fcntl(listenfd, F_GETFL, 0)) < 0 ){
		dprintf("Active_Thread(): socket fcntl() get error. Aborting...\n");
		active_thread_done = TRUE;
		return 0;
	}
	flag |= O_NONBLOCK;
	if(fcntl(listenfd, F_SETFL, flag) < 0){
		dprintf("Active_Thread(): socket fcntl() set error. Aborting...\n");
		active_thread_done = TRUE;
		return 0;
	}

	if(bind(listenfd, (sockaddr *)&sin, sizeof(servaddr))  < 0){
		dprintf("active_check_thread() :Unable to Bind ACTIVE_CHANNEL network\n");
		active_thread_done = TRUE;
		return 0;
	}
	if(listen(listenfd, 10) < 0){
		dprintf("active_check_thread() :Unable to listen ACTIVE_CHANNEL network\n");
		active_thread_done = TRUE;
		return 0;
	}
	ddprintf("active_check_thread() : accepting connections ... \n");

//init FDset, put listen socket into it, in the 1st position.
	maxfd = listenfd;
	maxi =  -1;
	for(i=0; i<FD_SETSIZE; i++)
		client[i] = -1;
	FD_ZERO(&allset);
	FD_SET(listenfd, &allset);

	tv.tv_sec = 0;
	tv.tv_usec = 0;

	while( !signal_quit_active ){
		rset = allset;
		errset = allset;
		nready = select( maxfd+1, &rset,NULL,&errset, &tv);
		if(FD_ISSET( listenfd , &rset)){	// new client connection
			clilen = sizeof(cliaddr);
			connfd = accept( listenfd, (sockaddr *)&cliaddr, &clilen);
			//check connfd ,
			if(connfd>0){
				for( i=0; i<FD_SETSIZE; i++){
					if(client[i] < 0){
						client[i] = connfd;		//save descriptor
						break;
					}
				}
				if( i == FD_SETSIZE){
					dprintf("Active_thread(): too many connections. Dropped this one-(%d)\n",connfd);
					close(connfd);			//drop this connection
				}
				else{
					FD_SET(connfd, &allset);	//add new descriptor to set
					if( connfd > maxfd)
						maxfd = connfd;
					if( i > maxi )
						maxi= i ;		// max index in client[] array
					dprintf("active_thread() : accepted a connection - (%d).\n",connfd);
					if( --nready <= 0 )
						continue;	//no more readable descriptors
				}
			}
			else{
				dprintf("accept(): <= 0.\n");
			}
		}
		for( i=0 ; i<=maxi; i++ ){	//check all clients for data
			if( (sockfd = client[i]) < 0 )
				continue;
			if( FD_ISSET( sockfd, &rset) ){
				if( (n = recv( sockfd, buf, 100,0)) <= 0 ){	//connection closed by client
					ddprintf("recv(): = %d.\n",n);
					close( sockfd );
					dprintf("active_thread() : dropped a connection - (%d). \n",sockfd);
					FD_CLR( sockfd, &allset);
					client[i] = -1;
				}
				else {	//if received data, ECHO it
					send( sockfd, buf, n, 0);
				}
				if ( --nready <= 0 )
					break;	//no more readable descriptors
			}
			if( FD_ISSET( sockfd, &errset) ){
				close( sockfd );
				dprintf("active_thread() : dropped a ERR connection - (%d). \n",sockfd);
				FD_CLR( sockfd, &allset);
				client[i] = -1;
				if ( --nready <= 0 )
					break;	//no more readable descriptors
			}
		}
		nap(5);
	}
	//close sockets in client[] array
	for(i =0; i<maxi; i++){
		if( (sockfd = client[i]) < 0 )
			continue;
		dprintf("active_thread() : EXITing, dropping connection - (%d). \n",sockfd);
		close(sockfd);
		FD_CLR( sockfd, &allset);
		client[i] = -1;
	}
	
// finalize network receiver and quit.
	close( listenfd );
	retrieve_time(time_str);
	dprintf("[%s] active_check_thread() received QUIT signal\n", time_str);
	active_thread_done = TRUE;
	dprintf("**active_check_thread() returned.\n");
	return 0;
}


/****************************************************/
int	showVersion( )
{
	char temp[80];
	sprintf(temp, "[ CODEC application ]");
	printf("%-20s : Version: [%s]\n", temp, SOFTWARE_Version );
	printf("%-20s : Compiled on: [%s %s]\n", temp,  __DATE__, __TIME__);
	return 0;
}

/****************************************************/
//for read from the shared-memory( communicate with other applications, MENU, CGI etc.).
/*
INPUT	@s		:the source text block.
OUTPUT	@strline	:point of string to store one line from the text block.
		@start_pos:store the next start postion in the text block after one line.
RETURN	:-1 = finished ; >0 = got one line, and still have lines in the text block.
*/
int	getaline(char *s, char * strline, int *start_pos)
{
	int i=0;
	if (*start_pos < 0 || *(s+*start_pos) == '\0')
		return -1;
	while(1){
		if( (*(s+*start_pos+i) != '\n')   && *(s+*start_pos+i) != '\0'){
			strline[i]=*(s+*start_pos+i);
			i++;
//			dprintf("%d\n",i);
		}
		else{
			if(*(s+*start_pos+i)!='\0'){ 	//data not finished yet, just a line
				strline[i] = '\0';
				*start_pos = *start_pos + i + 1;
				return i;
			}
			else{	//data finished
				strline[i]='\0';
				*start_pos = -1;
				return i;
			}
		}
	}
}
/****************************************************/
/*	Parse one line in config file (in XML format). 
INPUT	@entry	:one line string.
OUTPUT	@name	:parameter's name defined in INPUT line.
		@val		:parameters value set in INPUT ling.
RETURN	: 	1 = SUCCESS;
			-1=FAILURE;
*/
int configParseLine_XML(const std::string &entry, std::string &name, std::string &val)
{

	UINT32	startpos,pos;
	string 	st,st2;

	startpos = entry.find("<");
	if( startpos == string::npos)
		return -1;
	st = entry.substr(startpos+1);

	pos = st.find(">");
	if( pos == string::npos)
		return -1;
	name.assign(st , 0 , pos);
	
	st2 = st.substr( pos + 1 );
	pos = st2.find("<");
	if( pos == string::npos)
		return -1;
	val.assign(st2, 0, pos);

	 return 1;
}

/****************************************************/
/*	Set parameter to vweb2010.  
INPUT	@name	:name of the parameter defined in vweb2010 CONFIG_STRUCTURE.
		@val		:value of the parameter.
		@vw2010:point to the implementation of the vw2010 interface for set down new value.
		@info	:point to ConfigSetting structure for store local vars.
RETURN	:	SUCCESS;FAILURE	;
*/
int	configProcess( const string &name, const string &val, VW2010 *vw2010 , ConfigSetting *info)
{
	ULONG ul,ul2;
	UINT32 x_pos,n;
	string value,vid_h,vid_v;
	
	if(name=="Video_Stream_Address")
		{	//Encode param
			strcpy( info->enc_Video_Addr , val.c_str());
			info->enc_Video_Addr[val.length()] = '\0';
			cprintf("Encode output Video address: %s\n", info->enc_Video_Addr );
	  }
	else if(name=="Video_Stream_Port")
	{	//Encode param
			info->enc_Video_Port = (unsigned short) strtoul(val.c_str(),NULL,0);
			cprintf("Encode output Video port: %d\n",info->enc_Video_Port);
	}
	else if(name=="Audio_Stream_Address")
	{	//Encode param
			strcpy( info->enc_Audio_Addr , val.c_str());
			info->enc_Audio_Addr[val.length()] = '\0';
			cprintf("Encode output Audio address: %s\n", info->enc_Audio_Addr );
	}
	else if(name=="Audio_Stream_Port")
	{	//Encode param
			info->enc_Audio_Port = (unsigned short) strtoul(val.c_str(),NULL,0);
			cprintf("Encode output Audio port: %d\n",info->enc_Audio_Port);
	}
	else if(name=="dec_Stream_Address")
	{	//Decode param
			strcpy( info->dec_Stream_Addr , val.c_str());
			info->dec_Stream_Addr[val.length()] = '\0';
			cprintf("Decode input stream address: %s\n", info->dec_Stream_Addr );
	}else if(name=="dec_Stream_Port")
	{	//Encode param
			info->dec_Stream_Port = (unsigned short) strtoul(val.c_str(),NULL,0);
			cprintf("Decode input stream port: %d\n",info->dec_Stream_Port);
	}else if(name=="active_channel_port")
	{	//Active Channel port setting
			info->active_port = (unsigned short) strtoul(val.c_str(),NULL,0);
			cprintf("Active channel port: %d\n", info->active_port);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -