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