www.pudn.com > mini_remote.zip > VideoCap.cpp


// VideoCap.cpp: implementation of the CVideoCap class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
#include "VideoCap.h" 
 
CVideoCap *theOnlyOneGrabber=NULL; 
 
#ifdef _DEBUG 
#undef THIS_FILE 
static char THIS_FILE[]=__FILE__; 
#define new DEBUG_NEW 
#endif 
 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
CVideoCap::CVideoCap() 
{ 
	dwLastCallback=0; 
	imageData = NULL; 
} 
CVideoCap::~CVideoCap() 
{ 
	Destroy(); 
} 
 
void CVideoCap::Destroy() 
{ 
	capCaptureAbort(m_hWndCapture); 
	capSetCallbackOnVideoStream(m_hWndCapture, NULL); 
	Sleep(300); 
	capDriverDisconnect(m_hWndCapture); 
	theOnlyOneGrabber=NULL; 
	if (imageData!=NULL) 
	{ 
		delete imageData; 
		imageData=NULL; 
	} 
} 
 
// this is internal use only! 
BOOL validCallHint=FALSE; 
void CVideoCap::SetImageData(LPVOID data,DWORD data_len) 
{ 
	ASSERT(validCallHint); 
	if(!validCallHint)	return; 
	if(!imageData)		return; 
	memcpy(imageData,data,data_len); 
	imageDataSize = data_len; 
	dwLastCallback	=  timeGetTime(); 
} 
 
LPBYTE CVideoCap::GetImageData() 
{ 
	if(!imageData)	return NULL; 
	if((timeGetTime()-dwLastCallback)>20) 
		capGrabFrameNoStop(m_hWndCapture); 
	return (imageData); 
} 
 
DWORD CVideoCap::GetImageDataSize() 
{ 
	return imageDataSize; 
} 
 
BOOL CVideoCap::Create(CWnd *pParentWnd) 
{ 
	if(theOnlyOneGrabber) 
		return FALSE; 
	if (pParentWnd) 
		m_hWndCapture = ::capCreateCaptureWindow("Capture Window",WS_CHILD|WS_CLIPSIBLINGS|WS_VISIBLE,0,0,320,240,pParentWnd->m_hWnd, 0xffff); 
	else 
		m_hWndCapture = ::capCreateCaptureWindow("Capture Window",WS_VISIBLE,0,0,320,240,0,0); 
	if(!m_hWndCapture) 
		return FALSE; 
	::SetWindowLong(m_hWndCapture,GWL_EXSTYLE,WS_EX_TOOLWINDOW|WS_EX_STATICEDGE); 
	::MoveWindow(m_hWndCapture,0,0,0,0,TRUE); 
 
	if (!capDriverConnect(m_hWndCapture,0)) 
	{ 
		m_hWndCapture = NULL; 
		capDriverDisconnect(m_hWndCapture); 
		return FALSE; 
	} 
	if (!capGetVideoFormat(m_hWndCapture,&m_InInfo,sizeof(m_InInfo))) 
	{ 
		m_hWndCapture = NULL; 
		capDriverDisconnect(m_hWndCapture); 
		return FALSE; 
	} 
	m_InInfo.bmiHeader.biHeight=240; 
	m_InInfo.bmiHeader.biWidth=320; 
	if (!capSetVideoFormat(m_hWndCapture,&m_InInfo,sizeof(BITMAPINFO))) 
	{ 
		m_hWndCapture = NULL; 
		capDriverDisconnect(m_hWndCapture); 
		return FALSE; 
	} 
	if (!capPreviewRate(m_hWndCapture,10)) 
	{ 
		m_hWndCapture = NULL; 
		capDriverDisconnect(m_hWndCapture); 
		return FALSE; 
	} 
	theOnlyOneGrabber=this; 
	imageData = new BYTE[250000]; 
	if (!capSetCallbackOnFrame(m_hWndCapture,FrameCallBack)) 
	{ 
		m_hWndCapture = NULL; 
		capDriverDisconnect(m_hWndCapture); 
		return FALSE; 
	} 
	return TRUE; 
} 
 
static LRESULT CALLBACK FrameCallBack(HWND hWnd, LPVIDEOHDR lpVHdr) 
{ 
	ASSERT_VALID(theOnlyOneGrabber); 
	validCallHint = TRUE; 
	theOnlyOneGrabber ->SetImageData(lpVHdr->lpData,lpVHdr->dwBufferLength); 
	validCallHint = FALSE; 
	return TRUE; 
}