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 WEBRTC_EXAMPLES_PEERCONNECTION_CLIENT_MAIN_WND_H_ 12 #define WEBRTC_EXAMPLES_PEERCONNECTION_CLIENT_MAIN_WND_H_ 13 #pragma once 14 15 #include <map> 16 #include <string> 17 18 #include "talk/app/webrtc/mediastreaminterface.h" 19 #include "webrtc/examples/peerconnection/client/peer_connection_client.h" 20 #include "talk/media/base/mediachannel.h" 21 #include "talk/media/base/videocommon.h" 22 #include "talk/media/base/videoframe.h" 23 #include "talk/media/base/videorenderer.h" 24 #include "webrtc/base/win32.h" 25 26 class MainWndCallback { 27 public: 28 virtual void StartLogin(const std::string& server, int port) = 0; 29 virtual void DisconnectFromServer() = 0; 30 virtual void ConnectToPeer(int peer_id) = 0; 31 virtual void DisconnectFromCurrentPeer() = 0; 32 virtual void UIThreadCallback(int msg_id, void* data) = 0; 33 virtual void Close() = 0; 34 protected: ~MainWndCallback()35 virtual ~MainWndCallback() {} 36 }; 37 38 // Pure virtual interface for the main window. 39 class MainWindow { 40 public: ~MainWindow()41 virtual ~MainWindow() {} 42 43 enum UI { 44 CONNECT_TO_SERVER, 45 LIST_PEERS, 46 STREAMING, 47 }; 48 49 virtual void RegisterObserver(MainWndCallback* callback) = 0; 50 51 virtual bool IsWindow() = 0; 52 virtual void MessageBox(const char* caption, const char* text, 53 bool is_error) = 0; 54 55 virtual UI current_ui() = 0; 56 57 virtual void SwitchToConnectUI() = 0; 58 virtual void SwitchToPeerList(const Peers& peers) = 0; 59 virtual void SwitchToStreamingUI() = 0; 60 61 virtual void StartLocalRenderer(webrtc::VideoTrackInterface* local_video) = 0; 62 virtual void StopLocalRenderer() = 0; 63 virtual void StartRemoteRenderer( 64 webrtc::VideoTrackInterface* remote_video) = 0; 65 virtual void StopRemoteRenderer() = 0; 66 67 virtual void QueueUIThreadCallback(int msg_id, void* data) = 0; 68 }; 69 70 #ifdef WIN32 71 72 class MainWnd : public MainWindow { 73 public: 74 static const wchar_t kClassName[]; 75 76 enum WindowMessages { 77 UI_THREAD_CALLBACK = WM_APP + 1, 78 }; 79 80 MainWnd(const char* server, int port, bool auto_connect, bool auto_call); 81 ~MainWnd(); 82 83 bool Create(); 84 bool Destroy(); 85 bool PreTranslateMessage(MSG* msg); 86 87 virtual void RegisterObserver(MainWndCallback* callback); 88 virtual bool IsWindow(); 89 virtual void SwitchToConnectUI(); 90 virtual void SwitchToPeerList(const Peers& peers); 91 virtual void SwitchToStreamingUI(); 92 virtual void MessageBox(const char* caption, const char* text, 93 bool is_error); current_ui()94 virtual UI current_ui() { return ui_; } 95 96 virtual void StartLocalRenderer(webrtc::VideoTrackInterface* local_video); 97 virtual void StopLocalRenderer(); 98 virtual void StartRemoteRenderer(webrtc::VideoTrackInterface* remote_video); 99 virtual void StopRemoteRenderer(); 100 101 virtual void QueueUIThreadCallback(int msg_id, void* data); 102 handle()103 HWND handle() const { return wnd_; } 104 105 class VideoRenderer : public webrtc::VideoRendererInterface { 106 public: 107 VideoRenderer(HWND wnd, int width, int height, 108 webrtc::VideoTrackInterface* track_to_render); 109 virtual ~VideoRenderer(); 110 Lock()111 void Lock() { 112 ::EnterCriticalSection(&buffer_lock_); 113 } 114 Unlock()115 void Unlock() { 116 ::LeaveCriticalSection(&buffer_lock_); 117 } 118 119 // VideoRendererInterface implementation 120 virtual void SetSize(int width, int height); 121 virtual void RenderFrame(const cricket::VideoFrame* frame); 122 bmi()123 const BITMAPINFO& bmi() const { return bmi_; } image()124 const uint8_t* image() const { return image_.get(); } 125 126 protected: 127 enum { 128 SET_SIZE, 129 RENDER_FRAME, 130 }; 131 132 HWND wnd_; 133 BITMAPINFO bmi_; 134 rtc::scoped_ptr<uint8_t[]> image_; 135 CRITICAL_SECTION buffer_lock_; 136 rtc::scoped_refptr<webrtc::VideoTrackInterface> rendered_track_; 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: AutoLock(T * obj)144 explicit AutoLock(T* obj) : obj_(obj) { obj_->Lock(); } ~AutoLock()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: 179 rtc::scoped_ptr<VideoRenderer> local_renderer_; 180 rtc::scoped_ptr<VideoRenderer> remote_renderer_; 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_; 194 std::string server_; 195 std::string port_; 196 bool auto_connect_; 197 bool auto_call_; 198 }; 199 #endif // WIN32 200 201 #endif // WEBRTC_EXAMPLES_PEERCONNECTION_CLIENT_MAIN_WND_H_ 202