0AVDConfDlg.cpp
上传用户:tuheem
上传日期:2007-05-01
资源大小:21889k
文件大小:17k
源码类别:

多媒体编程

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "rtccore.h"
  3. #include "AVDConf.h"
  4. #include "AVDConfDlg.h"
  5. #include "MakeCallDlg.h"
  6. #include "incomingdlg.h"
  7. #include "defines.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. BSTR g_bstrAddr;
  14. class CAboutDlg : public CDialog
  15. {
  16. public:
  17. CAboutDlg();
  18. enum { IDD = IDD_ABOUTBOX };
  19. protected:
  20. virtual void DoDataExchange(CDataExchange* pDX);    
  21. protected:
  22. DECLARE_MESSAGE_MAP()
  23. };
  24. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  25. {
  26. }
  27. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  28. {
  29. CDialog::DoDataExchange(pDX);
  30. }
  31. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  32. END_MESSAGE_MAP()
  33. CAVDConfDlg::CAVDConfDlg(CWnd* pParent )
  34. : CDialog(CAVDConfDlg::IDD, pParent)
  35. {
  36. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  37. }
  38. void CAVDConfDlg::DoDataExchange(CDataExchange* pDX)
  39. {
  40. CDialog::DoDataExchange(pDX);
  41. DDX_Control(pDX, IDC_WHITEBOARD, m_cWhiteboard);
  42. DDX_Control(pDX, IDC_MESSAGING, m_cMessaging);
  43. DDX_Control(pDX, IDC_TUNING, m_cTuning);
  44.   DDX_Control(pDX, IDC_MAKECALL, m_cMakeCall);
  45.   DDX_Control(pDX, IDC_APPCALL, m_cAppCall);
  46. }
  47. BEGIN_MESSAGE_MAP(CAVDConfDlg, CDialog)
  48. ON_WM_PAINT()
  49. ON_WM_QUERYDRAGICON()
  50. ON_BN_CLICKED(IDC_MAKECALL, OnMakecall)
  51. ON_BN_CLICKED(IDC_APPCALL, OnAppcall)
  52. ON_BN_CLICKED(IDC_TUNING, OnTuning)
  53. ON_BN_CLICKED(IDC_MESSAGING, OnMessaging)
  54. ON_BN_CLICKED(IDM_ABOUTBOX, OnAboutbox)
  55. ON_BN_CLICKED(IDC_WHITEBOARD, OnWhiteboard)
  56. END_MESSAGE_MAP()
  57. BOOL CAVDConfDlg::OnInitDialog()
  58. {
  59. CDialog::OnInitDialog();
  60. SetIcon(m_hIcon, TRUE);
  61. SetIcon(m_hIcon, FALSE);
  62. if (InitRTCClient() != S_OK)
  63. {
  64. MessageBox ( "Initialization Error" );
  65. m_cMakeCall.EnableWindow ( FALSE );
  66. m_cAppCall.EnableWindow ( FALSE );
  67. m_cMessaging.EnableWindow ( FALSE );
  68. m_cTuning.EnableWindow ( FALSE );
  69. m_cWhiteboard.EnableWindow ( FALSE );
  70. }
  71. return TRUE;  
  72. }
  73. void CAVDConfDlg::OnPaint() 
  74. {
  75. if (IsIconic())
  76. {
  77. CPaintDC dc(this); 
  78. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  79. int cxIcon = GetSystemMetrics(SM_CXICON);
  80. int cyIcon = GetSystemMetrics(SM_CYICON);
  81. CRect rect;
  82. GetClientRect(&rect);
  83. int x = (rect.Width() - cxIcon + 1) / 2;
  84. int y = (rect.Height() - cyIcon + 1) / 2;
  85. dc.DrawIcon(x, y, m_hIcon);
  86. }
  87. else
  88. {
  89. CDialog::OnPaint();
  90. }
  91. }
  92. HCURSOR CAVDConfDlg::OnQueryDragIcon()
  93. {
  94. return (HCURSOR) m_hIcon;
  95. }
  96. void CAVDConfDlg::OnMakecall() 
  97. {
  98. MakeCallDlg cDlg;
  99. if (cDlg.DoModal() == IDOK)
  100. {
  101. /
  102. MakeCall (RTCST_PC_TO_PC, g_bstrAddr);
  103. }
  104. }
  105. void CAVDConfDlg::OnMessaging() 
  106. {
  107.   MakeCallDlg cDlg;
  108.  
  109. if (cDlg.DoModal() == IDOK)
  110.   {  
  111. MakeCall (RTCST_IM, g_bstrAddr);
  112. }
  113. }
  114. void CAVDConfDlg::OnAppcall() 
  115. {
  116. HRESULT hr;
  117. VARIANT_BOOL vbool = VARIANT_FALSE;
  118. m_pClient->get_IsT120AppletRunning(RTCTA_APPSHARING, &vbool);
  119. if (vbool == VARIANT_FALSE)
  120. hr = m_pClient->StartT120Applet ( RTCTA_APPSHARING );
  121. }
  122. }
  123. void CAVDConfDlg::OnWhiteboard() 
  124. {
  125. HRESULT hr;
  126. VARIANT_BOOL vbool = VARIANT_FALSE;
  127. m_pClient->get_IsT120AppletRunning(RTCTA_WHITEBOARD, &vbool);
  128. if (vbool == VARIANT_FALSE)
  129. hr = m_pClient->StartT120Applet ( RTCTA_WHITEBOARD );
  130. }
  131. }
  132. HRESULT CAVDConfDlg::MakeCall(RTC_SESSION_TYPE enType, BSTR bstrURI)
  133. {
  134.     HRESULT hr;
  135. if (enType != RTCST_IM)
  136.     {
  137.         
  138. if ((m_AVDlg) && (m_AVDlg.GetState () != RTCSS_IDLE))
  139.         {
  140. MessageBoxW (m_hWnd, L"An audio/video call is in progress!", NULL, MB_OK );
  141.             return S_FALSE;
  142.         }
  143.     }
  144.     IRTCSession * pSession = NULL;
  145.     hr = m_pClient->CreateSession(enType, NULL, NULL, 0, &pSession);
  146.     if (FAILED(hr))
  147.     {
  148.         return hr;
  149.     }
  150.     
  151.     hr = pSession->AddParticipant(bstrURI, NULL, &m_Participant);
  152. if (FAILED(hr))
  153.     {
  154.         SAFE_RELEASE(pSession);
  155.         return hr;
  156.     }
  157.     
  158.     hr = AddSession(pSession, enType);
  159.     
  160.     if (FAILED(hr))
  161.     {  
  162.             
  163.         return hr;
  164.     }
  165.     return S_OK;
  166. }
  167. HRESULT CAVDConfDlg::InitRTCClient()
  168. {
  169. HRESULT hr;
  170. m_pClient = NULL;
  171. m_pEvents = NULL;
  172. hr = CoInitialize(NULL);
  173. if (FAILED(hr))
  174. {
  175. return hr;
  176. }
  177. hr = CoCreateInstance (CLSID_RTCClient, NULL,
  178. CLSCTX_INPROC_SERVER, IID_IRTCClient,
  179. (LPVOID *)&m_pClient);
  180. if (FAILED(hr))
  181. {
  182. return hr;
  183. }
  184. hr = m_pClient->Initialize();
  185. if (FAILED(hr))
  186. {
  187. SAFE_RELEASE(m_pClient);
  188. return hr;
  189. }
  190. m_pClient->SetPreferredMediaTypes ( RTCMT_ALL, VARIANT_TRUE );
  191. long lEventMask = RTCEF_SESSION_STATE_CHANGE |
  192.   RTCEF_MESSAGING |
  193.   RTCEF_MEDIA |
  194.   RTCEF_INTENSITY |
  195.   RTCEF_CLIENT;
  196. hr = m_pClient->put_EventFilter( lEventMask );
  197.     if ( FAILED(hr) )
  198.     {
  199.         SAFE_RELEASE(m_pClient);
  200.         return hr;
  201.     }
  202.     m_pEvents = new CRTCEvents;
  203.     if (!m_pEvents)
  204.     {
  205.         SAFE_RELEASE(m_pClient);
  206.         return hr;
  207.     }
  208.     hr = m_pEvents->Advise( m_pClient, m_hWnd );
  209.     if ( FAILED(hr) )
  210.     {
  211.         SAFE_RELEASE(m_pClient);
  212.         return hr;
  213.     }
  214.     
  215.     hr = m_pClient->put_ListenForIncomingSessions(RTCLM_BOTH); 
  216.     if ( FAILED(hr) )
  217.     {
  218.     
  219.         SAFE_RELEASE(m_pClient);
  220.         return hr;
  221.     }
  222. if (m_AVDlg)
  223. m_AVDlg.SetState (RTCSS_IDLE);
  224. return S_OK;
  225. }
  226. LRESULT CAVDConfDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
  227. {
  228. switch (message)
  229. {
  230. case WM_RTC_EVENT:
  231. OnRTCEvent (message, wParam, lParam);
  232. break;
  233. }
  234. return CDialog::WindowProc(message, wParam, lParam);
  235. }
  236. HRESULT CAVDConfDlg::OnRTCEvent(UINT message, WPARAM wParam, LPARAM lParam)
  237. {
  238.     IDispatch * pDisp = (IDispatch *)lParam;
  239.     RTC_EVENT enEvent = (RTC_EVENT)wParam;
  240.     HRESULT hr;
  241.    
  242.     switch ( wParam )
  243.     {
  244.         case RTCE_SESSION_STATE_CHANGE:
  245.             {
  246.                 IRTCSessionStateChangeEvent * pEvent = NULL;
  247.                 
  248.                 
  249. hr = pDisp->QueryInterface( IID_IRTCSessionStateChangeEvent,
  250.                                             (void **)&pEvent );
  251.                 if (SUCCEEDED(hr))
  252.                 {
  253.                     OnRTCSessionStateChangeEvent(pEvent);
  254.                     SAFE_RELEASE(pEvent);
  255.                 }              
  256.             }
  257.             break;
  258.         case RTCE_MESSAGING:
  259.             {
  260.                 IRTCMessagingEvent * pEvent = NULL;
  261.                 
  262.                 
  263.                 hr = pDisp->QueryInterface( IID_IRTCMessagingEvent,
  264.                                             (void **)&pEvent );
  265.                 if (SUCCEEDED(hr))
  266.                 {
  267.                     OnRTCMessagingEvent(pEvent);
  268.                     SAFE_RELEASE(pEvent);
  269.                 }              
  270.             }
  271.             break;
  272.         case RTCE_MEDIA:
  273.             {
  274.                 IRTCMediaEvent * pEvent = NULL;
  275.                 
  276.                 hr = pDisp->QueryInterface( IID_IRTCMediaEvent,
  277.                                             (void **)&pEvent );
  278.                 if (SUCCEEDED(hr))
  279.                 {
  280.                     OnRTCMediaEvent(pEvent);
  281.                     SAFE_RELEASE(pEvent);
  282.                 }
  283.             }
  284.             break;
  285.         case RTCE_INTENSITY:
  286.             {
  287.                 IRTCIntensityEvent * pEvent = NULL;
  288.                 
  289.                 hr = pDisp->QueryInterface( IID_IRTCIntensityEvent,
  290.                                             (void **)&pEvent );
  291.                 if (SUCCEEDED(hr))
  292.                 {
  293.                     OnRTCIntensityEvent(pEvent);
  294.                     SAFE_RELEASE(pEvent);
  295.                 }
  296.             }
  297.             break;
  298.         case RTCE_CLIENT:
  299.             {
  300.                 IRTCClientEvent * pEvent = NULL;
  301.                 
  302.                 hr = pDisp->QueryInterface( IID_IRTCClientEvent,
  303.                                             (void **)&pEvent );
  304.                 if (SUCCEEDED(hr))
  305.                 {
  306.                     OnRTCClientEvent(pEvent);
  307.                     SAFE_RELEASE(pEvent);
  308.                 }
  309.             }
  310.             break;
  311.     }
  312.     
  313.     SAFE_RELEASE(pDisp);
  314.     return S_OK;
  315. }
  316. void CAVDConfDlg::OnTuning() 
  317. {
  318. if (m_pClient)
  319. m_pClient->InvokeTuningWizard((OAHWND)m_hWnd);
  320. }
  321. void CAVDConfDlg::OnRTCSessionStateChangeEvent(IRTCSessionStateChangeEvent *pEvent)
  322. {
  323.     IRTCSession * pSession = NULL;
  324.     RTC_SESSION_STATE enState;
  325.     HRESULT hr;
  326.     
  327. hr = pEvent->get_State(&enState);
  328.     if (FAILED(hr))
  329.     {
  330.         
  331.         return;
  332.     }
  333.     
  334. hr = pEvent->get_Session(&pSession);
  335.     if (FAILED(hr))
  336.     {
  337.         
  338.         return;
  339.     }
  340.     switch ( enState )
  341. {
  342. case RTCSS_INCOMING:
  343.         {
  344.             
  345.             RTC_SESSION_TYPE enType;
  346.             
  347. hr = pSession->get_Type(&enType);
  348.             if (FAILED(hr))
  349.             {
  350.                 
  351.                 SAFE_RELEASE(pSession);
  352.                 return;
  353.             }            
  354.             if (enType == RTCST_IM)
  355.             {
  356.                 
  357.                 m_pClient->PlayRing(RTCRT_MESSAGE, VARIANT_TRUE);
  358.             }
  359.             else            
  360.             {
  361.                 
  362.                 if ((m_AVDlg) && (m_AVDlg.GetState() != RTCSS_IDLE))
  363.                 {
  364.                     
  365.                     
  366.                     pSession->Terminate(RTCTR_BUSY);
  367.                     SAFE_RELEASE(pSession);
  368.                     return;
  369.                 }
  370.                     
  371.                 
  372.                 IRTCEnumParticipants * pEnum = NULL;
  373.                 IRTCParticipant * pParticipant = NULL;
  374.                 hr = pSession->EnumerateParticipants(&pEnum);
  375.                 if (FAILED(hr))
  376.                 {
  377.                     
  378.                     SAFE_RELEASE(pSession);
  379.                     return;
  380.                 }
  381.                 hr = pEnum->Next(1, &pParticipant, NULL);
  382.                 SAFE_RELEASE(pEnum);
  383.                 if (hr != S_OK)
  384.                 {
  385.                     
  386.                     SAFE_RELEASE(pSession);
  387.                     return;
  388.                 }
  389.                 
  390.                 BSTR bstrURI = NULL;
  391.                 hr = pParticipant->get_UserURI(&bstrURI);
  392.                 if (FAILED(hr))
  393.                 {
  394.                     
  395.                     SAFE_RELEASE(pSession);
  396.                     SAFE_RELEASE(pParticipant);
  397.                     return;
  398.                 }
  399.                 
  400.                 BSTR bstrName = NULL;
  401.                 hr = pParticipant->get_Name(&bstrName);
  402.                 SAFE_RELEASE(pParticipant);
  403.                 if (FAILED(hr))
  404.                 {
  405.                     
  406.                     SAFE_FREE_STRING(bstrURI);
  407.                     SAFE_RELEASE(pSession);
  408.                     return;
  409.                 }
  410.                 
  411.                 m_pClient->PlayRing(RTCRT_PHONE, VARIANT_TRUE);
  412.                 
  413.                 BOOL fAccept;
  414. CIncomingDlg cDlg;    
  415. SESSION_DATA pData;
  416. pData.bstrName = bstrName;
  417. pData.bstrURI = bstrURI;
  418. SetWindowLong ( m_hWnd, GWL_USERDATA, (LONG)&pData);
  419. fAccept = (cDlg.DoModal() == IDOK);
  420. SAFE_FREE_STRING(bstrURI);
  421. SAFE_FREE_STRING(bstrName);
  422.                     
  423.                 if (fAccept)
  424.                 {
  425.                     
  426.                     hr = pSession->Answer();
  427.                     if (FAILED(hr))
  428.                     {
  429.                         
  430.                         SAFE_RELEASE(pSession);
  431.                         return;
  432.                     }
  433.                 }
  434.                 else
  435.                 {
  436.                     
  437.                     pSession->Terminate(RTCTR_REJECT);
  438. SAFE_RELEASE(pSession);
  439.                     return;
  440.                 }
  441.             }  
  442.             
  443.             
  444.             
  445.             hr = AddSession(pSession, enType);
  446.             if (FAILED(hr))
  447.             {
  448.                 
  449.                 SAFE_RELEASE(pSession);
  450.                 return;
  451.             }
  452.         }        
  453. break;
  454. }
  455.     
  456. if (m_AVDlg)
  457. m_AVDlg.SetState (enState);
  458. }
  459. void CAVDConfDlg::OnRTCMediaEvent(IRTCMediaEvent *pEvent)
  460. {
  461.     long lMediaType;
  462.     RTC_MEDIA_EVENT_TYPE enType;
  463.     RTC_MEDIA_EVENT_REASON enReason;
  464.     HRESULT hr;
  465. hr = pEvent->get_MediaType(&lMediaType);
  466.     if (FAILED(hr))
  467.     {
  468.         
  469.         return;
  470.     }
  471.     hr = pEvent->get_EventType(&enType);   
  472.     if (FAILED(hr))
  473.     {
  474.         
  475.         return;
  476.     }
  477.     hr = pEvent->get_EventReason(&enReason);
  478.     if (FAILED(hr))
  479.     {
  480.         
  481.         return;
  482.     }
  483. if ((m_AVDlg) && (m_AVDlg.GetState () != RTCSS_IDLE))
  484.     {
  485.         
  486.         m_AVDlg.DeliverMedia(lMediaType, enType, enReason);
  487.     }
  488. }
  489. void CAVDConfDlg::OnRTCIntensityEvent(IRTCIntensityEvent *pEvent)
  490. {
  491.     RTC_AUDIO_DEVICE enDevice;
  492.     long lLevel, lMin, lMax;
  493.     HRESULT hr;
  494.     
  495. hr = pEvent->get_Direction(&enDevice);
  496.     if (FAILED(hr))
  497.     {
  498.         
  499.         return;
  500.     }
  501.     hr = pEvent->get_Level(&lLevel);
  502.     if (FAILED(hr))
  503.     {
  504.         
  505.         return;
  506.     }
  507.     hr = pEvent->get_Min(&lMin);
  508.     if (FAILED(hr))
  509.     {
  510.         
  511.         return;
  512.     }
  513.     hr = pEvent->get_Max(&lMax);
  514.     if (FAILED(hr))
  515.     {
  516.         
  517.         return;
  518.     }
  519.     
  520.     if ((lMax - lMin) == 0)
  521.     {
  522.         lLevel = 0;
  523.     }
  524.     else
  525.     {
  526.         lLevel = (lLevel - lMin) * 100 / (lMax - lMin);
  527.     }
  528. if (m_AVDlg.GetState () != RTCSS_IDLE)
  529.     {
  530.         
  531.         m_AVDlg.DeliverIntensity(enDevice, lLevel);
  532.     }
  533. }
  534. void CAVDConfDlg::OnRTCClientEvent(IRTCClientEvent *pEvent)
  535. {
  536.     HRESULT hr;
  537.     
  538.     RTC_CLIENT_EVENT_TYPE enEventType;
  539.     hr = pEvent->get_EventType(&enEventType);
  540.     if (FAILED(hr))
  541.     {
  542.         
  543.         return;
  544.     }
  545.     if ( enEventType == RTCCET_ASYNC_CLEANUP_DONE )
  546.     {
  547.         m_pClient->Shutdown();
  548. SAFE_RELEASE(m_pClient);
  549.         
  550.         DestroyWindow();
  551.     }
  552.     else
  553.     {
  554.         if (m_AVDlg.GetState() != RTCSS_IDLE)
  555.         {
  556.             
  557.             m_AVDlg.DeliverClient(enEventType);
  558.         }
  559.     }
  560. }
  561. HRESULT CAVDConfDlg::AddSession(IRTCSession *pSession, RTC_SESSION_TYPE enType)
  562. {
  563.     
  564.     BOOL fAVSession = (enType != RTCST_IM);
  565.     
  566. if (fAVSession)
  567. {
  568. BOOL ret = m_AVDlg.Create (IDD_AVDLG,this);
  569. if (!ret)
  570. {
  571. MessageBox ( "Error Creating Dialog" );
  572. }
  573. m_AVDlg.ShowWindow (SW_SHOW);
  574. m_AVDlg.SetParentClient (m_pClient);
  575. m_AVDlg.SetSession (pSession);
  576. }
  577. else
  578. {
  579. BOOL ret = m_cMessageDlg.Create (IDD_MESSAGEDLG, this);
  580. if (!ret)
  581. {
  582. MessageBox ( "Error Creating Dialog" );
  583. }
  584. m_cMessageDlg.ShowWindow (SW_SHOW);
  585. m_cMessageDlg.SetParentClient ( m_pClient );
  586. m_cMessageDlg.SetSession (pSession);
  587. }
  588. return S_OK;
  589. }
  590. HRESULT CAVDConfDlg::OnRTCMessagingEvent(IRTCMessagingEvent *pEvent)
  591. {
  592.     IRTCSession * pSession = NULL;
  593.     
  594.     IRTCParticipant * pParticipant = NULL;
  595.     RTC_MESSAGING_EVENT_TYPE enType;
  596.     RTC_MESSAGING_USER_STATUS enStatus;
  597.     BSTR bstrContentType = NULL;
  598.     BSTR bstrMessage = NULL;
  599.     HRESULT hr;
  600.     hr = pEvent->get_Session(&pSession);
  601.     if (FAILED(hr))
  602.     {
  603.         
  604.         return S_FALSE;
  605.     }
  606.     
  607.     hr = pEvent->get_EventType(&enType);
  608.     if (FAILED(hr))
  609.     {
  610.         
  611.         return S_FALSE;
  612.     }
  613.     hr = pEvent->get_Participant(&pParticipant);
  614.     if (FAILED(hr))
  615.     {
  616.         
  617.         return S_FALSE;
  618.     }
  619.     if (enType == RTCMSET_MESSAGE)
  620.     {
  621.         hr = pEvent->get_MessageHeader(&bstrContentType);
  622.         if (FAILED(hr))
  623.         {
  624.             
  625.             SAFE_RELEASE(pParticipant);
  626.             return S_FALSE;
  627.         }
  628.         hr = pEvent->get_Message(&bstrMessage);
  629.         if (FAILED(hr))
  630.         {
  631.             
  632.             SAFE_RELEASE(pParticipant);
  633.             SAFE_FREE_STRING(bstrContentType);
  634.             return S_FALSE;
  635.         }
  636.         
  637.         if (m_cMessageDlg)
  638. m_cMessageDlg.DeliverMessage(pParticipant, bstrContentType, bstrMessage);
  639.         SAFE_FREE_STRING(bstrContentType);
  640.         SAFE_FREE_STRING(bstrMessage);
  641.     }
  642.     else if (enType == RTCMSET_STATUS)
  643.     {
  644.         hr = pEvent->get_UserStatus(&enStatus);
  645.         if (FAILED(hr))
  646.         {
  647.             
  648.             return S_FALSE;
  649.         }
  650.         
  651.         m_cMessageDlg.DeliverUserStatus(pParticipant, enStatus);
  652.     }
  653.     SAFE_RELEASE(pParticipant); 
  654. return S_OK;
  655. }
  656. void CAVDConfDlg::OnOK() 
  657. {
  658. if (m_pClient)
  659. {
  660. m_pClient->StopT120Applets();
  661. m_pClient->PrepareForShutdown();
  662. if (m_pEvents)
  663. {
  664. m_pEvents->Unadvise( m_pClient);
  665. }
  666. }
  667. CDialog::OnOK();
  668. }
  669. void CAVDConfDlg::OnCancel ()
  670. {
  671. MessageBox ( "Please use the shutdownnbutton to quit." );
  672. return;
  673. }
  674. void CAVDConfDlg::OnAboutbox() 
  675. {
  676. CAboutDlg dlgAbout;
  677. dlgAbout.DoModal();
  678. }