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.

603 lines
18 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2012, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "talk/examples/peerconnection/client/main_wnd.h"
  28. #include <math.h>
  29. #include "talk/base/common.h"
  30. #include "talk/base/logging.h"
  31. #include "talk/examples/peerconnection/client/defaults.h"
  32. ATOM MainWnd::wnd_class_ = 0;
  33. const wchar_t MainWnd::kClassName[] = L"WebRTC_MainWnd";
  34. namespace {
  35. const char kConnecting[] = "Connecting... ";
  36. const char kNoVideoStreams[] = "(no video streams either way)";
  37. const char kNoIncomingStream[] = "(no incoming video)";
  38. void CalculateWindowSizeForText(HWND wnd, const wchar_t* text,
  39. size_t* width, size_t* height) {
  40. HDC dc = ::GetDC(wnd);
  41. RECT text_rc = {0};
  42. ::DrawText(dc, text, -1, &text_rc, DT_CALCRECT | DT_SINGLELINE);
  43. ::ReleaseDC(wnd, dc);
  44. RECT client, window;
  45. ::GetClientRect(wnd, &client);
  46. ::GetWindowRect(wnd, &window);
  47. *width = text_rc.right - text_rc.left;
  48. *width += (window.right - window.left) -
  49. (client.right - client.left);
  50. *height = text_rc.bottom - text_rc.top;
  51. *height += (window.bottom - window.top) -
  52. (client.bottom - client.top);
  53. }
  54. HFONT GetDefaultFont() {
  55. static HFONT font = reinterpret_cast<HFONT>(GetStockObject(DEFAULT_GUI_FONT));
  56. return font;
  57. }
  58. std::string GetWindowText(HWND wnd) {
  59. char text[MAX_PATH] = {0};
  60. ::GetWindowTextA(wnd, &text[0], ARRAYSIZE(text));
  61. return text;
  62. }
  63. void AddListBoxItem(HWND listbox, const std::string& str, LPARAM item_data) {
  64. LRESULT index = ::SendMessageA(listbox, LB_ADDSTRING, 0,
  65. reinterpret_cast<LPARAM>(str.c_str()));
  66. ::SendMessageA(listbox, LB_SETITEMDATA, index, item_data);
  67. }
  68. } // namespace
  69. MainWnd::MainWnd()
  70. : ui_(CONNECT_TO_SERVER), wnd_(NULL), edit1_(NULL), edit2_(NULL),
  71. label1_(NULL), label2_(NULL), button_(NULL), listbox_(NULL),
  72. destroyed_(false), callback_(NULL), nested_msg_(NULL) {
  73. }
  74. MainWnd::~MainWnd() {
  75. ASSERT(!IsWindow());
  76. }
  77. bool MainWnd::Create() {
  78. ASSERT(wnd_ == NULL);
  79. if (!RegisterWindowClass())
  80. return false;
  81. ui_thread_id_ = ::GetCurrentThreadId();
  82. wnd_ = ::CreateWindowExW(WS_EX_OVERLAPPEDWINDOW, kClassName, L"WebRTC",
  83. WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_CLIPCHILDREN,
  84. CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  85. NULL, NULL, GetModuleHandle(NULL), this);
  86. ::SendMessage(wnd_, WM_SETFONT, reinterpret_cast<WPARAM>(GetDefaultFont()),
  87. TRUE);
  88. CreateChildWindows();
  89. SwitchToConnectUI();
  90. return wnd_ != NULL;
  91. }
  92. bool MainWnd::Destroy() {
  93. BOOL ret = FALSE;
  94. if (IsWindow()) {
  95. ret = ::DestroyWindow(wnd_);
  96. }
  97. return ret != FALSE;
  98. }
  99. void MainWnd::RegisterObserver(MainWndCallback* callback) {
  100. callback_ = callback;
  101. }
  102. bool MainWnd::IsWindow() {
  103. return wnd_ && ::IsWindow(wnd_) != FALSE;
  104. }
  105. bool MainWnd::PreTranslateMessage(MSG* msg) {
  106. bool ret = false;
  107. if (msg->message == WM_CHAR) {
  108. if (msg->wParam == VK_TAB) {
  109. HandleTabbing();
  110. ret = true;
  111. } else if (msg->wParam == VK_RETURN) {
  112. OnDefaultAction();
  113. ret = true;
  114. } else if (msg->wParam == VK_ESCAPE) {
  115. if (callback_) {
  116. if (ui_ == STREAMING) {
  117. callback_->DisconnectFromCurrentPeer();
  118. } else {
  119. callback_->DisconnectFromServer();
  120. }
  121. }
  122. }
  123. } else if (msg->hwnd == NULL && msg->message == UI_THREAD_CALLBACK) {
  124. callback_->UIThreadCallback(static_cast<int>(msg->wParam),
  125. reinterpret_cast<void*>(msg->lParam));
  126. ret = true;
  127. }
  128. return ret;
  129. }
  130. void MainWnd::SwitchToConnectUI() {
  131. ASSERT(IsWindow());
  132. LayoutPeerListUI(false);
  133. ui_ = CONNECT_TO_SERVER;
  134. LayoutConnectUI(true);
  135. ::SetFocus(edit1_);
  136. }
  137. void MainWnd::SwitchToPeerList(const Peers& peers) {
  138. LayoutConnectUI(false);
  139. ::SendMessage(listbox_, LB_RESETCONTENT, 0, 0);
  140. AddListBoxItem(listbox_, "List of currently connected peers:", -1);
  141. Peers::const_iterator i = peers.begin();
  142. for (; i != peers.end(); ++i)
  143. AddListBoxItem(listbox_, i->second.c_str(), i->first);
  144. ui_ = LIST_PEERS;
  145. LayoutPeerListUI(true);
  146. ::SetFocus(listbox_);
  147. }
  148. void MainWnd::SwitchToStreamingUI() {
  149. LayoutConnectUI(false);
  150. LayoutPeerListUI(false);
  151. ui_ = STREAMING;
  152. }
  153. void MainWnd::MessageBox(const char* caption, const char* text, bool is_error) {
  154. DWORD flags = MB_OK;
  155. if (is_error)
  156. flags |= MB_ICONERROR;
  157. ::MessageBoxA(handle(), text, caption, flags);
  158. }
  159. void MainWnd::StartLocalRenderer(webrtc::VideoTrackInterface* local_video) {
  160. local_renderer_.reset(new VideoRenderer(handle(), 1, 1, local_video));
  161. }
  162. void MainWnd::StopLocalRenderer() {
  163. local_renderer_.reset();
  164. }
  165. void MainWnd::StartRemoteRenderer(webrtc::VideoTrackInterface* remote_video) {
  166. remote_renderer_.reset(new VideoRenderer(handle(), 1, 1, remote_video));
  167. }
  168. void MainWnd::StopRemoteRenderer() {
  169. remote_renderer_.reset();
  170. }
  171. void MainWnd::QueueUIThreadCallback(int msg_id, void* data) {
  172. ::PostThreadMessage(ui_thread_id_, UI_THREAD_CALLBACK,
  173. static_cast<WPARAM>(msg_id), reinterpret_cast<LPARAM>(data));
  174. }
  175. void MainWnd::OnPaint() {
  176. PAINTSTRUCT ps;
  177. ::BeginPaint(handle(), &ps);
  178. RECT rc;
  179. ::GetClientRect(handle(), &rc);
  180. VideoRenderer* local_renderer = local_renderer_.get();
  181. VideoRenderer* remote_renderer = remote_renderer_.get();
  182. if (ui_ == STREAMING && remote_renderer && local_renderer) {
  183. AutoLock<VideoRenderer> local_lock(local_renderer);
  184. AutoLock<VideoRenderer> remote_lock(remote_renderer);
  185. const BITMAPINFO& bmi = remote_renderer->bmi();
  186. int height = abs(bmi.bmiHeader.biHeight);
  187. int width = bmi.bmiHeader.biWidth;
  188. const uint8* image = remote_renderer->image();
  189. if (image != NULL) {
  190. HDC dc_mem = ::CreateCompatibleDC(ps.hdc);
  191. ::SetStretchBltMode(dc_mem, HALFTONE);
  192. // Set the map mode so that the ratio will be maintained for us.
  193. HDC all_dc[] = { ps.hdc, dc_mem };
  194. for (int i = 0; i < ARRAY_SIZE(all_dc); ++i) {
  195. SetMapMode(all_dc[i], MM_ISOTROPIC);
  196. SetWindowExtEx(all_dc[i], width, height, NULL);
  197. SetViewportExtEx(all_dc[i], rc.right, rc.bottom, NULL);
  198. }
  199. HBITMAP bmp_mem = ::CreateCompatibleBitmap(ps.hdc, rc.right, rc.bottom);
  200. HGDIOBJ bmp_old = ::SelectObject(dc_mem, bmp_mem);
  201. POINT logical_area = { rc.right, rc.bottom };
  202. DPtoLP(ps.hdc, &logical_area, 1);
  203. HBRUSH brush = ::CreateSolidBrush(RGB(0, 0, 0));
  204. RECT logical_rect = {0, 0, logical_area.x, logical_area.y };
  205. ::FillRect(dc_mem, &logical_rect, brush);
  206. ::DeleteObject(brush);
  207. int x = (logical_area.x / 2) - (width / 2);
  208. int y = (logical_area.y / 2) - (height / 2);
  209. StretchDIBits(dc_mem, x, y, width, height,
  210. 0, 0, width, height, image, &bmi, DIB_RGB_COLORS, SRCCOPY);
  211. if ((rc.right - rc.left) > 200 && (rc.bottom - rc.top) > 200) {
  212. const BITMAPINFO& bmi = local_renderer->bmi();
  213. image = local_renderer->image();
  214. int thumb_width = bmi.bmiHeader.biWidth / 4;
  215. int thumb_height = abs(bmi.bmiHeader.biHeight) / 4;
  216. StretchDIBits(dc_mem,
  217. logical_area.x - thumb_width - 10,
  218. logical_area.y - thumb_height - 10,
  219. thumb_width, thumb_height,
  220. 0, 0, bmi.bmiHeader.biWidth, -bmi.bmiHeader.biHeight,
  221. image, &bmi, DIB_RGB_COLORS, SRCCOPY);
  222. }
  223. BitBlt(ps.hdc, 0, 0, logical_area.x, logical_area.y,
  224. dc_mem, 0, 0, SRCCOPY);
  225. // Cleanup.
  226. ::SelectObject(dc_mem, bmp_old);
  227. ::DeleteObject(bmp_mem);
  228. ::DeleteDC(dc_mem);
  229. } else {
  230. // We're still waiting for the video stream to be initialized.
  231. HBRUSH brush = ::CreateSolidBrush(RGB(0, 0, 0));
  232. ::FillRect(ps.hdc, &rc, brush);
  233. ::DeleteObject(brush);
  234. HGDIOBJ old_font = ::SelectObject(ps.hdc, GetDefaultFont());
  235. ::SetTextColor(ps.hdc, RGB(0xff, 0xff, 0xff));
  236. ::SetBkMode(ps.hdc, TRANSPARENT);
  237. std::string text(kConnecting);
  238. if (!local_renderer->image()) {
  239. text += kNoVideoStreams;
  240. } else {
  241. text += kNoIncomingStream;
  242. }
  243. ::DrawTextA(ps.hdc, text.c_str(), -1, &rc,
  244. DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  245. ::SelectObject(ps.hdc, old_font);
  246. }
  247. } else {
  248. HBRUSH brush = ::CreateSolidBrush(::GetSysColor(COLOR_WINDOW));
  249. ::FillRect(ps.hdc, &rc, brush);
  250. ::DeleteObject(brush);
  251. }
  252. ::EndPaint(handle(), &ps);
  253. }
  254. void MainWnd::OnDestroyed() {
  255. PostQuitMessage(0);
  256. }
  257. void MainWnd::OnDefaultAction() {
  258. if (!callback_)
  259. return;
  260. if (ui_ == CONNECT_TO_SERVER) {
  261. std::string server(GetWindowText(edit1_));
  262. std::string port_str(GetWindowText(edit2_));
  263. int port = port_str.length() ? atoi(port_str.c_str()) : 0;
  264. callback_->StartLogin(server, port);
  265. } else if (ui_ == LIST_PEERS) {
  266. LRESULT sel = ::SendMessage(listbox_, LB_GETCURSEL, 0, 0);
  267. if (sel != LB_ERR) {
  268. LRESULT peer_id = ::SendMessage(listbox_, LB_GETITEMDATA, sel, 0);
  269. if (peer_id != -1 && callback_) {
  270. callback_->ConnectToPeer(peer_id);
  271. }
  272. }
  273. } else {
  274. MessageBoxA(wnd_, "OK!", "Yeah", MB_OK);
  275. }
  276. }
  277. bool MainWnd::OnMessage(UINT msg, WPARAM wp, LPARAM lp, LRESULT* result) {
  278. switch (msg) {
  279. case WM_ERASEBKGND:
  280. *result = TRUE;
  281. return true;
  282. case WM_PAINT:
  283. OnPaint();
  284. return true;
  285. case WM_SETFOCUS:
  286. if (ui_ == CONNECT_TO_SERVER) {
  287. SetFocus(edit1_);
  288. } else if (ui_ == LIST_PEERS) {
  289. SetFocus(listbox_);
  290. }
  291. return true;
  292. case WM_SIZE:
  293. if (ui_ == CONNECT_TO_SERVER) {
  294. LayoutConnectUI(true);
  295. } else if (ui_ == LIST_PEERS) {
  296. LayoutPeerListUI(true);
  297. }
  298. break;
  299. case WM_CTLCOLORSTATIC:
  300. *result = reinterpret_cast<LRESULT>(GetSysColorBrush(COLOR_WINDOW));
  301. return true;
  302. case WM_COMMAND:
  303. if (button_ == reinterpret_cast<HWND>(lp)) {
  304. if (BN_CLICKED == HIWORD(wp))
  305. OnDefaultAction();
  306. } else if (listbox_ == reinterpret_cast<HWND>(lp)) {
  307. if (LBN_DBLCLK == HIWORD(wp)) {
  308. OnDefaultAction();
  309. }
  310. }
  311. return true;
  312. case WM_CLOSE:
  313. if (callback_)
  314. callback_->Close();
  315. break;
  316. }
  317. return false;
  318. }
  319. // static
  320. LRESULT CALLBACK MainWnd::WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) {
  321. MainWnd* me = reinterpret_cast<MainWnd*>(
  322. ::GetWindowLongPtr(hwnd, GWL_USERDATA));
  323. if (!me && WM_CREATE == msg) {
  324. CREATESTRUCT* cs = reinterpret_cast<CREATESTRUCT*>(lp);
  325. me = reinterpret_cast<MainWnd*>(cs->lpCreateParams);
  326. me->wnd_ = hwnd;
  327. ::SetWindowLongPtr(hwnd, GWL_USERDATA, reinterpret_cast<LONG_PTR>(me));
  328. }
  329. LRESULT result = 0;
  330. if (me) {
  331. void* prev_nested_msg = me->nested_msg_;
  332. me->nested_msg_ = &msg;
  333. bool handled = me->OnMessage(msg, wp, lp, &result);
  334. if (WM_NCDESTROY == msg) {
  335. me->destroyed_ = true;
  336. } else if (!handled) {
  337. result = ::DefWindowProc(hwnd, msg, wp, lp);
  338. }
  339. if (me->destroyed_ && prev_nested_msg == NULL) {
  340. me->OnDestroyed();
  341. me->wnd_ = NULL;
  342. me->destroyed_ = false;
  343. }
  344. me->nested_msg_ = prev_nested_msg;
  345. } else {
  346. result = ::DefWindowProc(hwnd, msg, wp, lp);
  347. }
  348. return result;
  349. }
  350. // static
  351. bool MainWnd::RegisterWindowClass() {
  352. if (wnd_class_)
  353. return true;
  354. WNDCLASSEX wcex = { sizeof(WNDCLASSEX) };
  355. wcex.style = CS_DBLCLKS;
  356. wcex.hInstance = GetModuleHandle(NULL);
  357. wcex.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW + 1);
  358. wcex.hCursor = ::LoadCursor(NULL, IDC_ARROW);
  359. wcex.lpfnWndProc = &WndProc;
  360. wcex.lpszClassName = kClassName;
  361. wnd_class_ = ::RegisterClassEx(&wcex);
  362. ASSERT(wnd_class_ != 0);
  363. return wnd_class_ != 0;
  364. }
  365. void MainWnd::CreateChildWindow(HWND* wnd, MainWnd::ChildWindowID id,
  366. const wchar_t* class_name, DWORD control_style,
  367. DWORD ex_style) {
  368. if (::IsWindow(*wnd))
  369. return;
  370. // Child windows are invisible at first, and shown after being resized.
  371. DWORD style = WS_CHILD | control_style;
  372. *wnd = ::CreateWindowEx(ex_style, class_name, L"", style,
  373. 100, 100, 100, 100, wnd_,
  374. reinterpret_cast<HMENU>(id),
  375. GetModuleHandle(NULL), NULL);
  376. ASSERT(::IsWindow(*wnd) != FALSE);
  377. ::SendMessage(*wnd, WM_SETFONT, reinterpret_cast<WPARAM>(GetDefaultFont()),
  378. TRUE);
  379. }
  380. void MainWnd::CreateChildWindows() {
  381. // Create the child windows in tab order.
  382. CreateChildWindow(&label1_, LABEL1_ID, L"Static", ES_CENTER | ES_READONLY, 0);
  383. CreateChildWindow(&edit1_, EDIT_ID, L"Edit",
  384. ES_LEFT | ES_NOHIDESEL | WS_TABSTOP, WS_EX_CLIENTEDGE);
  385. CreateChildWindow(&label2_, LABEL2_ID, L"Static", ES_CENTER | ES_READONLY, 0);
  386. CreateChildWindow(&edit2_, EDIT_ID, L"Edit",
  387. ES_LEFT | ES_NOHIDESEL | WS_TABSTOP, WS_EX_CLIENTEDGE);
  388. CreateChildWindow(&button_, BUTTON_ID, L"Button", BS_CENTER | WS_TABSTOP, 0);
  389. CreateChildWindow(&listbox_, LISTBOX_ID, L"ListBox",
  390. LBS_HASSTRINGS | LBS_NOTIFY, WS_EX_CLIENTEDGE);
  391. ::SetWindowTextA(edit1_, GetDefaultServerName().c_str());
  392. ::SetWindowTextA(edit2_, "8888");
  393. }
  394. void MainWnd::LayoutConnectUI(bool show) {
  395. struct Windows {
  396. HWND wnd;
  397. const wchar_t* text;
  398. size_t width;
  399. size_t height;
  400. } windows[] = {
  401. { label1_, L"Server" },
  402. { edit1_, L"XXXyyyYYYgggXXXyyyYYYggg" },
  403. { label2_, L":" },
  404. { edit2_, L"XyXyX" },
  405. { button_, L"Connect" },
  406. };
  407. if (show) {
  408. const size_t kSeparator = 5;
  409. size_t total_width = (ARRAYSIZE(windows) - 1) * kSeparator;
  410. for (size_t i = 0; i < ARRAYSIZE(windows); ++i) {
  411. CalculateWindowSizeForText(windows[i].wnd, windows[i].text,
  412. &windows[i].width, &windows[i].height);
  413. total_width += windows[i].width;
  414. }
  415. RECT rc;
  416. ::GetClientRect(wnd_, &rc);
  417. size_t x = (rc.right / 2) - (total_width / 2);
  418. size_t y = rc.bottom / 2;
  419. for (size_t i = 0; i < ARRAYSIZE(windows); ++i) {
  420. size_t top = y - (windows[i].height / 2);
  421. ::MoveWindow(windows[i].wnd, x, top, windows[i].width, windows[i].height,
  422. TRUE);
  423. x += kSeparator + windows[i].width;
  424. if (windows[i].text[0] != 'X')
  425. ::SetWindowText(windows[i].wnd, windows[i].text);
  426. ::ShowWindow(windows[i].wnd, SW_SHOWNA);
  427. }
  428. } else {
  429. for (size_t i = 0; i < ARRAYSIZE(windows); ++i) {
  430. ::ShowWindow(windows[i].wnd, SW_HIDE);
  431. }
  432. }
  433. }
  434. void MainWnd::LayoutPeerListUI(bool show) {
  435. if (show) {
  436. RECT rc;
  437. ::GetClientRect(wnd_, &rc);
  438. ::MoveWindow(listbox_, 0, 0, rc.right, rc.bottom, TRUE);
  439. ::ShowWindow(listbox_, SW_SHOWNA);
  440. } else {
  441. ::ShowWindow(listbox_, SW_HIDE);
  442. InvalidateRect(wnd_, NULL, TRUE);
  443. }
  444. }
  445. void MainWnd::HandleTabbing() {
  446. bool shift = ((::GetAsyncKeyState(VK_SHIFT) & 0x8000) != 0);
  447. UINT next_cmd = shift ? GW_HWNDPREV : GW_HWNDNEXT;
  448. UINT loop_around_cmd = shift ? GW_HWNDLAST : GW_HWNDFIRST;
  449. HWND focus = GetFocus(), next;
  450. do {
  451. next = ::GetWindow(focus, next_cmd);
  452. if (IsWindowVisible(next) &&
  453. (GetWindowLong(next, GWL_STYLE) & WS_TABSTOP)) {
  454. break;
  455. }
  456. if (!next) {
  457. next = ::GetWindow(focus, loop_around_cmd);
  458. if (IsWindowVisible(next) &&
  459. (GetWindowLong(next, GWL_STYLE) & WS_TABSTOP)) {
  460. break;
  461. }
  462. }
  463. focus = next;
  464. } while (true);
  465. ::SetFocus(next);
  466. }
  467. //
  468. // MainWnd::VideoRenderer
  469. //
  470. MainWnd::VideoRenderer::VideoRenderer(
  471. HWND wnd, int width, int height,
  472. webrtc::VideoTrackInterface* track_to_render)
  473. : wnd_(wnd), rendered_track_(track_to_render) {
  474. ::InitializeCriticalSection(&buffer_lock_);
  475. ZeroMemory(&bmi_, sizeof(bmi_));
  476. bmi_.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  477. bmi_.bmiHeader.biPlanes = 1;
  478. bmi_.bmiHeader.biBitCount = 32;
  479. bmi_.bmiHeader.biCompression = BI_RGB;
  480. bmi_.bmiHeader.biWidth = width;
  481. bmi_.bmiHeader.biHeight = -height;
  482. bmi_.bmiHeader.biSizeImage = width * height *
  483. (bmi_.bmiHeader.biBitCount >> 3);
  484. rendered_track_->AddRenderer(this);
  485. }
  486. MainWnd::VideoRenderer::~VideoRenderer() {
  487. rendered_track_->RemoveRenderer(this);
  488. ::DeleteCriticalSection(&buffer_lock_);
  489. }
  490. void MainWnd::VideoRenderer::SetSize(int width, int height) {
  491. AutoLock<VideoRenderer> lock(this);
  492. bmi_.bmiHeader.biWidth = width;
  493. bmi_.bmiHeader.biHeight = -height;
  494. bmi_.bmiHeader.biSizeImage = width * height *
  495. (bmi_.bmiHeader.biBitCount >> 3);
  496. image_.reset(new uint8[bmi_.bmiHeader.biSizeImage]);
  497. }
  498. void MainWnd::VideoRenderer::RenderFrame(const cricket::VideoFrame* frame) {
  499. if (!frame)
  500. return;
  501. {
  502. AutoLock<VideoRenderer> lock(this);
  503. ASSERT(image_.get() != NULL);
  504. frame->ConvertToRgbBuffer(cricket::FOURCC_ARGB,
  505. image_.get(),
  506. bmi_.bmiHeader.biSizeImage,
  507. bmi_.bmiHeader.biWidth *
  508. bmi_.bmiHeader.biBitCount / 8);
  509. }
  510. InvalidateRect(wnd_, NULL, TRUE);
  511. }