10e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org/*
20e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * libjingle
30e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * Copyright 2012, Google Inc.
40e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *
50e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * Redistribution and use in source and binary forms, with or without
60e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * modification, are permitted provided that the following conditions are met:
70e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *
80e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *  1. Redistributions of source code must retain the above copyright notice,
90e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     this list of conditions and the following disclaimer.
100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *  2. Redistributions in binary form must reproduce the above copyright notice,
110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     this list of conditions and the following disclaimer in the documentation
120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     and/or other materials provided with the distribution.
130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *  3. The name of the author may not be used to endorse or promote products
140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     derived from this software without specific prior written permission.
150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *
160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org */
270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/examples/peerconnection/client/main_wnd.h"
290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include <math.h>
310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
32cf81adffe15fa8ea0f333432e41f6d504148f18abuildbot@webrtc.org#include "talk/examples/peerconnection/client/defaults.h"
332a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org#include "webrtc/base/common.h"
342a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org#include "webrtc/base/logging.h"
350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgATOM MainWnd::wnd_class_ = 0;
370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgconst wchar_t MainWnd::kClassName[] = L"WebRTC_MainWnd";
380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
392a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.orgusing rtc::sprintfn;
40249b3894e5fe5cd977313f0df6eb222d5010b561kjellander@webrtc.org
410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgnamespace {
420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgconst char kConnecting[] = "Connecting... ";
440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgconst char kNoVideoStreams[] = "(no video streams either way)";
450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgconst char kNoIncomingStream[] = "(no incoming video)";
460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgvoid CalculateWindowSizeForText(HWND wnd, const wchar_t* text,
480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                size_t* width, size_t* height) {
490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  HDC dc = ::GetDC(wnd);
500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  RECT text_rc = {0};
510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ::DrawText(dc, text, -1, &text_rc, DT_CALCRECT | DT_SINGLELINE);
520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ::ReleaseDC(wnd, dc);
530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  RECT client, window;
540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ::GetClientRect(wnd, &client);
550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ::GetWindowRect(wnd, &window);
560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  *width = text_rc.right - text_rc.left;
580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  *width += (window.right - window.left) -
590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org            (client.right - client.left);
600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  *height = text_rc.bottom - text_rc.top;
610e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  *height += (window.bottom - window.top) -
620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org             (client.bottom - client.top);
630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
650e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgHFONT GetDefaultFont() {
660e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  static HFONT font = reinterpret_cast<HFONT>(GetStockObject(DEFAULT_GUI_FONT));
670e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  return font;
680e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
690e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
700e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgstd::string GetWindowText(HWND wnd) {
710e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  char text[MAX_PATH] = {0};
720e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ::GetWindowTextA(wnd, &text[0], ARRAYSIZE(text));
730e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  return text;
740e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
750e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgvoid AddListBoxItem(HWND listbox, const std::string& str, LPARAM item_data) {
770e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  LRESULT index = ::SendMessageA(listbox, LB_ADDSTRING, 0,
780e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      reinterpret_cast<LPARAM>(str.c_str()));
790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ::SendMessageA(listbox, LB_SETITEMDATA, index, item_data);
800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}  // namespace
830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
84249b3894e5fe5cd977313f0df6eb222d5010b561kjellander@webrtc.orgMainWnd::MainWnd(const char* server, int port, bool auto_connect,
85249b3894e5fe5cd977313f0df6eb222d5010b561kjellander@webrtc.org                 bool auto_call)
860e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  : ui_(CONNECT_TO_SERVER), wnd_(NULL), edit1_(NULL), edit2_(NULL),
870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    label1_(NULL), label2_(NULL), button_(NULL), listbox_(NULL),
88249b3894e5fe5cd977313f0df6eb222d5010b561kjellander@webrtc.org    destroyed_(false), callback_(NULL), nested_msg_(NULL),
89249b3894e5fe5cd977313f0df6eb222d5010b561kjellander@webrtc.org    server_(server), auto_connect_(auto_connect), auto_call_(auto_call) {
90249b3894e5fe5cd977313f0df6eb222d5010b561kjellander@webrtc.org  char buffer[10] = {0};
91249b3894e5fe5cd977313f0df6eb222d5010b561kjellander@webrtc.org  sprintfn(buffer, sizeof(buffer), "%i", port);
92249b3894e5fe5cd977313f0df6eb222d5010b561kjellander@webrtc.org  port_ = buffer;
930e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
940e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
950e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgMainWnd::~MainWnd() {
960e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ASSERT(!IsWindow());
970e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
980e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
990e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgbool MainWnd::Create() {
1000e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ASSERT(wnd_ == NULL);
1010e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  if (!RegisterWindowClass())
1020e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    return false;
1030e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1040e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ui_thread_id_ = ::GetCurrentThreadId();
1050e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  wnd_ = ::CreateWindowExW(WS_EX_OVERLAPPEDWINDOW, kClassName, L"WebRTC",
1060e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_CLIPCHILDREN,
1070e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
1080e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      NULL, NULL, GetModuleHandle(NULL), this);
1090e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ::SendMessage(wnd_, WM_SETFONT, reinterpret_cast<WPARAM>(GetDefaultFont()),
1110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                TRUE);
1120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  CreateChildWindows();
1140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  SwitchToConnectUI();
1150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  return wnd_ != NULL;
1170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
1180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgbool MainWnd::Destroy() {
1200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  BOOL ret = FALSE;
1210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  if (IsWindow()) {
1220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    ret = ::DestroyWindow(wnd_);
1230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
1240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  return ret != FALSE;
1260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
1270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgvoid MainWnd::RegisterObserver(MainWndCallback* callback) {
1290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  callback_ = callback;
1300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
1310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgbool MainWnd::IsWindow() {
1330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  return wnd_ && ::IsWindow(wnd_) != FALSE;
1340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
1350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgbool MainWnd::PreTranslateMessage(MSG* msg) {
1370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool ret = false;
1380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  if (msg->message == WM_CHAR) {
1390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    if (msg->wParam == VK_TAB) {
1400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      HandleTabbing();
1410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      ret = true;
1420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    } else if (msg->wParam == VK_RETURN) {
1430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      OnDefaultAction();
1440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      ret = true;
1450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    } else if (msg->wParam == VK_ESCAPE) {
1460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      if (callback_) {
1470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        if (ui_ == STREAMING) {
1480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org          callback_->DisconnectFromCurrentPeer();
1490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        } else {
1500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org          callback_->DisconnectFromServer();
1510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        }
1520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      }
1530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    }
1540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  } else if (msg->hwnd == NULL && msg->message == UI_THREAD_CALLBACK) {
1550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    callback_->UIThreadCallback(static_cast<int>(msg->wParam),
1560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                reinterpret_cast<void*>(msg->lParam));
1570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    ret = true;
1580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
1590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  return ret;
1600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
1610e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgvoid MainWnd::SwitchToConnectUI() {
1630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ASSERT(IsWindow());
1640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  LayoutPeerListUI(false);
1650e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ui_ = CONNECT_TO_SERVER;
1660e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  LayoutConnectUI(true);
1670e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ::SetFocus(edit1_);
168249b3894e5fe5cd977313f0df6eb222d5010b561kjellander@webrtc.org
169249b3894e5fe5cd977313f0df6eb222d5010b561kjellander@webrtc.org  if (auto_connect_)
170249b3894e5fe5cd977313f0df6eb222d5010b561kjellander@webrtc.org    ::PostMessage(button_, BM_CLICK, 0, 0);
1710e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
1720e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1730e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgvoid MainWnd::SwitchToPeerList(const Peers& peers) {
1740e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  LayoutConnectUI(false);
1750e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ::SendMessage(listbox_, LB_RESETCONTENT, 0, 0);
1770e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1780e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  AddListBoxItem(listbox_, "List of currently connected peers:", -1);
1790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  Peers::const_iterator i = peers.begin();
1800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  for (; i != peers.end(); ++i)
1810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    AddListBoxItem(listbox_, i->second.c_str(), i->first);
1820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ui_ = LIST_PEERS;
1840e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  LayoutPeerListUI(true);
1850e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ::SetFocus(listbox_);
186249b3894e5fe5cd977313f0df6eb222d5010b561kjellander@webrtc.org
187249b3894e5fe5cd977313f0df6eb222d5010b561kjellander@webrtc.org  if (auto_call_ && peers.begin() != peers.end()) {
188249b3894e5fe5cd977313f0df6eb222d5010b561kjellander@webrtc.org    // Get the number of items in the list
189249b3894e5fe5cd977313f0df6eb222d5010b561kjellander@webrtc.org    LRESULT count = ::SendMessage(listbox_, LB_GETCOUNT, 0, 0);
190249b3894e5fe5cd977313f0df6eb222d5010b561kjellander@webrtc.org    if (count != LB_ERR) {
191249b3894e5fe5cd977313f0df6eb222d5010b561kjellander@webrtc.org      // Select the last item in the list
192249b3894e5fe5cd977313f0df6eb222d5010b561kjellander@webrtc.org      LRESULT selection = ::SendMessage(listbox_, LB_SETCURSEL , count - 1, 0);
193249b3894e5fe5cd977313f0df6eb222d5010b561kjellander@webrtc.org      if (selection != LB_ERR)
194249b3894e5fe5cd977313f0df6eb222d5010b561kjellander@webrtc.org        ::PostMessage(wnd_, WM_COMMAND, MAKEWPARAM(GetDlgCtrlID(listbox_),
195249b3894e5fe5cd977313f0df6eb222d5010b561kjellander@webrtc.org                                                   LBN_DBLCLK),
196249b3894e5fe5cd977313f0df6eb222d5010b561kjellander@webrtc.org                      reinterpret_cast<LPARAM>(listbox_));
197249b3894e5fe5cd977313f0df6eb222d5010b561kjellander@webrtc.org    }
198249b3894e5fe5cd977313f0df6eb222d5010b561kjellander@webrtc.org  }
1990e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
2000e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2010e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgvoid MainWnd::SwitchToStreamingUI() {
2020e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  LayoutConnectUI(false);
2030e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  LayoutPeerListUI(false);
2040e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ui_ = STREAMING;
2050e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
2060e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2070e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgvoid MainWnd::MessageBox(const char* caption, const char* text, bool is_error) {
2080e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  DWORD flags = MB_OK;
2090e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  if (is_error)
2100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    flags |= MB_ICONERROR;
2110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ::MessageBoxA(handle(), text, caption, flags);
2130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
2140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgvoid MainWnd::StartLocalRenderer(webrtc::VideoTrackInterface* local_video) {
2170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  local_renderer_.reset(new VideoRenderer(handle(), 1, 1, local_video));
2180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
2190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgvoid MainWnd::StopLocalRenderer() {
2210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  local_renderer_.reset();
2220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
2230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgvoid MainWnd::StartRemoteRenderer(webrtc::VideoTrackInterface* remote_video) {
2250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  remote_renderer_.reset(new VideoRenderer(handle(), 1, 1, remote_video));
2260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
2270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgvoid MainWnd::StopRemoteRenderer() {
2290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  remote_renderer_.reset();
2300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
2310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgvoid MainWnd::QueueUIThreadCallback(int msg_id, void* data) {
2330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ::PostThreadMessage(ui_thread_id_, UI_THREAD_CALLBACK,
2340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      static_cast<WPARAM>(msg_id), reinterpret_cast<LPARAM>(data));
2350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
2360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgvoid MainWnd::OnPaint() {
2380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  PAINTSTRUCT ps;
2390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ::BeginPaint(handle(), &ps);
2400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  RECT rc;
2420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ::GetClientRect(handle(), &rc);
2430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  VideoRenderer* local_renderer = local_renderer_.get();
2450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  VideoRenderer* remote_renderer = remote_renderer_.get();
2460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  if (ui_ == STREAMING && remote_renderer && local_renderer) {
2470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    AutoLock<VideoRenderer> local_lock(local_renderer);
2480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    AutoLock<VideoRenderer> remote_lock(remote_renderer);
2490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    const BITMAPINFO& bmi = remote_renderer->bmi();
2510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    int height = abs(bmi.bmiHeader.biHeight);
2520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    int width = bmi.bmiHeader.biWidth;
2530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    const uint8* image = remote_renderer->image();
2550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    if (image != NULL) {
2560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      HDC dc_mem = ::CreateCompatibleDC(ps.hdc);
2570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      ::SetStretchBltMode(dc_mem, HALFTONE);
2580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      // Set the map mode so that the ratio will be maintained for us.
2600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      HDC all_dc[] = { ps.hdc, dc_mem };
2610e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      for (int i = 0; i < ARRAY_SIZE(all_dc); ++i) {
2620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        SetMapMode(all_dc[i], MM_ISOTROPIC);
2630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        SetWindowExtEx(all_dc[i], width, height, NULL);
2640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        SetViewportExtEx(all_dc[i], rc.right, rc.bottom, NULL);
2650e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      }
2660e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2670e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      HBITMAP bmp_mem = ::CreateCompatibleBitmap(ps.hdc, rc.right, rc.bottom);
2680e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      HGDIOBJ bmp_old = ::SelectObject(dc_mem, bmp_mem);
2690e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2700e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      POINT logical_area = { rc.right, rc.bottom };
2710e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      DPtoLP(ps.hdc, &logical_area, 1);
2720e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2730e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      HBRUSH brush = ::CreateSolidBrush(RGB(0, 0, 0));
2740e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      RECT logical_rect = {0, 0, logical_area.x, logical_area.y };
2750e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      ::FillRect(dc_mem, &logical_rect, brush);
2760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      ::DeleteObject(brush);
2770e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2780e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      int x = (logical_area.x / 2) - (width / 2);
2790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      int y = (logical_area.y / 2) - (height / 2);
2800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      StretchDIBits(dc_mem, x, y, width, height,
2820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                    0, 0, width, height, image, &bmi, DIB_RGB_COLORS, SRCCOPY);
2830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2840e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      if ((rc.right - rc.left) > 200 && (rc.bottom - rc.top) > 200) {
2850e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        const BITMAPINFO& bmi = local_renderer->bmi();
2860e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        image = local_renderer->image();
2870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        int thumb_width = bmi.bmiHeader.biWidth / 4;
2880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        int thumb_height = abs(bmi.bmiHeader.biHeight) / 4;
2890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        StretchDIBits(dc_mem,
2900e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org            logical_area.x - thumb_width - 10,
2910e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org            logical_area.y - thumb_height - 10,
2920e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org            thumb_width, thumb_height,
2930e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org            0, 0, bmi.bmiHeader.biWidth, -bmi.bmiHeader.biHeight,
2940e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org            image, &bmi, DIB_RGB_COLORS, SRCCOPY);
2950e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      }
2960e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
2970e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      BitBlt(ps.hdc, 0, 0, logical_area.x, logical_area.y,
2980e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org             dc_mem, 0, 0, SRCCOPY);
2990e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3000e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      // Cleanup.
3010e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      ::SelectObject(dc_mem, bmp_old);
3020e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      ::DeleteObject(bmp_mem);
3030e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      ::DeleteDC(dc_mem);
3040e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    } else {
3050e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      // We're still waiting for the video stream to be initialized.
3060e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      HBRUSH brush = ::CreateSolidBrush(RGB(0, 0, 0));
3070e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      ::FillRect(ps.hdc, &rc, brush);
3080e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      ::DeleteObject(brush);
3090e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      HGDIOBJ old_font = ::SelectObject(ps.hdc, GetDefaultFont());
3110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      ::SetTextColor(ps.hdc, RGB(0xff, 0xff, 0xff));
3120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      ::SetBkMode(ps.hdc, TRANSPARENT);
3130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      std::string text(kConnecting);
3150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      if (!local_renderer->image()) {
3160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        text += kNoVideoStreams;
3170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      } else {
3180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        text += kNoIncomingStream;
3190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      }
3200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      ::DrawTextA(ps.hdc, text.c_str(), -1, &rc,
3210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org          DT_SINGLELINE | DT_CENTER | DT_VCENTER);
3220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      ::SelectObject(ps.hdc, old_font);
3230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    }
3240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  } else {
3250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    HBRUSH brush = ::CreateSolidBrush(::GetSysColor(COLOR_WINDOW));
3260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    ::FillRect(ps.hdc, &rc, brush);
3270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    ::DeleteObject(brush);
3280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
3290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ::EndPaint(handle(), &ps);
3310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
3320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgvoid MainWnd::OnDestroyed() {
3340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  PostQuitMessage(0);
3350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
3360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgvoid MainWnd::OnDefaultAction() {
3380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  if (!callback_)
3390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    return;
3400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  if (ui_ == CONNECT_TO_SERVER) {
3410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    std::string server(GetWindowText(edit1_));
3420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    std::string port_str(GetWindowText(edit2_));
3430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    int port = port_str.length() ? atoi(port_str.c_str()) : 0;
3440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    callback_->StartLogin(server, port);
3450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  } else if (ui_ == LIST_PEERS) {
3460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    LRESULT sel = ::SendMessage(listbox_, LB_GETCURSEL, 0, 0);
3470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    if (sel != LB_ERR) {
3480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      LRESULT peer_id = ::SendMessage(listbox_, LB_GETITEMDATA, sel, 0);
3490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      if (peer_id != -1 && callback_) {
3500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        callback_->ConnectToPeer(peer_id);
3510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      }
3520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    }
3530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  } else {
3540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    MessageBoxA(wnd_, "OK!", "Yeah", MB_OK);
3550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
3560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
3570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgbool MainWnd::OnMessage(UINT msg, WPARAM wp, LPARAM lp, LRESULT* result) {
3590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  switch (msg) {
3600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    case WM_ERASEBKGND:
3610e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      *result = TRUE;
3620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      return true;
3630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    case WM_PAINT:
3650e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      OnPaint();
3660e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      return true;
3670e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3680e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    case WM_SETFOCUS:
3690e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      if (ui_ == CONNECT_TO_SERVER) {
3700e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        SetFocus(edit1_);
3710e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      } else if (ui_ == LIST_PEERS) {
3720e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        SetFocus(listbox_);
3730e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      }
3740e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      return true;
3750e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    case WM_SIZE:
3770e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      if (ui_ == CONNECT_TO_SERVER) {
3780e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        LayoutConnectUI(true);
3790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      } else if (ui_ == LIST_PEERS) {
3800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        LayoutPeerListUI(true);
3810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      }
3820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      break;
3830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3840e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    case WM_CTLCOLORSTATIC:
3850e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      *result = reinterpret_cast<LRESULT>(GetSysColorBrush(COLOR_WINDOW));
3860e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      return true;
3870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    case WM_COMMAND:
3890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      if (button_ == reinterpret_cast<HWND>(lp)) {
3900e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        if (BN_CLICKED == HIWORD(wp))
3910e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org          OnDefaultAction();
3920e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      } else if (listbox_ == reinterpret_cast<HWND>(lp)) {
3930e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        if (LBN_DBLCLK == HIWORD(wp)) {
3940e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org          OnDefaultAction();
3950e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        }
3960e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      }
3970e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      return true;
3980e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
3990e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    case WM_CLOSE:
4000e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      if (callback_)
4010e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        callback_->Close();
4020e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      break;
4030e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
4040e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  return false;
4050e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
4060e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4070e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// static
4080e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgLRESULT CALLBACK MainWnd::WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) {
4090e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  MainWnd* me = reinterpret_cast<MainWnd*>(
4100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      ::GetWindowLongPtr(hwnd, GWLP_USERDATA));
4110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  if (!me && WM_CREATE == msg) {
4120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    CREATESTRUCT* cs = reinterpret_cast<CREATESTRUCT*>(lp);
4130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    me = reinterpret_cast<MainWnd*>(cs->lpCreateParams);
4140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    me->wnd_ = hwnd;
4150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    ::SetWindowLongPtr(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(me));
4160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
4170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  LRESULT result = 0;
4190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  if (me) {
4200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    void* prev_nested_msg = me->nested_msg_;
4210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    me->nested_msg_ = &msg;
4220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    bool handled = me->OnMessage(msg, wp, lp, &result);
4240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    if (WM_NCDESTROY == msg) {
4250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      me->destroyed_ = true;
4260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    } else if (!handled) {
4270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      result = ::DefWindowProc(hwnd, msg, wp, lp);
4280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    }
4290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    if (me->destroyed_ && prev_nested_msg == NULL) {
4310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      me->OnDestroyed();
4320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      me->wnd_ = NULL;
4330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      me->destroyed_ = false;
4340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    }
4350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    me->nested_msg_ = prev_nested_msg;
4370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  } else {
4380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    result = ::DefWindowProc(hwnd, msg, wp, lp);
4390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
4400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  return result;
4420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
4430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// static
4450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgbool MainWnd::RegisterWindowClass() {
4460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  if (wnd_class_)
4470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    return true;
4480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  WNDCLASSEX wcex = { sizeof(WNDCLASSEX) };
4500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  wcex.style = CS_DBLCLKS;
4510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  wcex.hInstance = GetModuleHandle(NULL);
4520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  wcex.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW + 1);
4530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  wcex.hCursor = ::LoadCursor(NULL, IDC_ARROW);
4540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  wcex.lpfnWndProc = &WndProc;
4550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  wcex.lpszClassName = kClassName;
4560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  wnd_class_ = ::RegisterClassEx(&wcex);
4570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ASSERT(wnd_class_ != 0);
4580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  return wnd_class_ != 0;
4590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
4600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4610e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgvoid MainWnd::CreateChildWindow(HWND* wnd, MainWnd::ChildWindowID id,
4620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                const wchar_t* class_name, DWORD control_style,
4630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                DWORD ex_style) {
4640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  if (::IsWindow(*wnd))
4650e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    return;
4660e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4670e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Child windows are invisible at first, and shown after being resized.
4680e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  DWORD style = WS_CHILD | control_style;
4690e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  *wnd = ::CreateWindowEx(ex_style, class_name, L"", style,
4700e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                          100, 100, 100, 100, wnd_,
4710e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                          reinterpret_cast<HMENU>(id),
4720e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                          GetModuleHandle(NULL), NULL);
4730e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ASSERT(::IsWindow(*wnd) != FALSE);
4740e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ::SendMessage(*wnd, WM_SETFONT, reinterpret_cast<WPARAM>(GetDefaultFont()),
4750e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                TRUE);
4760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
4770e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4780e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgvoid MainWnd::CreateChildWindows() {
4790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Create the child windows in tab order.
4800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  CreateChildWindow(&label1_, LABEL1_ID, L"Static", ES_CENTER | ES_READONLY, 0);
4810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  CreateChildWindow(&edit1_, EDIT_ID, L"Edit",
4820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                    ES_LEFT | ES_NOHIDESEL | WS_TABSTOP, WS_EX_CLIENTEDGE);
4830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  CreateChildWindow(&label2_, LABEL2_ID, L"Static", ES_CENTER | ES_READONLY, 0);
4840e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  CreateChildWindow(&edit2_, EDIT_ID, L"Edit",
4850e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                    ES_LEFT | ES_NOHIDESEL | WS_TABSTOP, WS_EX_CLIENTEDGE);
4860e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  CreateChildWindow(&button_, BUTTON_ID, L"Button", BS_CENTER | WS_TABSTOP, 0);
4870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  CreateChildWindow(&listbox_, LISTBOX_ID, L"ListBox",
4890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                    LBS_HASSTRINGS | LBS_NOTIFY, WS_EX_CLIENTEDGE);
4900e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
491249b3894e5fe5cd977313f0df6eb222d5010b561kjellander@webrtc.org  ::SetWindowTextA(edit1_, server_.c_str());
492249b3894e5fe5cd977313f0df6eb222d5010b561kjellander@webrtc.org  ::SetWindowTextA(edit2_, port_.c_str());
4930e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
4940e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
4950e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgvoid MainWnd::LayoutConnectUI(bool show) {
4960e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  struct Windows {
4970e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    HWND wnd;
4980e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    const wchar_t* text;
4990e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    size_t width;
5000e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    size_t height;
5010e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  } windows[] = {
5020e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    { label1_, L"Server" },
5030e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    { edit1_, L"XXXyyyYYYgggXXXyyyYYYggg" },
5040e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    { label2_, L":" },
5050e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    { edit2_, L"XyXyX" },
5060e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    { button_, L"Connect" },
5070e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  };
5080e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
5090e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  if (show) {
5100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    const size_t kSeparator = 5;
5110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    size_t total_width = (ARRAYSIZE(windows) - 1) * kSeparator;
5120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
5130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    for (size_t i = 0; i < ARRAYSIZE(windows); ++i) {
5140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      CalculateWindowSizeForText(windows[i].wnd, windows[i].text,
5150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                 &windows[i].width, &windows[i].height);
5160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      total_width += windows[i].width;
5170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    }
5180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
5190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    RECT rc;
5200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    ::GetClientRect(wnd_, &rc);
5210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    size_t x = (rc.right / 2) - (total_width / 2);
5220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    size_t y = rc.bottom / 2;
5230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    for (size_t i = 0; i < ARRAYSIZE(windows); ++i) {
5240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      size_t top = y - (windows[i].height / 2);
5251a04b881e0ef480802fb01b4fbe9bcd5388d2c69henrike@webrtc.org      ::MoveWindow(windows[i].wnd, static_cast<int>(x), static_cast<int>(top),
5261a04b881e0ef480802fb01b4fbe9bcd5388d2c69henrike@webrtc.org                   static_cast<int>(windows[i].width),
5271a04b881e0ef480802fb01b4fbe9bcd5388d2c69henrike@webrtc.org                   static_cast<int>(windows[i].height),
5280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                   TRUE);
5290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      x += kSeparator + windows[i].width;
5300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      if (windows[i].text[0] != 'X')
5310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        ::SetWindowText(windows[i].wnd, windows[i].text);
5320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      ::ShowWindow(windows[i].wnd, SW_SHOWNA);
5330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    }
5340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  } else {
5350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    for (size_t i = 0; i < ARRAYSIZE(windows); ++i) {
5360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      ::ShowWindow(windows[i].wnd, SW_HIDE);
5370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    }
5380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
5390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
5400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
5410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgvoid MainWnd::LayoutPeerListUI(bool show) {
5420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  if (show) {
5430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    RECT rc;
5440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    ::GetClientRect(wnd_, &rc);
5450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    ::MoveWindow(listbox_, 0, 0, rc.right, rc.bottom, TRUE);
5460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    ::ShowWindow(listbox_, SW_SHOWNA);
5470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  } else {
5480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    ::ShowWindow(listbox_, SW_HIDE);
5490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    InvalidateRect(wnd_, NULL, TRUE);
5500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
5510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
5520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
5530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgvoid MainWnd::HandleTabbing() {
5540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bool shift = ((::GetAsyncKeyState(VK_SHIFT) & 0x8000) != 0);
5550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  UINT next_cmd = shift ? GW_HWNDPREV : GW_HWNDNEXT;
5560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  UINT loop_around_cmd = shift ? GW_HWNDLAST : GW_HWNDFIRST;
5570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  HWND focus = GetFocus(), next;
5580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  do {
5590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    next = ::GetWindow(focus, next_cmd);
5600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    if (IsWindowVisible(next) &&
5610e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        (GetWindowLong(next, GWL_STYLE) & WS_TABSTOP)) {
5620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      break;
5630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    }
5640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
5650e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    if (!next) {
5660e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      next = ::GetWindow(focus, loop_around_cmd);
5670e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      if (IsWindowVisible(next) &&
5680e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org          (GetWindowLong(next, GWL_STYLE) & WS_TABSTOP)) {
5690e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org        break;
5700e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org      }
5710e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    }
5720e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    focus = next;
5730e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  } while (true);
5740e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ::SetFocus(next);
5750e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
5760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
5770e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//
5780e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// MainWnd::VideoRenderer
5790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org//
5800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
5810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgMainWnd::VideoRenderer::VideoRenderer(
5820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    HWND wnd, int width, int height,
5830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    webrtc::VideoTrackInterface* track_to_render)
5840e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    : wnd_(wnd), rendered_track_(track_to_render) {
5850e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ::InitializeCriticalSection(&buffer_lock_);
5860e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ZeroMemory(&bmi_, sizeof(bmi_));
5870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bmi_.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
5880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bmi_.bmiHeader.biPlanes = 1;
5890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bmi_.bmiHeader.biBitCount = 32;
5900e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bmi_.bmiHeader.biCompression = BI_RGB;
5910e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bmi_.bmiHeader.biWidth = width;
5920e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bmi_.bmiHeader.biHeight = -height;
5930e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bmi_.bmiHeader.biSizeImage = width * height *
5940e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                              (bmi_.bmiHeader.biBitCount >> 3);
5950e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  rendered_track_->AddRenderer(this);
5960e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
5970e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
5980e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgMainWnd::VideoRenderer::~VideoRenderer() {
5990e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  rendered_track_->RemoveRenderer(this);
6000e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  ::DeleteCriticalSection(&buffer_lock_);
6010e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
6020e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
6030e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgvoid MainWnd::VideoRenderer::SetSize(int width, int height) {
6040e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  AutoLock<VideoRenderer> lock(this);
6050e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
6060e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bmi_.bmiHeader.biWidth = width;
6070e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bmi_.bmiHeader.biHeight = -height;
6080e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  bmi_.bmiHeader.biSizeImage = width * height *
6090e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                               (bmi_.bmiHeader.biBitCount >> 3);
6100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  image_.reset(new uint8[bmi_.bmiHeader.biSizeImage]);
6110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
6120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
6130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgvoid MainWnd::VideoRenderer::RenderFrame(const cricket::VideoFrame* frame) {
6140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  if (!frame)
6150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    return;
6160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
6170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  {
6180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    AutoLock<VideoRenderer> lock(this);
6190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
6200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    ASSERT(image_.get() != NULL);
6210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org    frame->ConvertToRgbBuffer(cricket::FOURCC_ARGB,
6220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                              image_.get(),
6230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                              bmi_.bmiHeader.biSizeImage,
6240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                              bmi_.bmiHeader.biWidth *
6250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                              bmi_.bmiHeader.biBitCount / 8);
6260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  }
6270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  InvalidateRect(wnd_, NULL, TRUE);
6280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}
629