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