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

📄 mddemo.c

📁 海康威视视频采集卡linux开发包4.22版
💻 C
📖 第 1 页 / 共 3 页
字号:
/*
 * File
 * 	mddemo.c
 * 	
 * Description:	
 * 	This is a demo for the using for DS400xMD board
 * 	
 * Author:
 * 	write by yxq at 2005.7.25
 * 
 * (C) 2004-2010 Hangzhou Hikvision Digital Technology  Co. Ltd.
 *	
 * Last update: 2005-7-25
 */
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <gdk/gdkx.h>
#include <gtk/gtk.h>
#include <gtk/gtktext.h>
#include <gdk/gdk.h>
#include <SDL/SDL.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 <pthread.h>
#include <assert.h>
#include "tmclientpu.h"

//#define RAWSTREAM
GtkWidget *md_window;
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_decodefile_flag;
GtkWidget *w_decodestream;
GtkWidget *w_videoout;


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];

struct statfs bbuf;
static int selectport = 0;	
//timer
GTimer *count_timer = NULL;
gdouble elapse_time = 0;

int processtimer = 0;
time_t now;
struct tm *tm;
char timebuf[128];
char savepath[32] = ".";

/* frame infomations add at 2002.4.11 */
GtkWidget *w_decode_framerate;
GtkTextBuffer *w_decode_framerate_buffer;
GtkWidget *w_totalframe;
GtkTextBuffer *w_totalframe_buffer;
GtkWidget *w_channel;
GtkTextBuffer *w_channel_buffer;

GtkWidget *w_bit_rate;
GtkTextBuffer *w_bit_rate_buffer;

char starttime[100];

static SDL_Surface      *pOverlayScreen;
video_win v_win[MAX_PORT];
SDL_Rect dstRect[MAX_PORT];

int	bfileopenok=0;
sem_t	FileEndSem[MAX_PORT];
void*	PlayOver(void* arg);
pthread_t hDecodeOver[MAX_PORT];
PREVIEWCONFIG	displayconf[MAX_PORT];

int ChannelHandle[MAX_PORT];
long	TotalChannel = 0;
pthread_t hDisplayThread[MAX_PORT];
sem_t	beginSem[MAX_PORT];
void*	videodisplay(void* arg);
SDL_Overlay*	pOverlaySurface[MAX_PORT];
int		bDisplayOpen[MAX_PORT];
int		bDisplayClose[MAX_PORT];
int		displaythreadrun[MAX_PORT];
int		audiopreviewport = 0;
static int clickflag = 1;	
SDL_Rect	tmpRect;
static int clickport = -1;
sem_t	displaySem[MAX_PORT];
static int streamopenok = 0;
static	int clienthandle[MAX_PORT];
static int displayChannelCount = 0;
static unsigned long datasum[MAX_PORT];

char serverchan[MAX_PORT][16];
char serverip[MAX_PORT][64];
char* defaultserverip="192.0.0.126";
GtkWidget *w_set_serverip;
GtkWidget *w_set_serverchan;

#define	WIN_W	800
#define	WIN_H	700

void demo_exit();
void ReadDataCallBack(int StockHandle,char * pPacketBuffer,int nPacketSize);
int stopstreamdecode();

#ifdef  RAWSTREAM
int  imagefile_fd=0;
unsigned char imageBuf[704*576*2];
void ImageStreamCallback(unsigned int channelNumber, void* context)
{
        //fprintf(stderr,"Enter the image stream call back for chan %d!\n", channelNumber);
        if(imagefile_fd)
                write(imagefile_fd, imageBuf,704*576*3/2);
}
#endif

/* buttons operation functions */
static void hik_set_sensitive(char *name, int sensitive)
{
        GtkWidget *button;
        button = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(md_window), name));
        gtk_widget_set_sensitive(button, sensitive);
}

void set_serverip_callback(GtkWidget *widget)
{
	const char *str;	
	str = gtk_entry_get_text(GTK_ENTRY(w_set_serverip));
	strcpy(serverip[selectport], str);
	fprintf(stderr,"<demo_info> the chan %d have set sever ip %s!\n", selectport, serverip[selectport]);
}

void set_serverchan_callback(GtkWidget *widget)
{
	const char *str;	
	str = gtk_entry_get_text(GTK_ENTRY(w_set_serverchan));
	strcpy(serverchan[selectport], str);
}

void demo_exit()
{
	int port;
	int	i;
	if(processtimer){
		gtk_timeout_remove(processtimer);
		processtimer = 0;
	}
	if(count_timer){
		g_timer_destroy(count_timer);
		count_timer = NULL;
	}
	for(i=0; i<TotalChannel; i++)
	{
		if(bDisplayOpen[i])
		{
			bDisplayOpen[i] = 0;
			sem_post(&displaySem[i]);
			usleep(100);
			displaythreadrun[i] = 0;
			sem_post(&beginSem[i]);
			usleep(100);
		}
	}
	if(bfileopenok)
	{
		for(i=0;i<TotalChannel;i++)
	        {
			sem_post(&FileEndSem[i]);
        	}
	        bfileopenok = 0;
	}
	if(streamopenok)
		stopstreamdecode();

	for(i=0; i<TotalChannel; i++)
	{
		HW_ChannelClose(ChannelHandle[i]);
		sem_destroy(&FileEndSem[i]);
		sem_destroy(&beginSem[i]);
	}
	HW_ReleaseDecDevice();
#ifdef  RAWSTREAM
        if(imagefile_fd)
                close(imagefile_fd);
#endif

	gdk_flush();	
	SDL_Quit();
	gtk_main_quit();
	usleep(2000);
	printf("End ......\n");
}

static void dialog_cleanup(GtkWidget *dialog)
{
	gtk_grab_remove(dialog);
	gtk_widget_destroy(dialog);
}
static void file_open_cancel(GtkWidget *widget, GtkWidget *dialog)
{
	assert(dialog);
	bfileopenok=0;
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w_decodefile_flag), FALSE);
	dialog_cleanup(dialog);
}

static void file_open_ok(GtkWidget *widget, GtkWidget*fs)
{
	int	i;
	gchar* filename;	// here gtk_file_selection_get_filename return value is const, but HW_OpenFile parameter shouldn't be const
	filename = (gchar*)gtk_file_selection_get_filename(GTK_FILE_SELECTION(fs));
	fprintf(stderr,"+++++++++++++++++++++++<demo_info> Open the file %s!\n", filename);
	for(i=0;i<TotalChannel;i++)
	{
		if(HW_OpenFile(ChannelHandle[i], filename)<0)
			fprintf(stderr,"<demo_info>the md card open file failed, for 0x%x!\n", GetLastErrorNum());
		else
			fprintf(stderr,"<demo_info> the md open file ok!\n");
	}

	for(i=0;i<TotalChannel;i++)
	{
		if(HW_Play(ChannelHandle[i])<0)		
			fprintf(stderr,"<demo_info> play the file failed! error code 0x%x!\n", GetLastErrorNum());
		else{
			pthread_attr_t	attr;
			int* threadarg;
			threadarg = (int*)malloc(sizeof(int));
			*threadarg = i;
			sem_init(&FileEndSem[i], 0, 0);
			fprintf(stderr,"<demo_info> play the file ok,the file end sem is 0x%x!\n", &FileEndSem[i]);
			HW_SetFileEndMsg(ChannelHandle[i], &FileEndSem[i]);
			pthread_attr_init(&attr);
			pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
			pthread_create(&hDecodeOver[i], &attr, PlayOver,(void*)threadarg);
			bfileopenok++;
			sem_post(&beginSem[i]);
		}

	}
	fprintf(stderr,"++++++++++++++++++++++++++ <demo_info> file open operate ok!\n");
	//close the dialog
	dialog_cleanup(GTK_WIDGET(fs));
}

void video_out(GtkWidget* widget, gpointer data)
{
	REGION_PARAM param[MAX_DISPLAY_REGION];
	int i;
	
	if(GTK_TOGGLE_BUTTON(w_videoout)->active)
	{
		
		memset(param,0,sizeof(param));
		for(i=0;i<MAX_DISPLAY_REGION;i++)
		{
			param[i].r=i*8+128;
			param[i].g=i*16;
			param[i].b=i*24;
		}
		//to set the port0 as the 4 cif windows		
		param[0].left=0;	param[0].top=0;		param[0].width=352;		param[0].height=288;
		param[1].left=352;	param[1].top=0;		param[1].width=352;		param[1].height=288;
		param[2].left=0;	param[2].top=288;	param[2].width=352;		param[2].height=288;
		param[3].left=352;	param[3].top=288;	param[3].width=352;		param[3].height=288;
		SetDisplayRegion(0,4,param,0);
		//to set the port1 as the 1 d1 window
		param[0].left=0;	param[0].top=0;		param[0].width=704;		param[0].height=576;
		SetDisplayRegion(1,1,param,0);

		SetDecoderVideoOutput(0,0,1,0,0,0);
		SetDecoderVideoOutput(1,0,1,0,1,0);
		SetDecoderVideoOutput(2,0,1,0,2,0);
		/*
			Set the video from the last decode out to as the region4 in por0 and the whole port1
		*/
		SetDecoderVideoOutput(3,0,1,0,3,0);
		SetDecoderVideoOutput(3,1,1,1,0,0);

		SetDecoderAudioOutput(0, 1, 0);
		SetDecoderAudioOutput(1, 1, 1);
		
#ifdef RAWSTREAM
                //start
                if(SetDisplayVideoCapture(0, 1, 25, 704, 576, imageBuf)<0)
                        fprintf(stderr,"<demo_info>.............. set the image stream capture failed!\n");
                fprintf(stderr,"<demo_info> ................start image raw stream capture ok!\n");
#endif
	}else{
		//stop the video out
		SetDecoderVideoOutput(0,0,0,0,0,0);
		SetDecoderVideoOutput(1,0,0,0,1,0);
		SetDecoderVideoOutput(2,0,0,0,2,0);		
		SetDecoderVideoOutput(3,0,0,0,3,0);
		SetDecoderVideoOutput(3,1,0,1,0,0);
		//stop the audio out 
		SetDecoderAudioOutput(0, 0, 0);
		SetDecoderAudioOutput(1, 0, 1);
		
		for(i=0;i<displayChannelCount;i++)
			ClearDisplayRegion(i, 0xffff);		
#ifdef RAWSTREAM
                //stop
                if(SetDisplayVideoCapture(0, 0, 25, 704, 576, imageBuf)<0)
                        fprintf(stderr,"<demo_info> set the image stream capture failed!\n");
                fprintf(stderr,"<demo_info> stop image raw stream capture ok!\n");
#endif
	}
	return;
}

void decode_file(GtkWidget* widget, gpointer data)
{
	GtkWidget	*file_sel = NULL;
	int i;
	if(GTK_TOGGLE_BUTTON(w_decodefile_flag)->active)
	{
		file_sel = gtk_file_selection_new("Select a file to decode!");
		gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(file_sel)->ok_button),
			"clicked", GTK_SIGNAL_FUNC(file_open_ok), file_sel);
		gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(file_sel)->cancel_button),
			"clicked", GTK_SIGNAL_FUNC(file_open_cancel), file_sel);
		gtk_file_selection_hide_fileop_buttons(GTK_FILE_SELECTION(file_sel));
		gtk_file_selection_set_filename(GTK_FILE_SELECTION(file_sel), "test000.mp4");
		gtk_widget_show(file_sel);
	    hik_set_sensitive("b_DecodeStream", 0);
		hik_set_sensitive("b_videoaudioout", 1);
	}
	else{		//stop decode
		if(bfileopenok)
		{
			for(i=0;i<TotalChannel;i++)
			{
				sem_post(&FileEndSem[i]);
			}
			bfileopenok = 0;
		}
	    hik_set_sensitive("b_DecodeStream", 1);
		hik_set_sensitive("b_videoaudioout", 0);
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w_videoout), FALSE);
	}
	return;
}
int stopstreamdecode()
{
	int retval;
	int i;
	fprintf(stderr,"<demo_info> stop decoding the stream!\n");	
	
	for(i=0;i<TotalChannel;i++)
	{
		bDisplayOpen[i] = 0;
		sem_post(&displaySem[i]);
	}
	for(i=0;i<TotalChannel;i++)
	{
		HW_Stop(ChannelHandle[i]);
		HW_CloseStream(ChannelHandle[i]);
		MP4_ClientStopCapture(clienthandle[i]);
		retval = MP4_ClientStop(clienthandle[i]);
		if(retval == -1) 
			fprintf(stderr,"<demo_info>clientstop error\n");

	}

	MP4_ClientCleanup();
	gdk_window_clear(w_full_area->window);
	streamopenok = 0;
	return 0;
}
void decode_stream(GtkWidget* widget, gpointer data)
{
	int i;
	CLIENTSHOWRECT clientrect;
	CLIENT_VIDEOINFO Clientinfo;
	char	streamheader[128];
	int	header_len;
	if(GTK_TOGGLE_BUTTON(w_decodestream)->active)
	{

		/* init the net client connect */
		MP4_ClientSetNetPort(5050, 6050);
		MP4_SetDecoder(0);  //disable the client sdk to decode stream

		MP4_ClientStartup(clientrect, NULL);
		Clientinfo.m_bRight = 0; 
		//Clientinfo.m_bRemoteChannel = 0;
		Clientinfo.m_sUserName = "username1234567890";

⌨️ 快捷键说明

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