blob: 9f61a568fd22a87f4e4469dcc71aadd03db17201 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
Donald E Curtisa8736442015-08-05 15:48:13 -07002 * Copyright 2012 The WebRTC Project Authors. All rights reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
Donald E Curtisa8736442015-08-05 15:48:13 -07004 * 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.org28e20752013-07-10 00:45:36 +00009 */
10
11#ifndef PEERCONNECTION_SAMPLES_CLIENT_MAIN_WND_H_
12#define PEERCONNECTION_SAMPLES_CLIENT_MAIN_WND_H_
13#pragma once
14
15#include <map>
16#include <string>
17
18#include "talk/app/webrtc/mediastreaminterface.h"
Donald E Curtisa8736442015-08-05 15:48:13 -070019#include "webrtc/examples/peerconnection/client/peer_connection_client.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000020#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"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000024#include "webrtc/base/win32.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000025
26class 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:
35 virtual ~MainWndCallback() {}
36};
37
38// Pure virtual interface for the main window.
39class MainWindow {
40 public:
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(webrtc::VideoTrackInterface* remote_video) = 0;
64 virtual void StopRemoteRenderer() = 0;
65
66 virtual void QueueUIThreadCallback(int msg_id, void* data) = 0;
67};
68
69#ifdef WIN32
70
71class 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.org04025152014-07-01 16:28:13 +000079 MainWnd(const char* server, int port, bool auto_connect, bool auto_call);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080 ~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
104 class VideoRenderer : public webrtc::VideoRendererInterface {
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
118 // VideoRendererInterface implementation
119 virtual void SetSize(int width, int height);
120 virtual void RenderFrame(const cricket::VideoFrame* frame);
121
122 const BITMAPINFO& bmi() const { return bmi_; }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200123 const uint8_t* image() const { return image_.get(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124
125 protected:
126 enum {
127 SET_SIZE,
128 RENDER_FRAME,
129 };
130
131 HWND wnd_;
132 BITMAPINFO bmi_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200133 rtc::scoped_ptr<uint8_t[]> image_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000134 CRITICAL_SECTION buffer_lock_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000135 rtc::scoped_refptr<webrtc::VideoTrackInterface> rendered_track_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136 };
137
138 // A little helper class to make sure we always to proper locking and
139 // unlocking when working with VideoRenderer buffers.
140 template <typename T>
141 class AutoLock {
142 public:
143 explicit AutoLock(T* obj) : obj_(obj) { obj_->Lock(); }
144 ~AutoLock() { obj_->Unlock(); }
145 protected:
146 T* obj_;
147 };
148
149 protected:
150 enum ChildWindowID {
151 EDIT_ID = 1,
152 BUTTON_ID,
153 LABEL1_ID,
154 LABEL2_ID,
155 LISTBOX_ID,
156 };
157
158 void OnPaint();
159 void OnDestroyed();
160
161 void OnDefaultAction();
162
163 bool OnMessage(UINT msg, WPARAM wp, LPARAM lp, LRESULT* result);
164
165 static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp);
166 static bool RegisterWindowClass();
167
168 void CreateChildWindow(HWND* wnd, ChildWindowID id, const wchar_t* class_name,
169 DWORD control_style, DWORD ex_style);
170 void CreateChildWindows();
171
172 void LayoutConnectUI(bool show);
173 void LayoutPeerListUI(bool show);
174
175 void HandleTabbing();
176
177 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000178 rtc::scoped_ptr<VideoRenderer> local_renderer_;
179 rtc::scoped_ptr<VideoRenderer> remote_renderer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000180 UI ui_;
181 HWND wnd_;
182 DWORD ui_thread_id_;
183 HWND edit1_;
184 HWND edit2_;
185 HWND label1_;
186 HWND label2_;
187 HWND button_;
188 HWND listbox_;
189 bool destroyed_;
190 void* nested_msg_;
191 MainWndCallback* callback_;
192 static ATOM wnd_class_;
kjellander@webrtc.org04025152014-07-01 16:28:13 +0000193 std::string server_;
194 std::string port_;
195 bool auto_connect_;
196 bool auto_call_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000197};
198#endif // WIN32
199
200#endif // PEERCONNECTION_SAMPLES_CLIENT_MAIN_WND_H_