3 * Copyright 2012, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #ifndef PEERCONNECTION_SAMPLES_CLIENT_MAIN_WND_H_
29 #define PEERCONNECTION_SAMPLES_CLIENT_MAIN_WND_H_
35 #include "talk/app/webrtc/mediastreaminterface.h"
36 #include "talk/examples/peerconnection/client/peer_connection_client.h"
37 #include "talk/media/base/mediachannel.h"
38 #include "talk/media/base/videocommon.h"
39 #include "talk/media/base/videoframe.h"
40 #include "talk/media/base/videorenderer.h"
41 #include "webrtc/base/win32.h"
43 class MainWndCallback {
45 virtual void StartLogin(const std::string& server, int port) = 0;
46 virtual void DisconnectFromServer() = 0;
47 virtual void ConnectToPeer(int peer_id) = 0;
48 virtual void DisconnectFromCurrentPeer() = 0;
49 virtual void UIThreadCallback(int msg_id, void* data) = 0;
50 virtual void Close() = 0;
52 virtual ~MainWndCallback() {}
55 // Pure virtual interface for the main window.
58 virtual ~MainWindow() {}
66 virtual void RegisterObserver(MainWndCallback* callback) = 0;
68 virtual bool IsWindow() = 0;
69 virtual void MessageBox(const char* caption, const char* text,
72 virtual UI current_ui() = 0;
74 virtual void SwitchToConnectUI() = 0;
75 virtual void SwitchToPeerList(const Peers& peers) = 0;
76 virtual void SwitchToStreamingUI() = 0;
78 virtual void StartLocalRenderer(webrtc::VideoTrackInterface* local_video) = 0;
79 virtual void StopLocalRenderer() = 0;
80 virtual void StartRemoteRenderer(webrtc::VideoTrackInterface* remote_video) = 0;
81 virtual void StopRemoteRenderer() = 0;
83 virtual void QueueUIThreadCallback(int msg_id, void* data) = 0;
88 class MainWnd : public MainWindow {
90 static const wchar_t kClassName[];
93 UI_THREAD_CALLBACK = WM_APP + 1,
96 MainWnd(const char* server, int port, bool auto_connect, bool auto_call);
101 bool PreTranslateMessage(MSG* msg);
103 virtual void RegisterObserver(MainWndCallback* callback);
104 virtual bool IsWindow();
105 virtual void SwitchToConnectUI();
106 virtual void SwitchToPeerList(const Peers& peers);
107 virtual void SwitchToStreamingUI();
108 virtual void MessageBox(const char* caption, const char* text,
110 virtual UI current_ui() { return ui_; }
112 virtual void StartLocalRenderer(webrtc::VideoTrackInterface* local_video);
113 virtual void StopLocalRenderer();
114 virtual void StartRemoteRenderer(webrtc::VideoTrackInterface* remote_video);
115 virtual void StopRemoteRenderer();
117 virtual void QueueUIThreadCallback(int msg_id, void* data);
119 HWND handle() const { return wnd_; }
121 class VideoRenderer : public webrtc::VideoRendererInterface {
123 VideoRenderer(HWND wnd, int width, int height,
124 webrtc::VideoTrackInterface* track_to_render);
125 virtual ~VideoRenderer();
128 ::EnterCriticalSection(&buffer_lock_);
132 ::LeaveCriticalSection(&buffer_lock_);
135 // VideoRendererInterface implementation
136 virtual void SetSize(int width, int height);
137 virtual void RenderFrame(const cricket::VideoFrame* frame);
139 const BITMAPINFO& bmi() const { return bmi_; }
140 const uint8* image() const { return image_.get(); }
150 rtc::scoped_ptr<uint8[]> image_;
151 CRITICAL_SECTION buffer_lock_;
152 rtc::scoped_refptr<webrtc::VideoTrackInterface> rendered_track_;
155 // A little helper class to make sure we always to proper locking and
156 // unlocking when working with VideoRenderer buffers.
157 template <typename T>
160 explicit AutoLock(T* obj) : obj_(obj) { obj_->Lock(); }
161 ~AutoLock() { obj_->Unlock(); }
178 void OnDefaultAction();
180 bool OnMessage(UINT msg, WPARAM wp, LPARAM lp, LRESULT* result);
182 static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp);
183 static bool RegisterWindowClass();
185 void CreateChildWindow(HWND* wnd, ChildWindowID id, const wchar_t* class_name,
186 DWORD control_style, DWORD ex_style);
187 void CreateChildWindows();
189 void LayoutConnectUI(bool show);
190 void LayoutPeerListUI(bool show);
192 void HandleTabbing();
195 rtc::scoped_ptr<VideoRenderer> local_renderer_;
196 rtc::scoped_ptr<VideoRenderer> remote_renderer_;
208 MainWndCallback* callback_;
209 static ATOM wnd_class_;
217 #endif // PEERCONNECTION_SAMPLES_CLIENT_MAIN_WND_H_