
// LT_UVC_MFCDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "LT_UVC_MFC.h"
#include "LT_UVC_MFCDlg.h"
#include "afxdialogex.h"
#include "resource.h"
#include "VideoSetting.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CLT_UVC_MFCDlg 对话框
Video_Parame g_VideoSystemParamter;
extern TCHAR g_App_path[MAX_PATH];

int VideoStatusCallback(ULONG i,int FrameType,void *Context)
{
	CLT_UVC_MFCDlg *dlg = (CLT_UVC_MFCDlg *)Context;
	if(dlg)
	{
		dlg->SysVideoStautsCallback(i,FrameType);
	}
	return 0;
}
int RawStreamCallback(ULONG i,void *DataBuf,int FrameType,int width,int height,void *Context)
{

	CLT_UVC_MFCDlg *dlg = (CLT_UVC_MFCDlg *)Context;
	if(dlg)
	{
		dlg->StreamCallback(i,DataBuf,FrameType,width,height);
	}
	return 0;
}
int RawAudioCallback(ULONG i,void *DataBuf,int FrameType,int size,void *Context)
{

	CLT_UVC_MFCDlg *dlg = (CLT_UVC_MFCDlg *)Context;
	if(dlg)
	{
		dlg->AudioCallback(i,DataBuf,FrameType,size);
	}
	return 0;
}
void CLT_UVC_MFCDlg::SysVideoStautsCallback(ULONG i,int FrameType)
{
	
	if(FrameType ==0)
	{
		TRACE("Lost Video[%d]\n",i);
	}
}


void CLT_UVC_MFCDlg::OnSet(int index)
{
	//if(	g_VideoSystemParamter.m_Max_Video_Channels<=0) return;
	CVideoSetting dlg;
	dlg.m_currIndex =index;
	m_Current = index;
	Show_Result();
	GetVideoDeviceInfo();
	dlg.m_pParent = this;
	dlg.DoModal();

}
void CLT_UVC_MFCDlg::StreamCallback(ULONG i,void *DataBuf,int FrameType,int width,int height)
{
	//TRACE("CH[%d]-w=%d,h=%d\n",i,width,height);
}
void CLT_UVC_MFCDlg::AudioCallback(ULONG i,void *DataBuf,int FrameType,int size)
{
	//TRACE("Audio CH[%d]size = %d\n",i,size);
}
CLT_UVC_MFCDlg::CLT_UVC_MFCDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CLT_UVC_MFCDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CLT_UVC_MFCDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_VIDEO_CH1, m_Static_View_CH[0]);
	DDX_Control(pDX, IDC_VIDEO_CH2, m_Static_View_CH[1]);
	DDX_Control(pDX, IDC_VIDEO_CH3, m_Static_View_CH[2]);
	DDX_Control(pDX, IDC_VIDEO_CH4, m_Static_View_CH[3]);
	DDX_Control(pDX, IDC_VIDEO_CH5, m_Static_View_CH[4]);
	DDX_Control(pDX, IDC_VIDEO_CH6, m_Static_View_CH[5]);
	DDX_Control(pDX, IDC_VIDEO_CH7, m_Static_View_CH[6]);
	DDX_Control(pDX, IDC_VIDEO_CH8, m_Static_View_CH[7]);
	DDX_Control(pDX, IDC_VIDEO_CH9, m_Static_View_CH[8]);
	DDX_Control(pDX, IDC_VIDEO_CH10, m_Static_View_CH[9]);
	DDX_Control(pDX, IDC_VIDEO_CH11, m_Static_View_CH[10]);
	DDX_Control(pDX, IDC_VIDEO_CH12, m_Static_View_CH[11]);
	DDX_Control(pDX, IDC_VIDEO_CH13, m_Static_View_CH[12]);
	DDX_Control(pDX, IDC_VIDEO_CH14, m_Static_View_CH[13]);
	DDX_Control(pDX, IDC_VIDEO_CH15, m_Static_View_CH[14]);
	DDX_Control(pDX, IDC_VIDEO_CH16, m_Static_View_CH[15]);
	DDX_Control(pDX, IDC_VIDEO_CH17, m_Static_View_CH[16]);
	DDX_Control(pDX, IDC_VIDEO_CH18, m_Static_View_CH[17]);
	DDX_Control(pDX, IDC_VIDEO_CH19, m_Static_View_CH[18]);
	DDX_Control(pDX, IDC_VIDEO_CH20, m_Static_View_CH[19]);
	DDX_Control(pDX, IDC_VIDEO_CH21, m_Static_View_CH[20]);
	DDX_Control(pDX, IDC_VIDEO_CH22, m_Static_View_CH[21]);
	DDX_Control(pDX, IDC_VIDEO_CH23, m_Static_View_CH[22]);
	DDX_Control(pDX, IDC_VIDEO_CH24, m_Static_View_CH[23]);
	DDX_Control(pDX, IDC_VIDEO_CH25, m_Static_View_CH[24]);
	DDX_Control(pDX, IDC_VIDEO_CH26, m_Static_View_CH[25]);
	DDX_Control(pDX, IDC_VIDEO_CH27, m_Static_View_CH[26]);
	DDX_Control(pDX, IDC_VIDEO_CH28, m_Static_View_CH[27]);
	DDX_Control(pDX, IDC_VIDEO_CH29, m_Static_View_CH[28]);
	DDX_Control(pDX, IDC_VIDEO_CH30, m_Static_View_CH[29]);
	DDX_Control(pDX, IDC_VIDEO_CH31, m_Static_View_CH[30]);
	DDX_Control(pDX, IDC_VIDEO_CH32, m_Static_View_CH[31]);
	DDX_Control(pDX, IDC_VIDEO_CH33, m_Static_View_CH[32]);
	DDX_Control(pDX, IDC_VIDEO_CH34, m_Static_View_CH[33]);
	DDX_Control(pDX, IDC_VIDEO_CH35, m_Static_View_CH[34]);
	DDX_Control(pDX, IDC_VIDEO_CH36, m_Static_View_CH[35]);
	DDX_Control(pDX, IDC_VIDEO_CH37, m_Static_View_CH[36]);
	DDX_Control(pDX, IDC_VIDEO_CH38, m_Static_View_CH[37]);
	DDX_Control(pDX, IDC_VIDEO_CH39, m_Static_View_CH[38]);
	DDX_Control(pDX, IDC_VIDEO_CH40, m_Static_View_CH[39]);
	DDX_Control(pDX, IDC_VIDEO_CH41, m_Static_View_CH[40]);
	DDX_Control(pDX, IDC_VIDEO_CH42, m_Static_View_CH[41]);
	DDX_Control(pDX, IDC_VIDEO_CH43, m_Static_View_CH[42]);
	DDX_Control(pDX, IDC_VIDEO_CH44, m_Static_View_CH[43]);
	DDX_Control(pDX, IDC_VIDEO_CH45, m_Static_View_CH[44]);
	DDX_Control(pDX, IDC_VIDEO_CH46, m_Static_View_CH[45]);
	DDX_Control(pDX, IDC_VIDEO_CH47, m_Static_View_CH[46]);
	DDX_Control(pDX, IDC_VIDEO_CH48, m_Static_View_CH[47]);
	DDX_Control(pDX, IDC_VIDEO_CH49, m_Static_View_CH[48]);
	DDX_Control(pDX, IDC_VIDEO_CH50, m_Static_View_CH[49]);
	DDX_Control(pDX, IDC_VIDEO_CH51, m_Static_View_CH[50]);
	DDX_Control(pDX, IDC_VIDEO_CH52, m_Static_View_CH[51]);
	DDX_Control(pDX, IDC_VIDEO_CH53, m_Static_View_CH[52]);
	DDX_Control(pDX, IDC_VIDEO_CH54, m_Static_View_CH[53]);
	DDX_Control(pDX, IDC_VIDEO_CH55, m_Static_View_CH[54]);
	DDX_Control(pDX, IDC_VIDEO_CH56, m_Static_View_CH[55]);
	DDX_Control(pDX, IDC_VIDEO_CH57, m_Static_View_CH[56]);
	DDX_Control(pDX, IDC_VIDEO_CH58, m_Static_View_CH[57]);
	DDX_Control(pDX, IDC_VIDEO_CH59, m_Static_View_CH[58]);
	DDX_Control(pDX, IDC_VIDEO_CH60, m_Static_View_CH[59]);
	DDX_Control(pDX, IDC_VIDEO_CH61, m_Static_View_CH[60]);
	DDX_Control(pDX, IDC_VIDEO_CH62, m_Static_View_CH[61]);
	DDX_Control(pDX, IDC_VIDEO_CH63, m_Static_View_CH[62]);
	DDX_Control(pDX, IDC_VIDEO_CH64, m_Static_View_CH[63]);
	DDX_Control(pDX, IDC_CURR_INDEX, m_CurrInex_Edit);
	DDX_Control(pDX, IDC_RES_lIST, m_com_res_list);
	DDX_Control(pDX, IDC_AUTO_OPEN, m_ThreadTestBu_ST);
	DDX_Control(pDX, IDC_CLOSE_VIDEO, m_ThreadTestBu_Close);
}

BEGIN_MESSAGE_MAP(CLT_UVC_MFCDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_CLOSESYS, &CLT_UVC_MFCDlg::OnBnClickedClosesys)
	ON_BN_CLICKED(IDC_CLOSE_VIDEO, &CLT_UVC_MFCDlg::OnBnClickedCloseVideo)
	ON_MESSAGE(WM_VIEW_DBCLICK,OnMsgViewDbClick)
	ON_MESSAGE(WM_VIEW_CLICK,OnMsgViewClick)
	ON_BN_CLICKED(IDC_ADD_SRC3, &CLT_UVC_MFCDlg::OnBnClickedAddSrc3)
	ON_BN_CLICKED(IDC_SET_COLOR, &CLT_UVC_MFCDlg::OnBnClickedSetColor)
	ON_BN_CLICKED(IDC_SET_COLOR_M, &CLT_UVC_MFCDlg::OnBnClickedSetColorM)
	ON_BN_CLICKED(IDC_REOPEN, &CLT_UVC_MFCDlg::OnBnClickedReopen)
	ON_CBN_SELCHANGE(IDC_RES_lIST, &CLT_UVC_MFCDlg::OnCbnSelchangeReslist)
	ON_BN_CLICKED(IDC_AUTO_OPEN, &CLT_UVC_MFCDlg::OnBnClickedAutoOpen)
END_MESSAGE_MAP()


// CLT_UVC_MFCDlg 消息处理程序

BOOL CLT_UVC_MFCDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 设置此对话框的图标。当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	//------------------
	Get_Application_path();
	Set_Default_Config_Setting();
	m_nViewMode = 16;
	InitRect();
	IniNormalRect();
	Set64CHPreView();
	m_InitDone =0;
	InitVideoSys();
	Show_Result();
	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}
void CLT_UVC_MFCDlg::Set64CHPreView() 
{
	// TODO: Add your control notification handler code here
	m_pCurRectArr = &m_r64Vid[0][0];
	int nBeginScreen = 0;
	int	nScreenStatus;
	nScreenStatus =64;
	m_nViewMode = 64;
	ShowHideScreen(nBeginScreen,nScreenStatus);
}
void CLT_UVC_MFCDlg::Set9CHPreView() 
{
	// TODO: Add your control notification handler code here
	m_pCurRectArr = &m_rNineVid[0][0];
	int nBeginScreen = 0;
	int	nScreenStatus;
	nScreenStatus =9;
	m_nViewMode = 9;
	ShowHideScreen(nBeginScreen,nScreenStatus);
}
void CLT_UVC_MFCDlg::Set16CHPreView() 
{
	// TODO: Add your control notification handler code here
	m_pCurRectArr = &m_rSixteenVid[0][0];
	int nBeginScreen = 0;
	int	nScreenStatus;
	nScreenStatus =16;
	m_nViewMode = 16;
	ShowHideScreen(nBeginScreen,nScreenStatus);
	//UpdateVideoPreview();
}
void CLT_UVC_MFCDlg::Set4CHPreView() 
{
	// TODO: Add your control notification handler code here
	m_pCurRectArr = &m_r4Vid[0][0];
	int nBeginScreen = 0;
	int	nScreenStatus;
	nScreenStatus =4;
	m_nViewMode = 4;
	ShowHideScreen(nBeginScreen,nScreenStatus);	
}
void CLT_UVC_MFCDlg::ShowHideScreen(int nBegin, int nNum)
{
	int nIndex = 0;
	int i = 0;
	CRect rect;
	for (i=0; i<nNum; i++)
	{
		nIndex = (i+nBegin)%MAX_VIDEO_CHANNEL;
		rect = *(m_pCurRectArr+i);
		m_Static_View_CH[nIndex].MoveWindow(rect);
		m_Static_View_CH[nIndex].ShowWindow(SW_SHOW);	// show 
	}
	for (i=nNum; i<MAX_VIDEO_CHANNEL; i++)
	{
		
		nIndex = (i+nBegin)%MAX_VIDEO_CHANNEL;
		//m_Static_View_CH[nIndex].ShowWindow(SW_HIDE);		// hide	
		m_Static_View_CH[nIndex].MoveWindow(CRect(0,0,0,0));
		m_Static_View_CH[nIndex].ShowWindow(SW_HIDE);		// hide
		
	}
}
#define VIDEO_CLEARANCE 1
void CLT_UVC_MFCDlg::IniNormalRect()
{
	CRect m_rOneVid(m_Onerect);
	//m_rOneVid = 
	int height	= m_rOneVid.Height();
	int width	= m_rOneVid.Width();
    int i,j,offsetX,offsetY;
	  SIZE size;
	size.cx = (width-VIDEO_CLEARANCE*3)/4;
	size.cy = (height-VIDEO_CLEARANCE*3)/4;
	m_rSixteenVid[0][0] = CRect(m_rOneVid.TopLeft(), size);
	offsetX = m_rSixteenVid[0][0].Width() + VIDEO_CLEARANCE;
	offsetY = m_rSixteenVid[0][0].Height() + VIDEO_CLEARANCE;
	for (i=0; i<4; i++)
	{
		for ( j=0; j<4; j++)
		{
			m_rSixteenVid[i][j] = m_rSixteenVid[0][0];
			m_rSixteenVid[i][j].OffsetRect(offsetX*j, offsetY*i);
		}
	}
	size.cx = (width-VIDEO_CLEARANCE*2)/3;
	size.cy = (height-VIDEO_CLEARANCE*2)/3;
	m_rNineVid[0][0] = CRect(m_rOneVid.TopLeft(), size);
	offsetX = m_rNineVid[0][0].Width() + VIDEO_CLEARANCE;
	offsetY = m_rNineVid[0][0].Height() + VIDEO_CLEARANCE;
	for (i=0; i<3; i++)
	{
		for ( j=0; j<3; j++)
		{
			m_rNineVid[i][j] = m_rNineVid[0][0];
			m_rNineVid[i][j].OffsetRect(offsetX*j, offsetY*i);
		}
	}

	size.cx = (width-VIDEO_CLEARANCE*1)/2;
	size.cy = (height-VIDEO_CLEARANCE*1)/2;
	m_r4Vid[0][0] = CRect(m_rOneVid.TopLeft(), size);
	offsetX = m_r4Vid[0][0].Width() + VIDEO_CLEARANCE;
	offsetY = m_r4Vid[0][0].Height() + VIDEO_CLEARANCE;
	for (i=0; i<2; i++)
	{
		for ( j=0; j<2; j++)
		{
			m_r4Vid[i][j] = m_r4Vid[0][0];
			m_r4Vid[i][j].OffsetRect(offsetX*j, offsetY*i);
		}
	}


	size.cx = (width-VIDEO_CLEARANCE*5)/6;
	size.cy = (height-VIDEO_CLEARANCE*5)/6;
	m_r36Vid[0][0] = CRect(m_rOneVid.TopLeft(), size);
	offsetX = m_r36Vid[0][0].Width() + VIDEO_CLEARANCE;
	offsetY = m_r36Vid[0][0].Height() + VIDEO_CLEARANCE;
	for (i=0; i<6; i++)
	{
		for ( j=0; j<6; j++)
		{
			m_r36Vid[i][j] = m_r36Vid[0][0];
			m_r36Vid[i][j].OffsetRect(offsetX*j, offsetY*i);
		}
	}
	//------------------------------
	size.cx = (width-VIDEO_CLEARANCE*7)/8;
	size.cy = (height-VIDEO_CLEARANCE*7)/8;
	m_r64Vid[0][0] = CRect(m_rOneVid.TopLeft(), size);
	offsetX = m_r64Vid[0][0].Width() + VIDEO_CLEARANCE;
	offsetY = m_r64Vid[0][0].Height() + VIDEO_CLEARANCE;
	for (i=0; i<8; i++)
	{
		for ( j=0; j<8; j++)
		{
			m_r64Vid[i][j] = m_r64Vid[0][0];
			m_r64Vid[i][j].OffsetRect(offsetX*j, offsetY*i);
		}
	}
	//----------------------------
}
void CLT_UVC_MFCDlg::InitRect()
{
	m_Current = 0;
	m_bOne = FALSE;
    for(int i = 0;i<MAX_VIDEO_CHANNEL;i++)
	{
		m_Static_View_CH[i].GetClientRect(&m_rect[i]);
		m_Static_View_CH[i].m_nIndex = i;		
	}
	m_Onerect.left = 10;
	m_Onerect.top = 20;
	m_Onerect.right =800;
	m_Onerect.bottom = 600;
}
// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CLT_UVC_MFCDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CLT_UVC_MFCDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CLT_UVC_MFCDlg::OnBnClickedClosesys()
{
	// TODO: 在此添加控件通知处理程序代码
	 ExitVideoSys();
	 CDialog::OnOK();
}
void CLT_UVC_MFCDlg::ReOpenSys(int index)
{
	
}
void CLT_UVC_MFCDlg::OpenSys()
{
	int i;
	int m_videoIndex;
	//TRACE("OpenSys m_ExitThread=%d\n",m_ExitThread);
	if(m_ExitThread)
	{
		return;
	}
	InitVideoSys();
	if(	g_VideoSystemParamter.m_Max_Video_Channels>0)
	{
		for( i = 0;i<	g_VideoSystemParamter.m_Max_Video_Channels;i++)
		{
			TRACE("OpenVideo[%d]\n",i);
			OpenVideo(i);
			OpenAudio(i);
			AudioOutPlay(i);
		}

	} 
}
void CLT_UVC_MFCDlg::GetVideoDeviceInfo()
{
	int i;

	GetCurrSystemDevice();
	//-------------
	g_VideoSystemParamter.m_Max_Video_Channels=  GetVideoTotalChannels();
	g_VideoSystemParamter.m_Max_Audio_Channels=  GetAudioTotalChannels();
	//TRACE("m_Max_Video_Channels=%d  m_Max_Audio_Channels =%d \n",g_VideoSystemParamter.m_Max_Video_Channels,g_VideoSystemParamter.m_Max_Audio_Channels);
	for( i = 0;i<	g_VideoSystemParamter.m_Max_Video_Channels;i++)
	{
		memset(g_VideoSystemParamter.m_VideoName[i],0x00,MAX_PATH);
		g_VideoSystemParamter.m_Max_ResSize[i]= GetVideoMaxResIndex(i);
		GetVideoNameByIndex(i,g_VideoSystemParamter.m_VideoName[i]);
		if(g_VideoSystemParamter.m_Max_ResSize[i]>0)
		{
			for(int k=0; k<g_VideoSystemParamter.m_Max_ResSize[i];k++)
			{
				GetVideoMaxResSizeByIndex(i,k,&g_VideoSystemParamter.m_VideoRes[i][k].width,&g_VideoSystemParamter.m_VideoRes[i][k].height,&g_VideoSystemParamter.m_VideoRes[i][k].fps);
			}
		}
		TRACE("[Video-%d]=%s\n",i,g_VideoSystemParamter.m_VideoName[i]);
	}
	for( i = 0;i<	g_VideoSystemParamter.m_Max_Audio_Channels;i++)
	{
		memset(g_VideoSystemParamter.m_AudioName[i],0x00,MAX_PATH);
		GetAudioNameByIndex(i,g_VideoSystemParamter.m_AudioName[i]);
		TRACE("[Audio-%d]=%s\n",i,g_VideoSystemParamter.m_AudioName[i]);
	}
	if(	g_VideoSystemParamter.m_Max_Video_Channels <5)
	{
		Set4CHPreView();
	}
	else if(	g_VideoSystemParamter.m_Max_Video_Channels <9)
	{
		Set9CHPreView();
	}
	else if(g_VideoSystemParamter.m_Max_Video_Channels <17)
	{
		Set16CHPreView();
	}
	else
	{
		Set64CHPreView();
	}
}
void CLT_UVC_MFCDlg::InitVideoSys()
{
	int i;
	if(m_InitDone ==1)
	{
		ExitVideoSys();
	}
	//SetMax_VideoSize(1920, 1200);
	TRACE("InitVideoSys\n");
	InitHwDSPs();
	GetVideoDeviceInfo();
	//-------------------------------
	m_InitDone =1;
	//------------
}
void CLT_UVC_MFCDlg::ExitVideoSys()
{
	//SetCloneView(SDC_TOPOLOGY_CLONE); 
	//TRACE("ExitVideoSys m_InitDone=%d \n",m_InitDone);
	if(m_InitDone ==0)
	{
		DeInitHwDSPs();
		return;
	}
	for(int i = 0;i<MAX_VIDEO_CHANNEL;i++)
	{
		CloseVideo(i);
		CloseAudio(i);
	}
	DeInitHwDSPs();
	m_InitDone =0;
}

void  CLT_UVC_MFCDlg::Set_Default_Config_Setting(void)
{
	g_VideoSystemParamter.m_Max_Video_Channels =0;
	g_VideoSystemParamter.m_Max_Audio_Channels =0;
	for(int i = 0;i<MAX_VIDEO_CHANNEL;i++)
	{
		for(int k=0; k<MAX_VIDEO_RESSIZE;k++)
		{
			g_VideoSystemParamter.m_VideoRes[i][k].fps =0;
			g_VideoSystemParamter.m_VideoRes[i][k].width = 0;
			g_VideoSystemParamter.m_VideoRes[i][k].height = 0;
			

		}
		g_VideoSystemParamter.m_AudioIndex[i]=0; //i+1;
		g_VideoSystemParamter.m_VideoIndex[i] =i;
		memset(&g_VideoSystemParamter.m_AudioName[i],0x00,MAX_PATH);
		memset(&g_VideoSystemParamter.m_VideoName[i],0x00,MAX_PATH);
		m_CurrSelResIndex[i] =0;
		g_VideoSystemParamter.m_VideoResIndex[i]=1;
		m_hVideoDSP[i] =-1;
		m_hAudioDSP[i] =-1;
	}
}

void CLT_UVC_MFCDlg::OnBnClickedCloseVideo()
{
	TRACE("KillVideoTestThread\n");
	m_ExitThread = 1;
}
LRESULT CLT_UVC_MFCDlg::OnMsgViewDbClick(WPARAM wParam, LPARAM lParam)
{
	m_bOne = !m_bOne;
	int i;
    if(m_bOne)
	{
		for( i = 0;i<MAX_VIDEO_CHANNEL;i++)
		{
			m_Static_View_CH[i].MoveWindow(CRect(0,0,0,0));
		}
		m_Static_View_CH[m_Current].MoveWindow(&m_Onerect);
	}
	else
	{
		if(m_nViewMode == 4)
		{
			Set4CHPreView();
		}
		else if(m_nViewMode == 9)
		{
			 Set9CHPreView();
		}
		else if(m_nViewMode == 16)
		{
		
			 Set16CHPreView();
		}
		else if(m_nViewMode == 64)
		{
		
			 Set64CHPreView();
		}
	}
	
	for( i = 0;i<g_VideoSystemParamter.m_Max_Video_Channels;i++)
	{
		if(m_hVideoDSP[i] != -1)
		{
			m_Static_View_CH[i].GetClientRect(&m_rect[i]);
			StartVideoPreview(m_hVideoDSP[i],m_Static_View_CH[i].m_hWnd,&m_rect[i]);
			
		}
	}
	return 0;
}

LRESULT CLT_UVC_MFCDlg::OnMsgViewClick(WPARAM wParam, LPARAM lParam)
{
    m_Current = wParam;
	Show_Result();
	return 0;
}
void CLT_UVC_MFCDlg::Show_Result()
{
	CString mst;
	mst.Format("%d",m_Current);
	m_CurrInex_Edit.SetWindowTextA(mst);
	m_com_res_list.ResetContent();
	for(int k=0; k<g_VideoSystemParamter.m_Max_ResSize[m_Current];k++)
	{
		mst.Format("%dX%d(%d)",g_VideoSystemParamter.m_VideoRes[m_Current][k].width,g_VideoSystemParamter.m_VideoRes[m_Current][k].height,g_VideoSystemParamter.m_VideoRes[m_Current][k].fps);
		m_com_res_list.AddString(mst);
	}
	m_com_res_list.SetCurSel(m_CurrSelResIndex[m_Current]);
}
void CLT_UVC_MFCDlg::OnBnClickedAddSrc3()
{
	// TODO: 在此添加控件通知处理程序代码
	 char szFile[256];
	 CTime time = CTime::GetCurrentTime();
	 sprintf(szFile,"D:\\%02d%02d%02d%02d%02d.bmp",m_Current+1,time.GetDay(),time.GetHour(),time.GetMinute(),time.GetSecond());
	 if(m_hVideoDSP[m_Current] !=-1)
	 {
		SaveCaptureImage(m_hVideoDSP[m_Current],szFile);
	 }
}


void CLT_UVC_MFCDlg::OnBnClickedSetColor()
{
	// TODO: 在此添加控件通知处理程序代码
	OpenVideoColorSetting(m_hVideoDSP[m_Current]);
}


void CLT_UVC_MFCDlg::OnBnClickedSetColorM()
{
	// TODO: 在此添加控件通知处理程序代码
   int Brightness;
   int Contrast;
   int Saturation;
   int Hue;
   		Brightness=0x08;
		Contrast=146;
		Saturation=136;
		Hue=0x00;
	SetVideoPara(m_hVideoDSP[m_Current],Brightness,Contrast,Saturation,Hue);
}
void CLT_UVC_MFCDlg::CloseVideo(int index)
{
	if(m_hVideoDSP[index]!= -1)
	{
		StopVideoCapture(m_hVideoDSP[index]);
		m_hVideoDSP[index] =-1;
	}

}
void CLT_UVC_MFCDlg::CloseAudio(int index)
{
	if(m_hAudioDSP[index]!= -1)
	{
		StopAudioCapture(m_hAudioDSP[index]);
		m_hAudioDSP[index] =-1;
	}

}
void CLT_UVC_MFCDlg::OpenVideo(int index)
{
	int m_videoIndex;
	int m_videoResIndex;
	m_videoIndex = g_VideoSystemParamter.m_VideoIndex[index];
	m_videoResIndex = g_VideoSystemParamter.m_VideoResIndex[index];
	//m_videoIndex = m_videoIndex-1;
	if((m_videoIndex>=0)&&(m_videoIndex<g_VideoSystemParamter.m_Max_Video_Channels))
	{
		
		CloseVideo(index);
		m_hVideoDSP[index] = VideoChannelOpen(index,m_videoIndex,g_VideoSystemParamter.m_VideoRes[m_videoIndex][m_videoResIndex].width,g_VideoSystemParamter.m_VideoRes[m_videoIndex][m_videoResIndex].height,g_VideoSystemParamter.m_VideoRes[m_videoIndex][m_videoResIndex].fps);
		if(m_hVideoDSP[index] != -1)
		{
			RegisterRAWDirectCallback(index,RawStreamCallback,this);
			RegisterVideoStatusCallback(index,VideoStatusCallback,this);
			m_Static_View_CH[index].GetClientRect(&m_rect[index]);
			StartVideoPreview(m_hVideoDSP[index],m_Static_View_CH[index].m_hWnd,&m_rect[index]);
		}
		else
		{
			TRACE("OpenVideo[%d]Fail\n",index);
		} 
	}
	
}
int CLT_UVC_MFCDlg::OpenAudio(int index)
{
	int m_audioIndex;
	m_audioIndex = g_VideoSystemParamter.m_AudioIndex[index];
	//m_audioIndex = m_audioIndex-1;
	if((m_audioIndex>=0)&&(m_audioIndex<g_VideoSystemParamter.m_Max_Audio_Channels))
	{
		
		CloseAudio(index);
		m_hAudioDSP[index] = AudioChannelOpen(index,m_audioIndex,MAX_AUDIO_BUFSIZE);
		RegisterAudioDirectCallback(index,RawAudioCallback,this);
		AudioOutOpen(index,this->m_hWnd);
		//AudioOutPlay(index);  // not aoto player
		return 1;
	}
	else
	{
		return 0;
	}
}

void CLT_UVC_MFCDlg::OnBnClickedAddSrc2()
{
	// TODO: 在此添加控件通知处理程序代码
}


void CLT_UVC_MFCDlg::OnBnClickedReopen()
{
	// TODO: 在此添加控件通知处理程序代码
	//ExitVideoSys();
	//InitVideoSys();
	//Show_Result();
	CloseVideo(m_Current);
	OpenVideo(m_Current);
}


void CLT_UVC_MFCDlg::OnCbnSelchangeReslist()
{
	// TODO: 在此添加控件通知处理程序代码
	m_CurrSelResIndex[m_Current] = m_com_res_list.GetCurSel();
}



void CLT_UVC_MFCDlg::OnBnClickedAutoOpen()
{
	// TODO: 在此添加控件通知处理程序代码
	CreateVideoTestThread();
}

DWORD WINAPI VideoTestThread(LPVOID lParam)
{
	CLT_UVC_MFCDlg *pOwner = (CLT_UVC_MFCDlg *)lParam;
	HANDLE hMulEvents[1+1];
	hMulEvents[0] = pOwner->m_hKillEvent;
	for(int i=0; i <1; i++)
	{
		hMulEvents[i+1] = pOwner->m_hEvent[i];
	}
	DWORD dwResult = 0;
	int dwTimeOutIndex =0;
	pOwner->m_hExitCnt ++;
	int ThreadTestTimer=0;
	while (pOwner->m_ExitThread==0) 
	{ 
		
		dwResult=WaitForMultipleObjects(2,hMulEvents,FALSE,INFINITE);
		switch(dwResult)
		{
			case WAIT_OBJECT_0:
			goto finished;
			break;
			default:
			{
				if(ThreadTestTimer>30)
				{
						pOwner->DealWithTestEvent();
						SetEvent(pOwner->m_hEvent[0]);
						ThreadTestTimer =0;
				}
				else
				{
						SetEvent(pOwner->m_hEvent[0]);
						ThreadTestTimer++;
				}
			}
			break;
		}
		Sleep(200);
	}
finished:
	if(pOwner->m_hExitEvent)
	{
		pOwner->ExitVideoSys();
		SetEvent(pOwner->m_hExitEvent);
	}
	pOwner->KillVideoTestThread();
	pOwner->m_hExitCnt =0;
	TRACE("VideoTestThread Exit\n");
	return 0;
}
void  CLT_UVC_MFCDlg::CreateVideoTestThread()
{
	m_hKillEvent = CreateEvent(NULL,FALSE,FALSE,NULL);
	m_hExitEvent = CreateEvent(NULL,FALSE,FALSE,NULL);
	m_ExitThread =0;
    if(m_hKillEvent== NULL)
	{		
		goto ERROR_CREATE_THREAD;	
	}	
	for( int i = 0;i<1;i++)
	{
		m_hEvent[i] = CreateEvent(NULL,FALSE,FALSE,NULL);
		if(m_hEvent[i]== NULL)
		{	
			goto ERROR_CREATE_THREAD;
		}
	}
	//------------------------------------
	DWORD dw;
	m_hThread = CreateThread(NULL,0, VideoTestThread,this,0,&dw);
	if(m_hThread!=NULL )
	{	
	}
	else
	{
		goto ERROR_CREATE_THREAD;
	}
	SetEvent(m_hEvent[0]);
	m_ThreadTestBu_ST.EnableWindow(FALSE);
	m_ThreadTestBu_Close.EnableWindow(TRUE);
	return;
ERROR_CREATE_THREAD:
	KillVideoTestThread();
}
 void CLT_UVC_MFCDlg::KillVideoTestThread()
{
	if(m_hThread)
	{
		SetEvent(m_hKillEvent);
		if(m_hExitEvent)
		{
			WaitForSingleObject(m_hExitEvent, INFINITE);
			CloseHandle(m_hExitEvent);
			m_hExitEvent = NULL;
		}
		CloseHandle(m_hThread);
		m_hThread = NULL;
	}
	if(m_hKillEvent)
	{
		CloseHandle(m_hKillEvent);
		m_hKillEvent = NULL;		  
	}
	for(int i = 0;i<1;i++)
	{
		
		if(m_hEvent[i])
		{
			
			CloseHandle(m_hEvent[i]);
			m_hEvent[i] = NULL;	
		}
	}
	
	for(int i = 0;i<MAX_VIDEO_CHANNEL;i++)
	{
			CloseVideo(i);
			CloseAudio(i);
	}
	Invalidate();
		m_ThreadTestBu_ST.EnableWindow(TRUE);
		m_ThreadTestBu_Close.EnableWindow(FALSE);
}

  void   CLT_UVC_MFCDlg::DealWithTestEvent()
{
	ExitVideoSys();
	Invalidate();
	//Sleep(2000);
	//-------------
	OpenSys();
	//-------------
	//SetEvent(m_hEvent[0]);
 }