📄 hello.cpp
字号:
#include <iostream>#include <stdlib.h>extern "C"{#include <dlfcn.h>}#include <string.h>#include <stdint.h>#include <unistd.h>#include <time.h>#include <pthread.h>//#include<QtGui/QApplication>//#include<QtGui/QWidget>//#include<QtGui/QLabel>//#include<QtCore/QTextCodec>extern "C"{#include"/usr/include/hk/analyzedata.h"}#include"/usr/include/hk/hcnetsdk.h"#include"/usr/include/hk/m4play.h"#include"/usr/include/hk/playsdkpu.h"using namespace std;#define SERVER_IP "192.0.2.1" #define USER_NAME "admin"#define PASSWD "12345"#define PORT 8000#define LOCAL_IP "192.0.2.2"#define LOCAL_PORT 7200 #define TRUE 1#define FALSE 0#define CIF_WIDTH 352#define CIF_HEIGTH 288#define MAXCHANNUM 3int VIEW_NUM = 1;int glUserID = -1;int gbExit = 1;int glRealHandle[MAXCHANNUM] = {-1};int iRet = 0;int bGetSysHeader[MAXCHANNUM] = {0};long g_lAnalyzeHandle = -1;int cerror(){char *pcError = NULL; pcError = dlerror(); if (pcError) { cout<<"dlopen Error info:"<<pcError<<endl; return -1; }}DWORD gfMessCallBack(int lCommand, char *sDVRIP, char *pBuf, DWORD dwBufLen, void* dwUser) //报警消息回调函数 //lCommand 消息类型 sDVRIp 服务器IP地址 pbuf 存放信息的缓冲区{ void *FuncLib2; FuncLib2 = dlopen("libanalyze.so", RTLD_LAZY); cerror();//int iRet = 0;typedef DWORD (*GetLastError)(); // 定义函数指针,用来接受so文件里的函数GetLastError getlastError = (GetLastError)dlsym(FuncLib2, "NET_DVR_GetLastError");cerror(); int lIndex = -1; int nStartChan = 1;//默认起始通道为1 if(lCommand < EXCEPTION_EXCHANGE) {//报警 int i=0; NET_DVR_ALARMINFO m_alarminfo; //由NET_DVR_SetDVRMessCallBack()设置的回调函数fMessCallBack回调上来的一条报警信 //息的内容 memcpy(&m_alarminfo, pBuf, sizeof(NET_DVR_ALARMINFO)); //将pbuf中的消息读入NET_DVR_ALARMINFO结构体 switch (lCommand) { case COMM_ALARM: switch (m_alarminfo.dwAlarmType) { case 0: for (i=0; i<MAX_CHANNUM; i++) { if (m_alarminfo.dwAlarmRelateChannel[i] == 1) { printf("服务器%s发生信号量报警 报警输入%d,触发通道号%d\n", sDVRIP, m_alarminfo.dwAlarmInputNumber+1, i+nStartChan); } } break; case 1: for (i=0; i<MAX_DISKNUM; i++) { if (m_alarminfo.dwDiskNumber[i] == 1) { printf("服务器%s发生硬盘满报警 硬盘号%d \n", sDVRIP, i); } } break; case 2: for (i=0; i<MAX_CHANNUM; i++) { if (m_alarminfo.dwChannel[i] == 1) { printf("服务器%s发生信号丢失报警 通道号%d \n", sDVRIP, i+nStartChan); } } break; case 3: for (i=0; i<MAX_CHANNUM; i++) { if (m_alarminfo.dwChannel[i] == 1) { printf("服务器%s发生移动侦测报警 通道号%d \n", sDVRIP, i+nStartChan); } } break; case 4: for (i=0; i<MAX_DISKNUM; i++) { if (m_alarminfo.dwDiskNumber[i] == 1) { printf("服务器%s发生硬盘未格式化报警 硬盘号%d \n", sDVRIP, i); } } break; case 5: for (i=0; i<MAX_DISKNUM; i++) { if (m_alarminfo.dwDiskNumber[i] == 1) { printf("服务器%s发生读写硬盘出错报警 硬盘号%d \n", sDVRIP, i); } } break; case 6: for (i=0; i<MAX_CHANNUM; i++) { if (m_alarminfo.dwChannel[i] == 1) { printf("服务器%s发生遮挡报警 通道号%d \n", sDVRIP, i+nStartChan); } } break; case 7: for (i=0; i<MAX_CHANNUM; i++) { if (m_alarminfo.dwChannel[i] == 1) { printf("服务器%s发生制式不匹配 通道号%d \n", sDVRIP, i+nStartChan); } } break; case 8: printf("服务器%s发生非法访问! \n", sDVRIP); break; default: printf("报警类型:未知 %d\n",m_alarminfo.dwAlarmType); break; } break; case COMM_TRADEINFO: NET_DVR_TRADEINFO m_TradeInfo; memcpy(&m_TradeInfo, pBuf, sizeof(NET_DVR_TRADEINFO)); printf("服务器%s收到卡号信息%s \n", sDVRIP, m_TradeInfo.CardNumber); break; default: break; } } else {//异常 lIndex = *((int *)pBuf); switch (lCommand) { case EXCEPTION_EXCHANGE: printf("DVR[%s] network exception error:%d\n", sDVRIP, (*getlastError)()); break; case EXCEPTION_AUDIOEXCHANGE: printf("lIndex[%d] DVR[%s] vaudioexchange exception error:%d\n", lIndex, sDVRIP, (*getlastError)()); break; case EXCEPTION_ALARM: printf("lIndex[%d] DVR[%s] alarm exception error:%d\n", lIndex, sDVRIP, (*getlastError)()); break; case EXCEPTION_PREVIEW: printf("lIndex[%d] DVR[%s] preview exception error:%d\n", lIndex, sDVRIP, (*getlastError)()); break; case EXCEPTION_SERIAL: printf("lIndex[%d] DVR[%s] serial exception error:%d\n", lIndex, sDVRIP, (*getlastError)()); break; default: break; } } return 1;iRet = dlclose(FuncLib2);cerror();}void Wait(){ printf("\n======== Press 'q' to quit ============\n"); do { sleep(1); } while(getc(stdin) != 'q'); }void RealDataCallBack (int lRealHandle, //NET_DVR_RealPlay()的返回值 DWORD dwDataType, /*数据类型 #define NET_DVR_SYSHEAD 1 系统头数据 #define NET_DVR_STREAMDATA 2 流数据 #define NET_DVR_REALPLAYEXCEPTION 111 预览异常*/ char *pBuffer, //存放数据的缓冲区指针 DWORD dwBufSize, //缓冲区的大小 DWORD dwUser) //用户数据,就是上面输入的用户数据{ if(dwDataType == NET_DVR_SYSHEAD) { bGetSysHeader[dwUser] = 1; } if(dwDataType == NET_DVR_REALPLAYEXCEPTION) { fprintf(stderr, "RealPlay exception!\n"); } #ifdef ANALYZE AnalyzeDataInputData(dwUser, (unsigned char *)pBuffer, dwBufSize); //HIKANA_InputData(dwUser, (unsigned char *)pBuffer, dwBufSize);#else /* fprintf(stderr, "lRealHandle[%d]: Get StreamData! Type[%d], BufSize[%d], dwUser[%d]\n", lRealHandle, dwDataType, dwBufSize, dwUser);*/#endif}void RealPlay(NET_DVR_DEVICEINFO *devInfo) //参数为设备信息结构{ void *FuncLib1; FuncLib1 = dlopen("libhcnetsdk.so", RTLD_LAZY); cerror(); void *FuncLib2; FuncLib2 = dlopen("libanalyze.so", RTLD_LAZY); cerror(); void *FuncLib3; FuncLib3 = dlopen("libm4play.so", RTLD_LAZY | RTLD_GLOBAL); cerror(); int nMode = PTOPTCPMODE; //TCP模式 /*printf("Please input link mode: [0-PTOPTCPMODE; 1-PTOPUDPMODE; 3-PTOPRTPMODE]\n"); scanf("%d", &nMode );*/ int linkmode = nMode;//PTOPUDPMODE/*PTOPTCPMODE*/PTOPRTPMODE; int usesdl = TRUE; //int usesdl = FALSE; NET_DVR_CLIENTINFO clientInfo; //图像预览参数结构 clientInfo.lChannel = devInfo->byStartChan; clientInfo.lLinkMode = linkmode;/* | 0x80000000*///sub-stream: | 0x80000000 clientInfo.hPlayWnd.x = 0; //图像预览参数 横坐标 clientInfo.hPlayWnd.y = 0; //图像预览参数 纵坐标 INITINFO InitInfo; //画布大小结构,在Mplay4.h中定义 if (usesdl) { InitInfo.uWidth = CIF_WIDTH; InitInfo.uHeight = CIF_HEIGTH; } else { InitInfo.uWidth = 0; InitInfo.uHeight = 0; } typedef BOOL (*DRAWAREAINIT)(INITINFO);DRAWAREAINIT drawareainit = (DRAWAREAINIT)dlsym(FuncLib1, "NET_DVR_DrawAreaInit");cerror(); if(!(*drawareainit)(InitInfo)) //设定画布 { printf("DrawAreaInit failed\n"); return; } if(usesdl) { clientInfo.hPlayWnd.uWidth = CIF_WIDTH; clientInfo.hPlayWnd.uHeight = CIF_HEIGTH; } else { clientInfo.hPlayWnd.uWidth = 0; clientInfo.hPlayWnd.uHeight = 0; } clientInfo.sMultiCastIP = "234.5.6.7"; //多组播地址 int i; //char filename[80] = {0}; if (devInfo->byChanNum > 1) //判断是否多通道 (设为1) { VIEW_NUM = 1; } else { VIEW_NUM = 1; } for(i=0; i<VIEW_NUM; i++) { clientInfo.lChannel = devInfo->byStartChan + i; if(usesdl) { clientInfo.hPlayWnd.x = CIF_WIDTH * (i % 2) ; clientInfo.hPlayWnd.y = CIF_HEIGTH * (i / 2); }#ifdef ANALYZE AnalyzeDataOpenStream(i); //g_lAnalyzeHandle = HIKANA_CreateStream(1024*1024); pthread_t hMainThread; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); gbExit = 1; char reg[1] = {0}; if ( pthread_create( &hMainThread, &attr, GetFrameThread, (void *)&i ) < 0 ) { return ; } #endif typedef BOOL (*REALPLAY)(int,LPNET_DVR_CLIENTINFO);REALPLAY realplay = (REALPLAY)dlsym(FuncLib1, "NET_DVR_RealPlay");cerror(); glRealHandle[i] = (*realplay)(glUserID, &clientInfo); if(glRealHandle[i] < 0) { printf(" channel:%d RealPlay failed \n", clientInfo.lChannel); return ; } /* time_t t1,t2; while (1) { for (i=0; i<3; i++) { time(&t1); if (!NET_DVR_PTZControlWithSpeed(glRealHandle[i], PAN_LEFT, 0, 4)) { printf( "PTZ left start failed. error[%d]\n", NET_DVR_GetLastError() ); } else printf("PTZ left start...\n"); time(&t2); printf("spend time:%ds\n", t2-t1); sleep(1); time(&t1); if (!NET_DVR_PTZControlWithSpeed(glRealHandle[i], PAN_LEFT, 1, 4)) { printf( "PTZ left stop failed. error[%d]\n", NET_DVR_GetLastError() ); } else printf("PTZ left stop...\n"); time(&t2); printf("spend time:%ds\n", t2-t1); sleep(1); } for (i=0; i<3; i++) { time(&t1); if (!NET_DVR_PTZControlWithSpeed(glRealHandle[i], PAN_RIGHT, 0, 4)) { printf( "PTZ right start failed. error[%d]\n", NET_DVR_GetLastError() ); } else printf("PTZ right start...\n"); time(&t2); printf("spend time:%ds\n", t2-t1); sleep(1); time(&t1); if (!NET_DVR_PTZControlWithSpeed(glRealHandle[i], PAN_RIGHT, 1, 4)) { printf( "PTZ right stop failed. error[%d]\n", NET_DVR_GetLastError() ); } else printf("PTZ right stop...\n"); time(&t2); printf("spend time:%ds\n", t2-t1); sleep(1); } } */#if 1 typedef BOOL (*SAVEREALDATA)(int,char*);SAVEREALDATA saverealdata = (SAVEREALDATA)dlsym(FuncLib1 , "NET_DVR_SaveRealData");cerror(); (*saverealdata)(glRealHandle[i], "real.mp4"); //保存捕获到的数据到指定的文件 /* to set callback function */typedef BOOL (*SETREALDATACALLBACK)(int ,void(*fRealDataCallBack) (int, DWORD,char *,DWORD ,DWORD ),DWORD );SETREALDATACALLBACK setrealdatacallback = (SETREALDATACALLBACK)dlsym(FuncLib1 , "NET_DVR_SetRealDataCallBack"); cerror(); (*setrealdatacallback)(glRealHandle[i], RealDataCallBack, g_lAnalyzeHandle);cerror(); #endif /* DWORD BrightValue; DWORD ContrastValue; DWORD SaturationValue; DWORD HueValue; NET_DVR_ClientGetVideoEffect(glRealHandle[i], &BrightValue, &ContrastValue, &SaturationValue, &HueValue); */ //printf("RealHandle:%d BrightValue:%d ContrastValue:%d SaturationValue:%d HueValue:%d \n",glRealHandle[i], BrightValue, ContrastValue, SaturationValue, HueValue);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -