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