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

📄 netmeetingdlg.cpp

📁 Netmeeting是Windows系统自带的网上聊天软件
💻 CPP
字号:
// NetMeetingDlg.cpp : implementation file
//

#include "stdafx.h"
#include "NetMeeting.h"
#include "NetMeetingDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


int count=0;
int retvalue=0;
unsigned char cdata[10000];
unsigned char vdata[30000];
int cbuffer_size=10000;
int buffersize=80000;

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
void OwnWriteFunction(int byte)
{
	if(count<cbuffer_size)
		cdata[count]=(unsigned char)byte;
	
	count++;
	
}

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()

/////////////////////////////////////////////////////////////////////////////
// CNetMeetingDlg dialog

CNetMeetingDlg::CNetMeetingDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CNetMeetingDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CNetMeetingDlg)
	m_chClient = -1;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	m_pVideoCapture = NULL;
	
	m_pRevBMP = NULL;
	m_pBackBMP = NULL;
	m_pSocketListen = NULL;
	m_pSocketClient = NULL;
	memset(&m_lastFirstPacket,0,sizeof(FirstPacket));
	m_lastFirstPacket.nBackSize = 1;
	m_lastFirstPacket.nCurrentSize = 0;
	m_lRevSize = 0;
	m_bAccept = FALSE;
}

void CNetMeetingDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CNetMeetingDlg)
	DDX_Control(pDX, IDS_263_CODE, m_st263Code);
	DDX_Control(pDX, IDS_SHOW_VIDEO, m_stShowVideo);
	DDX_Radio(pDX, IDR_SERVER, m_chClient);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CNetMeetingDlg, CDialog)
	//{{AFX_MSG_MAP(CNetMeetingDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDS_START, OnStart)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNetMeetingDlg message handlers

BOOL CNetMeetingDlg::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
	
	m_chClient = 0;
	UpdateData(FALSE);

	InitLookupTable();
	// Initialize the compressor
	cparams.format = CPARAM_QCIF;
	InitH263Encoder(&cparams); 
	WriteByteFunction = OwnWriteFunction;
	InitH263Decoder();

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CNetMeetingDlg::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 CNetMeetingDlg::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 CNetMeetingDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CNetMeetingDlg::OnStart() 
{
	UpdateData();
	if (!m_chClient)
	{
		m_pVideoCapture = new CVideoCapture(this);	
		m_pVideoCapture->InitDevice();
		m_pVideoCapture->StartVideo();
	}
	m_stShowVideo.GetClientRect(&m_rectClient);
	m_st263Code.GetClientRect(&m_rect263Video);
	m_stShowVideo.UpdateData();

	InitNetWork();
	
}

void CNetMeetingDlg::Display(BITMAPINFO &bitmapInfo, PBYTE &pBuffer)
{
	StretchDIBits(m_stShowVideo.GetDC()->GetSafeHdc(),
		m_rectClient.left,m_rectClient.top,m_rectClient.Width(),m_rectClient.Height(),
		0,0,bitmapInfo.bmiHeader.biWidth,bitmapInfo.bmiHeader.biHeight,
			pBuffer,&bitmapInfo,DIB_RGB_COLORS,SRCCOPY);
}

void CNetMeetingDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	if (m_pVideoCapture != NULL)
	{
		m_pVideoCapture->StopVideo();
	}	
	ExitH263Encoder(&cparams);	
	// close decoder ......
	ExitH263Decoder();
}
//////////////////////////////////////////////////////////////////////////
//			接受请求
//////////////////////////////////////////////////////////////////////////
void CNetMeetingDlg::ServerAccept()
{
	m_pSocketListen->Accept(m_socketServer);
	m_bAccept = TRUE;
}
//////////////////////////////////////////////////////////////////////////
//			接受数据,并且显示出来
//////////////////////////////////////////////////////////////////////////
void CNetMeetingDlg::RecevieData()
{
	m_pSocketClient->Receive(&m_sendPacket,sizeof(Packet));
	
	if (m_sendPacket.bHead)
	{
		if (!m_bAccept)
		{
			m_bAccept = TRUE;
			memcpy(&m_lastFirstPacket,m_sendPacket.byData,sizeof(FirstPacket));
			m_pRevBMP = new BYTE[m_lastFirstPacket.nCurrentSize];
			return	;
		}
		PBYTE pTemp =new BYTE[m_lastFirstPacket.nBackSize];
		memset(pTemp,0,m_lastFirstPacket.nBackSize);
		retvalue = DecompressFrame(m_pRevBMP,m_lastFirstPacket.nCurrentSize,
			pTemp,m_lastFirstPacket.nBackSize);

		StretchDIBits(m_st263Code.GetDC()->GetSafeHdc(),
			m_rect263Video.left,m_rect263Video.top,m_rect263Video.Width(),m_rect263Video.Height(),
			0,0,m_lastFirstPacket.bitmapInfo.bmiHeader.biWidth,m_lastFirstPacket.bitmapInfo.bmiHeader.biHeight,
			pTemp,&m_lastFirstPacket.bitmapInfo,DIB_RGB_COLORS,SRCCOPY);
		
		delete []pTemp;
		delete []m_pRevBMP;
		m_pRevBMP = NULL;
		
		memcpy(&m_lastFirstPacket,m_sendPacket.byData,sizeof(FirstPacket));
		m_pRevBMP = new BYTE[m_lastFirstPacket.nCurrentSize];
		m_lRevSize = 0;		
	}
	else
	{
		memcpy(m_pRevBMP+m_lRevSize,m_sendPacket.byData,m_sendPacket.lSize);
		m_lRevSize += m_sendPacket.lSize;
	}	
}

void CNetMeetingDlg::Display263Video(BITMAPINFO &bitmapInfo, PBYTE &pBuffer,int nSize)
{
	ConvertRGB2YUV(IMAGE_WIDTH,IMAGE_HEIGHT,pBuffer,yuv);
	// Reset the counter
	count=0;
	Bits bits;
	//Compress the data...to h263
	cparams.format=CPARAM_QCIF;
	cparams.inter = CPARAM_INTRA;  
	cparams.Q_intra = 3;
	cparams.data=yuv;		//  Data in YUV format...
	CompressFrame(&cparams,&bits);
	
	retvalue=count;

	if (!m_bAccept)
	{
		return ;
	}
	FirstPacket fp;
	memset(&fp,0,sizeof(fp));
	fp.nBackSize = nSize;
	fp.nCurrentSize = count;
	memcpy(&fp.bitmapInfo,&bitmapInfo,sizeof(BITMAPINFO));
	m_sendPacket.bHead = TRUE;
	memset(m_sendPacket.byData,0,4000);
	memcpy(m_sendPacket.byData,(void *)&fp,sizeof(FirstPacket));
	m_socketServer.Send(&m_sendPacket,sizeof(Packet));
	m_sendPacket.bHead = FALSE;
	int nSendSize = 0;
	while (1)
	{
		if ((nSendSize + 4000) < count)
		{
			memcpy(m_sendPacket.byData,cdata + nSendSize,4000);
			m_sendPacket.lSize = 4000;
			m_socketServer.Send(&m_sendPacket,sizeof(Packet));
			nSendSize += 4000;
		}
		else
		{
			memcpy(m_sendPacket.byData,cdata + nSendSize,count - nSendSize);
			m_sendPacket.lSize = count - nSendSize;
			m_socketServer.Send(&m_sendPacket,sizeof(Packet));
			break;
		}
	}
/*
	for(int i=0;i<retvalue;i++)
	{
		vdata[i]=cdata[i];
	}	
	
//	unsigned char rgbdata[80000];
	unsigned char *rgbdata  = new unsigned char [nSize];
	retvalue=DecompressFrame(vdata,retvalue,rgbdata,nSize);
	
	if(!retvalue)
	{
		return;
	}
	StretchDIBits(m_st263Code.GetDC()->GetSafeHdc(),
		m_rect263Video.left,m_rect263Video.top,m_rect263Video.Width(),m_rect263Video.Height(),
		0,0,bitmapInfo.bmiHeader.biWidth,bitmapInfo.bmiHeader.biHeight,
			rgbdata,&bitmapInfo,DIB_RGB_COLORS,SRCCOPY);
	delete []rgbdata;
	*/
}

void CNetMeetingDlg::InitNetWork()
{
	if (!m_chClient)			//服务器
	{
		m_pSocketListen = new CListenSocket(this);
		if(!m_pSocketListen->Create(5001))
			AfxMessageBox("Create socket");
		if(!m_pSocketListen->Listen(5))
			AfxMessageBox("listen socket");
		this->SetWindowText("Server");
	}
	else							//客户端
	{
		m_pSocketClient = new CClientSocket(this);
		m_pSocketClient->Create(0);
		if(!m_pSocketClient->Connect("127.0.0.1",5001))
			AfxMessageBox("client");
		this->SetWindowText("Client");
	}
}

⌨️ 快捷键说明

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