www.pudn.com > IP.rar > AVDConfDlg.cpp, change:2009-04-07,size:17092b


#include "stdafx.h" 
#include "rtccore_i.c" 
#include "rtccore.h" 
#include "AVDConf.h" 
#include "AVDConfDlg.h" 
#include "MakeCallDlg.h" 
#include "incomingdlg.h" 
#include "defines.h" 
//Download by http://www.codefans.net 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
BSTR	g_bstrAddr; 
class CAboutDlg : public CDialog 
{ 
public: 
	CAboutDlg(); 
	 
	enum { IDD = IDD_ABOUTBOX }; 
	 
	 
	 
	protected: 
	virtual void DoDataExchange(CDataExchange* pDX);     
	 
protected: 
	 
	 
	DECLARE_MESSAGE_MAP() 
}; 
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) 
{ 
	 
	 
} 
void CAboutDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	 
	 
} 
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) 
	 
		 
	 
END_MESSAGE_MAP() 
CAVDConfDlg::CAVDConfDlg(CWnd* pParent ) 
	: CDialog(CAVDConfDlg::IDD, pParent) 
{ 
	 
	 
	 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
void CAVDConfDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	 
	DDX_Control(pDX, IDC_WHITEBOARD, m_cWhiteboard); 
	DDX_Control(pDX, IDC_MESSAGING, m_cMessaging); 
	DDX_Control(pDX, IDC_TUNING, m_cTuning); 
 	DDX_Control(pDX, IDC_MAKECALL, m_cMakeCall); 
 	DDX_Control(pDX, IDC_APPCALL, m_cAppCall); 
	 
} 
BEGIN_MESSAGE_MAP(CAVDConfDlg, CDialog) 
	 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(IDC_MAKECALL, OnMakecall) 
	ON_BN_CLICKED(IDC_APPCALL, OnAppcall) 
	ON_BN_CLICKED(IDC_TUNING, OnTuning) 
	ON_BN_CLICKED(IDC_MESSAGING, OnMessaging) 
	ON_BN_CLICKED(IDM_ABOUTBOX, OnAboutbox) 
	ON_BN_CLICKED(IDC_WHITEBOARD, OnWhiteboard) 
	 
END_MESSAGE_MAP() 
BOOL CAVDConfDlg::OnInitDialog() 
{ 
	CDialog::OnInitDialog(); 
	 
	SetIcon(m_hIcon, TRUE);			 
	SetIcon(m_hIcon, FALSE);		 
	 
	 
	if (InitRTCClient() != S_OK) 
	{ 
		 
		MessageBox ( "Initialization Error" ); 
		m_cMakeCall.EnableWindow ( FALSE ); 
		m_cAppCall.EnableWindow ( FALSE ); 
		m_cMessaging.EnableWindow ( FALSE ); 
		m_cTuning.EnableWindow ( FALSE ); 
		m_cWhiteboard.EnableWindow ( FALSE ); 
	} 
	 
	return TRUE;   
} 
void CAVDConfDlg::OnPaint()  
{ 
	if (IsIconic()) 
	{ 
		CPaintDC dc(this);  
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); 
		 
		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; 
		 
		dc.DrawIcon(x, y, m_hIcon); 
	} 
	else 
	{ 
		CDialog::OnPaint(); 
	} 
} 
HCURSOR CAVDConfDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
void CAVDConfDlg::OnMakecall()  
{ 
	MakeCallDlg cDlg; 
	 
	if (cDlg.DoModal() == IDOK) 
	{ 
		 
		MakeCall (RTCST_PC_TO_PC, g_bstrAddr); 
	} 
} 
void CAVDConfDlg::OnMessaging()  
{ 
 	MakeCallDlg cDlg; 
  
	if (cDlg.DoModal() == IDOK) 
 	{													  
		MakeCall (RTCST_IM, g_bstrAddr); 
	} 
} 
void CAVDConfDlg::OnAppcall()  
{ 
	HRESULT	hr; 
	VARIANT_BOOL	vbool = VARIANT_FALSE; 
	m_pClient->get_IsT120AppletRunning(RTCTA_APPSHARING, &vbool); 
	if (vbool == VARIANT_FALSE) 
	{  
		hr = m_pClient->StartT120Applet ( RTCTA_APPSHARING ); 
	} 
} 
void CAVDConfDlg::OnWhiteboard()  
{ 
	HRESULT	hr; 
	VARIANT_BOOL	vbool = VARIANT_FALSE; 
	m_pClient->get_IsT120AppletRunning(RTCTA_WHITEBOARD, &vbool); 
	if (vbool == VARIANT_FALSE) 
	{  
		hr = m_pClient->StartT120Applet ( RTCTA_WHITEBOARD ); 
	} 
} 
HRESULT CAVDConfDlg::MakeCall(RTC_SESSION_TYPE enType, BSTR bstrURI) 
{ 
    HRESULT hr; 
	if (enType != RTCST_IM) 
    { 
         
		if ((m_AVDlg) && (m_AVDlg.GetState () != RTCSS_IDLE)) 
        { 
			MessageBoxW (m_hWnd, L"An audio/video call is in progress!", NULL, MB_OK ); 
            return S_FALSE; 
        } 
    } 
    IRTCSession * pSession = NULL; 
    hr = m_pClient->CreateSession(enType, NULL, NULL, 0, &pSession); 
    if (FAILED(hr)) 
    { 
        return hr; 
    } 
     
    hr = pSession->AddParticipant(bstrURI, NULL, &m_Participant); 
	if (FAILED(hr)) 
    { 
        SAFE_RELEASE(pSession); 
        return hr; 
    } 
     
    hr = AddSession(pSession, enType); 
     
    if (FAILED(hr)) 
    {   
             
        return hr; 
    } 
    return S_OK; 
} 
HRESULT CAVDConfDlg::InitRTCClient() 
{ 
	HRESULT hr; 
	m_pClient = NULL; 
	m_pEvents = NULL; 
	 
	hr = CoInitialize(NULL); 
	if (FAILED(hr)) 
	{ 
		return hr; 
	} 
	hr = CoCreateInstance (CLSID_RTCClient, NULL, 
		CLSCTX_INPROC_SERVER, IID_IRTCClient, 
		(LPVOID *)&m_pClient); 
	if (FAILED(hr)) 
	{ 
		return hr; 
	} 
	hr = m_pClient->Initialize(); 
	if (FAILED(hr)) 
	{ 
		SAFE_RELEASE(m_pClient); 
		return hr; 
	} 
	m_pClient->SetPreferredMediaTypes ( RTCMT_ALL, VARIANT_TRUE ); 
	long lEventMask = RTCEF_SESSION_STATE_CHANGE | 
					  RTCEF_MESSAGING | 
					  RTCEF_MEDIA | 
					  RTCEF_INTENSITY | 
					  RTCEF_CLIENT; 
	hr = m_pClient->put_EventFilter( lEventMask ); 
    if ( FAILED(hr) ) 
    { 
        SAFE_RELEASE(m_pClient); 
        return hr; 
    } 
    m_pEvents = new CRTCEvents; 
    if (!m_pEvents) 
    { 
        SAFE_RELEASE(m_pClient); 
        return hr; 
    } 
    hr = m_pEvents->Advise( m_pClient, m_hWnd ); 
    if ( FAILED(hr) ) 
    { 
        SAFE_RELEASE(m_pClient); 
        return hr; 
    } 
     
    hr = m_pClient->put_ListenForIncomingSessions(RTCLM_BOTH);  
	 
    if ( FAILED(hr) ) 
    { 
     
        SAFE_RELEASE(m_pClient); 
        return hr; 
    } 
	 
	if (m_AVDlg) 
		m_AVDlg.SetState (RTCSS_IDLE); 
	return S_OK; 
} 
LRESULT CAVDConfDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)  
{ 
	switch (message) 
	{ 
	case WM_RTC_EVENT: 
		OnRTCEvent (message, wParam, lParam); 
		break; 
	} 
	return CDialog::WindowProc(message, wParam, lParam); 
} 
HRESULT CAVDConfDlg::OnRTCEvent(UINT message, WPARAM wParam, LPARAM lParam) 
{ 
    IDispatch * pDisp = (IDispatch *)lParam; 
    RTC_EVENT enEvent = (RTC_EVENT)wParam; 
    HRESULT hr; 
    
    switch ( wParam ) 
    { 
        case RTCE_SESSION_STATE_CHANGE: 
            { 
                IRTCSessionStateChangeEvent * pEvent = NULL; 
                 
                 
				hr = pDisp->QueryInterface( IID_IRTCSessionStateChangeEvent, 
                                            (void **)&pEvent ); 
                if (SUCCEEDED(hr)) 
                { 
                    OnRTCSessionStateChangeEvent(pEvent); 
                    SAFE_RELEASE(pEvent); 
                }               
            } 
            break; 
        case RTCE_MESSAGING: 
            { 
                IRTCMessagingEvent * pEvent = NULL; 
                 
                 
                hr = pDisp->QueryInterface( IID_IRTCMessagingEvent, 
                                            (void **)&pEvent ); 
                if (SUCCEEDED(hr)) 
                { 
                    OnRTCMessagingEvent(pEvent); 
                    SAFE_RELEASE(pEvent); 
                }               
            } 
            break; 
        case RTCE_MEDIA: 
            { 
                IRTCMediaEvent * pEvent = NULL; 
                 
                hr = pDisp->QueryInterface( IID_IRTCMediaEvent, 
                                            (void **)&pEvent ); 
                if (SUCCEEDED(hr)) 
                { 
                    OnRTCMediaEvent(pEvent); 
                    SAFE_RELEASE(pEvent); 
                } 
            } 
            break; 
        case RTCE_INTENSITY: 
            { 
                IRTCIntensityEvent * pEvent = NULL; 
                 
                hr = pDisp->QueryInterface( IID_IRTCIntensityEvent, 
                                            (void **)&pEvent ); 
                if (SUCCEEDED(hr)) 
                { 
                    OnRTCIntensityEvent(pEvent); 
                    SAFE_RELEASE(pEvent); 
                } 
            } 
            break; 
        case RTCE_CLIENT: 
            { 
	                IRTCClientEvent * pEvent = NULL; 
                 
                hr = pDisp->QueryInterface( IID_IRTCClientEvent, 
                                            (void **)&pEvent ); 
                if (SUCCEEDED(hr)) 
                { 
                    OnRTCClientEvent(pEvent); 
                    SAFE_RELEASE(pEvent); 
                } 
            } 
            break; 
    } 
     
    SAFE_RELEASE(pDisp); 
    return S_OK; 
} 
void CAVDConfDlg::OnTuning()  
{ 
	if (m_pClient) 
		m_pClient->InvokeTuningWizard((OAHWND)m_hWnd);  //利用实时通信接口提供的一个向导,用来调节设置摄像头和麦克风 
} 
void CAVDConfDlg::OnRTCSessionStateChangeEvent(IRTCSessionStateChangeEvent *pEvent) 
{ 
    IRTCSession * pSession = NULL; 
    RTC_SESSION_STATE enState; 
    HRESULT hr; 
     
	hr = pEvent->get_State(&enState); 
    if (FAILED(hr)) 
    { 
         
        return; 
    } 
     
	hr = pEvent->get_Session(&pSession); 
    if (FAILED(hr)) 
    { 
         
        return; 
    } 
    switch ( enState ) 
	{ 
	case RTCSS_INCOMING: 
        { 
			 
             
            RTC_SESSION_TYPE enType; 
             
			hr = pSession->get_Type(&enType); 
            if (FAILED(hr)) 
            { 
                 
                SAFE_RELEASE(pSession); 
                return; 
            }             
            if (enType == RTCST_IM) 
            { 
                 
                m_pClient->PlayRing(RTCRT_MESSAGE, VARIANT_TRUE); 
            } 
            else             
            { 
                 
                if ((m_AVDlg) && (m_AVDlg.GetState() != RTCSS_IDLE)) 
                { 
                     
                     
                    pSession->Terminate(RTCTR_BUSY); 
                    SAFE_RELEASE(pSession); 
                    return; 
                } 
                     
                 
                IRTCEnumParticipants * pEnum = NULL; 
                IRTCParticipant * pParticipant = NULL; 
                hr = pSession->EnumerateParticipants(&pEnum); 
                if (FAILED(hr)) 
                { 
                     
                    SAFE_RELEASE(pSession); 
                    return; 
                } 
                hr = pEnum->Next(1, &pParticipant, NULL); 
                SAFE_RELEASE(pEnum); 
                if (hr != S_OK) 
                { 
                     
                    SAFE_RELEASE(pSession); 
                    return; 
                } 
                 
                BSTR bstrURI = NULL; 
                hr = pParticipant->get_UserURI(&bstrURI); 
                if (FAILED(hr)) 
                { 
                     
                    SAFE_RELEASE(pSession); 
                    SAFE_RELEASE(pParticipant); 
                    return; 
                } 
                 
                BSTR bstrName = NULL; 
                hr = pParticipant->get_Name(&bstrName); 
                SAFE_RELEASE(pParticipant); 
                if (FAILED(hr)) 
                { 
                     
                    SAFE_FREE_STRING(bstrURI); 
                    SAFE_RELEASE(pSession); 
                    return; 
                } 
                 
                m_pClient->PlayRing(RTCRT_PHONE, VARIANT_TRUE); 
                 
                BOOL fAccept; 
				CIncomingDlg	cDlg;	    
				SESSION_DATA	pData; 
				pData.bstrName = bstrName; 
				pData.bstrURI = bstrURI; 
				SetWindowLong ( m_hWnd, GWL_USERDATA, (LONG)&pData); 
				 
				 
				fAccept = (cDlg.DoModal() == IDOK); 
				 
				SAFE_FREE_STRING(bstrURI); 
				SAFE_FREE_STRING(bstrName); 
                     
                if (fAccept) 
                { 
                     
                    hr = pSession->Answer(); 
                    if (FAILED(hr)) 
                    { 
                         
                        SAFE_RELEASE(pSession); 
                        return; 
                    } 
                } 
                else 
                { 
                     
                    pSession->Terminate(RTCTR_REJECT); 
					SAFE_RELEASE(pSession); 
                    return; 
                } 
            }   
             
             
             
            hr = AddSession(pSession, enType); 
            if (FAILED(hr)) 
            { 
                 
                SAFE_RELEASE(pSession); 
                return; 
            } 
        }         
		break; 
	} 
     
	if (m_AVDlg) 
		m_AVDlg.SetState (enState); 
} 
void CAVDConfDlg::OnRTCMediaEvent(IRTCMediaEvent *pEvent) 
{ 
    long lMediaType; 
    RTC_MEDIA_EVENT_TYPE enType; 
    RTC_MEDIA_EVENT_REASON enReason; 
    HRESULT hr; 
	hr = pEvent->get_MediaType(&lMediaType); 
    if (FAILED(hr)) 
    { 
         
        return; 
    } 
    hr = pEvent->get_EventType(&enType);			   
    if (FAILED(hr)) 
    { 
         
        return; 
    } 
    hr = pEvent->get_EventReason(&enReason); 
    if (FAILED(hr)) 
    { 
         
        return; 
    } 
	if ((m_AVDlg) && (m_AVDlg.GetState () != RTCSS_IDLE)) 
    { 
         
        m_AVDlg.DeliverMedia(lMediaType, enType, enReason); 
    } 
} 
void CAVDConfDlg::OnRTCIntensityEvent(IRTCIntensityEvent *pEvent) 
{ 
    RTC_AUDIO_DEVICE enDevice; 
    long lLevel, lMin, lMax; 
    HRESULT hr; 
     
	hr = pEvent->get_Direction(&enDevice); 
    if (FAILED(hr)) 
    { 
         
        return; 
    } 
    hr = pEvent->get_Level(&lLevel); 
    if (FAILED(hr)) 
    { 
         
        return; 
    } 
    hr = pEvent->get_Min(&lMin); 
    if (FAILED(hr)) 
    { 
         
        return; 
    } 
    hr = pEvent->get_Max(&lMax); 
    if (FAILED(hr)) 
    { 
         
        return; 
    } 
     
    if ((lMax - lMin) == 0) 
    { 
        lLevel = 0; 
    } 
    else 
    { 
        lLevel = (lLevel - lMin) * 100 / (lMax - lMin); 
    } 
	if (m_AVDlg.GetState () != RTCSS_IDLE) 
    { 
         
        m_AVDlg.DeliverIntensity(enDevice, lLevel); 
    } 
} 
void CAVDConfDlg::OnRTCClientEvent(IRTCClientEvent *pEvent) 
{ 
    HRESULT hr; 
     
    RTC_CLIENT_EVENT_TYPE enEventType; 
    hr = pEvent->get_EventType(&enEventType); 
    if (FAILED(hr)) 
    { 
         
        return; 
    } 
    if ( enEventType == RTCCET_ASYNC_CLEANUP_DONE ) 
    { 
        m_pClient->Shutdown(); 
		SAFE_RELEASE(m_pClient); 
		 
         
        DestroyWindow(); 
    } 
    else 
    { 
        if (m_AVDlg.GetState() != RTCSS_IDLE) 
        { 
             
            m_AVDlg.DeliverClient(enEventType); 
        } 
    } 
} 
HRESULT CAVDConfDlg::AddSession(IRTCSession *pSession, RTC_SESSION_TYPE enType) 
{ 
     
    BOOL fAVSession = (enType != RTCST_IM); 
     
	 
	if (fAVSession) 
	{ 
		BOOL ret = m_AVDlg.Create (IDD_AVDLG,this); 
		if (!ret) 
		{ 
			MessageBox ( "Error Creating Dialog" ); 
		} 
		m_AVDlg.ShowWindow (SW_SHOW); 
		m_AVDlg.SetParentClient (m_pClient); 
		m_AVDlg.SetSession (pSession); 
	} 
	else 
	{ 
		BOOL ret = m_cMessageDlg.Create (IDD_MESSAGEDLG, this); 
		if (!ret) 
		{ 
			MessageBox ( "Error Creating Dialog" ); 
		} 
		m_cMessageDlg.ShowWindow (SW_SHOW); 
		m_cMessageDlg.SetParentClient ( m_pClient ); 
		m_cMessageDlg.SetSession (pSession); 
	} 
	return S_OK; 
} 
HRESULT CAVDConfDlg::OnRTCMessagingEvent(IRTCMessagingEvent *pEvent) 
{ 
    IRTCSession * pSession = NULL; 
     
    IRTCParticipant * pParticipant = NULL; 
    RTC_MESSAGING_EVENT_TYPE enType; 
    RTC_MESSAGING_USER_STATUS enStatus; 
    BSTR bstrContentType = NULL; 
    BSTR bstrMessage = NULL; 
    HRESULT hr; 
    hr = pEvent->get_Session(&pSession); 
    if (FAILED(hr)) 
    { 
         
        return S_FALSE; 
    } 
     
    hr = pEvent->get_EventType(&enType); 
    if (FAILED(hr)) 
    { 
         
        return S_FALSE; 
    } 
    hr = pEvent->get_Participant(&pParticipant); 
    if (FAILED(hr)) 
    { 
         
        return S_FALSE; 
    } 
    if (enType == RTCMSET_MESSAGE) 
    { 
        hr = pEvent->get_MessageHeader(&bstrContentType); 
        if (FAILED(hr)) 
        { 
             
            SAFE_RELEASE(pParticipant); 
            return S_FALSE; 
        } 
        hr = pEvent->get_Message(&bstrMessage); 
        if (FAILED(hr)) 
        { 
             
            SAFE_RELEASE(pParticipant); 
            SAFE_FREE_STRING(bstrContentType); 
            return S_FALSE; 
        } 
         
        if (m_cMessageDlg) 
			m_cMessageDlg.DeliverMessage(pParticipant, bstrContentType, bstrMessage); 
        SAFE_FREE_STRING(bstrContentType); 
        SAFE_FREE_STRING(bstrMessage); 
    } 
    else if (enType == RTCMSET_STATUS) 
    { 
        hr = pEvent->get_UserStatus(&enStatus); 
        if (FAILED(hr)) 
        { 
             
            return S_FALSE; 
        } 
         
        m_cMessageDlg.DeliverUserStatus(pParticipant, enStatus); 
    } 
    SAFE_RELEASE(pParticipant);  
	 
	return S_OK; 
} 
void CAVDConfDlg::OnOK()  
{ 
	if (m_pClient) 
	{ 
		m_pClient->StopT120Applets(); 
		m_pClient->PrepareForShutdown(); 
		if (m_pEvents) 
		{ 
			m_pEvents->Unadvise( m_pClient); 
		} 
	} 
	CDialog::OnOK(); 
} 
void CAVDConfDlg::OnCancel () 
{	 
	MessageBox ( "Please use the shutdown\nbutton to quit." ); 
	return; 
} 
void CAVDConfDlg::OnAboutbox()  
{ 
	CAboutDlg dlgAbout; 
	dlgAbout.DoModal(); 
}