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