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

📄 demodlg.cpp

📁 采集卡demo
💻 CPP
📖 第 1 页 / 共 4 页
字号:
//		SetupBitrateControl(ChannelHandle[0], 128000);	
//		SetupSubChannel(ChannelHandle[0], 0);						//change to main channel

		for(int i = 0; i < GetTotalDSPs(); i++){
			if(m_bDspPreset[i]){
				char fileName[256];
				CTime m_StartTime1=CTime::GetCurrentTime();
				CString csStartTime=m_StartTime1.Format("%Y%m%d%H%M%S");
				sprintf(fileName, "ch%02d_%s.264", i,csStartTime);

				gFileHandle[i] = _open(fileName, _O_CREAT | _O_BINARY | _O_WRONLY| _O_TRUNC, _S_IREAD | _S_IWRITE);
				if(gFileHandle[i] == -1){
					TRACE("file open error\n");
					AfxMessageBox("can not open file\n");
					return;
				}
///add v34
				if (bEncodeCifAndQcif[i])
				{
					sprintf(fileName, "stream_sub_%d_%d.264", i, gFileNum++);
					gFileHandleQcif[i] = _open(fileName, _O_CREAT | _O_BINARY | _O_WRONLY| _O_TRUNC, _S_IREAD | _S_IWRITE);
					if(gFileHandleQcif[i] == -1){
						TRACE("file open error\n");
						AfxMessageBox("can not open file\n");
						return;
					}
				}
				gChannelFrames[i] = 0;
				gChannelTotalLength[i].QuadPart = 0;
				gChannelFramesLost[i] = 0;
				gChannelOverflow[i] = 0;
				gCurrentFileLen[i] = 0;
				StartVideoCapture(ChannelHandle[i]);	
				// could not be start again untill stopped first
				//m_bDspPreset[i] = FALSE;
				gCaptureStartedNum++;
				// let the threads have chance to run
				//Sleep(500);
			}else
				gFileHandle[i] = -1;
			// note:must bigger than keyframeintervals
			alarmFile[i].Reset();

			//			以下是对Setup()的说明:
			//			第一个参数5表示预录5秒钟的音视频数据,这个数据是移动侦测侦上来以前的数据,也就是报警前数据
			//			第二个参数无效,同时把int AlarmFile::Trigger(int fileHandle)
			//			里面的	if(startWrite){
			//					framesToWriteImmediatly = framesToStopWrite;
			//					canStopWrite = FALSE;
			//				}
			//			这一段代码予以了屏蔽,关于警后数据的设置及其设置请参考回调函数
			//			int __cdecl StreamDirectReadCallback(ULONG channelNum,void *DataBuf,DWORD Length,int frameType,void *context)
			//			里面if(bAlarmFileSave)部分
			//
			//
			alarmFile[i].Setup(5*(25+25), 0); 

		}
		StartTime = timeGetTime();
		LastTime = timeGetTime();
		bStartCapture = TRUE;
		if(gCaptureStartedNum){
			GetDlgItem(IDC_STOP)->EnableWindow(TRUE);
			GetDlgItem(IDC_START)->EnableWindow(FALSE);
			//GetDlgItem(IDC_SETTINGS)->EnableWindow(FALSE);
		}
	}
}

void CHKVisionDlg::LogMessage(const unsigned char *buf, void *context)
{
	if(!IsWindow(this->m_hWnd))return;

	int len = m_Logwin.GetWindowTextLength();

	CString str;
	 
	if (len >= 16384)
	{
		m_Logwin.GetWindowText(str);
		m_Logwin.SetWindowText(str.GetBuffer(len)+16384);
		len -= 16384;
	}

	str = buf;

	m_Logwin.SetSel(len, len);

	m_Logwin.ReplaceSel(str);

}

void CHKVisionDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	m_bLog = FALSE;
	OnButton11();
	KillTimer(gTimer);
	KillTimer(m_MatrixSwitch);
	if (bAudioPre)	//close
	{
		for(int i=0; i < GetTotalDSPs(); i++)
		{
			SetAudioPreview(ChannelHandle[i],FALSE);
		}
		bAudioPre = FALSE;
	}

	if(gCaptureStartedNum)
		OnStop();

	StopVideoPreview();
	for(int i= 0 ;i < GetTotalDSPs(); i++)
	{
		ChannelClose(ChannelHandle[i]);
	}
	gCaptureStartedNum = 0;

	DeInitDSPs();	


	_close(logFile);
	CDialog::OnClose();
}



void CHKVisionDlg::OnStop() 
{
	// TODO: Add your control notification handler code here
	CSelectDSP selDialog;
	selDialog.DSPPreset(m_bDspPreset, GetTotalDSPs());

	if(selDialog.DoModal() == IDOK)
	{	LogWrite("Stop capture\r\n");
		TRACE("SelectDSP:OK button clicked\n");
		selDialog.GetDSPsChoiced(m_bDspPreset);
		for(int i = 0; i < GetTotalDSPs(); i++){
			if(m_bDspPreset[i]){
				ASSERT(gFileHandle[i] != -1);
				StopVideoCapture(ChannelHandle[i]);
				//lseek(gFileHandle[i], 0, SEEK_SET);
				//FRAMES_STATISTICS fs;
				//GetFramesStatistics(ChannelHandle[i], &fs);
				//ULONG frames = fs.AudioFrames + fs.VideoFrames;
				//TRACE("channel %i has %x frames written\n", i, frames);
#define END_CODE 0x00000002
				ULONG endCode = END_CODE;
				_write(gFileHandle[i], &endCode, sizeof(ULONG));
				_close(gFileHandle[i]);
				gFileHandle[i] = 0;
///add v34
				if (bEncodeCifAndQcif[i])
				{
					_close(gFileHandleQcif[i]);
					gFileHandleQcif[i] =0;
				}
				gCaptureStartedNum--;
			}
		}
	
		bStartCapture = FALSE;
//		bTestData = FALSE;
		if(!gCaptureStartedNum){
			GetDlgItem(IDC_START)->EnableWindow(TRUE);
			//GetDlgItem(IDC_SETTINGS)->EnableWindow(TRUE);
			GetDlgItem(IDC_STOP)->EnableWindow(FALSE);
		}
	}
}

void CHKVisionDlg::StartVideoPreview()
{
	RECT rc;
	GetClientRect(&rc);
	//::StartVideoPreview(ChannelHandle[0], GetDlgItem(IDC_VIDEOWIN)->m_hWnd, &rc, FALSE, vdfYUV422Planar, 25);
}

void CHKVisionDlg::StopVideoPreview()
{
	TRACE("StopVideoPreview\n");
	for(int i = 0; i < GetTotalDSPs(); i++){
		::StopVideoPreview(ChannelHandle[i]);
	}
}

void CHKVisionDlg::OnPreview() 
{
	// TODO: Add your control notification handler code here
	static BOOLEAN bVideoStarted = FALSE;
	if(bVideoStarted){
		StopVideoPreview();
		bVideoStarted = FALSE;
	}else{
		StartVideoPreview();
		bVideoStarted = TRUE;
	}
}




HBRUSH CHKVisionDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	
	// TODO: Change any attributes of the DC here
	if((nCtlColor== CTLCOLOR_EDIT) && (pWnd->GetDlgCtrlID() == IDC_EDIT1)){
		pDC->SetTextColor (RGB (255, 0 , 0));
		pDC->SetBkColor(gBackgroundColor);
		hbr = (HBRUSH) m_brush1;
	}

	if((nCtlColor== CTLCOLOR_EDIT) && (pWnd->GetDlgCtrlID() == IDC_LOGWIN)){
		pDC->SetTextColor (RGB (255, 0 , 0));
		pDC->SetBkColor(RGB(0, 0, 0));
//		pDC->SetBkColor(RGB(255, 0, 255));
		hbr = (HBRUSH) m_brush2;
	}
	
	// TODO: Return a different brush if the default is not desired
	return hbr;
}

void CHKVisionDlg::OnShowWindow(BOOL bShow, UINT nStatus) 
{
	CDialog::OnShowWindow(bShow, nStatus);
	
	// TODO: Add your message handler code here
	TRACE("Onshowwindow m_hwnd is %x\n", m_hWnd);

	m_bLog = TRUE;


}

void CHKVisionDlg::OnMove(int x, int y) 
{
	CDialog::OnMove(x, y);

	if(!dlgInited)return;  
	
	// TODO: Add your message handler code here

	::SendMessage(GetDlgItem(IDC_VIDEOWIN)->m_hWnd, WM_PAINT, 0, 0);

	
}


void CHKVisionDlg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	// TODO: Add your message handler code here and/or call default
	
	CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}

void CHKVisionDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	// TODO: Add your message handler code here and/or call default
	static int bright=128, contrast=64, sat=64, hue=0;
	VideoStandard_t standard;
	int i;
	
	if((GetDlgItem(IDC_SLIDER_BRIGHT) == pScrollBar) && (nSBCode != SB_ENDSCROLL)){
		/*for(int i = 0; i < GetTotalDSPs(); i++)*/{
			if(iLastSelect == -1)
				i = 0;
			else i = iLastSelect;
			bright = (nPos * 255) / 100;
			TRACE("b=%i c=%i s=%i h=%i\n", bright, contrast, sat, hue);
			SetVideoPara(ChannelHandle[i], bright, contrast, sat, hue);
		}
	}

	if((GetDlgItem(IDC_SLIDER_CONTRAST) == pScrollBar) && (nSBCode != SB_ENDSCROLL)){
		/*for(int i = 0; i < GetTotalDSPs(); i++)*/{
			if(iLastSelect == -1)
				i = 0;
			else i = iLastSelect;
			contrast = (nPos * 127) / 100;
			TRACE("b=%i c=%i s=%i h=%i\n", bright, contrast, sat, hue);
			SetVideoPara(ChannelHandle[i], bright, contrast, sat, hue);
		}
	}

	if((GetDlgItem(IDC_SLIDER_SAT) == pScrollBar) && (nSBCode != SB_ENDSCROLL)){
		/*for(int i = 0; i < GetTotalDSPs(); i++)*/{
			if(iLastSelect == -1)
				i = 0;
			else i = iLastSelect;
			sat = (nPos * 127) / 100;
			TRACE("b=%i c=%i s=%i h=%i\n", bright, contrast, sat, hue);
			SetVideoPara(ChannelHandle[i], bright, contrast, sat, hue);
		}
	}

	if((GetDlgItem(IDC_SLIDER_HUE) == pScrollBar) && (nSBCode != SB_ENDSCROLL)){
		/*for(int i = 0; i < GetTotalDSPs(); i++)*/{
			if(iLastSelect == -1)
				i = 0;
			else i = iLastSelect;
			hue = (nPos * 255) / 100;
			TRACE("b=%i c=%i s=%i h=%i\n", bright, contrast, sat, hue);
			SetVideoPara(ChannelHandle[i], bright, contrast, sat, hue);
		}
	}


	CDialog::OnVScroll(nSBCode, nPos, pScrollBar);
}





int CHKVisionDlg::OnUpdateCounters(UINT wparam, INT lparam)
{
	CString str;
	str.Format("%i", wparam);
	GetDlgItem(IDC_IFRAMES)->SetWindowText((LPCTSTR)str);
	
	return 0;
}



void CHKVisionDlg::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CDialog::OnLButtonDown(nFlags, point);
}


unsigned char FileHeader[64][200];
int FileHeaderLen;


int  GetIdFromPacket(int frameType,	UCHAR *StreamBuf);
int CHKVisionDlg::OnDataReady(UINT channelNum, INT lparam)
{
	ULONG length = 500000;
	int frameType;
	int status;
	BOOL breakable;
	static BOOL iframefileOpened = FALSE;
	int nframetype =0;
	// TRACE("OnDataReady\n");
	// if cap images we need clean the queue here
//	if(!gCaptureStartedNum && !gSignleCaptureNum) 
//		return 0;
//	do
//	{
		length = 1000000;
	 status = ReadStreamData(ChannelHandle[channelNum], StreamBuf,  &length, &frameType);
	 // error occured
	 if(status < 0){
		 TRACE("error:ReadStreamData %x\n", status);
		 LogWrite("channel %d ReadStreamData error , code=%x\r\n", channelNum, status);
		 return 0;
	 }
///add v34 
// nframetype value:	 1 cif head; 2 qcif head; 3 cif frame; 4 qcif frame; 5 audio frame; 0 others
//we must process stream separate while encoding cif and qcif at same time
//stream of cif is saved to cif files, qcif's saved to qcif files, audio stream is saved to all
	 if (bEncodeCifAndQcif[channelNum])
		nframetype = GetSubChannelStreamType(&StreamBuf,frameType);


	 breakable = status;
	 // no errors
	 if(frameType > 0) {
		 if(frameType == PktSysHeader){
			 if (bEncodeCifAndQcif[channelNum])
			 {
				 if (nframetype == 1)  //get cif file header
				 {
					memcpy(FileHeader[channelNum],StreamBuf,length);
					FileHeaderLen = length;
				 }
				 else if (nframetype ==2)	//get qcif or qqcif file header
				 {
					memcpy(FileHeaderQcif[channelNum], StreamBuf, length);
					TRACE("Ch%02d get Qcifhead or QQcifhead!\n",channelNum);
					FileHeaderLen = length;
				 }
			 }
			 else
			 {
				 // store the file header
				 memcpy(FileHeader[channelNum], StreamBuf, length);
				 FileHeaderLen = length;
				 //return 0;
			 }
		 }
		 if(frameType == PktMotionDetection){
			int result[4];
			// channel 1 set as qcif encoding
			// others set as cif encoding
			//if(channelNum == 0)
//				m_VideoWin.DrawVect(channelNum, (char *)StreamBuf);
			//else 
			m_bMoving[channelNum] = TRUE;
			memcpy(motionData[channelNum], StreamBuf,length);
//			TRACE("motion data received\n");

			 MotionAnalyzer(ChannelHandle[channelNum], (char *)StreamBuf, 2, result);
			 
			 if((result[0] + result[1] + result[2] + result[3])){
				 if (channelNum ==0)
					TRACE("CH=%d motion detect result %d %d %d %d\n", channelNum,result[0], result[1], result[2], result[3]);
				 alarmFile[channelNum].Trigger(gFileHandle[channelNum]);
			 }
			 return 0;
		 }
		 if((frameType == PktSFrames) && gSignleCaptureNum){
			 // single i frame
			 TRACE("iframe cap\n");
			 if(!iframefileOpened){
				 gIFrameFileHandle = _open("iframe.264", _O_CREAT | _O_BINARY | _O_WRONLY| _O_TRUNC, _S_IREAD | _S_IWRITE);
				 _write(gIFrameFileHandle, FileHeader[channelNum], FileHeaderLen);
				 iframefileOpened = TRUE;
			 }
			 _write(gIFrameFileHandle, StreamBuf, length);
			 gSignleCaptureNum--;
			 // Note:this frame should be store in the normal file
		 }

	 }
//	 else
//		 return 0;
 
	 if(length == 0){
		 TRACE("no data ?\n");
		 return 0;
	 }
//	DWORD ID = GetIdFromPacket(frameType,	StreamBuf);


	ULONG currentTime = timeGetTime();

///add v34 
	if (bEncodeCifAndQcif[channelNum] )
	{
		if (nframetype ==3  || nframetype ==5)     //we just sum cif file length 
		{
			gChannelTotalLength[channelNum].QuadPart += length;
			gCurrentFileLen[channelNum] += length;
		}
	}
	else
	{
		gChannelTotalLength[channelNum].QuadPart += length;
		gCurrentFileLen[channelNum] += length;
	}

	if(ULONG s = (currentTime - StartTime)/1000){
		CString str;
		if(iLastSelect != -1)
			str.Format("%d", (gChannelTotalLength[iLastSelect].QuadPart /s)*8);
		else 
			str.Format("%d", (gChannelTotalLength[0].QuadPart /s)*8);

		//GetDlgItem(IDC_BPS)->SetWindowText((LPCTSTR)str);
	}

	if((gCurrentFileLen[channelNum] > gFileSize * 1000 * 1000) && breakable && bStartCapture){
		TRACE("switch file:frametype=%x\n", frameType);
		_close(gFileHandle[channelNum]);
//add v34, qcif file close
		if (bEncodeCifAndQcif[channelNum])
			_close(gFileHandleQcif[channelNum]);
		// create new file
		char fileName[256];
		CTime m_StartTime1=CTime::GetCurrentTime();
		CString csStartTime=m_StartTime1.Format("%Y%m%d%H%M%S");
		sprintf(fileName, "ch%02d_%s.264", channelNum,csStartTime);
		gFileHandle[channelNum] = _open(fileName, _O_CREAT | _O_BINARY | _O_WRONLY| _O_TRUNC, _S_IREAD | _S_IWRITE);
		// write the header first
		_write(gFileHandle[channelNum], FileHeader[channelNum], FileHeaderLen);
//add v34, qcif file create
		if (bEncodeCifAndQcif[channelNum])
		{
			sprintf(fileName, "ch%02d_%s_sub.264", channelNum,csStartTime);
			gFileHandleQcif[channelNum] = _open(fileName, _O_CREAT | _O_BINARY | _O_WRONLY| _O_TRUNC, _S_IREAD | _S_IWRITE);
			// write the header first
			_write(gFileHandleQcif[channelNum], FileHeaderQcif[channelNum], FileHeaderLen);
		}

⌨️ 快捷键说明

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