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

📄 dsdemo.c

📁 海康威视视频采集卡linux开发包4.22版
💻 C
📖 第 1 页 / 共 5 页
字号:
/*
 * File
 * 	dsdemo.c
 * 	
 * Description:	
 * 	This is a loop test program and net server demo for DS-40xxHC card.
 * 	
 * Last update: 2005-7-25
 */

#include <stdio.h>
#include <stdlib.h>
#include <gdk/gdkx.h>
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <SDL/SDL.h>
#include <X11/Xlib.h>


#include <sys/vfs.h>	//statfs()
#include <time.h>
#include <dirent.h> 	//opendir()
#include <unistd.h> 	//unlink(), getcwd()
#include <sys/stat.h>	//stat()
#include <string.h>
#include <fcntl.h>
#include <semaphore.h>
#include <errno.h>

#include "DataType.h"
#include "ds40xxsdk.h"
#include "tmserverpu.h"
#include <pthread.h>

#define WRITE_FILE

#define BMPFILE
#define OSDDEMO

#define LOGO

#define PREVIEWOPEN 
int	bPreviewOpen[MAX_PORT];
PREVIEWCONFIG	previewconf[MAX_PORT];
SDL_Overlay*	pOverlaySurface[MAX_PORT];
static int	g_bPreviewStart = 0;
int preview_loop(gpointer data);
SDL_Rect	tmpRect;
//#define	USE_PREVIEW_TIMER
#ifdef USE_PREVIEW_TIMER
static int	useSyncSem = 0;
#else
static int	useSyncSem = 1;
#endif
static int      previewtimer = 0;
pthread_t	hPreviewThread[MAX_PORT];
void*		videopreview(void *arg);
UINT		bPreviewThreadRun[MAX_PORT];
sem_t		previewSem[MAX_PORT];
///////////////////////////
//#define TEST_GETORIIMAGE
static int	imagetimer = 0;

//#define MOTION_DEBUG
//#define RAWSTREAM

#define YUVFILE
int qcif_flag[MAX_PORT];
int hcif_flag[MAX_PORT];
int cifqcif_flag[MAX_PORT];
int D1_flag[MAX_PORT];
int dcif_flag[MAX_PORT];

static int totalcard, totalport;
RECT video_rect[MAX_PORT];
video_win v_win[MAX_PORT];
VideoStandard_t videostandard[MAX_PORT];
video_info v_info[MAX_PORT];
//add at 2002.4.10
GtkWidget *w_full_area;
video_win full_win;
SDL_Rect full_rect;

GdkColormap *camp;
GdkGC *gc1, *gc2; /* gc1 for title window, gc2 for video window */

GtkWidget *title;
GtkWidget *w_buttons[3];
char *button_str[3] = {" Start ", "  Stop ", "  Exit  "};
GtkWidget *w_image;
GtkWidget *w_record_flag;
GtkWidget *w_motion_detect_flag;
GtkWidget *w_I_frame;

GtkWidget *w_status[3];
GtkTextBuffer *w_status_buffer[3];

char *status_str[3] = {"Data", "Time", "Free space"};

GtkWidget *w_info_scale[4];
char *info_str[4] = {"Brightness","Contrast","Saturation",  " Hue "}; 
GtkObject *w_adj[4];

//timer
int processtimer = 0;
time_t now;
struct tm *tm;
char timebuf[100];
char savepath[50] = ".";

GTimer *count_timer = NULL;
gdouble elapse_time = 0;

/* add at 2002.4.1 */
int count[MAX_PORT];
int fixsize =  512 * 1024; //4M bitrate
struct stat statbuf;
char filename[MAX_PORT][40];
/* add at 2002 4.4 */
int old_count[MAX_PORT];
static int fullflag = 0;	/* space full flag */
static int limitsize = 0;
static int selectport = 0;	

/* frame infomations add at 2002.4.11 */
GtkWidget *w_audio_framerate;
GtkTextBuffer *w_audio_framerate_buffer;
GtkWidget *w_video_framerate;
GtkTextBuffer *w_video_framerate_buffer;
GtkWidget *w_totalframe;
GtkTextBuffer *w_totalframe_buffer;

GtkWidget *w_channel;
GtkTextBuffer *w_channel_buffer;

GtkWidget *w_netusercount;
GtkTextBuffer *w_netusercount_buffer;

GtkWidget *w_net_transfer;
/*add for audio preview at 2002.5.14 */
GtkWidget *w_audio_preview;
int audiopreviewport = 0;

/*add at 2003.5.28, for start/stop preview*/
GtkWidget *w_preview;

/* add at 2004.8 for test that send string to client */
GtkWidget *w_stringtoclient;
 
int ChannelHandle[MAX_PORT];

int record_fd[MAX_PORT];
int index_fd[MAX_PORT]; //add at 2005.6.2 to test the index create
INDEX_NODE      nodeinfo[MAX_PORT]; //add at 2005.6.2 to test the index
int cifqcif_fd[MAX_PORT];
int video_height[MAX_PORT];

//int captureflag[MAX_PORT];
char file_head[MAX_PORT][40];
char cifqciffile_head[MAX_PORT][40];
int record_flag[MAX_PORT];


/*add at 2002.4.27 */
struct statfs bbuf;
int looptimer = 0;
int timeconst = 60 * 5;	/* 5 minuts */
char starttime[100];

/* for motion detection */
int sensitivity = 1;
unsigned char motion_data[MAX_PORT][4 * 18];	
//unsigned short motion_data[MAX_PORT][(22) * (18)];
RECT motion_rect[MAX_PORT];
RECT motion_list[MAX_PORT];
int full_win_flag = 0;
static int clickflag = 1;	
static int clickport = -1;

static int net_flag = 0;
static int net_head_flag[MAX_PORT];
static int subchan_net_head_flag[MAX_PORT];

int head_size = 0;

//watch file dparaments  
unsigned long old_frames[MAX_PORT];
unsigned long current_frames[MAX_PORT];
unsigned long zero_count[MAX_PORT];


/* add for set logo by wrh at 2002.4.12 */
int logo_w, logo_h;
unsigned char *logoptr;
int flogoyuv;

//unsigned short format1[] = {16,19,'O','f','f','i','c','e','\0'};
//unsigned short format1[] = {16, 19, '办', '公', '室','\0'};
unsigned short format1[] = {76, 240,/*500,*/ '办', '公', '点','\0'};
unsigned short format2[] = {140,240,/*500,*/_OSD_YEAR4,'-',_OSD_MONTH2,'-',_OSD_DAY,' ',_OSD_HOUR24,':',_OSD_MINUTE,':',_OSD_SECOND,'\0'};

/* yxq test at 2004.4.15*/
//unsigned short format2[] = {16,19,0xbcfa,0xc249,0xf1b9,'\0'};
//unsigned short format2[] = {16,19,0xe7ae,0xe1aa,0x73b7,0x4bac,0x50b6,0x64a5,'\0'};
//unsigned short format2[] = {16,19,0xa2b3,0xa3ce,0x6cdb,0x6ebd,'\0'};

//for bit rate watch
GtkWidget *w_set_bit_rate;

//BitrateControlType_t brc = brCBR;
BitrateControlType_t brc = brVBR;

gdouble  bit_rate_time[MAX_PORT];
unsigned int bit_rate_size[MAX_PORT];
char set_bit_rate_str[MAX_PORT][20];

GtkWidget *w_bit_rate;
GtkTextBuffer *w_bit_rate_buffer;

static SDL_Surface      *pOverlayScreen;
SDL_Rect dstRect[MAX_PORT];

#define	WIN_W	800
#define	WIN_H	700


unsigned short stream_type = STREAM_TYPE_AVSYNC;

void my_exit();

void ImageStreamCallback(unsigned int channelNumber, void* context);
int  imagefile_fd=0; 
unsigned char imageBuf[704*576*2];


//timer for get original image 
int getimage_loop(gpointer data)
{
	static int image_count = 0;
        unsigned char imagebuf[704 * 576 * 2];
        int size; //;= 384 * 288 *2;
        char filename[64];
	int port;
	int retval;
                                
	for(port = 0; port < totalport; port++){
		retval = GetOriginalImage(ChannelHandle[port], imagebuf, &size);
                sprintf(filename, "cap%d%d.bmp", port, image_count);
                if(retval < 0){
			printf("GetOriginalImage return %d\n", retval);
                        continue;
		}
                else{
                        printf("GetOriginalImage OK size = %d\n", size);
                                                                                                                            
                        if(size == 704 * 576 * 2)//PAL
				SaveYUVToBmpFile(filename, imagebuf, 704, 576);
                        if(size == 704 * 480 * 2)//NTSC
                                SaveYUVToBmpFile(filename, imagebuf, 704, 480);
                }
	}
	image_count++;
	return TRUE;
}

void end_record(int port)
{	
	if(record_fd[port]){
		close(record_fd[port]);
		record_fd[port] = -1;
	}
	if(cifqcif_fd[port]){
		close(cifqcif_fd[port]);
		cifqcif_fd[port] = -1;
	}
	if(index_fd[port]){
		close(index_fd[port]);
		index_fd[port] = -1;
	}
}


//callback for net server
void message_callback(char *buf, int length)
{
	printf("Get a message from client: length = %d, message = %s\n",length, buf); 
}

int check_IP_callback(int channel, char *sIP)
{
	//printf("Check IP: channel = %d, IP= %s\n", channel, sIP);
	return 0;
}

int check_password_callback(char *username, int namelen, char *password, int passlen)
{
	//printf("Check password: username = %s, namelen = %d, password = %s, passname = %d\n", username, namelen, password, passlen);
	return 0;
}

int checkIpAndPass(int channel, char *username, int namelen, char *password, int passlen, char *sIP)
{
	printf("Check IP: channel = %d, IP= %s; username = %s, namelen = %d, password = %s, passname = %d\n", channel, sIP, username, namelen, password, passlen);
	return 0;
}
void disconnect_callback(int nport, char* sIP)
{
	printf("the client from ip %s to channel %d disconnect!\n", sIP,nport);
}

void start_capture_callback(int port)	//for server start
{
	if((!record_flag[port]) && ( port < totalport)){//fixed at 2004.11.11
		g_timer_start(count_timer);
		StartVideoCapture(ChannelHandle[port]);
		if(cifqcif_flag[port])
		{
			StartSubVideoCapture(ChannelHandle[port]);	
			fprintf(stderr,"<demo_info> start sub channel capture for chan%d!\n", port);
		}
	}
}

void stop_capture_callback(int port)	//for server stop
{
	if((!record_flag[port]) && (port < totalport)){//fixed at 2004.11.11
		g_timer_stop(count_timer);
		StopVideoCapture(ChannelHandle[port]);
	}
}
//end callback for net server

void net_transfer_callback(GtkWidget *widget, gpointer data)
{
	int port;
	SERVER_VIDEOINFO videoinfo;
	
	if(GTK_TOGGLE_BUTTON(widget)->active)
	{//start net transfer
		for(port = 0; port < totalport; port++){
			MP4_ServerSetBufNum(port, 60);
		}

		MP4_ServerSetStart(start_capture_callback);
		MP4_ServerSetStop(stop_capture_callback);
		MP4_ServerSetMessage(message_callback);
		MP4_ServerSetNetPort(5050, 6050);
		videoinfo.m_channum = totalport;
	//printf("net_transfer_callback(): videoinfo.m_channum %d, totalport = %d\n", videoinfo.m_channum, totalport);
		if(MP4_ServerStart(&videoinfo) < 0){
			net_flag = 0;
			printf("Net start failed;\n");
		}
		else{	//fixed by wrh
			MP4_ServerCheckIP(check_IP_callback);
			MP4_ServerCheckPassword(check_password_callback);	
			MP4_ServerCheckIPandPassword(checkIpAndPass);
			MP4_ServerDisconnectEvent(disconnect_callback);
			net_flag = 1;
		}
	}
	else{//stop net transfer
		net_flag = 0;
		videoinfo.m_channum = 0;
		printf("call MP4_serverStop(), totalport = %d\n", totalport);
		MP4_ServerStop();
		memset(net_head_flag, 0, sizeof(net_head_flag));
                memset(subchan_net_head_flag, 0, sizeof(subchan_net_head_flag));
	}
}

void preview_callback(GtkWidget *widget, gpointer data)
{
	int port;
#if 1
#ifdef PREVIEWOPEN
	
	if(GTK_TOGGLE_BUTTON(widget)->active)
	{//start preview
		for(port = 0; port < totalport; port++){
		//	StartVideoPreview(ChannelHandle[port], dstRect[port].w, dstRect[port].h);
			bPreviewOpen[port] = 1;
		}
	}
	else//stop preview
	{
		gdk_window_clear(w_full_area->window);
		if(full_win_flag){
			//StopVideoPreview(ChannelHandle[clickport]);
			bPreviewOpen[clickport] = 0;
			clickflag ^= 1;
		}
		else
			for(port = 0; port < totalport; port++)
			{
				//StopVideoPreview(ChannelHandle[port]);
				bPreviewOpen[port] = 0;
			}
	}

	full_win_flag = 0;
#endif

⌨️ 快捷键说明

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