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