📄 clienth264dlg.cpp
字号:
// ClientH264Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "ClientH264.h"
#include "ClientH264Dlg.h"
#include "DXSurfaceMgr.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include <afxtempl.h>
#include <windows.h>
#include <winsock.h>
#include <malloc.h>
#define CLIENT_SOCKET_ERROR 1
#define SOCKET_OK 0
#pragma comment(lib, "wsock32.lib")
int nPlaying = 1; //indicate whether video is playing
int nZoom = 0; //need scale the video frame
char strIP[256] = "127.0.0.1"; //the server ip
unsigned long nData = 0; //data received in current tick
unsigned long nTicks = 0; //data received in last tick
unsigned long nFrames = 0; //total frames that have been played
CList<char *, char *> snippets; //snippets that received by network
CList<BYTE *, BYTE *> frames; //frames buffer
CRITICAL_SECTION lockSnippet; //snippet accessing lock
CRITICAL_SECTION lockFrame; //frame accessing lock
#include "avcodec.h"
#include "avformat.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include "Convert.h"
#define INBUF_SIZE 1024 //snippet size
#define FRAME_WIDTH 352 //CIF frame width
#define FRAME_HEIGHT 288 //CIF frame height
#define SPEED_REFRESH 1 //refresh timer
#ifdef __cplusplus
extern "C"
{
#include "avutil.h"
#include "avcodec.h"
#include "avformat.h"
#include "swscale.h"
}
#else
#include "avutil.h"
#include "avcodec.h"
#include "avformat.h"
#include "swscale.h"
#endif
ColorSpaceConversions conv; //YUV and RGB color space converter
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CClientH264Dlg dialog
CClientH264Dlg::CClientH264Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CClientH264Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CClientH264Dlg)
m_strServerIP = _T(strIP);
m_strAveFrames = _T("平均帧率:");
m_strTimer = _T("计时时间:");
m_strTotalFrames = _T("累计帧数:");
m_strTotalReceive = _T("累计接收:");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CClientH264Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CClientH264Dlg)
DDX_Text(pDX, IDC_EDIT_SERVER_IP, m_strServerIP);
DDV_MaxChars(pDX, m_strServerIP, 255);
DDX_Text(pDX, IDC_STATIC_AVE_FRAMES, m_strAveFrames);
DDX_Text(pDX, IDC_STATIC_TIMER, m_strTimer);
DDX_Text(pDX, IDC_STATIC_TOTAL_FRAMES, m_strTotalFrames);
DDX_Text(pDX, IDC_STATIC_TOTAL_RECEIVE, m_strTotalReceive);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CClientH264Dlg, CDialog)
//{{AFX_MSG_MAP(CClientH264Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_CONNECT, OnButtonConnect)
ON_WM_CLOSE()
ON_WM_DESTROY()
ON_WM_TIMER()
ON_BN_CLICKED(IDC_STATIC_SCREEN, OnStaticScreen)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CClientH264Dlg message handlers
BOOL CClientH264Dlg::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
SetTimer(SPEED_REFRESH, 1000, NULL);
return TRUE; // return TRUE unless you set the focus to a control
}
void CClientH264Dlg::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 CClientH264Dlg::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 CClientH264Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void socketError(char *str)
{
MessageBox(NULL, str, "SOCKET ERROR", MB_OK);
WSACleanup();
}
//thread that receives h264 bit stream
UINT H264ReceivingThread(LPVOID param)
{
WORD version;
WSADATA wsaData;
int rVal=0;
version = MAKEWORD(1,1);
WSAStartup(version,(LPWSADATA)&wsaData);
LPHOSTENT hostEntry;
//store information about the server
hostEntry = gethostbyname(strIP);
if(!hostEntry)
{
socketError("Failed gethostbyname()");
//WSACleanup();
return CLIENT_SOCKET_ERROR;
}
//create the socket
SOCKET client = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if(client == SOCKET_ERROR)
{
socketError("Failed socket()");
return CLIENT_SOCKET_ERROR;
}
//fill in the sockaddr_in struct
SOCKADDR_IN serverInfo;
serverInfo.sin_family = PF_INET;
serverInfo.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);
serverInfo.sin_port = htons(9999);
rVal = connect(client,(LPSOCKADDR)&serverInfo, sizeof(serverInfo));
if(rVal==SOCKET_ERROR)
{
socketError("Failed connect()");
return CLIENT_SOCKET_ERROR;
}
char buf[1024] = {0};
while (nPlaying)
{
//receiving data from connected socket
rVal = recv(client, buf, 1024, 0);
if(rVal == SOCKET_ERROR)
{
break;
}
if (rVal == 0) //if the server disconnected
{
nPlaying = 0; //terminate playing
break;
}
nData += rVal;
//put snippet to snippets-pool
EnterCriticalSection(&lockSnippet);
//==================================================================
if (snippets.GetCount() > 32) //remove the outdated h264 code data
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -