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.

633 lines
19 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2012 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "examples/peerconnection/client/main_wnd.h"
  11. #include <math.h>
  12. #include "api/video/i420_buffer.h"
  13. #include "examples/peerconnection/client/defaults.h"
  14. #include "rtc_base/arraysize.h"
  15. #include "rtc_base/checks.h"
  16. #include "rtc_base/logging.h"
  17. #include "third_party/libyuv/include/libyuv/convert_argb.h"
  18. ATOM MainWnd::wnd_class_ = 0;
  19. const wchar_t MainWnd::kClassName[] = L"WebRTC_MainWnd";
  20. namespace {
  21. const char kConnecting[] = "Connecting... ";
  22. const char kNoVideoStreams[] = "(no video streams either way)";
  23. const char kNoIncomingStream[] = "(no incoming video)";
  24. void CalculateWindowSizeForText(HWND wnd,
  25. const wchar_t* text,
  26. size_t* width,
  27. size_t* height) {
  28. HDC dc = ::GetDC(wnd);
  29. RECT text_rc = {0};
  30. ::DrawTextW(dc, text, -1, &text_rc, DT_CALCRECT | DT_SINGLELINE);
  31. ::ReleaseDC(wnd, dc);
  32. RECT client, window;
  33. ::GetClientRect(wnd, &client);
  34. ::GetWindowRect(wnd, &window);
  35. *width = text_rc.right - text_rc.left;
  36. *width += (window.right - window.left) - (client.right - client.left);
  37. *height = text_rc.bottom - text_rc.top;
  38. *height += (window.bottom - window.top) - (client.bottom - client.top);
  39. }
  40. HFONT GetDefaultFont() {
  41. static HFONT font = reinterpret_cast<HFONT>(GetStockObject(DEFAULT_GUI_FONT));
  42. return font;
  43. }
  44. std::string GetWindowText(HWND wnd) {
  45. char text[MAX_PATH] = {0};
  46. ::GetWindowTextA(wnd, &text[0], ARRAYSIZE(text));
  47. return text;
  48. }
  49. void AddListBoxItem(HWND listbox, const std::string& str, LPARAM item_data) {
  50. LRESULT index = ::SendMessageA(listbox, LB_ADDSTRING, 0,
  51. reinterpret_cast<LPARAM>(str.c_str()));
  52. ::SendMessageA(listbox, LB_SETITEMDATA, index, item_data);
  53. }
  54. } // namespace
  55. MainWnd::MainWnd(const char* server,
  56. int port,
  57. bool auto_connect,
  58. bool auto_call)
  59. : ui_(CONNECT_TO_SERVER),
  60. wnd_(NULL),
  61. edit1_(NULL),
  62. edit2_(NULL),
  63. label1_(NULL),
  64. label2_(NULL),
  65. button_(NULL),
  66. listbox_(NULL),
  67. destroyed_(false),
  68. nested_msg_(NULL),
  69. callback_(NULL),
  70. server_(server),
  71. auto_connect_(auto_connect),
  72. auto_call_(auto_call) {
  73. char buffer[10];
  74. snprintf(buffer, sizeof(buffer), "%i", port);
  75. port_ = buffer;
  76. }
  77. MainWnd::~MainWnd() {
  78. RTC_DCHECK(!IsWindow());
  79. }
  80. bool MainWnd::Create() {
  81. RTC_DCHECK(wnd_ == NULL);
  82. if (!RegisterWindowClass())
  83. return false;
  84. ui_thread_id_ = ::GetCurrentThreadId();
  85. wnd_ =
  86. ::CreateWindowExW(WS_EX_OVERLAPPEDWINDOW, kClassName, L"WebRTC",
  87. WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_CLIPCHILDREN,
  88. CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  89. CW_USEDEFAULT, NULL, NULL, GetModuleHandle(NULL), this);
  90. ::SendMessage(wnd_, WM_SETFONT, reinterpret_cast<WPARAM>(GetDefaultFont()),
  91. TRUE);
  92. CreateChildWindows();
  93. SwitchToConnectUI();
  94. return wnd_ != NULL;
  95. }
  96. bool MainWnd::Destroy() {
  97. BOOL ret = FALSE;
  98. if (IsWindow()) {
  99. ret = ::DestroyWindow(wnd_);
  100. }
  101. return ret != FALSE;
  102. }
  103. void MainWnd::RegisterObserver(MainWndCallback* callback) {
  104. callback_ = callback;
  105. }
  106. bool MainWnd::IsWindow() {
  107. return wnd_ && ::IsWindow(wnd_) != FALSE;
  108. }
  109. bool MainWnd::PreTranslateMessage(MSG* msg) {
  110. bool ret = false;
  111. if (msg->message == WM_CHAR) {
  112. if (msg->wParam == VK_TAB) {
  113. HandleTabbing();
  114. ret = true;
  115. } else if (msg->wParam == VK_RETURN) {
  116. OnDefaultAction();
  117. ret = true;
  118. } else if (msg->wParam == VK_ESCAPE) {
  119. if (callback_) {
  120. if (ui_ == STREAMING) {
  121. callback_->DisconnectFromCurrentPeer();
  122. } else {
  123. callback_->DisconnectFromServer();
  124. }
  125. }
  126. }
  127. } else if (msg->hwnd == NULL && msg->message == UI_THREAD_CALLBACK) {
  128. callback_->UIThreadCallback(static_cast<int>(msg->wParam),
  129. reinterpret_cast<void*>(msg->lParam));
  130. ret = true;
  131. }
  132. return ret;
  133. }
  134. void MainWnd::SwitchToConnectUI() {
  135. RTC_DCHECK(IsWindow());
  136. LayoutPeerListUI(false);
  137. ui_ = CONNECT_TO_SERVER;
  138. LayoutConnectUI(true);
  139. ::SetFocus(edit1_);
  140. if (auto_connect_)
  141. ::PostMessage(button_, BM_CLICK, 0, 0);
  142. }
  143. void MainWnd::SwitchToPeerList(const Peers& peers) {
  144. LayoutConnectUI(false);
  145. ::SendMessage(listbox_, LB_RESETCONTENT, 0, 0);
  146. AddListBoxItem(listbox_, "List of currently connected peers:", -1);
  147. Peers::const_iterator i = peers.begin();
  148. for (; i != peers.end(); ++i)
  149. AddListBoxItem(listbox_, i->second.c_str(), i->first);
  150. ui_ = LIST_PEERS;
  151. LayoutPeerListUI(true);
  152. ::SetFocus(listbox_);
  153. if (auto_call_ && peers.begin() != peers.end()) {
  154. // Get the number of items in the list
  155. LRESULT count = ::SendMessage(listbox_, LB_GETCOUNT, 0, 0);
  156. if (count != LB_ERR) {
  157. // Select the last item in the list
  158. LRESULT selection = ::SendMessage(listbox_, LB_SETCURSEL, count - 1, 0);
  159. if (selection != LB_ERR)
  160. ::PostMessage(wnd_, WM_COMMAND,
  161. MAKEWPARAM(GetDlgCtrlID(listbox_), LBN_DBLCLK),
  162. reinterpret_cast<LPARAM>(listbox_));
  163. }
  164. }
  165. }
  166. void MainWnd::SwitchToStreamingUI() {
  167. LayoutConnectUI(false);
  168. LayoutPeerListUI(false);
  169. ui_ = STREAMING;
  170. }
  171. void MainWnd::MessageBox(const char* caption, const char* text, bool is_error) {
  172. DWORD flags = MB_OK;
  173. if (is_error)
  174. flags |= MB_ICONERROR;
  175. ::MessageBoxA(handle(), text, caption, flags);
  176. }
  177. void MainWnd::StartLocalRenderer(webrtc::VideoTrackInterface* local_video) {
  178. local_renderer_.reset(new VideoRenderer(handle(), 1, 1, local_video));
  179. }
  180. void MainWnd::StopLocalRenderer() {
  181. local_renderer_.reset();
  182. }
  183. void MainWnd::StartRemoteRenderer(webrtc::VideoTrackInterface* remote_video) {
  184. remote_renderer_.reset(new VideoRenderer(handle(), 1, 1, remote_video));
  185. }
  186. void MainWnd::StopRemoteRenderer() {
  187. remote_renderer_.reset();
  188. }
  189. void MainWnd::QueueUIThreadCallback(int msg_id, void* data) {
  190. ::PostThreadMessage(ui_thread_id_, UI_THREAD_CALLBACK,
  191. static_cast<WPARAM>(msg_id),
  192. reinterpret_cast<LPARAM>(data));
  193. }
  194. void MainWnd::OnPaint() {
  195. PAINTSTRUCT ps;
  196. ::BeginPaint(handle(), &ps);
  197. RECT rc;
  198. ::GetClientRect(handle(), &rc);
  199. VideoRenderer* local_renderer = local_renderer_.get();
  200. VideoRenderer* remote_renderer = remote_renderer_.get();
  201. if (ui_ == STREAMING && remote_renderer && local_renderer) {
  202. AutoLock<VideoRenderer> local_lock(local_renderer);
  203. AutoLock<VideoRenderer> remote_lock(remote_renderer);
  204. const BITMAPINFO& bmi = remote_renderer->bmi();
  205. int height = abs(bmi.bmiHeader.biHeight);
  206. int width = bmi.bmiHeader.biWidth;
  207. const uint8_t* image = remote_renderer->image();
  208. if (image != NULL) {
  209. HDC dc_mem = ::CreateCompatibleDC(ps.hdc);
  210. ::SetStretchBltMode(dc_mem, HALFTONE);
  211. // Set the map mode so that the ratio will be maintained for us.
  212. HDC all_dc[] = {ps.hdc, dc_mem};
  213. for (size_t i = 0; i < arraysize(all_dc); ++i) {
  214. SetMapMode(all_dc[i], MM_ISOTROPIC);
  215. SetWindowExtEx(all_dc[i], width, height, NULL);
  216. SetViewportExtEx(all_dc[i], rc.right, rc.bottom, NULL);
  217. }
  218. HBITMAP bmp_mem = ::CreateCompatibleBitmap(ps.hdc, rc.right, rc.bottom);
  219. HGDIOBJ bmp_old = ::SelectObject(dc_mem, bmp_mem);
  220. POINT logical_area = {rc.right, rc.bottom};
  221. DPtoLP(ps.hdc, &logical_area, 1);
  222. HBRUSH brush = ::CreateSolidBrush(RGB(0, 0, 0));
  223. RECT logical_rect = {0, 0, logical_area.x, logical_area.y};
  224. ::FillRect(dc_mem, &logical_rect, brush);
  225. ::DeleteObject(brush);
  226. int x = (logical_area.x / 2) - (width / 2);
  227. int y = (logical_area.y / 2) - (height / 2);
  228. StretchDIBits(dc_mem, x, y, width, height, 0, 0, width, height, image,
  229. &bmi, DIB_RGB_COLORS, SRCCOPY);
  230. if ((rc.right - rc.left) > 200 && (rc.bottom - rc.top) > 200) {
  231. const BITMAPINFO& bmi = local_renderer->bmi();
  232. image = local_renderer->image();
  233. int thumb_width = bmi.bmiHeader.biWidth / 4;
  234. int thumb_height = abs(bmi.bmiHeader.biHeight) / 4;
  235. StretchDIBits(dc_mem, logical_area.x - thumb_width - 10,
  236. logical_area.y - thumb_height - 10, thumb_width,
  237. thumb_height, 0, 0, bmi.bmiHeader.biWidth,
  238. -bmi.bmiHeader.biHeight, image, &bmi, DIB_RGB_COLORS,
  239. SRCCOPY);
  240. }
  241. BitBlt(ps.hdc, 0, 0, logical_area.x, logical_area.y, dc_mem, 0, 0,
  242. SRCCOPY);
  243. // Cleanup.
  244. ::SelectObject(dc_mem, bmp_old);
  245. ::DeleteObject(bmp_mem);
  246. ::DeleteDC(dc_mem);
  247. } else {
  248. // We're still waiting for the video stream to be initialized.
  249. HBRUSH brush = ::CreateSolidBrush(RGB(0, 0, 0));
  250. ::FillRect(ps.hdc, &rc, brush);
  251. ::DeleteObject(brush);
  252. HGDIOBJ old_font = ::SelectObject(ps.hdc, GetDefaultFont());
  253. ::SetTextColor(ps.hdc, RGB(0xff, 0xff, 0xff));
  254. ::SetBkMode(ps.hdc, TRANSPARENT);
  255. std::string text(kConnecting);
  256. if (!local_renderer->image()) {
  257. text += kNoVideoStreams;
  258. } else {
  259. text += kNoIncomingStream;
  260. }
  261. ::DrawTextA(ps.hdc, text.c_str(), -1, &rc,
  262. DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  263. ::SelectObject(ps.hdc, old_font);
  264. }
  265. } else {
  266. HBRUSH brush = ::CreateSolidBrush(::GetSysColor(COLOR_WINDOW));
  267. ::FillRect(ps.hdc, &rc, brush);
  268. ::DeleteObject(brush);
  269. }
  270. ::EndPaint(handle(), &ps);
  271. }
  272. void MainWnd::OnDestroyed() {
  273. PostQuitMessage(0);
  274. }
  275. void MainWnd::OnDefaultAction() {
  276. if (!callback_)
  277. return;
  278. if (ui_ == CONNECT_TO_SERVER) {
  279. std::string server(GetWindowText(edit1_));
  280. std::string port_str(GetWindowText(edit2_));
  281. int port = port_str.length() ? atoi(port_str.c_str()) : 0;
  282. callback_->StartLogin(server, port);
  283. } else if (ui_ == LIST_PEERS) {
  284. LRESULT sel = ::SendMessage(listbox_, LB_GETCURSEL, 0, 0);
  285. if (sel != LB_ERR) {
  286. LRESULT peer_id = ::SendMessage(listbox_, LB_GETITEMDATA, sel, 0);
  287. if (peer_id != -1 && callback_) {
  288. callback_->ConnectToPeer(peer_id);
  289. }
  290. }
  291. } else {
  292. ::MessageBoxA(wnd_, "OK!", "Yeah", MB_OK);
  293. }
  294. }
  295. bool MainWnd::OnMessage(UINT msg, WPARAM wp, LPARAM lp, LRESULT* result) {
  296. switch (msg) {
  297. case WM_ERASEBKGND:
  298. *result = TRUE;
  299. return true;
  300. case WM_PAINT:
  301. OnPaint();
  302. return true;
  303. case WM_SETFOCUS:
  304. if (ui_ == CONNECT_TO_SERVER) {
  305. SetFocus(edit1_);
  306. } else if (ui_ == LIST_PEERS) {
  307. SetFocus(listbox_);
  308. }
  309. return true;
  310. case WM_SIZE:
  311. if (ui_ == CONNECT_TO_SERVER) {
  312. LayoutConnectUI(true);
  313. } else if (ui_ == LIST_PEERS) {
  314. LayoutPeerListUI(true);
  315. }
  316. break;
  317. case WM_CTLCOLORSTATIC:
  318. *result = reinterpret_cast<LRESULT>(GetSysColorBrush(COLOR_WINDOW));
  319. return true;
  320. case WM_COMMAND:
  321. if (button_ == reinterpret_cast<HWND>(lp)) {
  322. if (BN_CLICKED == HIWORD(wp))
  323. OnDefaultAction();
  324. } else if (listbox_ == reinterpret_cast<HWND>(lp)) {
  325. if (LBN_DBLCLK == HIWORD(wp)) {
  326. OnDefaultAction();
  327. }
  328. }
  329. return true;
  330. case WM_CLOSE:
  331. if (callback_)
  332. callback_->Close();
  333. break;
  334. }
  335. return false;
  336. }
  337. // static
  338. LRESULT CALLBACK MainWnd::WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) {
  339. MainWnd* me =
  340. reinterpret_cast<MainWnd*>(::GetWindowLongPtr(hwnd, GWLP_USERDATA));
  341. if (!me && WM_CREATE == msg) {
  342. CREATESTRUCT* cs = reinterpret_cast<CREATESTRUCT*>(lp);
  343. me = reinterpret_cast<MainWnd*>(cs->lpCreateParams);
  344. me->wnd_ = hwnd;
  345. ::SetWindowLongPtr(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(me));
  346. }
  347. LRESULT result = 0;
  348. if (me) {
  349. void* prev_nested_msg = me->nested_msg_;
  350. me->nested_msg_ = &msg;
  351. bool handled = me->OnMessage(msg, wp, lp, &result);
  352. if (WM_NCDESTROY == msg) {
  353. me->destroyed_ = true;
  354. } else if (!handled) {
  355. result = ::DefWindowProc(hwnd, msg, wp, lp);
  356. }
  357. if (me->destroyed_ && prev_nested_msg == NULL) {
  358. me->OnDestroyed();
  359. me->wnd_ = NULL;
  360. me->destroyed_ = false;
  361. }
  362. me->nested_msg_ = prev_nested_msg;
  363. } else {
  364. result = ::DefWindowProc(hwnd, msg, wp, lp);
  365. }
  366. return result;
  367. }
  368. // static
  369. bool MainWnd::RegisterWindowClass() {
  370. if (wnd_class_)
  371. return true;
  372. WNDCLASSEXW wcex = {sizeof(WNDCLASSEX)};
  373. wcex.style = CS_DBLCLKS;
  374. wcex.hInstance = GetModuleHandle(NULL);
  375. wcex.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW + 1);
  376. wcex.hCursor = ::LoadCursor(NULL, IDC_ARROW);
  377. wcex.lpfnWndProc = &WndProc;
  378. wcex.lpszClassName = kClassName;
  379. wnd_class_ = ::RegisterClassExW(&wcex);
  380. RTC_DCHECK(wnd_class_ != 0);
  381. return wnd_class_ != 0;
  382. }
  383. void MainWnd::CreateChildWindow(HWND* wnd,
  384. MainWnd::ChildWindowID id,
  385. const wchar_t* class_name,
  386. DWORD control_style,
  387. DWORD ex_style) {
  388. if (::IsWindow(*wnd))
  389. return;
  390. // Child windows are invisible at first, and shown after being resized.
  391. DWORD style = WS_CHILD | control_style;
  392. *wnd = ::CreateWindowExW(ex_style, class_name, L"", style, 100, 100, 100, 100,
  393. wnd_, reinterpret_cast<HMENU>(id),
  394. GetModuleHandle(NULL), NULL);
  395. RTC_DCHECK(::IsWindow(*wnd) != FALSE);
  396. ::SendMessage(*wnd, WM_SETFONT, reinterpret_cast<WPARAM>(GetDefaultFont()),
  397. TRUE);
  398. }
  399. void MainWnd::CreateChildWindows() {
  400. // Create the child windows in tab order.
  401. CreateChildWindow(&label1_, LABEL1_ID, L"Static", ES_CENTER | ES_READONLY, 0);
  402. CreateChildWindow(&edit1_, EDIT_ID, L"Edit",
  403. ES_LEFT | ES_NOHIDESEL | WS_TABSTOP, WS_EX_CLIENTEDGE);
  404. CreateChildWindow(&label2_, LABEL2_ID, L"Static", ES_CENTER | ES_READONLY, 0);
  405. CreateChildWindow(&edit2_, EDIT_ID, L"Edit",
  406. ES_LEFT | ES_NOHIDESEL | WS_TABSTOP, WS_EX_CLIENTEDGE);
  407. CreateChildWindow(&button_, BUTTON_ID, L"Button", BS_CENTER | WS_TABSTOP, 0);
  408. CreateChildWindow(&listbox_, LISTBOX_ID, L"ListBox",
  409. LBS_HASSTRINGS | LBS_NOTIFY, WS_EX_CLIENTEDGE);
  410. ::SetWindowTextA(edit1_, server_.c_str());
  411. ::SetWindowTextA(edit2_, port_.c_str());
  412. }
  413. void MainWnd::LayoutConnectUI(bool show) {
  414. struct Windows {
  415. HWND wnd;
  416. const wchar_t* text;
  417. size_t width;
  418. size_t height;
  419. } windows[] = {
  420. {label1_, L"Server"}, {edit1_, L"XXXyyyYYYgggXXXyyyYYYggg"},
  421. {label2_, L":"}, {edit2_, L"XyXyX"},
  422. {button_, L"Connect"},
  423. };
  424. if (show) {
  425. const size_t kSeparator = 5;
  426. size_t total_width = (ARRAYSIZE(windows) - 1) * kSeparator;
  427. for (size_t i = 0; i < ARRAYSIZE(windows); ++i) {
  428. CalculateWindowSizeForText(windows[i].wnd, windows[i].text,
  429. &windows[i].width, &windows[i].height);
  430. total_width += windows[i].width;
  431. }
  432. RECT rc;
  433. ::GetClientRect(wnd_, &rc);
  434. size_t x = (rc.right / 2) - (total_width / 2);
  435. size_t y = rc.bottom / 2;
  436. for (size_t i = 0; i < ARRAYSIZE(windows); ++i) {
  437. size_t top = y - (windows[i].height / 2);
  438. ::MoveWindow(windows[i].wnd, static_cast<int>(x), static_cast<int>(top),
  439. static_cast<int>(windows[i].width),
  440. static_cast<int>(windows[i].height), TRUE);
  441. x += kSeparator + windows[i].width;
  442. if (windows[i].text[0] != 'X')
  443. ::SetWindowTextW(windows[i].wnd, windows[i].text);
  444. ::ShowWindow(windows[i].wnd, SW_SHOWNA);
  445. }
  446. } else {
  447. for (size_t i = 0; i < ARRAYSIZE(windows); ++i) {
  448. ::ShowWindow(windows[i].wnd, SW_HIDE);
  449. }
  450. }
  451. }
  452. void MainWnd::LayoutPeerListUI(bool show) {
  453. if (show) {
  454. RECT rc;
  455. ::GetClientRect(wnd_, &rc);
  456. ::MoveWindow(listbox_, 0, 0, rc.right, rc.bottom, TRUE);
  457. ::ShowWindow(listbox_, SW_SHOWNA);
  458. } else {
  459. ::ShowWindow(listbox_, SW_HIDE);
  460. InvalidateRect(wnd_, NULL, TRUE);
  461. }
  462. }
  463. void MainWnd::HandleTabbing() {
  464. bool shift = ((::GetAsyncKeyState(VK_SHIFT) & 0x8000) != 0);
  465. UINT next_cmd = shift ? GW_HWNDPREV : GW_HWNDNEXT;
  466. UINT loop_around_cmd = shift ? GW_HWNDLAST : GW_HWNDFIRST;
  467. HWND focus = GetFocus(), next;
  468. do {
  469. next = ::GetWindow(focus, next_cmd);
  470. if (IsWindowVisible(next) &&
  471. (GetWindowLong(next, GWL_STYLE) & WS_TABSTOP)) {
  472. break;
  473. }
  474. if (!next) {
  475. next = ::GetWindow(focus, loop_around_cmd);
  476. if (IsWindowVisible(next) &&
  477. (GetWindowLong(next, GWL_STYLE) & WS_TABSTOP)) {
  478. break;
  479. }
  480. }
  481. focus = next;
  482. } while (true);
  483. ::SetFocus(next);
  484. }
  485. //
  486. // MainWnd::VideoRenderer
  487. //
  488. MainWnd::VideoRenderer::VideoRenderer(
  489. HWND wnd,
  490. int width,
  491. int height,
  492. webrtc::VideoTrackInterface* track_to_render)
  493. : wnd_(wnd), rendered_track_(track_to_render) {
  494. ::InitializeCriticalSection(&buffer_lock_);
  495. ZeroMemory(&bmi_, sizeof(bmi_));
  496. bmi_.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  497. bmi_.bmiHeader.biPlanes = 1;
  498. bmi_.bmiHeader.biBitCount = 32;
  499. bmi_.bmiHeader.biCompression = BI_RGB;
  500. bmi_.bmiHeader.biWidth = width;
  501. bmi_.bmiHeader.biHeight = -height;
  502. bmi_.bmiHeader.biSizeImage =
  503. width * height * (bmi_.bmiHeader.biBitCount >> 3);
  504. rendered_track_->AddOrUpdateSink(this, rtc::VideoSinkWants());
  505. }
  506. MainWnd::VideoRenderer::~VideoRenderer() {
  507. rendered_track_->RemoveSink(this);
  508. ::DeleteCriticalSection(&buffer_lock_);
  509. }
  510. void MainWnd::VideoRenderer::SetSize(int width, int height) {
  511. AutoLock<VideoRenderer> lock(this);
  512. if (width == bmi_.bmiHeader.biWidth && height == bmi_.bmiHeader.biHeight) {
  513. return;
  514. }
  515. bmi_.bmiHeader.biWidth = width;
  516. bmi_.bmiHeader.biHeight = -height;
  517. bmi_.bmiHeader.biSizeImage =
  518. width * height * (bmi_.bmiHeader.biBitCount >> 3);
  519. image_.reset(new uint8_t[bmi_.bmiHeader.biSizeImage]);
  520. }
  521. void MainWnd::VideoRenderer::OnFrame(const webrtc::VideoFrame& video_frame) {
  522. {
  523. AutoLock<VideoRenderer> lock(this);
  524. rtc::scoped_refptr<webrtc::I420BufferInterface> buffer(
  525. video_frame.video_frame_buffer()->ToI420());
  526. if (video_frame.rotation() != webrtc::kVideoRotation_0) {
  527. buffer = webrtc::I420Buffer::Rotate(*buffer, video_frame.rotation());
  528. }
  529. SetSize(buffer->width(), buffer->height());
  530. RTC_DCHECK(image_.get() != NULL);
  531. libyuv::I420ToARGB(buffer->DataY(), buffer->StrideY(), buffer->DataU(),
  532. buffer->StrideU(), buffer->DataV(), buffer->StrideV(),
  533. image_.get(),
  534. bmi_.bmiHeader.biWidth * bmi_.bmiHeader.biBitCount / 8,
  535. buffer->width(), buffer->height());
  536. }
  537. InvalidateRect(wnd_, NULL, TRUE);
  538. }