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

📄 hkvisiondlg.cpp

📁 基于海康威视DVR的完整远程监控程序
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// HKVisionDlg.cpp : implementation file
//
#include "stdafx.h"
#include "HKVision.h"
#include "HKVisionDlg.h"
#include "SelectDSP.h"
#include "settingsDlg.h"
#include "hikserver.h"
#include "hikvisionsdk.h"

#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <io.h>
#include <stdio.h>
#include <mmsystem.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define WM_MYCOMMAND (WM_USER+1000)  // 

#define DIALTYPE	0	//dialing
#define NORMATYPE	1	//Wan or Lan

#define UPDATE_COUNTERS		__T("Update Frame Counters")
#define DATA_READY			__T("Data ready")

BOOL bCapture = FALSE;

unsigned char StreamBuf[1000000];

int __cdecl StreamReadCallback(ULONG ChannelNum, void *Context);
typedef int (*STREAM_DIRECT_READ_CALLBACK)(ULONG channelNumber,void *DataBuf,DWORD Length,int FrameType,void *context);
int __cdecl StreamDirectReadCallback(ULONG channelNumber,void *DataBuf,DWORD Length,int FrameType,void *context);


unsigned char FileHeader[64][200];
int FileHeaderLen = 0;
int    gFileHandleQcif[MAX_CHANNELS];

int icount = 0;;

int KeyFrameInterva[10] = {25,67,76,100,100,100,100,121,211,211};
int FrameRate[10] = {25,25,25,25,20,10,5,4,3,2};
int IQ[10] = {10,10,12,15,18,20,20,22,24,24};
int PQ[10] = {10,10,12,15,18,20,20,22,24,24};
int BQ[10] = {10,15,15,20,21,23,23,25,27,27};
int nowstate[16];


int servertype;

UINT usframe = 0;

BOOL chantype = FALSE;

DWORD dcurrentwin=0;
int g_nChannelTotal;

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

int SplitRects(int width, int height, int cols, int rows, RECT *rectList);

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHKVisionDlg dialog

CHKVisionDlg::CHKVisionDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CHKVisionDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CHKVisionDlg)
	m_brightness = 0;
	m_contrast = 0;
	m_hue = 0;
	m_saturation = 0;
	m_iframes = 0;
	m_pbframes = 0;
	m_lostframes = 0;
	m_audioframes = 0;
	m_ip = _T("");
	m_sframe = FALSE;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CHKVisionDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CHKVisionDlg)
	DDX_Slider(pDX, IDC_SLIDER_BRIGHT, m_brightness);
	DDX_Slider(pDX, IDC_SLIDER_CONTRAST, m_contrast);
	DDX_Slider(pDX, IDC_SLIDER_HUE, m_hue);
	DDX_Slider(pDX, IDC_SLIDER_SAT, m_saturation);
	DDX_Text(pDX, IDC_IFRAMES, m_iframes);
	DDX_Text(pDX, IDC_PBFRAMES, m_pbframes);
	DDX_Text(pDX, IDC_LOST_FRAMES, m_lostframes);
	DDX_Text(pDX, IDC_AUDIOFRAMES, m_audioframes);
	DDX_Text(pDX, IDC_IP, m_ip);
	DDX_Check(pDX, IDC_CAPTURESFRAME, m_sframe);
	//}}AFX_DATA_MAP
}

static const UINT MsgUpdateCounters = ::RegisterWindowMessage(UPDATE_COUNTERS);
static const UINT MsgDataReady = ::RegisterWindowMessage(DATA_READY);

BEGIN_MESSAGE_MAP(CHKVisionDlg, CDialog)
	ON_MESSAGE(WM_MYCOMMAND,OnMyCommand)
	//{{AFX_MSG_MAP(CHKVisionDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_START, OnStart)
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_STOP, OnStop)
	ON_BN_CLICKED(IDC_PREVIEW, OnPreview)
	ON_WM_CTLCOLOR()
	ON_WM_SHOWWINDOW()
	ON_WM_MOVE()
	ON_WM_HSCROLL()
	ON_WM_VSCROLL()
	ON_WM_LBUTTONDOWN()
	ON_BN_CLICKED(IDC_SETTINGS, OnSettings)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_CAPIMAGE, OnCapimage)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
	ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
	ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
	ON_BN_CLICKED(IDC_BUTTON8, OnButton8)
	ON_BN_CLICKED(IDC_CAPTURESFRAME, OnCapturesframe)
	ON_BN_CLICKED(IDC_BUTTON9, OnButton9)
	ON_BN_CLICKED(IDC_SUBSTART, OnSubstart)
	ON_REGISTERED_MESSAGE(MsgUpdateCounters, OnUpdateCounters)
	ON_REGISTERED_MESSAGE(MsgDataReady, OnDataReady)
	ON_BN_CLICKED(IDC_STOPSUBCAP, OnStopsubcap)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHKVisionDlg message handlers
int LogWrite(const char *Format, ...);

HANDLE ChannelHandle[MAX_CHANNELS];
//int    logFile;
int    gFileHandle[MAX_CHANNELS];
BOOLEAN dlgInited = FALSE;
HWND gMainWnd;
RECT rectList[MAX_CHANNELS];
BOOL bDdrawMode = FALSE;
ULONG   gChannelFrames[MAX_CHANNELS];
ULONG   gChannelTotalLength[MAX_CHANNELS];
ULONG	gChannelFramesLost[MAX_CHANNELS];
ULONG	gChannelOverflow[MAX_CHANNELS];
ULONG	gCurrentFileLen[MAX_CHANNELS];
ULONG	gCaptureStartedNum = 0;
UINT    gTimer;
UINT    gCapImages;
ULONG	gFileSize = 100; // 100 mb
int		gFileNum = 0;

COLORREF gBackgroundColor = RGB(10, 10, 10);
extern int iLastSelect;

ULONG StartTime;
ULONG LastTime;
///add ver3.4 by h
//int    gFileHandleQcif[MAX_CHANNELS];					//子通道同时编码时qcif录象文件句柄
unsigned char FileHeaderQcif[MAX_CHANNELS][200];		//QCIF Header,when 
BOOL bEncodeCifAndQcif[MAX_CHANNELS];					//子通道同时编码时通道标志


void CALLBACK StartCap(int port)
{
	StartVideoCapture(ChannelHandle[port]);	
}


void CALLBACK StopCap(int port)
{
	StopVideoCapture(ChannelHandle[port]);
}

int CALLBACK CheckIP(DWORD nChannel,char* nIP)
{
/*
	CString ctemp;
	ctemp.Format("%s",nIP);

	if (ctemp == "192.0.0.215")
		return -1;
*/
	return 0;

}

int CALLBACK checkpassword(char *username,WORD namelen,char *password,WORD passlen)
{
/*
	if ((username[0] == '1') && (username[1] == '2') && (username[2] == '3') && (password[0] == 'w'))
		return 0;
	else
		return -1;
*/
	return 0;

}


void CALLBACK SetIBP(int port,int framerat)
{
	SetIBPMode(ChannelHandle[port],211,2,2,framerat);
}

void CALLBACK MakeIFrame(int port)
{
	CaptureIFrame(ChannelHandle[port]);
}


BOOL CHKVisionDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	int i;


	servertype = NORMATYPE;
	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);
	m_brush1.CreateSolidBrush(gBackgroundColor);   
	m_brush2.CreateSolidBrush(RGB(0,0,0));   
	m_bLog = FALSE;

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here

	m_brightness = 50;
	m_contrast = 50;
	m_hue = 0;
	m_saturation = 50;
	m_iframes = 0;
	m_pbframes = 0;
	m_audioframes = 0;
	m_lostframes = 0;

	for(i = 0; i < GetTotalDSPs(); i++){
		m_bDspPreset[i] = TRUE;
		bEncodeCifAndQcif[i] = FALSE;
		gFileHandle[i] = -1;
	}
	m_sframe = FALSE;

	UpdateData(FALSE);

	if(InitDSPs() < 0){
		//afxDump<<"error:can not init DSPs\n";
		AfxMessageBox("can not init DSPs\n");
		return FALSE;
	}

	CString title;
	CString dsps;
	dsps.Format("    %d channels Found", GetTotalDSPs());
	GetWindowText(title);
	title += dsps;
	SetWindowText(title);

	dlgInited = TRUE;

	m_VideoWin.SubclassDlgItem(IDC_VIDEOWIN, this);

	for(i = 0; i < GetTotalDSPs(); i++)
	{
		ChannelHandle[i] = ChannelOpen(i);
		if (ChannelHandle[i]<0) 
		{
			AfxMessageBox("channel open error > 0");

		}
		else if (ChannelHandle[i] ==(HANDLE) 0xffff)
		{
			AfxMessageBox("channel open error 0xffff");

		}

		gChannelTotalLength[i] = 0;
		nowstate[i]=0;

		if (servertype == DIALTYPE)
		{
			SetIBPMode(ChannelHandle[i],211,2,1,8);
			SetDefaultQuant(ChannelHandle[i],18,18,23);
			SetStreamType(ChannelHandle[i],STREAM_TYPE_VIDEO);
		}
		else
		{
			SetIBPMode(ChannelHandle[i],100,2,1,25);
			SetDefaultQuant(ChannelHandle[i],15,15,20);
		}
	}

	if (servertype == DIALTYPE)
	{
		for(i = 0; i < GetTotalDSPs(); i++)
			SetEncoderPictureFormat(ChannelHandle[i], ENC_QCIF_FORMAT);

	}
	else
	{

		for(i = 0; i < GetTotalDSPs(); i++)
		{
			if ( i==0 )
			{
				//when initiated,set the first channel as 4CIF encode,others as CIF
				SetEncoderPictureFormat(ChannelHandle[0], ENC_4CIF_FORMAT);
				bEncodeCifAndQcif[0] = FALSE;
			}
			else
			{
				SetEncoderPictureFormat(ChannelHandle[i], ENC_CIF_FORMAT);
			}

		}

	}

//	int id = IDC_CHECK2;
//	for(i = 0; i < MAX_CHANNELS; i++){
//		GetDlgItem(id + i)->EnableWindow(FALSE);
//	}
	RegisterStreamDirectReadCallback(::StreamDirectReadCallback,this);
	RegisterMessageNotifyHandle(m_hWnd, MsgDataReady);


	MP4_ServerSetMessage(WM_MYCOMMAND,this->m_hWnd);

	gCapImages = 0;

	SetOverlayColorKey(gBackgroundColor);

	gTimer = SetTimer(1, 1000, 0);
	SetTimer(2,2000,0);
	SetTimer(5,5000,0);

	for (i=0;i<MAX_CHANNELS;i++)
		gCurrentFileLen[i] = 0;

	SERVER_VIDEOINFO videoinfo;
	g_nChannelTotal = GetTotalDSPs();

	for( i=0 ; i < g_nChannelTotal; i++ )
	{
		if(i == 0)
		{
			MP4_ServerSetBufNum(i,90);
		}
		else
		{
			MP4_ServerSetBufNum(i,80);
		}
		
		if (servertype == DIALTYPE)
			videoinfo.m_datatype[i] = DIALING;
		else
			videoinfo.m_datatype[i] = NORMAL;
	}

	videoinfo.m_datatype[0] = SMALLPIC;

	videoinfo.m_channum = g_nChannelTotal;
	videoinfo.m_waittime = 2;

	MP4_ServerSetStart(StartCap);
	MP4_ServerSetStop(StopCap);

	MP4_ServerSetIBPMode(SetIBP);
	MP4_ServerSetCapIFrame(MakeIFrame);

	MP4_ServerSetTTL(64);
	MP4_ServerSetNetPort(5050,6050);

	MP4_ServerCheckIP(CheckIP);
	MP4_ServerCheckPassword(checkpassword);

	//set the max connector of  channel 0
	MP4_ServerMaxUser(0,24);

	//如果想不使用缺省方式进行多播,
	//可以调用下面的函数设置自己的多播信息
	//详细信息请参考SDK文档
//	MP4_ServerCastGroup(TRUE,0,"228.0.0.132",9988);

	if (!MP4_ServerStart(&videoinfo))
	{
		MessageBox("error","error",MB_OK);
	}

	StartTime = timeGetTime();


	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CHKVisionDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{

⌨️ 快捷键说明

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