blob: 0f9e7b1f89a7b3fe099113525ce4ea958f4be6db [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
Donald E Curtisa8736442015-08-05 15:48:13 -07002 * Copyright 2012 The WebRTC Project Authors. All rights reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
Donald E Curtisa8736442015-08-05 15:48:13 -07004 * 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.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
Donald E Curtisa8736442015-08-05 15:48:13 -070011#include "webrtc/examples/peerconnection/client/main_wnd.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
13#include <math.h>
14
nisse9f8e37b2016-09-01 01:06:23 -070015#include "libyuv/convert_argb.h"
nisseaf916892017-01-10 07:44:26 -080016#include "webrtc/api/video/i420_buffer.h"
Donald E Curtisa8736442015-08-05 15:48:13 -070017#include "webrtc/examples/peerconnection/client/defaults.h"
tfarina5237aaf2015-11-10 23:44:30 -080018#include "webrtc/base/arraysize.h"
nisseede5da42017-01-12 05:15:36 -080019#include "webrtc/base/checks.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000020#include "webrtc/base/common.h"
21#include "webrtc/base/logging.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000022
23ATOM MainWnd::wnd_class_ = 0;
24const wchar_t MainWnd::kClassName[] = L"WebRTC_MainWnd";
25
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000026using rtc::sprintfn;
kjellander@webrtc.org04025152014-07-01 16:28:13 +000027
henrike@webrtc.org28e20752013-07-10 00:45:36 +000028namespace {
29
30const char kConnecting[] = "Connecting... ";
31const char kNoVideoStreams[] = "(no video streams either way)";
32const char kNoIncomingStream[] = "(no incoming video)";
33
34void CalculateWindowSizeForText(HWND wnd, const wchar_t* text,
35 size_t* width, size_t* height) {
36 HDC dc = ::GetDC(wnd);
37 RECT text_rc = {0};
38 ::DrawText(dc, text, -1, &text_rc, DT_CALCRECT | DT_SINGLELINE);
39 ::ReleaseDC(wnd, dc);
40 RECT client, window;
41 ::GetClientRect(wnd, &client);
42 ::GetWindowRect(wnd, &window);
43
44 *width = text_rc.right - text_rc.left;
45 *width += (window.right - window.left) -
46 (client.right - client.left);
47 *height = text_rc.bottom - text_rc.top;
48 *height += (window.bottom - window.top) -
49 (client.bottom - client.top);
50}
51
52HFONT GetDefaultFont() {
53 static HFONT font = reinterpret_cast<HFONT>(GetStockObject(DEFAULT_GUI_FONT));
54 return font;
55}
56
57std::string GetWindowText(HWND wnd) {
58 char text[MAX_PATH] = {0};
59 ::GetWindowTextA(wnd, &text[0], ARRAYSIZE(text));
60 return text;
61}
62
63void 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
69} // namespace
70
kjellander@webrtc.org04025152014-07-01 16:28:13 +000071MainWnd::MainWnd(const char* server, int port, bool auto_connect,
72 bool auto_call)
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073 : ui_(CONNECT_TO_SERVER), wnd_(NULL), edit1_(NULL), edit2_(NULL),
74 label1_(NULL), label2_(NULL), button_(NULL), listbox_(NULL),
kjellander@webrtc.org04025152014-07-01 16:28:13 +000075 destroyed_(false), callback_(NULL), nested_msg_(NULL),
76 server_(server), auto_connect_(auto_connect), auto_call_(auto_call) {
77 char buffer[10] = {0};
78 sprintfn(buffer, sizeof(buffer), "%i", port);
79 port_ = buffer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080}
81
82MainWnd::~MainWnd() {
nisseede5da42017-01-12 05:15:36 -080083 RTC_DCHECK(!IsWindow());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084}
85
86bool MainWnd::Create() {
nisseede5da42017-01-12 05:15:36 -080087 RTC_DCHECK(wnd_ == NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088 if (!RegisterWindowClass())
89 return false;
90
91 ui_thread_id_ = ::GetCurrentThreadId();
92 wnd_ = ::CreateWindowExW(WS_EX_OVERLAPPEDWINDOW, kClassName, L"WebRTC",
93 WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_CLIPCHILDREN,
94 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
95 NULL, NULL, GetModuleHandle(NULL), this);
96
97 ::SendMessage(wnd_, WM_SETFONT, reinterpret_cast<WPARAM>(GetDefaultFont()),
98 TRUE);
99
100 CreateChildWindows();
101 SwitchToConnectUI();
102
103 return wnd_ != NULL;
104}
105
106bool MainWnd::Destroy() {
107 BOOL ret = FALSE;
108 if (IsWindow()) {
109 ret = ::DestroyWindow(wnd_);
110 }
111
112 return ret != FALSE;
113}
114
115void MainWnd::RegisterObserver(MainWndCallback* callback) {
116 callback_ = callback;
117}
118
119bool MainWnd::IsWindow() {
120 return wnd_ && ::IsWindow(wnd_) != FALSE;
121}
122
123bool MainWnd::PreTranslateMessage(MSG* msg) {
124 bool ret = false;
125 if (msg->message == WM_CHAR) {
126 if (msg->wParam == VK_TAB) {
127 HandleTabbing();
128 ret = true;
129 } else if (msg->wParam == VK_RETURN) {
130 OnDefaultAction();
131 ret = true;
132 } else if (msg->wParam == VK_ESCAPE) {
133 if (callback_) {
134 if (ui_ == STREAMING) {
135 callback_->DisconnectFromCurrentPeer();
136 } else {
137 callback_->DisconnectFromServer();
138 }
139 }
140 }
141 } else if (msg->hwnd == NULL && msg->message == UI_THREAD_CALLBACK) {
142 callback_->UIThreadCallback(static_cast<int>(msg->wParam),
143 reinterpret_cast<void*>(msg->lParam));
144 ret = true;
145 }
146 return ret;
147}
148
149void MainWnd::SwitchToConnectUI() {
nisseede5da42017-01-12 05:15:36 -0800150 RTC_DCHECK(IsWindow());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151 LayoutPeerListUI(false);
152 ui_ = CONNECT_TO_SERVER;
153 LayoutConnectUI(true);
154 ::SetFocus(edit1_);
kjellander@webrtc.org04025152014-07-01 16:28:13 +0000155
156 if (auto_connect_)
157 ::PostMessage(button_, BM_CLICK, 0, 0);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000158}
159
160void MainWnd::SwitchToPeerList(const Peers& peers) {
161 LayoutConnectUI(false);
162
163 ::SendMessage(listbox_, LB_RESETCONTENT, 0, 0);
164
165 AddListBoxItem(listbox_, "List of currently connected peers:", -1);
166 Peers::const_iterator i = peers.begin();
167 for (; i != peers.end(); ++i)
168 AddListBoxItem(listbox_, i->second.c_str(), i->first);
169
170 ui_ = LIST_PEERS;
171 LayoutPeerListUI(true);
172 ::SetFocus(listbox_);
kjellander@webrtc.org04025152014-07-01 16:28:13 +0000173
174 if (auto_call_ && peers.begin() != peers.end()) {
175 // Get the number of items in the list
176 LRESULT count = ::SendMessage(listbox_, LB_GETCOUNT, 0, 0);
177 if (count != LB_ERR) {
178 // Select the last item in the list
179 LRESULT selection = ::SendMessage(listbox_, LB_SETCURSEL , count - 1, 0);
180 if (selection != LB_ERR)
181 ::PostMessage(wnd_, WM_COMMAND, MAKEWPARAM(GetDlgCtrlID(listbox_),
182 LBN_DBLCLK),
183 reinterpret_cast<LPARAM>(listbox_));
184 }
185 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000186}
187
188void MainWnd::SwitchToStreamingUI() {
189 LayoutConnectUI(false);
190 LayoutPeerListUI(false);
191 ui_ = STREAMING;
192}
193
194void MainWnd::MessageBox(const char* caption, const char* text, bool is_error) {
195 DWORD flags = MB_OK;
196 if (is_error)
197 flags |= MB_ICONERROR;
198
199 ::MessageBoxA(handle(), text, caption, flags);
200}
201
202
203void MainWnd::StartLocalRenderer(webrtc::VideoTrackInterface* local_video) {
204 local_renderer_.reset(new VideoRenderer(handle(), 1, 1, local_video));
205}
206
207void MainWnd::StopLocalRenderer() {
208 local_renderer_.reset();
209}
210
211void MainWnd::StartRemoteRenderer(webrtc::VideoTrackInterface* remote_video) {
212 remote_renderer_.reset(new VideoRenderer(handle(), 1, 1, remote_video));
213}
214
215void MainWnd::StopRemoteRenderer() {
216 remote_renderer_.reset();
217}
218
219void MainWnd::QueueUIThreadCallback(int msg_id, void* data) {
220 ::PostThreadMessage(ui_thread_id_, UI_THREAD_CALLBACK,
221 static_cast<WPARAM>(msg_id), reinterpret_cast<LPARAM>(data));
222}
223
224void MainWnd::OnPaint() {
225 PAINTSTRUCT ps;
226 ::BeginPaint(handle(), &ps);
227
228 RECT rc;
229 ::GetClientRect(handle(), &rc);
230
231 VideoRenderer* local_renderer = local_renderer_.get();
232 VideoRenderer* remote_renderer = remote_renderer_.get();
233 if (ui_ == STREAMING && remote_renderer && local_renderer) {
234 AutoLock<VideoRenderer> local_lock(local_renderer);
235 AutoLock<VideoRenderer> remote_lock(remote_renderer);
236
237 const BITMAPINFO& bmi = remote_renderer->bmi();
238 int height = abs(bmi.bmiHeader.biHeight);
239 int width = bmi.bmiHeader.biWidth;
240
Peter Boström0c4e06b2015-10-07 12:23:21 +0200241 const uint8_t* image = remote_renderer->image();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000242 if (image != NULL) {
243 HDC dc_mem = ::CreateCompatibleDC(ps.hdc);
244 ::SetStretchBltMode(dc_mem, HALFTONE);
245
246 // Set the map mode so that the ratio will be maintained for us.
247 HDC all_dc[] = { ps.hdc, dc_mem };
tfarina5237aaf2015-11-10 23:44:30 -0800248 for (int i = 0; i < arraysize(all_dc); ++i) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000249 SetMapMode(all_dc[i], MM_ISOTROPIC);
250 SetWindowExtEx(all_dc[i], width, height, NULL);
251 SetViewportExtEx(all_dc[i], rc.right, rc.bottom, NULL);
252 }
253
254 HBITMAP bmp_mem = ::CreateCompatibleBitmap(ps.hdc, rc.right, rc.bottom);
255 HGDIOBJ bmp_old = ::SelectObject(dc_mem, bmp_mem);
256
257 POINT logical_area = { rc.right, rc.bottom };
258 DPtoLP(ps.hdc, &logical_area, 1);
259
260 HBRUSH brush = ::CreateSolidBrush(RGB(0, 0, 0));
261 RECT logical_rect = {0, 0, logical_area.x, logical_area.y };
262 ::FillRect(dc_mem, &logical_rect, brush);
263 ::DeleteObject(brush);
264
265 int x = (logical_area.x / 2) - (width / 2);
266 int y = (logical_area.y / 2) - (height / 2);
267
268 StretchDIBits(dc_mem, x, y, width, height,
269 0, 0, width, height, image, &bmi, DIB_RGB_COLORS, SRCCOPY);
270
271 if ((rc.right - rc.left) > 200 && (rc.bottom - rc.top) > 200) {
272 const BITMAPINFO& bmi = local_renderer->bmi();
273 image = local_renderer->image();
274 int thumb_width = bmi.bmiHeader.biWidth / 4;
275 int thumb_height = abs(bmi.bmiHeader.biHeight) / 4;
276 StretchDIBits(dc_mem,
277 logical_area.x - thumb_width - 10,
278 logical_area.y - thumb_height - 10,
279 thumb_width, thumb_height,
280 0, 0, bmi.bmiHeader.biWidth, -bmi.bmiHeader.biHeight,
281 image, &bmi, DIB_RGB_COLORS, SRCCOPY);
282 }
283
284 BitBlt(ps.hdc, 0, 0, logical_area.x, logical_area.y,
285 dc_mem, 0, 0, SRCCOPY);
286
287 // Cleanup.
288 ::SelectObject(dc_mem, bmp_old);
289 ::DeleteObject(bmp_mem);
290 ::DeleteDC(dc_mem);
291 } else {
292 // We're still waiting for the video stream to be initialized.
293 HBRUSH brush = ::CreateSolidBrush(RGB(0, 0, 0));
294 ::FillRect(ps.hdc, &rc, brush);
295 ::DeleteObject(brush);
296
297 HGDIOBJ old_font = ::SelectObject(ps.hdc, GetDefaultFont());
298 ::SetTextColor(ps.hdc, RGB(0xff, 0xff, 0xff));
299 ::SetBkMode(ps.hdc, TRANSPARENT);
300
301 std::string text(kConnecting);
302 if (!local_renderer->image()) {
303 text += kNoVideoStreams;
304 } else {
305 text += kNoIncomingStream;
306 }
307 ::DrawTextA(ps.hdc, text.c_str(), -1, &rc,
308 DT_SINGLELINE | DT_CENTER | DT_VCENTER);
309 ::SelectObject(ps.hdc, old_font);
310 }
311 } else {
312 HBRUSH brush = ::CreateSolidBrush(::GetSysColor(COLOR_WINDOW));
313 ::FillRect(ps.hdc, &rc, brush);
314 ::DeleteObject(brush);
315 }
316
317 ::EndPaint(handle(), &ps);
318}
319
320void MainWnd::OnDestroyed() {
321 PostQuitMessage(0);
322}
323
324void MainWnd::OnDefaultAction() {
325 if (!callback_)
326 return;
327 if (ui_ == CONNECT_TO_SERVER) {
328 std::string server(GetWindowText(edit1_));
329 std::string port_str(GetWindowText(edit2_));
330 int port = port_str.length() ? atoi(port_str.c_str()) : 0;
331 callback_->StartLogin(server, port);
332 } else if (ui_ == LIST_PEERS) {
333 LRESULT sel = ::SendMessage(listbox_, LB_GETCURSEL, 0, 0);
334 if (sel != LB_ERR) {
335 LRESULT peer_id = ::SendMessage(listbox_, LB_GETITEMDATA, sel, 0);
336 if (peer_id != -1 && callback_) {
337 callback_->ConnectToPeer(peer_id);
338 }
339 }
340 } else {
341 MessageBoxA(wnd_, "OK!", "Yeah", MB_OK);
342 }
343}
344
345bool MainWnd::OnMessage(UINT msg, WPARAM wp, LPARAM lp, LRESULT* result) {
346 switch (msg) {
347 case WM_ERASEBKGND:
348 *result = TRUE;
349 return true;
350
351 case WM_PAINT:
352 OnPaint();
353 return true;
354
355 case WM_SETFOCUS:
356 if (ui_ == CONNECT_TO_SERVER) {
357 SetFocus(edit1_);
358 } else if (ui_ == LIST_PEERS) {
359 SetFocus(listbox_);
360 }
361 return true;
362
363 case WM_SIZE:
364 if (ui_ == CONNECT_TO_SERVER) {
365 LayoutConnectUI(true);
366 } else if (ui_ == LIST_PEERS) {
367 LayoutPeerListUI(true);
368 }
369 break;
370
371 case WM_CTLCOLORSTATIC:
372 *result = reinterpret_cast<LRESULT>(GetSysColorBrush(COLOR_WINDOW));
373 return true;
374
375 case WM_COMMAND:
376 if (button_ == reinterpret_cast<HWND>(lp)) {
377 if (BN_CLICKED == HIWORD(wp))
378 OnDefaultAction();
379 } else if (listbox_ == reinterpret_cast<HWND>(lp)) {
380 if (LBN_DBLCLK == HIWORD(wp)) {
381 OnDefaultAction();
382 }
383 }
384 return true;
385
386 case WM_CLOSE:
387 if (callback_)
388 callback_->Close();
389 break;
390 }
391 return false;
392}
393
394// static
395LRESULT CALLBACK MainWnd::WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) {
396 MainWnd* me = reinterpret_cast<MainWnd*>(
397 ::GetWindowLongPtr(hwnd, GWLP_USERDATA));
398 if (!me && WM_CREATE == msg) {
399 CREATESTRUCT* cs = reinterpret_cast<CREATESTRUCT*>(lp);
400 me = reinterpret_cast<MainWnd*>(cs->lpCreateParams);
401 me->wnd_ = hwnd;
402 ::SetWindowLongPtr(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(me));
403 }
404
405 LRESULT result = 0;
406 if (me) {
407 void* prev_nested_msg = me->nested_msg_;
408 me->nested_msg_ = &msg;
409
410 bool handled = me->OnMessage(msg, wp, lp, &result);
411 if (WM_NCDESTROY == msg) {
412 me->destroyed_ = true;
413 } else if (!handled) {
414 result = ::DefWindowProc(hwnd, msg, wp, lp);
415 }
416
417 if (me->destroyed_ && prev_nested_msg == NULL) {
418 me->OnDestroyed();
419 me->wnd_ = NULL;
420 me->destroyed_ = false;
421 }
422
423 me->nested_msg_ = prev_nested_msg;
424 } else {
425 result = ::DefWindowProc(hwnd, msg, wp, lp);
426 }
427
428 return result;
429}
430
431// static
432bool MainWnd::RegisterWindowClass() {
433 if (wnd_class_)
434 return true;
435
436 WNDCLASSEX wcex = { sizeof(WNDCLASSEX) };
437 wcex.style = CS_DBLCLKS;
438 wcex.hInstance = GetModuleHandle(NULL);
439 wcex.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW + 1);
440 wcex.hCursor = ::LoadCursor(NULL, IDC_ARROW);
441 wcex.lpfnWndProc = &WndProc;
442 wcex.lpszClassName = kClassName;
443 wnd_class_ = ::RegisterClassEx(&wcex);
nisseede5da42017-01-12 05:15:36 -0800444 RTC_DCHECK(wnd_class_ != 0);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000445 return wnd_class_ != 0;
446}
447
448void MainWnd::CreateChildWindow(HWND* wnd, MainWnd::ChildWindowID id,
449 const wchar_t* class_name, DWORD control_style,
450 DWORD ex_style) {
451 if (::IsWindow(*wnd))
452 return;
453
454 // Child windows are invisible at first, and shown after being resized.
455 DWORD style = WS_CHILD | control_style;
456 *wnd = ::CreateWindowEx(ex_style, class_name, L"", style,
457 100, 100, 100, 100, wnd_,
458 reinterpret_cast<HMENU>(id),
459 GetModuleHandle(NULL), NULL);
nisseede5da42017-01-12 05:15:36 -0800460 RTC_DCHECK(::IsWindow(*wnd) != FALSE);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000461 ::SendMessage(*wnd, WM_SETFONT, reinterpret_cast<WPARAM>(GetDefaultFont()),
462 TRUE);
463}
464
465void MainWnd::CreateChildWindows() {
466 // Create the child windows in tab order.
467 CreateChildWindow(&label1_, LABEL1_ID, L"Static", ES_CENTER | ES_READONLY, 0);
468 CreateChildWindow(&edit1_, EDIT_ID, L"Edit",
469 ES_LEFT | ES_NOHIDESEL | WS_TABSTOP, WS_EX_CLIENTEDGE);
470 CreateChildWindow(&label2_, LABEL2_ID, L"Static", ES_CENTER | ES_READONLY, 0);
471 CreateChildWindow(&edit2_, EDIT_ID, L"Edit",
472 ES_LEFT | ES_NOHIDESEL | WS_TABSTOP, WS_EX_CLIENTEDGE);
473 CreateChildWindow(&button_, BUTTON_ID, L"Button", BS_CENTER | WS_TABSTOP, 0);
474
475 CreateChildWindow(&listbox_, LISTBOX_ID, L"ListBox",
476 LBS_HASSTRINGS | LBS_NOTIFY, WS_EX_CLIENTEDGE);
477
kjellander@webrtc.org04025152014-07-01 16:28:13 +0000478 ::SetWindowTextA(edit1_, server_.c_str());
479 ::SetWindowTextA(edit2_, port_.c_str());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000480}
481
482void MainWnd::LayoutConnectUI(bool show) {
483 struct Windows {
484 HWND wnd;
485 const wchar_t* text;
486 size_t width;
487 size_t height;
488 } windows[] = {
489 { label1_, L"Server" },
490 { edit1_, L"XXXyyyYYYgggXXXyyyYYYggg" },
491 { label2_, L":" },
492 { edit2_, L"XyXyX" },
493 { button_, L"Connect" },
494 };
495
496 if (show) {
497 const size_t kSeparator = 5;
498 size_t total_width = (ARRAYSIZE(windows) - 1) * kSeparator;
499
500 for (size_t i = 0; i < ARRAYSIZE(windows); ++i) {
501 CalculateWindowSizeForText(windows[i].wnd, windows[i].text,
502 &windows[i].width, &windows[i].height);
503 total_width += windows[i].width;
504 }
505
506 RECT rc;
507 ::GetClientRect(wnd_, &rc);
508 size_t x = (rc.right / 2) - (total_width / 2);
509 size_t y = rc.bottom / 2;
510 for (size_t i = 0; i < ARRAYSIZE(windows); ++i) {
511 size_t top = y - (windows[i].height / 2);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000512 ::MoveWindow(windows[i].wnd, static_cast<int>(x), static_cast<int>(top),
513 static_cast<int>(windows[i].width),
514 static_cast<int>(windows[i].height),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000515 TRUE);
516 x += kSeparator + windows[i].width;
517 if (windows[i].text[0] != 'X')
518 ::SetWindowText(windows[i].wnd, windows[i].text);
519 ::ShowWindow(windows[i].wnd, SW_SHOWNA);
520 }
521 } else {
522 for (size_t i = 0; i < ARRAYSIZE(windows); ++i) {
523 ::ShowWindow(windows[i].wnd, SW_HIDE);
524 }
525 }
526}
527
528void MainWnd::LayoutPeerListUI(bool show) {
529 if (show) {
530 RECT rc;
531 ::GetClientRect(wnd_, &rc);
532 ::MoveWindow(listbox_, 0, 0, rc.right, rc.bottom, TRUE);
533 ::ShowWindow(listbox_, SW_SHOWNA);
534 } else {
535 ::ShowWindow(listbox_, SW_HIDE);
536 InvalidateRect(wnd_, NULL, TRUE);
537 }
538}
539
540void MainWnd::HandleTabbing() {
541 bool shift = ((::GetAsyncKeyState(VK_SHIFT) & 0x8000) != 0);
542 UINT next_cmd = shift ? GW_HWNDPREV : GW_HWNDNEXT;
543 UINT loop_around_cmd = shift ? GW_HWNDLAST : GW_HWNDFIRST;
544 HWND focus = GetFocus(), next;
545 do {
546 next = ::GetWindow(focus, next_cmd);
547 if (IsWindowVisible(next) &&
548 (GetWindowLong(next, GWL_STYLE) & WS_TABSTOP)) {
549 break;
550 }
551
552 if (!next) {
553 next = ::GetWindow(focus, loop_around_cmd);
554 if (IsWindowVisible(next) &&
555 (GetWindowLong(next, GWL_STYLE) & WS_TABSTOP)) {
556 break;
557 }
558 }
559 focus = next;
560 } while (true);
561 ::SetFocus(next);
562}
563
564//
565// MainWnd::VideoRenderer
566//
567
568MainWnd::VideoRenderer::VideoRenderer(
569 HWND wnd, int width, int height,
570 webrtc::VideoTrackInterface* track_to_render)
571 : wnd_(wnd), rendered_track_(track_to_render) {
572 ::InitializeCriticalSection(&buffer_lock_);
573 ZeroMemory(&bmi_, sizeof(bmi_));
574 bmi_.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
575 bmi_.bmiHeader.biPlanes = 1;
576 bmi_.bmiHeader.biBitCount = 32;
577 bmi_.bmiHeader.biCompression = BI_RGB;
578 bmi_.bmiHeader.biWidth = width;
579 bmi_.bmiHeader.biHeight = -height;
580 bmi_.bmiHeader.biSizeImage = width * height *
581 (bmi_.bmiHeader.biBitCount >> 3);
Niels Möller8f597622016-03-23 10:33:07 +0100582 rendered_track_->AddOrUpdateSink(this, rtc::VideoSinkWants());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000583}
584
585MainWnd::VideoRenderer::~VideoRenderer() {
Niels Möller8f597622016-03-23 10:33:07 +0100586 rendered_track_->RemoveSink(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000587 ::DeleteCriticalSection(&buffer_lock_);
588}
589
590void MainWnd::VideoRenderer::SetSize(int width, int height) {
591 AutoLock<VideoRenderer> lock(this);
592
guoweis@webrtc.org00c509a2015-03-12 21:37:26 +0000593 if (width == bmi_.bmiHeader.biWidth && height == bmi_.bmiHeader.biHeight) {
594 return;
595 }
596
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000597 bmi_.bmiHeader.biWidth = width;
598 bmi_.bmiHeader.biHeight = -height;
599 bmi_.bmiHeader.biSizeImage = width * height *
600 (bmi_.bmiHeader.biBitCount >> 3);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200601 image_.reset(new uint8_t[bmi_.bmiHeader.biSizeImage]);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000602}
603
Niels Möller8f597622016-03-23 10:33:07 +0100604void MainWnd::VideoRenderer::OnFrame(
nisseacd935b2016-11-11 03:55:13 -0800605 const webrtc::VideoFrame& video_frame) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000606
607 {
608 AutoLock<VideoRenderer> lock(this);
609
nisseacd935b2016-11-11 03:55:13 -0800610 rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer(
nisseaf916892017-01-10 07:44:26 -0800611 video_frame.video_frame_buffer());
612 if (video_frame.rotation() != webrtc::kVideoRotation_0) {
613 buffer = webrtc::I420Buffer::Rotate(*buffer, video_frame.rotation());
614 }
guoweis@webrtc.org00c509a2015-03-12 21:37:26 +0000615
nisseacd935b2016-11-11 03:55:13 -0800616 SetSize(buffer->width(), buffer->height());
guoweis@webrtc.org00c509a2015-03-12 21:37:26 +0000617
nisseede5da42017-01-12 05:15:36 -0800618 RTC_DCHECK(image_.get() != NULL);
nisse9f8e37b2016-09-01 01:06:23 -0700619 libyuv::I420ToARGB(buffer->DataY(), buffer->StrideY(),
620 buffer->DataU(), buffer->StrideU(),
621 buffer->DataV(), buffer->StrideV(),
622 image_.get(),
623 bmi_.bmiHeader.biWidth *
624 bmi_.bmiHeader.biBitCount / 8,
625 buffer->width(), buffer->height());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000626 }
627 InvalidateRect(wnd_, NULL, TRUE);
628}