?? dsserver.cpp
字號:
#include "DsServer.h"#include <stdio.h>#include <string.h>#include <unistd.h>#include <signal.h>#include <stdlib.h>#include <sys/stat.h>#include <sys/types.h>#include <sys/wait.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <errno.h>#include <fcntl.h>#include <sys/vfs.h> //statfs()#include <time.h>#include <pthread.h>#include <sys/ipc.h>#include <sys/shm.h>#include <sys/sem.h>#include <dirent.h>#include <semaphore.h>#include <SDL/SDL.h>#include <dirent.h> //opendir()#include "test.h"#include "DataType.h"#include "ds40xxsdk.h"#include "tmserverpu.h"#include "doconfig.h"#include "vodserver.h"#include "Pelco.h"#include "adrport.h"#define COM_PORT 0typedef struct{ unsigned long FilePos; /* the locate for I frame in file */ unsigned long Reserve; unsigned long FrameNum; /* the video frame num, include I frame and BBP frame,exclude the audio frame */}INDEX_NODE, *PINDEX_NODE;typedef unsigned char UB;typedef struct{ unsigned short iType; //報警設備類別 unsigned short iAlarmType; //報警類型 unsigned short iChannel; //報警通道 unsigned short iReserve; //備用}TAlarm;#define INVALID_PID -1#define CONFIG_FILE "/etc/dvr/config.ini"#define MAX_CHANNELS 64unsigned short *gRecord=NULL;unsigned long *gCurrentFileSize=NULL;unsigned short gSubRecord[4];unsigned short gFormat1;unsigned short gFormat2;unsigned short gRecordFileSize;unsigned short gMoveRecord;unsigned short isMoving[4];char savepath[255];char alarmip[25];int gFileHandleQcif[MAX_CHANNELS]; //子通道同時編碼時qcif錄象文件句柄unsigned char FileHeaderQcif[MAX_CHANNELS][200]; //QCIF Header,whenunsigned int bEncodeCifAndQcif[MAX_CHANNELS]; //子通道同時編碼時通道標志unsigned char FileHeader[64][200];unsigned int net_flag[4] = {0,0,0,0};static int net_head_flag[4];static int subchan_net_head_flag[4];int cifqcif_fd[4];char cifqciffile_head[4][40];char *file_head=NULL;int *record_fd=NULL;int index_fd[4]; //add at 2005.6.2 to test the index createINDEX_NODE nodeinfo[4]; //add at 2005.6.2 to test the indexint *head_size = NULL;//unsigned short format1[100];// = {8,30,'辦','f','f','i','c','e','\0'};unsigned short format2[] = {8,3,_OSD_YEAR4,'年',_OSD_MONTH2,'月',_OSD_DAY,'日',' ',_OSD_HOUR24,':',_OSD_MINUTE,':',_OSD_SECOND,'\0'};int gcheckpass(char * usename, int namelen, char * password, int passlen);int gcheckip(int iChannel, char * sIP);void gmessage(char * buf, int iLen);void callback(int channelNum,char * buf,int frametype,int length);void stop_capture_callback(int port);void start_capture_callback(int port);void disconnect_callback(int nport, char* sIP);int checkIpAndPass(int channel, char *username, int namelen, char *password, int passlen, char *sIP);void motiondetectcallback(ULONG channel,int bMotion,void * content);void AlarmInfo(unsigned char a,unsigned char b);int FileHeaderLen = 0;int audio_port = 0;pid_t writedata_pid;int fd = -1;//=================================================#define SHM_KEY1 0x11223344#define SHM_KEY2 0x11223345#define SHM_KEY3 0x11223346#define SHM_KEY4 0x11223347#define SHM_KEY5 0x11223348#define SHM_KEY6 0x11223349#define SHM_KEY7 0x11223350#define SHM_KEY8 0x11223351int shm_id1;int shm_id2;int shm_id3;int shm_id4;int shm_id5;int shm_id6;int shm_id7;int shm_id8;int * pInt=NULL;int *gAlarm = NULL;int channelHandle[MAX_CHANNELS];int * channelCount;int * isExit=NULL;int * isSF =NULL;int oldSF = 0;int * isAlarm0 = NULL;int *isAlarm1 =NULL;int *isAlarm2 =NULL;int *isAlarm3 =NULL;DsServer::DsServer(){ fd = OpenAdrPort(COM_PORT); isServerStarted = false;}DsServer::~DsServer(){ CloseAdrPort(fd); if(isServerStarted) { StopServer(); }}int DsServer::StartServer(){ int i,retVal, chan; int index = 0; int servertype = 1; SERVER_VIDEOINFO videoinfo; RECT winRect; struct sigaction action; sigset_t block_mask, old_mask; STREAM_READ_CALLBACK pStreamRead; pStreamRead = (STREAM_READ_CALLBACK)callback; int tei=0; char data[4]; char RecordTail[50]; char SubRecordTail[50]; int m_brightness = 50; int m_contrast = 50; int m_hue = 0; int m_saturation = 50; //================================= //讀取配置文件信息 //================================= memset(savepath,255,0); if(access("/etc/dvr/config.ini",F_OK)!=0) { fprintf(stderr,"Config File doesn't Exist!\n"); return 1; } if (CreateShareMemory()) { printf("Create Share memory error!\n"); return 2; } *gAlarm = 1; for(i=0;i<4;i++){ sprintf(RecordTail,"StartRecord%d",i); *(gRecord+i*sizeof(unsigned short)) = ReadInt(CONFIG_FILE,"Server",RecordTail,0); } for(i=0;i<4;i++){ sprintf(SubRecordTail,"StartSubRecord%d",i); gSubRecord[i] = ReadInt(CONFIG_FILE,"Server","StartSubRecord",0); isMoving[i] = 0; } gFormat1 = ReadInt(CONFIG_FILE,"Server","DateInfo",0); gFormat2 = ReadInt(CONFIG_FILE,"Server","LocationInfo",0); servertype = ReadInt(CONFIG_FILE,"Server","LinkType",0); gRecordFileSize = ReadInt(CONFIG_FILE,"Server","FileSize",0); ReadString(CONFIG_FILE,"Server","SavePath",savepath,""); ReadString(CONFIG_FILE,"Server","AlarmIP",alarmip,""); gMoveRecord = ReadInt(CONFIG_FILE,"Server","MoveRecord",0); //================================= retVal = InitDSPs(); if(retVal == 0){//fixed at 2005.1.13 fprintf(stderr,"Do InitDSPs Error(%08x)\n",GetLastErrorNum()); return 3; } fprintf(stderr,"Success!----InitDSPs()\n"); *channelCount = GetTotalChannels(); fprintf(stderr,"channelcount = %d\n",*channelCount); //open channel VideoStandard_t videostandard; for(i=0;i<*channelCount;i++){ channelHandle[i] = ChannelOpen( i ,callback); //SetBitrateControlMode(channelHandle[i], brVBR); //SetupBitrateControl(channelHandle[i], 768000); GetVideoPara(channelHandle[i], &videostandard,&m_brightness, &m_contrast, &m_saturation, &m_hue); SetVideoPara(channelHandle[i], m_brightness, m_contrast, m_saturation, m_hue); if(servertype) { SetEncoderPictureFormat(channelHandle[i], ENC_DCIF_FORMAT); SetIBPMode(channelHandle[i],80,2,0,15); //100,2,0,15 18,18,23 圖像效果還可以,1小時10M SetDefaultQuant(channelHandle[i], 12, 12, 15); //90,2,0,18 16, 16, 21 1 H 1 M //SetEncoderPictureFormat(channelHandle[i], ENC_CIF_FORMAT); //SetIBPMode(channelHandle[i],120,2,0,10); //100,2,0,15 18,18,23 圖像效果還可以,1小時10M //SetDefaultQuant(channelHandle[i], 14, 14, 17); //90,2,0,18 16, 16, 21 1 H 1 M }else{ SetEncoderPictureFormat(channelHandle[i], ENC_QCIF_FORMAT); SetIBPMode(channelHandle[i],211,2,1,8); SetDefaultQuant(channelHandle[i],18,18,23); //SetStreamType(ChannelHandle[i],STREAM_TYPE_AVSYNC); } videoinfo.m_datatype[i]=servertype; } videoinfo.m_channum = *channelCount; /* get cardno */ #if 0 { unsigned int boardtype; char serialno[20], serialno_str[20]; int j; for(i = 0; i < *channelCount; i++) { GetBoardInfo(ChannelHandle[i], &boardtype, serialno); for(j = 0; j < 12; j++){ serialno_str[j] = serialno[j] + 0x30; } serialno_str[j] = '\0'; printf("card[%d]'s serialno = %s\n", i, serialno_str); } } #endif memset(net_head_flag, 0, sizeof(net_head_flag)); memset(net_flag, 0, sizeof(net_flag)); for(i=0;i<*channelCount;i++) MP4_ServerSetBufNum(i,90); //MP4_ServerSetStart(start_capture_callback); //MP4_ServerSetStop(stop_capture_callback); MP4_ServerSetMessage(gmessage); MP4_ServerSetNetPort(5050,6050); if(MP4_ServerStart(&videoinfo)< 0){ printf("Net start failed;\n"); }else{ MP4_ServerCheckIP(gcheckip); MP4_ServerCheckPassword(gcheckpass); //MP4_ServerCheckIPandPassword(checkIpAndPass); MP4_ServerDisconnectEvent(disconnect_callback); //net_flag = 1; } if(gFormat1){ sleep(1); for(chan=0;chan<*channelCount;chan++){ usleep(40); int iTemp; unsigned short format1[255]; if(gFormat2){ char lMsg[255]; memset(lMsg,0,255); int lLen = ReadString(CONFIG_FILE,"Server","LocationMsg",lMsg,""); format1[0]=8; format1[1]=32; //for(iTemp=0;iTemp<lLen;iTemp+=2) iTemp=0; index = 0; while(index<lLen) { if(lMsg[index] == '\\') { index++; if(index+1>=lLen) { break; } format1[iTemp+2] = (unsigned short)(((lMsg[index]<<8)|0x00FF)&(lMsg[index+1])); iTemp++; index+=2; } else { format1[iTemp+2] = (unsigned short)(lMsg[index]); iTemp++; index++; } } format1[iTemp+2]='\0'; }else{ format1[0]='0'; format1[1]='0'; format1[2]='\0'; } if(SetOsdDisplayMode(channelHandle[chan],0xff,1,0x0000, format2, format1)<0) { fprintf(stderr,"<demo_info> set osd failed!\n"); return -1; } if(SetOsd(channelHandle[chan],1)<0) { fprintf(stderr,"<demo_info> start osd failed!\n"); return -1; } }//for(chan=0;cahn<*channelCount;chan++) }/* if(gFormat2) { int logo_w, logo_h; unsigned char *logoptr; logoptr = (unsigned char *)malloc(213 * 18 * 2); memset(logoptr, 0, sizeof(logoptr)); if(LoadYUVFromBmpFile("logo.bmp", logoptr, 213 * 18 * 2, &logo_w, &logo_h)<0) { fprintf(stderr,"Load bmp file error\n"); } SetLogo(ChannelHandle[i], 576, 448, logo_w, logo_h, logoptr); SetLogoDisplayMode(ChannelHandle[i], 0xff, 0x0, 0x0, 0, 0); if(logoptr) free(logoptr); } */#if 0 //SetEncodePictureFormat(channelHandle[1],ENC_QCIF_FORMAT); sleep(1); if(SetAudioPreview(channelHandle[audio_port], 1)<0) { fprintf(stderr,"<demo_info> set audio failed!\n"); return -1; }#endif for(i=0;i<*channelCount;i++) { if(SetAudioPreview(channelHandle[i], 1)<0) { fprintf(stderr,"<demo_info> set audio failed!\n"); //return -1; } } //start capture for(i=0;i<*channelCount;i++){ if(*(gRecord+i*sizeof(unsigned short)) ){//fixed at 2004.11.11 //g_timer_start(count_timer); if(StartVideoCapture(channelHandle[i])<0) { fprintf(stderr,"<demo_info>start video capture failed!error num:0x%x\n", GetLastErrorNum()); return -1; } if(gSubRecord[i]) { StartSubVideoCapture(channelHandle[i]); fprintf(stderr,"<info> start sub channel capture for chan%d!\n", i); } } } fprintf(stderr,"<info> Start video capture ok!\n"); //是否進行移動偵測 if (*gAlarm) { sleep(1); RECT motion_rect[*channelCount]; int video_height[*channelCount]; VideoStandard_t videostandard[*channelCount]; video_info v_info[*channelCount]; for(chan=0;chan<*channelCount;chan++){ GetVideoPara(channelHandle[chan], &videostandard[chan], &v_info[chan].Brightness, &v_info[chan].Contrast, &v_info[chan].Saturation, &v_info[chan].Hue); //調整移動偵測靈敏度,低速的時候不監測 //AdjustMotionDetectPrecision(channelHandle[chan], 2, 2, 0); /* disable slow motion detect */ if(videostandard[chan] == StandardNTSC) video_height[chan] = 480; else video_height[chan] = 576; motion_rect[chan].RectLeft = 0; motion_rect[chan].RectTop = 0; motion_rect[chan].RectRight = 704 - 1; motion_rect[chan].RectBottom = video_height[chan] -1 ; if(SetupMotionDetectionEx(channelHandle[chan],2,2,0,20, &motion_rect[chan],1,motiondetectcallback, 0) < 0) printf("setup motion detect(port %d) failed\n",i); StartMotionDetection(channelHandle[chan]); } }else{ for(chan=0;chan<*channelCount;chan++){ StopMotionDetection(channelHandle[chan]); } } CreateThread(); isServerStarted = true; return 0;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -