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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef EXAMPLES_PEERCONNECTION_CLIENT_MAIN_WND_H_ |
| 12 | #define EXAMPLES_PEERCONNECTION_CLIENT_MAIN_WND_H_ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 13 | |
| 14 | #include <map> |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 15 | #include <memory> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 16 | #include <string> |
| 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "api/mediastreaminterface.h" |
| 19 | #include "api/video/video_frame.h" |
| 20 | #include "examples/peerconnection/client/peer_connection_client.h" |
| 21 | #include "media/base/mediachannel.h" |
| 22 | #include "media/base/videocommon.h" |
Mirko Bonadei | e062385 | 2018-02-01 11:17:40 +0100 | [diff] [blame^] | 23 | #if defined(WEBRTC_WIN) |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 24 | #include "rtc_base/win32.h" |
Mirko Bonadei | e062385 | 2018-02-01 11:17:40 +0100 | [diff] [blame^] | 25 | #endif // WEBRTC_WIN |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 26 | |
| 27 | class MainWndCallback { |
| 28 | public: |
| 29 | virtual void StartLogin(const std::string& server, int port) = 0; |
| 30 | virtual void DisconnectFromServer() = 0; |
| 31 | virtual void ConnectToPeer(int peer_id) = 0; |
| 32 | virtual void DisconnectFromCurrentPeer() = 0; |
| 33 | virtual void UIThreadCallback(int msg_id, void* data) = 0; |
| 34 | virtual void Close() = 0; |
| 35 | protected: |
| 36 | virtual ~MainWndCallback() {} |
| 37 | }; |
| 38 | |
| 39 | // Pure virtual interface for the main window. |
| 40 | class MainWindow { |
| 41 | public: |
| 42 | virtual ~MainWindow() {} |
| 43 | |
| 44 | enum UI { |
| 45 | CONNECT_TO_SERVER, |
| 46 | LIST_PEERS, |
| 47 | STREAMING, |
| 48 | }; |
| 49 | |
| 50 | virtual void RegisterObserver(MainWndCallback* callback) = 0; |
| 51 | |
| 52 | virtual bool IsWindow() = 0; |
| 53 | virtual void MessageBox(const char* caption, const char* text, |
| 54 | bool is_error) = 0; |
| 55 | |
| 56 | virtual UI current_ui() = 0; |
| 57 | |
| 58 | virtual void SwitchToConnectUI() = 0; |
| 59 | virtual void SwitchToPeerList(const Peers& peers) = 0; |
| 60 | virtual void SwitchToStreamingUI() = 0; |
| 61 | |
| 62 | virtual void StartLocalRenderer(webrtc::VideoTrackInterface* local_video) = 0; |
| 63 | virtual void StopLocalRenderer() = 0; |
jbauch | 70625e5 | 2015-12-09 14:18:14 -0800 | [diff] [blame] | 64 | virtual void StartRemoteRenderer( |
| 65 | webrtc::VideoTrackInterface* remote_video) = 0; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 66 | virtual void StopRemoteRenderer() = 0; |
| 67 | |
| 68 | virtual void QueueUIThreadCallback(int msg_id, void* data) = 0; |
| 69 | }; |
| 70 | |
| 71 | #ifdef WIN32 |
| 72 | |
| 73 | class MainWnd : public MainWindow { |
| 74 | public: |
| 75 | static const wchar_t kClassName[]; |
| 76 | |
| 77 | enum WindowMessages { |
| 78 | UI_THREAD_CALLBACK = WM_APP + 1, |
| 79 | }; |
| 80 | |
kjellander@webrtc.org | 0402515 | 2014-07-01 16:28:13 +0000 | [diff] [blame] | 81 | MainWnd(const char* server, int port, bool auto_connect, bool auto_call); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 82 | ~MainWnd(); |
| 83 | |
| 84 | bool Create(); |
| 85 | bool Destroy(); |
| 86 | bool PreTranslateMessage(MSG* msg); |
| 87 | |
| 88 | virtual void RegisterObserver(MainWndCallback* callback); |
| 89 | virtual bool IsWindow(); |
| 90 | virtual void SwitchToConnectUI(); |
| 91 | virtual void SwitchToPeerList(const Peers& peers); |
| 92 | virtual void SwitchToStreamingUI(); |
| 93 | virtual void MessageBox(const char* caption, const char* text, |
| 94 | bool is_error); |
| 95 | virtual UI current_ui() { return ui_; } |
| 96 | |
| 97 | virtual void StartLocalRenderer(webrtc::VideoTrackInterface* local_video); |
| 98 | virtual void StopLocalRenderer(); |
| 99 | virtual void StartRemoteRenderer(webrtc::VideoTrackInterface* remote_video); |
| 100 | virtual void StopRemoteRenderer(); |
| 101 | |
| 102 | virtual void QueueUIThreadCallback(int msg_id, void* data); |
| 103 | |
| 104 | HWND handle() const { return wnd_; } |
| 105 | |
nisse | acd935b | 2016-11-11 03:55:13 -0800 | [diff] [blame] | 106 | class VideoRenderer : public rtc::VideoSinkInterface<webrtc::VideoFrame> { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 107 | public: |
| 108 | VideoRenderer(HWND wnd, int width, int height, |
| 109 | webrtc::VideoTrackInterface* track_to_render); |
| 110 | virtual ~VideoRenderer(); |
| 111 | |
| 112 | void Lock() { |
| 113 | ::EnterCriticalSection(&buffer_lock_); |
| 114 | } |
| 115 | |
| 116 | void Unlock() { |
| 117 | ::LeaveCriticalSection(&buffer_lock_); |
| 118 | } |
| 119 | |
Niels Möller | 8f59762 | 2016-03-23 10:33:07 +0100 | [diff] [blame] | 120 | // VideoSinkInterface implementation |
nisse | acd935b | 2016-11-11 03:55:13 -0800 | [diff] [blame] | 121 | void OnFrame(const webrtc::VideoFrame& frame) override; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 122 | |
| 123 | const BITMAPINFO& bmi() const { return bmi_; } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 124 | const uint8_t* image() const { return image_.get(); } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 125 | |
| 126 | protected: |
Niels Möller | 8f59762 | 2016-03-23 10:33:07 +0100 | [diff] [blame] | 127 | void SetSize(int width, int height); |
| 128 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 129 | enum { |
| 130 | SET_SIZE, |
| 131 | RENDER_FRAME, |
| 132 | }; |
| 133 | |
| 134 | HWND wnd_; |
| 135 | BITMAPINFO bmi_; |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 136 | std::unique_ptr<uint8_t[]> image_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 137 | CRITICAL_SECTION buffer_lock_; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 138 | rtc::scoped_refptr<webrtc::VideoTrackInterface> rendered_track_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | // A little helper class to make sure we always to proper locking and |
| 142 | // unlocking when working with VideoRenderer buffers. |
| 143 | template <typename T> |
| 144 | class AutoLock { |
| 145 | public: |
| 146 | explicit AutoLock(T* obj) : obj_(obj) { obj_->Lock(); } |
| 147 | ~AutoLock() { obj_->Unlock(); } |
| 148 | protected: |
| 149 | T* obj_; |
| 150 | }; |
| 151 | |
| 152 | protected: |
| 153 | enum ChildWindowID { |
| 154 | EDIT_ID = 1, |
| 155 | BUTTON_ID, |
| 156 | LABEL1_ID, |
| 157 | LABEL2_ID, |
| 158 | LISTBOX_ID, |
| 159 | }; |
| 160 | |
| 161 | void OnPaint(); |
| 162 | void OnDestroyed(); |
| 163 | |
| 164 | void OnDefaultAction(); |
| 165 | |
| 166 | bool OnMessage(UINT msg, WPARAM wp, LPARAM lp, LRESULT* result); |
| 167 | |
| 168 | static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp); |
| 169 | static bool RegisterWindowClass(); |
| 170 | |
| 171 | void CreateChildWindow(HWND* wnd, ChildWindowID id, const wchar_t* class_name, |
| 172 | DWORD control_style, DWORD ex_style); |
| 173 | void CreateChildWindows(); |
| 174 | |
| 175 | void LayoutConnectUI(bool show); |
| 176 | void LayoutPeerListUI(bool show); |
| 177 | |
| 178 | void HandleTabbing(); |
| 179 | |
| 180 | private: |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 181 | std::unique_ptr<VideoRenderer> local_renderer_; |
| 182 | std::unique_ptr<VideoRenderer> remote_renderer_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 183 | UI ui_; |
| 184 | HWND wnd_; |
| 185 | DWORD ui_thread_id_; |
| 186 | HWND edit1_; |
| 187 | HWND edit2_; |
| 188 | HWND label1_; |
| 189 | HWND label2_; |
| 190 | HWND button_; |
| 191 | HWND listbox_; |
| 192 | bool destroyed_; |
| 193 | void* nested_msg_; |
| 194 | MainWndCallback* callback_; |
| 195 | static ATOM wnd_class_; |
kjellander@webrtc.org | 0402515 | 2014-07-01 16:28:13 +0000 | [diff] [blame] | 196 | std::string server_; |
| 197 | std::string port_; |
| 198 | bool auto_connect_; |
| 199 | bool auto_call_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 200 | }; |
| 201 | #endif // WIN32 |
| 202 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 203 | #endif // EXAMPLES_PEERCONNECTION_CLIENT_MAIN_WND_H_ |