📄 videonetdlg.cpp
字号:
////////////////////////////////////////////////////////////////////////////
//
//
// Project : VideoNet version 1.1.
// Description : Peer to Peer Video Conferencing over the LAN.
// Author : Nagareshwar Y Talekar ( nsry2002@yahoo.co.in)
// Date : 15-6-2004.
//
//
// File description :
// Name : VideoNetDlg.cpp
// Details : Main dialog class
//
//
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "VideoNet.h"
#include "VideoNetDlg.h"
#include "Resource.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CVideoNetDlg dialog
// Global varialbes...
int count=0;
unsigned char cdata[10000];
int cbuffer_size=10000;
unsigned char rgbdata[80000];
int buffersize=80000;
// Callback function gets invoked during compression...
// It returns the compressed data byte by byte...
void OwnWriteFunction(int byte)
{
if(count<cbuffer_size)
cdata[count]=(unsigned char)byte;
count++;
}
CVideoNetDlg::CVideoNetDlg(CWnd* pParent /*=NULL*/)
: CDialog(CVideoNetDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CVideoNetDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CVideoNetDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CVideoNetDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CVideoNetDlg, CDialog)
//{{AFX_MSG_MAP(CVideoNetDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
// Conference menu handlers
ON_COMMAND(CONF_CONNECT,OnConnect)
ON_COMMAND(CONF_DISCONNECT,OnDisconnect)
ON_COMMAND(CONF_EXIT,OnCancel)
// View menu handlers
ON_COMMAND(VIEW_LOCAL,OnViewLocal)
ON_COMMAND(VIEW_REMOTE,OnViewRemote)
// Audio menu handlers
ON_COMMAND(AUDIO_CONTROL,OnVolumeControl)
ON_COMMAND(AUDIO_SEND,OnAudioSend)
ON_COMMAND(AUDIO_RECEIVE,OnAudioReceive)
// Video menu handlers
ON_COMMAND(VIDEO_SEND,OnVideoSend)
ON_COMMAND(VIDEO_RECEIVE,OnVideoReceive)
// Help menu handlers
ON_COMMAND(HELP_ABOUT,OnAbout)
ON_COMMAND(IDC_BUTTON1,OnSendMesg)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
BOOL CVideoNetDlg::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);
}
}
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// Open log file...
log.Open("main.log",CFile::modeCreate | CFile::modeWrite);
// Disable the menu items...at startup
menu=this->GetMenu();
menu->EnableMenuItem(CONF_CONNECT,MF_ENABLED);
menu->EnableMenuItem(CONF_DISCONNECT,MF_DISABLED | MF_GRAYED);
menu->EnableMenuItem(AUDIO_SEND,MF_GRAYED);
menu->EnableMenuItem(AUDIO_RECEIVE,MF_GRAYED);
menu->EnableMenuItem(VIDEO_SEND,MF_GRAYED);
menu->EnableMenuItem(VIDEO_RECEIVE,MF_GRAYED);
menu->EnableMenuItem(VIEW_LOCAL,MF_GRAYED);
menu->EnableMenuItem(VIEW_REMOTE,MF_GRAYED);
list=(CListBox*)GetDlgItem(IDC_LIST1);
bsend=(CButton*)GetDlgItem(IDC_BUTTON1);
// Disable the send button
bsend->EnableWindow(FALSE);
// Create Recording thread
record=new RecordSound(this);
record->CreateThread();
// Create Player thread
play=new PlaySound1(this);
play->CreateThread();
// Prepare for capture video from webcam...
vidcap=new VideoCapture();
vidcap->SetDialog(this);
if(vidcap->Initialize()) // success
{
this->m_bmpinfo=&vidcap->m_bmpinfo;
isVideo=TRUE;
// Initialize the compressor
// Initialize table for RGB to YUV conversion
InitLookupTable();
// Initialize the compressor
cparams.format = CPARAM_QCIF;
InitH263Encoder(&cparams);
// Set up the callback function
WriteByteFunction = OwnWriteFunction;
}
else
{
isVideo=FALSE;
// Setup bmpinfo structure yourself
m_bmpinfo=new BITMAPINFO;
m_bmpinfo->bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
m_bmpinfo->bmiHeader.biWidth=IMAGE_WIDTH;
m_bmpinfo->bmiHeader.biHeight=IMAGE_HEIGHT;
m_bmpinfo->bmiHeader.biPlanes=1;
m_bmpinfo->bmiHeader.biBitCount=24;
m_bmpinfo->bmiHeader.biCompression=0;
m_bmpinfo->bmiHeader.biSizeImage=0;
m_bmpinfo->bmiHeader.biXPelsPerMeter=0;
m_bmpinfo->bmiHeader.biYPelsPerMeter=0;
m_bmpinfo->bmiHeader.biClrUsed=0;
m_bmpinfo->bmiHeader.biClrImportant=0;
// Disable menu items...
menu->EnableMenuItem(VIEW_LOCAL,MF_DISABLED | MF_GRAYED);
menu->EnableMenuItem(VIDEO_SEND,MF_DISABLED | MF_GRAYED);
}
// Capturing is not yet started...
isCaptureStarted=FALSE;
isViewLocalVideo=TRUE;
isViewRemoteVideo=TRUE;
isAudioSend=TRUE;
isAudioReceive=TRUE;
isVideoSend=TRUE;
isVideoReceive=TRUE;
// Initialize decompressor
InitH263Decoder();
// Adjust display windows
CWnd *wnd,*bwnd;
CRect rect,brect;
// For local video display window
wnd=this->GetDlgItem(IDC_LOCALVIDEO);
bwnd=this->GetDlgItem(IDC_LOCALBORDER);
bwnd->GetWindowRect(brect);
ScreenToClient(brect);
local_wnd_x=brect.TopLeft().x+(brect.Width()-IMAGE_WIDTH)/2;
local_wnd_y=brect.TopLeft().y+(brect.Height()-IMAGE_HEIGHT)/2;
// Centre the local video window
wnd->SetWindowPos(&wndTop,local_wnd_x-4,local_wnd_y-4,IMAGE_WIDTH+9,IMAGE_HEIGHT+9,SWP_SHOWWINDOW | SWP_DRAWFRAME);
// For remote video display window
wnd=this->GetDlgItem(IDC_REMOTEVIDEO); // Video display window
bwnd=this->GetDlgItem(IDC_REMOTEBORDER); // Border window...
bwnd->GetWindowRect(brect);
ScreenToClient(brect);
remote_wnd_x=brect.TopLeft().x+(brect.Width()-IMAGE_WIDTH)/2;
remote_wnd_y=brect.TopLeft().y+(brect.Height()-IMAGE_HEIGHT)/2;
// Centre the remote video window
wnd->SetWindowPos(&wndTop,remote_wnd_x-4,remote_wnd_y-4,IMAGE_WIDTH+9,IMAGE_HEIGHT+9,SWP_SHOWWINDOW | SWP_DRAWFRAME);
// Get Dialog DC
m_hdc=this->GetDC()->m_hDC;
// Initialize DIB for drawing...
hdib=::DrawDibOpen();
if(hdib!=NULL)
{
::DrawDibBegin(hdib,
m_hdc,
-1, // don't stretch
-1, // don't stretch
&m_bmpinfo->bmiHeader,
IMAGE_WIDTH, // width of image
IMAGE_HEIGHT, // height of image
0
);
}
// Setup the connect dialog box
connectdlg=new ConnectDlg(NULL);
connectdlg->SetParent(this);
// Setup the parent for differnt datagram sockets
dcontrol.SetParent(this);
daudio.SetParent(this);
dvideo.SetParent(this);
dtext.SetParent(this);
// Create control datagram socket for handling messages
dcontrol.CreateSocket(PORT_CONTROL,TYPE_CONTROL);
// Conference not yet started...
isStarted=FALSE;
return TRUE; // return TRUE unless you set the focus to a control
}
void CVideoNetDlg::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 CVideoNetDlg::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();
}
}
HCURSOR CVideoNetDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CVideoNetDlg::OnCancel()
{
// If conference is going on...then
// it must be destroyed...
if(isStarted)
DestroyConference();
// Close the control session
dcontrol.CloseSocket();
// End recording and player thread
record->PostThreadMessage(WM_RECORDSOUND_ENDTHREAD,0,0);
play->PostThreadMessage(WM_PLAYSOUND_ENDTHREAD,0,0);
// Close the video capture device
vidcap->Destroy();
// Close graphics....
if(hdib!=NULL)
{
::DrawDibEnd(hdib);
::DrawDibClose(hdib);
}
// Close the encoder
if(isVideo)
ExitH263Encoder(&cparams);
// close decoder ......
ExitH263Decoder();
CDialog::OnCancel();
}
/**
* Connect to remote host
* Invoked when user presses connect menu item
* Displays the connect dialog box
*/
void CVideoNetDlg::OnConnect()
{
// Show the connect dialog box
connectdlg->DoModal();
}
/**
* Disconnect the remote host
* Invoked when user presses disconnect menu item
* Stops the current conference
*/
void CVideoNetDlg::OnDisconnect()
{
// Send notification to the remote host...
dcontrol.SendControlMessage(MESG_DISCONNECT,NULL);
DestroyConference();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -