1 /* 2 * Copyright 2012 The WebRTC Project Authors. All rights reserved. 3 * 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. 9 */ 10 11 #ifndef EXAMPLES_PEERCONNECTION_CLIENT_MAIN_WND_H_ 12 #define EXAMPLES_PEERCONNECTION_CLIENT_MAIN_WND_H_ 13 14 #include <map> 15 #include <memory> 16 #include <string> 17 18 #include "api/media_stream_interface.h" 19 #include "api/video/video_frame.h" 20 #include "examples/peerconnection/client/peer_connection_client.h" 21 #include "media/base/media_channel.h" 22 #include "media/base/video_common.h" 23 #if defined(WEBRTC_WIN) 24 #include "rtc_base/win32.h" 25 #endif // WEBRTC_WIN 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 36 protected: ~MainWndCallback()37 virtual ~MainWndCallback() {} 38 }; 39 40 // Pure virtual interface for the main window. 41 class MainWindow { 42 public: ~MainWindow()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; 54 virtual void MessageBox(const char* caption, 55 const char* text, 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; 66 virtual void StartRemoteRenderer( 67 webrtc::VideoTrackInterface* remote_video) = 0; 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 83 MainWnd(const char* server, int port, bool auto_connect, bool auto_call); 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(); 95 virtual void MessageBox(const char* caption, const char* text, bool is_error); current_ui()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 handle()105 HWND handle() const { return wnd_; } 106 107 class VideoRenderer : public rtc::VideoSinkInterface<webrtc::VideoFrame> { 108 public: 109 VideoRenderer(HWND wnd, 110 int width, 111 int height, 112 webrtc::VideoTrackInterface* track_to_render); 113 virtual ~VideoRenderer(); 114 Lock()115 void Lock() { ::EnterCriticalSection(&buffer_lock_); } 116 Unlock()117 void Unlock() { ::LeaveCriticalSection(&buffer_lock_); } 118 119 // VideoSinkInterface implementation 120 void OnFrame(const webrtc::VideoFrame& frame) override; 121 bmi()122 const BITMAPINFO& bmi() const { return bmi_; } image()123 const uint8_t* image() const { return image_.get(); } 124 125 protected: 126 void SetSize(int width, int height); 127 128 enum { 129 SET_SIZE, 130 RENDER_FRAME, 131 }; 132 133 HWND wnd_; 134 BITMAPINFO bmi_; 135 std::unique_ptr<uint8_t[]> image_; 136 CRITICAL_SECTION buffer_lock_; 137 rtc::scoped_refptr<webrtc::VideoTrackInterface> rendered_track_; 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: AutoLock(T * obj)145 explicit AutoLock(T* obj) : obj_(obj) { obj_->Lock(); } ~AutoLock()146 ~AutoLock() { obj_->Unlock(); } 147 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, 172 ChildWindowID id, 173 const wchar_t* class_name, 174 DWORD control_style, 175 DWORD ex_style); 176 void CreateChildWindows(); 177 178 void LayoutConnectUI(bool show); 179 void LayoutPeerListUI(bool show); 180 181 void HandleTabbing(); 182 183 private: 184 std::unique_ptr<VideoRenderer> local_renderer_; 185 std::unique_ptr<VideoRenderer> remote_renderer_; 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_; 199 std::string server_; 200 std::string port_; 201 bool auto_connect_; 202 bool auto_call_; 203 }; 204 #endif // WIN32 205 206 #endif // EXAMPLES_PEERCONNECTION_CLIENT_MAIN_WND_H_ 207