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

📄 demodlg.cpp

📁 采集卡demo
💻 CPP
📖 第 1 页 / 共 4 页
字号:
				::StopVideoPreview(ChannelHandle[i]);
		}
	}
}

void CHKVisionDlg::OnCapiframe() 
{
	// TODO: Add your control notification handler code here

	gSignleCaptureNum++;

	CaptureIFrame(ChannelHandle[iLastSelect]);

}
	


BOOL CHKVisionDlg::PreCreateWindow(CREATESTRUCT& cs) 
{
	// TODO: Add your specialized code here and/or call the base class
	cs.style = WS_EX_TOPMOST|WS_POPUP ; 
	return CDialog::PreCreateWindow(cs);
}



int __cdecl StreamReadCallback(ULONG ChannelNum, void *Context)
{
	CHKVisionDlg *hkdlg = (CHKVisionDlg *)Context;
	hkdlg->OnDataReady(ChannelNum, 0);
	return 0;
}


//typedef int (*STREAM_DIRECT_READ_CALLBACK)(ULONG channelNumber,void *DataBuf,DWORD Length,int FrameType,void *context);
int __cdecl StreamDirectReadCallback(ULONG channelNum,void *DataBuf,DWORD Length,int frameType,void *context)
{

	BOOL breakable;

	int nframetype =0;
	CHKVisionDlg *hkdlg = (CHKVisionDlg *)context;

	if(frameType >0)
	{
		if (frameType == PktSysHeader)
		{
			memcpy(FileHeader[channelNum],DataBuf,Length);
			FileHeaderLen = Length;
		}
		else if (frameType == PktSubSysHeader)
		{
			memcpy(FileHeaderQcif[channelNum],DataBuf,Length);
			FileHeaderLen = Length;
		}
		if (frameType == PktMotionDetection)
		{
			int result[4];

			if(bOverlayMode)
			{
				hkdlg->m_VideoWin.DrawVectEx(channelNum, (char *)DataBuf);
			}
			else
			{
				hkdlg->m_bMoving[channelNum] = TRUE;
				memcpy(motionData[channelNum],(char*)DataBuf,Length);
			}
			
			MotionAnalyzer(ChannelHandle[channelNum],(char*)DataBuf,15,result);
			if((result[0] + result[1] + result[2] + result[3]))
			{
				if (channelNum ==0)
					TRACE("!!!!!!!!!!!!!!!!CH=%d motion detect result is %d %d %d %d\n",channelNum,result[0],\
					result[1],result[2],result[3]);
				// Trigger函数的作用是把报警前的数据按setup里面的设置,把相应的警前帧写入文件
				if(!hkdlg->baftermotion[channelNum])
					alarmFile[channelNum].Trigger(gFileHandle[channelNum]);
				//每次有移动侦测帧上来,自动把ualreadywriteframes[channelNum]清为0 ,为写入警后帧作准备
				hkdlg->ualreadywriteframes[channelNum]=0;
				hkdlg->baftermotion[channelNum]=TRUE;
			}
			return 0;
		}
		if (frameType == PktIFrames || frameType ==PktSubIFrames)
			breakable = TRUE;
		else
			breakable = FALSE;

	}
	gChannelTotalLength[channelNum].QuadPart +=Length;
	gCurrentFileLen[channelNum] +=Length;
	if ((gCurrentFileLen[channelNum] > gFileSize*1000*1000) && (breakable) )
	{
		char fileName[256];
		CTime m_StartTime1=CTime::GetCurrentTime();
		CString csStartTime=m_StartTime1.Format("%Y%m%d%H%M%S");
		
		
		if (gFileHandle[channelNum])
		{
			_close(gFileHandle[channelNum]);
			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(gFileHandle[channelNum],FileHeader[channelNum],FileHeaderLen);
		}
		if (gFileHandleQcif[channelNum])
		{
			_close(gFileHandleQcif[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(gFileHandleQcif[channelNum],FileHeaderQcif[channelNum],FileHeaderLen);
		}
		gCurrentFileLen[channelNum] -=gFileSize*1000*1000;
	}
	if(bAlarmFileSave)
	{
		if(frameType == PktSysHeader)
			_write(gFileHandle[channelNum],DataBuf,Length);
		else
		{
			//统计移动侦测帧上来之后的音视频数据,统计它们 ,并把它们写入文件,每一次有 新的移动侦测数据上来,
			//ualreadywriteframes都会先清0,然后统计写入文件的音视频数据
			hkdlg->ualreadywriteframes[channelNum] += hkdlg->TypeToFrames((FrameType_t)frameType);
			if (hkdlg->ualreadywriteframes[channelNum] <6*(25+25)) //录制移动侦测帧上来之后 6秒钟的音视频数据,也就是警后数据
				_write(gFileHandle[channelNum],DataBuf,Length);
			else
			{
				//  如果移动侦测上来之后,写入文件的音视频数据已经足够,那么把接下来上来的音视频数据压缓冲,
				//  压入缓冲的数据有两种处理方式:1 如果缓冲满了,自动被删除;2 作为下次报警上来的警前帧写入文件; 
				alarmFile[channelNum].FramePush(gFileHandle[channelNum],(unsigned char *)DataBuf,Length,(FrameType_t)frameType,breakable);
				hkdlg->baftermotion[channelNum] = FALSE;
			}
		}
	}
	else
		{
			if(frameType ==PktAudioFrames)
			{
				_write(gFileHandle[channelNum],DataBuf,Length);
				_write(gFileHandleQcif[channelNum],DataBuf,Length);
								
			}else if (frameType ==PktSubIFrames || frameType ==PktSubPFrames || frameType == PktSubBBPFrames || frameType == PktSubSysHeader)
			{
				
				_write(gFileHandleQcif[channelNum],DataBuf,Length);
				
			}else 
			{
				_write(gFileHandle[channelNum],DataBuf,Length);
				
			}
		}
		return 0;
}

void CHKVisionDlg::OnButton7() 
{
	// TODO: Add your control notification handler code here
	for(int i=0; i < GetTotalChannels(); 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_sub.264", i,csStartTime);
		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 ;
		}
		SetSubStreamType(ChannelHandle[i],STREAM_TYPE_AVSYNC);
//		SetSubEncoderPictureFormat(ChannelHandle[i],ENC_CIF_FORMAT);
		SetupSubChannel(ChannelHandle[i],1);// switch to sub channel for setting
		SetBitrateControlMode(ChannelHandle[i], brVBR );
		SetupBitrateControl(ChannelHandle[i],2000000);
		SetDefaultQuant(ChannelHandle[i], 16, 16, 18);
		SetIBPMode(ChannelHandle[i],100,2,2,25);
		StartSubVideoCapture(ChannelHandle[i]);
		SetupSubChannel(ChannelHandle[i],0);//switch to main channel
	}
	GetDlgItem(IDC_SUBSTART)->EnableWindow(FALSE);
	GetDlgItem(IDC_SUBSTOP)->EnableWindow(TRUE);
}

void CHKVisionDlg::OnSubstop() 
{
	// TODO: Add your control notification handler code here
	for(int i=0; i < GetTotalChannels(); i++)
	{
		StopSubVideoCapture(ChannelHandle[i]);
		_close(gFileHandleQcif[i]);
		gFileHandleQcif[i] = 0;
	}
	GetDlgItem(IDC_SUBSTART)->EnableWindow(TRUE);
	GetDlgItem(IDC_SUBSTOP)->EnableWindow(FALSE);
	
}

void CHKVisionDlg::StopDrawFun(DWORD nport)
{
	StopRegisterDrawFun(nport);

}

#define WIDTH	704
#define HEIGHT	576
#define  nSize WIDTH*HEIGHT*3/2
static BOOL bFirstCapimage = TRUE;
static FILE *pImageFile = NULL;
unsigned char imageBuf[nSize];
static UINT count=0;
static ULONGLONG imageSize =0;

void ImageStreamCallback(UINT channelNumber,void * Context)
{
	if(pImageFile)
	{
		printf("Demo have received image!channel:%u\n",channelNumber);
		fwrite(imageBuf,1,HEIGHT*WIDTH*3/2,pImageFile);
		imageSize +=WIDTH*HEIGHT*3/2;
		if (imageSize >=100*1000*1000)
		{
			fclose(pImageFile);
			if (count >=300)
				count =0;
			char name[50];
			sprintf(name,"image%02u_%04u.yuv",0,count++);
			pImageFile=fopen(name,"wb");
			imageSize =0;
		}
	}
	else
		printf("error open file\n");
	Sleep(1);
}

void CHKVisionDlg::OnStartImageCap() 
{
	// TODO: Add your control notification handler code here
		if (bFirstCapimage)
	{
		char name[50];
		sprintf(name,"image%02u_%04u.yuv",0,count++);
		pImageFile=fopen(name,"wb");
		bFirstCapimage = FALSE;
	}
	printf("SetImageStream start\n");
	SetImageStream(ChannelHandle[0],1,0,WIDTH,HEIGHT,imageBuf);

	if(!bFirstCapimage){
			GetDlgItem(IDC_BUTTON5)->EnableWindow(FALSE);
			GetDlgItem(IDC_BUTTON6)->EnableWindow(TRUE);
			}
	return ;
}

void CHKVisionDlg::OnStopImageCap() 
{
	// TODO: Add your control notification handler code here
	printf("SetImageStream stop\n");
	SetImageStream(ChannelHandle[0],0,0,0,0,0);
	bFirstCapimage = TRUE;
	fclose(pImageFile);
	count =0;
	if(bFirstCapimage){
			GetDlgItem(IDC_BUTTON5)->EnableWindow(TRUE);
			GetDlgItem(IDC_BUTTON6)->EnableWindow(FALSE);
			}
	return ;
}

void CHKVisionDlg::OnButton8() 
{
	// TODO: Add your control notification handler code here
	CCSetupDec dlg;
	int iArea ;
	int iDecNum=0;//第一块解码卡的第0路视频输出
	if (dlg.DoModal() ==IDOK)
	{
	
		if (dlg.m_Previewtype==0)
			iArea =1;
		else if (dlg.m_Previewtype ==1)
			iArea =4;
		else if (dlg.m_Previewtype==2)
			iArea =16;
		else if (dlg.m_Previewtype==3)
			iArea =2;
		else if (dlg.m_Previewtype ==4)
			iArea =3;
	}

	SetupRegion(iDecNum,iArea);

	
}

void CHKVisionDlg::OnButton9() 
{
	// TODO: Add your control notification handler code here
	CCSetupDec dlg;
	int iArea ;
	int iDecNum=1;//第一块解码卡的第1路视频输出
	if (dlg.DoModal() ==IDOK)
	{
	
		if (dlg.m_Previewtype==0)
			iArea =1;
		else if (dlg.m_Previewtype ==1)
			iArea =4;
		else if (dlg.m_Previewtype==2)
			iArea =16;
		else if (dlg.m_Previewtype==3)
			iArea =2;
		else if (dlg.m_Previewtype ==4)
			iArea =3;
	}
	
	SetupRegion(iDecNum,iArea);
	
}

void CHKVisionDlg::OnButton10() 
{
	// TODO: Add your control notification handler code here
	if(!m_bSwitchMetrix)
	{
		m_bSwitchMetrix =TRUE ;
		m_MatrixSwitch=SetTimer(1010,1000,NULL);
		GetDlgItem(IDC_BUTTON10)->SetWindowText((LPCTSTR)"Stop Matrix Switch");
	}
	else
	{
		m_bSwitchMetrix =FALSE;
		KillTimer(1010);
		GetDlgItem(IDC_BUTTON10)->SetWindowText((LPCTSTR)"Start Matrix Switch");
	}
}



void CHKVisionDlg::OnButton11() 
{
	// TODO: Add your control notification handler code here
	KillTimer(m_MatrixSwitch);
	for (int i=0;i<4;i++)
	{
		for(int j=0;j<64;j++){
			SetEncoderVideoExtOutput(j,0,0,i,i,0);
		}
	}
	ClearDisplayRegion(0,0xffffffff);
	ClearDisplayRegion(1,0xffffffff);
	ClearDisplayRegion(2,0xffffffff);
	ClearDisplayRegion(3,0xffffffff);
}

void CHKVisionDlg::SetupRegion(int iDecNum, int iArea)
{
	REGION_PARAM param[MAX_DISPLAY_REGION];
	
	memset(param,0,sizeof(param));
	for(int i=0;i<MAX_DISPLAY_REGION;i++)
	{
//		param[i].color=RGB(19,35,236);
		param[i].color=RGB(10,10,10);
		param[i].param=0;
	}
	
	if (iArea ==1)
	{
		param[0].left = 0; param[0].top =0; param[0].width=704; param[0].height =576;
		SetDisplayRegion(iDecNum,1,param,0);
		SetEncoderVideoExtOutput(1+iDecNum*16,0,1,iDecNum,0,0);
		
	}
	else if(iArea ==4)
	{
		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(iDecNum,4,param,0);
			
		//我们从每一块DSP的第一路取出来做CIF预览供解码卡输出
		SetEncoderVideoExtOutput(0+iDecNum*16,0,1,iDecNum,0,0);
		SetEncoderVideoExtOutput(4+iDecNum*16,0,1,iDecNum,1,0);
		SetEncoderVideoExtOutput(8+iDecNum*16,0,1,iDecNum,2,0);
		SetEncoderVideoExtOutput(12+iDecNum*16,0,1,iDecNum,3,0);
		
	}
	else if(iArea ==16)
	{
		for(int i = 0; i < 4; i++)
		{
			for(int j =0; j <4; j++)
			{
				param[i*4+j].left = j*176;
				param[i*4+j].top = i*144;
				param[i*4+j].width =176;
				param[i*4+j].height =144;
			}
		}
		SetDisplayRegion(iDecNum,16,param,0);
		SetEncoderVideoExtOutput(0+iDecNum*16,0,1,iDecNum,0,0);
		SetEncoderVideoExtOutput(1+iDecNum*16,0,1,iDecNum,1,0);
		SetEncoderVideoExtOutput(2+iDecNum*16,0,1,iDecNum,2,0);
		SetEncoderVideoExtOutput(3+iDecNum*16,0,1,iDecNum,3,0);
		SetEncoderVideoExtOutput(4+iDecNum*16,0,1,iDecNum,4,0);
		SetEncoderVideoExtOutput(5+iDecNum*16,0,1,iDecNum,5,0);
		SetEncoderVideoExtOutput(6+iDecNum*16,0,1,iDecNum,6,0);
		SetEncoderVideoExtOutput(7+iDecNum*16,0,1,iDecNum,7,0);
		SetEncoderVideoExtOutput(8+iDecNum*16,0,1,iDecNum,8,0);
		SetEncoderVideoExtOutput(9+iDecNum*16,0,1,iDecNum,9,0);
		SetEncoderVideoExtOutput(10+iDecNum*16,0,1,iDecNum,10,0);
		SetEncoderVideoExtOutput(11+iDecNum*16,0,1,iDecNum,11,0);
		SetEncoderVideoExtOutput(12+iDecNum*16,0,1,iDecNum,12,0);
		SetEncoderVideoExtOutput(13+iDecNum*16,0,1,iDecNum,13,0);
		SetEncoderVideoExtOutput(14+iDecNum*16,0,1,iDecNum,14,0);
		SetEncoderVideoExtOutput(15+iDecNum*16,0,1,iDecNum,15,0);
	}
	else if(iArea ==2)
	{
		param[0].left = 0; param[0].top =0; param[0].width=704; param[0].height =576;
		param[1].left = 0; param[1].top =0; param[1].width=176; param[1].height =144;
		SetDisplayRegion(iDecNum,2,param,0);
		SetEncoderVideoExtOutput(3+iDecNum*16,0,1,iDecNum,0,0);
		SetEncoderVideoExtOutput(5+iDecNum*16,0,1,iDecNum,1,0);
	}
	else if(iArea ==3)
	{
		param[0].left=0;	param[0].top=0;		param[0].width=704;		param[0].height=288;
		param[1].left=0;	param[1].top=288;	param[1].width=352;		param[1].height=288;
		param[2].left=352;	param[2].top=288;	param[2].width=352;		param[2].height=288;
		SetDisplayRegion(iDecNum,3,param,0);
		SetEncoderVideoExtOutput(7+iDecNum*16,0,1,iDecNum,0,0);
		SetEncoderVideoExtOutput(9+iDecNum*16,0,1,iDecNum,1,0);
		SetEncoderVideoExtOutput(11+iDecNum*16,0,1,iDecNum,2,0);
	}
	

}

void CHKVisionDlg::OnSerial() 
{
	// TODO: Add your control notification handler code here
	CBoardList dlg;
	dlg.DoModal();
}

int CHKVisionDlg::TypeToFrames(FrameType_t frameType)
{
	switch(frameType){
	case PktIFrames:
	case PktPFrames:
		return 1;
	case PktBBPFrames:
		return 3;
	default:
		return 1;
	}
}





















⌨️ 快捷键说明

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