📄 test1300dlg.cpp
字号:
// test1300Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "test1300.h"
#include "test1300Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
DWORD WINAPI AcceptThread(LPVOID lpV);
extern "C" {
void yv12_to_rgb24_mmx(unsigned char *dst,
int dst_stride,
unsigned char *y_src,
unsigned char *u_src,
unsigned char *v_src,
int y_stride, int uv_stride,
int width, int height);
}
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
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()
/////////////////////////////////////////////////////////////////////////////
// CTest1300Dlg dialog
CTest1300Dlg::CTest1300Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CTest1300Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTest1300Dlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
memset( biOut, 0, sizeof(BITMAPINFO)+256);
memset( biIn, 0, sizeof(BITMAPINFO)+256);
m_lpbiIn = (PBITMAPINFOHEADER)&biIn;
m_lpbiOut = (PBITMAPINFOHEADER)&biOut;
m_hAcceptThread = NULL;
m_pDataSock = NULL;
m_hIC = NULL;
lpDD = NULL; // DirectDraw object
lpDDSPrimary = NULL; // DirectDraw primary surface
lpDDSOne = NULL; // Offscreen surface 1
lpClipper = NULL; // clipper for primary
m_bShowUpdated = TRUE;
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTest1300Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTest1300Dlg)
DDX_Control(pDX, IDC_SHOW_BOX, m_ShowBox);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTest1300Dlg, CDialog)
//{{AFX_MSG_MAP(CTest1300Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_SIZE()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTest1300Dlg message handlers
BOOL CTest1300Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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);
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
// (1) initial Decompress
m_lpbiIn->biSize = sizeof(BITMAPINFOHEADER);
m_lpbiIn->biWidth = DEFAULT_WIDTH;
m_lpbiIn->biHeight = DEFAULT_HEIGHT;
m_lpbiIn->biCompression = mmioStringToFOURCC("XFM4", 0);
m_lpbiIn->biBitCount = 16;
m_lpbiIn->biSizeImage = m_lpbiIn->biWidth*m_lpbiIn->biHeight*4+2048;
m_lpbiIn->biPlanes = 1;
m_lpbiIn->biXPelsPerMeter = 0;
m_lpbiIn->biYPelsPerMeter = 0;
m_lpbiIn->biClrImportant = 0;
m_lpbiIn->biClrUsed = 0;
m_hIC = ICOpen(mmioStringToFOURCC("VIDC", 0), m_lpbiIn->biCompression, ICMODE_FASTDECOMPRESS );
if ( m_hIC==NULL ) {
TRACE0("ICOpen return error\n");
return FALSE;
}
//Get Decompress OutPut Format
int nOutSize;
int ret;
nOutSize = ICDecompressGetFormatSize(m_hIC, m_lpbiIn);
m_lpbiOut->biSize = nOutSize;
m_lpbiOut->biWidth = m_lpbiIn->biWidth;
m_lpbiOut->biHeight = m_lpbiIn->biHeight;
m_lpbiOut->biCompression = BI_RGB; // default display format
m_lpbiOut->biBitCount = 24;
m_lpbiOut->biSizeImage = m_lpbiOut->biWidth*m_lpbiOut->biHeight*3+2048;
m_lpbiOut->biPlanes = 1;
m_lpbiOut->biXPelsPerMeter = 0;
m_lpbiOut->biYPelsPerMeter = 0;
m_lpbiOut->biClrImportant = 0;
m_lpbiOut->biClrUsed = 0;
ret = ICDecompressQuery(m_hIC, m_lpbiIn, m_lpbiOut);
if ( ret!=ICERR_OK ) {
ICClose ( m_hIC );
m_hIC = NULL;
TRACE0("Decompress Format Error\n");
return FALSE;
}
if ( ICDecompressBegin(m_hIC , m_lpbiIn, m_lpbiOut) != ICERR_OK) {
ICClose ( m_hIC );
m_hIC = NULL;
TRACE0("DecompressBegin Error\n");
return FALSE;
}
// (2) initial DirectDraw
if ( CreateDirectDraw(m_ShowBox.m_hWnd, m_lpbiOut)==FALSE ) {
ICClose ( m_hIC );
m_hIC = NULL;
TRACE0("CreateDirectDraw Error\n");
return FALSE;
}
// create listen server
char lip[256]="0.0.0.0";
int lport=3358;
if ( m_Socket.CreateTCPServer(lip, lport)==FALSE ) {
MessageBox("Socket Create Error\n");
return FALSE;
}
DWORD idListen;
//Begin a thread to accept connection
m_hAcceptThread = CreateThread(NULL, 0, AcceptThread,
(LPVOID)this, 0, &idListen);
if ( m_hAcceptThread==NULL ){
m_Socket.Close();
TRACE0("AcceptThread Create Error\n");
return FALSE;
}
return TRUE; // return TRUE unless you set the focus to a control
}
void CTest1300Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CTest1300Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
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;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CTest1300Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
BOOL CTest1300Dlg::DestroyWindow()
{
// TODO: Add your specialized code here and/or call the base class
if ( m_pDataSock!=NULL ) m_pDataSock->Close();
m_Socket.Close();
int i=0;
while ( m_hAcceptThread!=NULL && i++<300 ) {
Sleep(10);
}
DestroyDirectDraw();
if ( m_hIC!=NULL ) {
ICDecompressEnd(m_hIC);
ICClose(m_hIC);
}
return CDialog::DestroyWindow();
}
BOOL CTest1300Dlg::CreateDirectDraw(HWND hWnd, PBITMAPINFOHEADER lpbiOut)
{
DDSURFACEDESC ddsd;
HRESULT ddrval;
ddrval = DirectDrawCreate( NULL, &lpDD, NULL );
if( ddrval != DD_OK ) {
return FALSE;
}
ddrval = lpDD->SetCooperativeLevel( hWnd, DDSCL_NORMAL );
if( ddrval != DD_OK ) {
DestroyDirectDraw();
return FALSE;
}
// Create the primary surface
ZeroMemory(&ddsd, sizeof(ddsd));
ddsd.dwSize = sizeof( ddsd );
ddsd.dwFlags = DDSD_CAPS ;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
ddrval = lpDD->CreateSurface( &ddsd, &lpDDSPrimary, NULL );
if( ddrval != DD_OK ) {
DestroyDirectDraw();
return FALSE;
}
// create a clipper for the primary surface
ddrval = lpDD->CreateClipper( 0, &lpClipper, NULL );
if( ddrval != DD_OK ) {
DestroyDirectDraw();
return FALSE;
}
ddrval = lpClipper->SetHWnd( 0, hWnd );
if( ddrval != DD_OK ) {
DestroyDirectDraw();
return FALSE;
}
ddrval = lpDDSPrimary->SetClipper( lpClipper );
if( ddrval != DD_OK ) {
DestroyDirectDraw();
return FALSE;
}
// Create Offscreen Surface
ZeroMemory(&ddsd, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
ddsd.dwWidth = lpbiOut->biWidth;
ddsd.dwHeight = lpbiOut->biHeight;
ddrval = lpDD->CreateSurface(&ddsd, &lpDDSOne, NULL);
if( ddrval != DD_OK ) {
DestroyDirectDraw();
return FALSE;
}
m_bShowUpdated = FALSE;
return TRUE;
}
void CTest1300Dlg::DestroyDirectDraw()
{
if ( lpDDSPrimary ) { lpDDSPrimary->Release();lpDDSPrimary = NULL; }
if ( lpDDSOne ) { lpDDSOne->Release(); lpDDSOne = NULL; }
if ( lpClipper ) { lpClipper->Release(); lpClipper = NULL; }
if ( lpDD ) { lpDD->Release(); lpDD = NULL; }
return;
}
DWORD WINAPI AcceptThread(LPVOID lpV)
{
CTest1300Dlg *pDlg = (CTest1300Dlg *)lpV;
XSocket *pListenSock = &(pDlg->m_Socket);
XSocket *pSock=NULL;
unsigned char pPlayBuffer[DEFAULT_WIDTH*DEFAULT_HEIGHT*4];
unsigned char pbuffer[MAX_DATA_SIZE+sizeof(struct packet_header)];
PacketHeader *header=(PacketHeader*)pbuffer;
BOOL bFirstFrame=TRUE;
DWORD t1, t2, num;
FILE *fp=NULL;
int i420_num = 0;
TRACE0("test \n");
while( 1 ) {
pDlg->m_pDataSock = pListenSock->Accept();
if( pDlg->m_pDataSock == NULL ) {//listen socket closed, if not reconnect, remove it
break;
}
fp = fopen("C:\\Capture.yuv", "ab");
bFirstFrame = TRUE;
// Receive Packet Header
while( pDlg->m_pDataSock->Recv((char*)header, HEADERSIZE)==HEADERSIZE ) {
// TRACE3("Header: Version=0x%x, Type=0x%x, Length=%d, ", header->version,
// header->type, header->length);
// TRACE1("reserved=0x%x\n", header->reserved);
if ( header->version!=0x48 ) {
TRACE0("receive non-valid packet header, version error\n");
break;
}
if ( header->reserved!=0xffff) {
TRACE0("receive non-valid packet header, reserved error\n");
break;
}
// Receive
if ( pDlg->m_pDataSock->Recv(header->data, header->length-HEADERSIZE)
!=(header->length-HEADERSIZE) ) {
TRACE0("receive non-valid packet data\n");
break;
}
// TRACE3("The Last Three bytes is 0x%x, 0x%x, 0x%x\n",
// header->data[header->length-HEADERSIZE-3],
// header->data[header->length-HEADERSIZE-2],
// header->data[header->length-HEADERSIZE-1]);
// check
switch(header->type) {
case IFRAME:
//TRACE0("IFRAME\n");
// Decode
if (ICDecompress(pDlg->m_hIC, 0, pDlg->m_lpbiIn, header->data,
pDlg->m_lpbiOut, pPlayBuffer) != ICERR_OK ) {
// Handle the decompression error that occurred.
pDlg->m_pDataSock->Close();
break;
}
break;
case PFRAME:
//TRACE0("PFRAME\n");
// Decode
if (ICDecompress(pDlg->m_hIC, 0, pDlg->m_lpbiIn, header->data,
pDlg->m_lpbiOut, pPlayBuffer) != ICERR_OK ) {
// Handle the decompression error that occurred.
pDlg->m_pDataSock->Close();
break;
}
// if ( bFirstFrame==TRUE ) {
// TRACE1("request first I frame when receive 0x%x type frame\n", header->type);
// continue;
// }
break;
case I420FRAME:
if ( fp!=NULL ) {
i420_num++;
fwrite(header->data, sizeof(unsigned char), pDlg->m_lpbiOut->biWidth*pDlg->m_lpbiOut->biHeight*3/2, fp);
TRACE1("fwrite %d frames\n", i420_num);
}
yv12_to_rgb24_mmx(pPlayBuffer, pDlg->m_lpbiOut->biWidth*3,
(unsigned char*)header->data,
(unsigned char*)(header->data+pDlg->m_lpbiOut->biWidth*pDlg->m_lpbiOut->biHeight),
(unsigned char*)(header->data+pDlg->m_lpbiOut->biWidth*pDlg->m_lpbiOut->biHeight*5/4),
pDlg->m_lpbiOut->biWidth, pDlg->m_lpbiOut->biWidth/2, pDlg->m_lpbiOut->biWidth, pDlg->m_lpbiOut->biHeight);
break;
default:
TRACE1("non-valid 0x%x type frame\n", header->type);
break;
}
if ( bFirstFrame==TRUE ) {
t1 = timeGetTime();
t2 = t1;
num = 0;
bFirstFrame=FALSE;
}
else {
t2 = timeGetTime();
num++;
if ( (t2-t1)>5000 ) {
bFirstFrame=TRUE;
TRACE1(" FPS is %ffps\n", (num*1000.0) / (t2-t1));
}
}
// Show
pDlg->ShowImage(pPlayBuffer);
}
pDlg->m_pDataSock->Close();
delete pDlg->m_pDataSock;
pDlg->m_pDataSock = NULL;
i420_num = 0;
if ( fp!=NULL ) {
fclose(fp);
fp = NULL;
}
}
pDlg->m_hAcceptThread = NULL;
TRACE0("Leave AcceptThread\n");
return 0;
}
BOOL CTest1300Dlg::ShowImage(void *pBuffer)
{
HRESULT ddrval;
HDC hDCSOne;
RECT srcRect, dstRect;
if ( m_bShowUpdated==TRUE ) {
DestroyDirectDraw();
if ( CreateDirectDraw(m_ShowBox.m_hWnd, m_lpbiOut)==FALSE ) return FALSE;
}
srcRect.top = 0; srcRect.left= 0;
srcRect.bottom = DEFAULT_HEIGHT; srcRect.right=DEFAULT_WIDTH;
::GetWindowRect(m_ShowBox.m_hWnd, &dstRect); // cost 10/100000 ms
// dstRect.right = dstRect.left+DEFAULT_WIDTH;
// dstRect.bottom= dstRect.top+DEFAULT_HEIGHT;
ddrval = lpDDSOne->GetDC(&hDCSOne);
if ( ddrval!=DD_OK ) {
TRACE0("DDSOne GetDC error\n");
return FALSE;
}
if ( 0 ) { // flip
::SetDIBitsToDevice(hDCSOne, 0, 0, m_lpbiOut->biWidth, -m_lpbiOut->biHeight,
0, -m_lpbiOut->biHeight-1,
-m_lpbiOut->biHeight-1, -m_lpbiOut->biHeight, pBuffer, (BITMAPINFO*)m_lpbiOut, DIB_RGB_COLORS);
}
else {
::SetDIBitsToDevice(hDCSOne, 0, 0, m_lpbiOut->biWidth, m_lpbiOut->biHeight,
0, 0, 0, m_lpbiOut->biHeight, pBuffer, (BITMAPINFO*)m_lpbiOut, DIB_RGB_COLORS);
}
ddrval = lpDDSOne->ReleaseDC(hDCSOne);
ddrval = lpDDSPrimary->Blt( &dstRect, lpDDSOne, &srcRect, /* DDBLT_DONOTWAIT | */DDBLT_ASYNC, NULL);
if ( ddrval!=DD_OK ) {
TRACE0("lpDDSPrimary Blt error\n");
return FALSE;
}
return TRUE;
}
void CTest1300Dlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
// Set ShowBox to Size
if ( ::IsWindow(m_ShowBox.m_hWnd) ) {
BOOL br = m_ShowBox.SetWindowPos(NULL, 0, 0, cx-60, cy-60, SWP_NOMOVE | SWP_SHOWWINDOW);
m_bShowUpdated = TRUE;
TRACE3("cx %d, cy %d, return %d\n", cx, cy, br);
}
}
void CTest1300Dlg::OnButton1()
{
// TODO: Add your control notification handler code here
printf("test\n");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -