Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

357 lines
8.9 KiB

  1. /*******************************************************************************
  2. Module: work.cpp
  3. Author: Qianbo Huai
  4. Abstract:
  5. implements the main function of the bridge test application
  6. *******************************************************************************/
  7. #include "stdafx.h"
  8. #include <stdio.h>
  9. #include "work.h"
  10. // command line
  11. LPSTR glpCmdLine = NULL;
  12. // dialog
  13. HWND ghDlg = NULL;
  14. // true: exit button on dialog was clicked
  15. bool gfExitButton = false;
  16. // bridge
  17. CBridge *gpBridge = NULL;
  18. // callback func in dialog
  19. BOOL
  20. CALLBACK
  21. MainDialogProc (
  22. HWND hDlg,
  23. UINT uMsg,
  24. WPARAM wParam,
  25. LPARAM lParam
  26. );
  27. // func to deal with TAPI events
  28. HRESULT
  29. OnTapiEvent (
  30. TAPI_EVENT TapiEvent,
  31. IDispatch *pEvent,
  32. LPWSTR *ppszMessage
  33. );
  34. // set status message on dialog
  35. void
  36. SetStatusMessage (LPWSTR pszMessage);
  37. /*//////////////////////////////////////////////////////////////////////////////
  38. WinMain
  39. ////*/
  40. int
  41. WINAPI
  42. WinMain (
  43. HINSTANCE hInst,
  44. HINSTANCE hPrevInst,
  45. LPSTR lpCmdLine,
  46. int nShowCmd
  47. )
  48. {
  49. // init com
  50. if (FAILED (CoInitializeEx(NULL, COINIT_MULTITHREADED)))
  51. {
  52. return 0;
  53. }
  54. // keep command line which determines which SDP to join
  55. glpCmdLine = lpCmdLine;
  56. // init CBridge
  57. gpBridge = new CBridge ();
  58. if (gpBridge==NULL)
  59. {
  60. printf ("Failed to init CBridge\n");
  61. return 0;
  62. }
  63. // init TAPI and H323 call listen
  64. if (FAILED(gpBridge->InitTapi()))
  65. {
  66. printf ("Failed to init TAPI\n");
  67. return 0;
  68. }
  69. // start dialog box
  70. if (!DialogBox (hInst, MAKEINTRESOURCE(IDD_MAINDLG), NULL, MainDialogProc))
  71. {
  72. printf ("Failed to init dialog\n");
  73. }
  74. // dialog finished
  75. gpBridge->ShutdownTapi ();
  76. delete gpBridge;
  77. CoUninitialize ();
  78. return 1;
  79. }
  80. /*//////////////////////////////////////////////////////////////////////////////
  81. Callback for dialog
  82. ////*/
  83. BOOL
  84. CALLBACK
  85. MainDialogProc (
  86. HWND hDlg,
  87. UINT uMsg,
  88. WPARAM wParam,
  89. LPARAM lParam
  90. )
  91. {
  92. LPWSTR pszMessage;
  93. switch (uMsg)
  94. {
  95. case WM_INITDIALOG:
  96. {
  97. ghDlg = hDlg;
  98. SetStatusMessage (L"Waiting for incoming H323 call");
  99. // disable disconnect button
  100. SendDlgItemMessage (
  101. ghDlg,
  102. IDC_DISCONNECT,
  103. BM_SETSTYLE,
  104. BS_PUSHBUTTON,
  105. 0
  106. );
  107. EnableWindow (
  108. GetDlgItem (ghDlg, IDC_DISCONNECT),
  109. FALSE
  110. );
  111. return 0;
  112. }
  113. case WM_PRIVATETAPIEVENT:
  114. {
  115. if (FAILED(OnTapiEvent ((TAPI_EVENT)wParam, (IDispatch *)lParam, &pszMessage)))
  116. {
  117. DoMessage (pszMessage);
  118. }
  119. return 0;
  120. }
  121. case WM_COMMAND:
  122. {
  123. switch (LOWORD(wParam))
  124. {
  125. case IDC_EXIT:
  126. {
  127. gpBridge->Clear ();
  128. gfExitButton = true;
  129. // check if in connection
  130. if (!IsWindowEnabled (GetDlgItem (ghDlg, IDC_DISCONNECT)))
  131. {
  132. // not in connection
  133. EndDialog (ghDlg, 0);
  134. }
  135. // else
  136. // remember exit button is clicked
  137. // do not call EndDialog because a disconnect event is to come
  138. return 1;
  139. }
  140. case IDC_DISCONNECT:
  141. {
  142. gpBridge->Clear ();
  143. SetStatusMessage (L"Waiting for incoming H323 call");
  144. // disable disconnect button
  145. SendDlgItemMessage (
  146. ghDlg,
  147. IDC_DISCONNECT,
  148. BM_SETSTYLE,
  149. BS_PUSHBUTTON,
  150. 0
  151. );
  152. EnableWindow (
  153. GetDlgItem (ghDlg, IDC_DISCONNECT),
  154. FALSE
  155. );
  156. // check if exit button is clicked
  157. if (gfExitButton)
  158. EndDialog (ghDlg, 0);
  159. return 1;
  160. }
  161. }
  162. return 0;
  163. }
  164. default:
  165. return 0;
  166. }
  167. }
  168. /*//////////////////////////////////////////////////////////////////////////////
  169. Popup message box
  170. ////*/
  171. WCHAR gMsgBoxTitle[] = L"TAPI 3.0 Bridge Test Application";
  172. void
  173. DoMessage (LPWSTR pszMessage)
  174. {
  175. MessageBox (
  176. ghDlg,
  177. pszMessage,
  178. gMsgBoxTitle,
  179. MB_OK
  180. );
  181. }
  182. /*//////////////////////////////////////////////////////////////////////////////
  183. Status message
  184. ////*/
  185. void
  186. SetStatusMessage (LPWSTR pszMessage)
  187. {
  188. SetDlgItemText (ghDlg, IDC_STATUS, pszMessage);
  189. }
  190. /*//////////////////////////////////////////////////////////////////////////////
  191. Deals with TAPI events
  192. ////*/
  193. HRESULT OnTapiEvent (
  194. TAPI_EVENT TapiEvent,
  195. IDispatch *pEvent,
  196. LPWSTR *ppszMessage
  197. )
  198. {
  199. HRESULT hr = S_OK;
  200. switch (TapiEvent)
  201. {
  202. case TE_CALLNOTIFICATION:
  203. {
  204. // if h323 call and to us, init h323 call
  205. hr = gpBridge->CreateH323Call (pEvent);
  206. if (FAILED(hr))
  207. *ppszMessage = L"H323 not created";
  208. break;
  209. }
  210. case TE_CALLSTATE:
  211. {
  212. CALL_STATE cs;
  213. ITCallStateEvent *pCallStateEvent = NULL;
  214. *ppszMessage = L"Call state failed";
  215. // get call state event
  216. hr = pEvent->QueryInterface (
  217. IID_ITCallStateEvent,
  218. (void **)&pCallStateEvent
  219. );
  220. if (FAILED(hr)) break;
  221. // get call state
  222. hr = pCallStateEvent->get_State (&cs);
  223. pCallStateEvent->Release ();
  224. if (FAILED(hr)) break;
  225. // if offering, connect
  226. if (CS_OFFERING == cs)
  227. {
  228. // check if h323 call created successful
  229. if (!gpBridge->HasH323Call ())
  230. {
  231. hr = S_OK;
  232. break;
  233. }
  234. // create sdp call
  235. hr = gpBridge->CreateSDPCall ();
  236. if (FAILED(hr)) {
  237. gpBridge->Clear ();
  238. *ppszMessage = L"Failed to create SDP call";
  239. break;
  240. }
  241. // bridge call
  242. hr = gpBridge->BridgeCalls ();
  243. if (FAILED(hr)) {
  244. gpBridge->Clear ();
  245. *ppszMessage = L"Failed to bridge calls";
  246. break;
  247. }
  248. SetStatusMessage (L"In call ...");
  249. // enable disconnect button
  250. SendDlgItemMessage (
  251. ghDlg,
  252. IDC_DISCONNECT,
  253. BM_SETSTYLE,
  254. BS_DEFPUSHBUTTON,
  255. 0
  256. );
  257. EnableWindow (
  258. GetDlgItem (ghDlg, IDC_DISCONNECT),
  259. TRUE
  260. );
  261. SetFocus (GetDlgItem (ghDlg, IDC_DISCONNECT));
  262. }
  263. // if disconnect
  264. else if (CS_DISCONNECTED == cs)
  265. {
  266. PostMessage (ghDlg, WM_COMMAND, IDC_DISCONNECT, 0);
  267. hr = S_OK;
  268. }
  269. break;
  270. }
  271. case TE_CALLMEDIA:
  272. {
  273. CALL_MEDIA_EVENT cme;
  274. ITCallMediaEvent *pCallMediaEvent;
  275. // get call media event
  276. hr = pEvent->QueryInterface (
  277. IID_ITCallMediaEvent,
  278. (void **)&pCallMediaEvent
  279. );
  280. if (FAILED(hr)) break;
  281. // get the event
  282. hr = pCallMediaEvent->get_Event (&cme);
  283. if (FAILED(hr)) break;
  284. // check media event
  285. switch (cme)
  286. {
  287. case CME_STREAM_FAIL:
  288. hr = E_FAIL;
  289. DoMessage( L"Stream failed");
  290. break;
  291. case CME_TERMINAL_FAIL:
  292. hr = E_FAIL;
  293. DoMessage( L"Terminal failed");
  294. break;
  295. default:
  296. break;
  297. }
  298. // we no longer need this interface.
  299. pCallMediaEvent->Release();
  300. break;
  301. }
  302. default:
  303. break;
  304. }
  305. pEvent->Release(); // we addrefed it CTAPIEventNotification::Event()
  306. return hr;
  307. }