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