www.pudn.com > AVChat0926.rar > AVChatDlg.cpp


// AVChatDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include  
#include "AVChat.h" 
#include "AVChatDlg.h" 
 
#include "CRoleDlg.h" 
#include "CSelfCheckDlg.h" 
#include "CCallDlg.h" 
 
#include "UNetwork.h" 
#include "CRoleAdmin.h" 
//#include "CServerAdmin.h" 
//#include "CClientAdmin.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CAVChatDlg dialog 
 
CAVChatDlg::CAVChatDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CAVChatDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CAVChatDlg) 
	mStatus = _T(""); 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
	mRole   = SERVER; 
	mActor  = NULL; 
	mInitTimer = 0; 
} 
 
void CAVChatDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CAVChatDlg) 
	DDX_Control(pDX, IDC_VIDEO_WINDOW, mVideoWindow); 
	DDX_Control(pDX, IDC_BUTTON_DISCONNECT, mButtonDisconnect); 
	DDX_Control(pDX, IDC_BUTTON_CALL, mButtonCall); 
	DDX_Text(pDX, IDC_STATIC_STATUS, mStatus); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CAVChatDlg, CDialog) 
	//{{AFX_MSG_MAP(CAVChatDlg) 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(IDC_BUTTON_CALL, OnButtonCall) 
	ON_WM_DESTROY() 
	ON_WM_TIMER() 
	ON_BN_CLICKED(IDC_BUTTON_DISCONNECT, OnButtonDisconnect) 
	ON_MESSAGE(WM_ModifyFilterGraph, OnModifyFilterGraph)	 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CAVChatDlg message handlers 
 
BOOL CAVChatDlg::OnInitDialog() 
{ 
	CDialog::OnInitDialog(); 
 
	// 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 
		 
	// To show the main dialog first, 
	// use a timer to do initialization 
	mInitTimer = SetTimer(100, 100, NULL); 
//	Initialize(); // you can try it directly instead of using a timer 
 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
// 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 CAVChatDlg::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 CAVChatDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
void CAVChatDlg::OnDestroy()  
{ 
	if (mActor) 
	{ 
		mActor->Disconnect(); 
		delete mActor; 
		mActor = NULL; 
	} 
 
	CDialog::OnDestroy(); 
} 
 
/************************************************************************/ 
/*  
	功能:1,通过一系列交互完成角色,采集参数的初始化 
         2,初始化网络发送SOCKET的建立,接收线程启动 
*/ 
/************************************************************************/ 
void CAVChatDlg::Initialize(void) 
{ 
	SAFE_DELETE(mActor); 
	mActor = new CRoleAdmin(); 
	mActor->AddMsgReceiver(this); 
	mActor->SetVideoWindow(&mVideoWindow); 
	mActor->SetAVDevice(&mVideoDevice, &mAudioDevice); 
	mActor->Init(); 
 
	// Self checking and adjusting... 
	CSelfCheckDlg chkDlg; 
	chkDlg.SetAVDevice(&mVideoDevice, &mAudioDevice); 
	chkDlg.DoModal(); 
	mActor->SetDeviceConfig(chkDlg.GetDeviceConfig()); 
	 
	mActor->StartAVChat(); 
 
	ReflectStatus(); 
} 
 
void CAVChatDlg::ReflectStatus(void) 
{ 
	// Get local machine's IP info 
	char hostName[100]; 
	char hostIP[50]; 
	UNetwork::GetHostInfo(hostIP, hostName); 
 
	if (mRole == SERVER) 
	{ 
		mStatus.Format("Role: server. (%s - %s)", 
			hostName, hostIP); 
		mButtonCall.EnableWindow(FALSE); 
		mButtonDisconnect.EnableWindow(FALSE); 
	} 
	else 
	{ 
		mStatus.Format("Role: client. (%s - %s)", 
			hostName, hostIP); 
		mButtonDisconnect.EnableWindow(FALSE); 
	} 
	 
	UpdateData(FALSE); 
} 
 
void CAVChatDlg::OnTimer(UINT nIDEvent)  
{ 
	if (nIDEvent == mInitTimer) 
	{ 
		KillTimer(mInitTimer); 
		mInitTimer = 0; 
 
		Initialize(); 
	} 
 
	CDialog::OnTimer(nIDEvent); 
} 
 
// Build filter graph in the main thead, becuase the main thread 
// has message loop, especially to Video Renderer creating 
bool CAVChatDlg::ReceiveMessage(MessageT inMessage, void * ioParam, void * ioParam2) 
{ 
	if (inMessage == msg_ModifyFilterGraph) 
	{ 
		SendMessage(WM_ModifyFilterGraph, *((BOOL*)ioParam)); 
		return true; 
	} 
 
	return CMsgReceiver::ReceiveMessage(inMessage, ioParam, ioParam2); 
} 
 
LRESULT CAVChatDlg::OnModifyFilterGraph(WPARAM inWParam, LPARAM inLParam) 
{ 
	if (mActor) 
	{ 
		if (inWParam)  // Is building ? 
		{ 
			mActor->Activate(); 
		} 
		else 
		{ 
			mActor->Deactivate(); 
		} 
	} 
 
	return 0; 
} 
 
///////////////////////////////////////////////////////////////////////////// 
void CAVChatDlg::OnButtonCall()  
{ 
	CCallDlg  callDlg; 
	if (callDlg.DoModal() == IDOK) 
	{ 
		// Verify the role! 
		if (mRole == CLIENT && mActor) 
		{ 
			mActor->SetTargetIP(callDlg.GetServerIP()); 
			mActor->CallServer(); 
 
			// Update control status 
			mButtonCall.EnableWindow(FALSE); 
			mButtonDisconnect.EnableWindow(TRUE); 
		} 
	} 
} 
 
void CAVChatDlg::OnButtonDisconnect()  
{ 
	if (mActor) 
	{ 
		mActor->Disconnect(); 
 
		// Update control status 
		mButtonCall.EnableWindow(mRole == CLIENT); 
		mButtonDisconnect.EnableWindow(FALSE); 
 
		// After disconnect, give users a chance to change role 
		mActor->Init(); 
	//	Initialize(); 
	} 
} 
/////////////////////////////////////////////////////////////////////////////