blob: 8edf81ffb357687d0e2f34f75abeea01b78a7c7d [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef EXAMPLES_PEERCONNECTION_CLIENT_MAIN_WND_H_
12#define EXAMPLES_PEERCONNECTION_CLIENT_MAIN_WND_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000013
14#include <map>
kwibergbfefb032016-05-01 14:53:46 -070015#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000016#include <string>
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "api/mediastreaminterface.h"
19#include "api/video/video_frame.h"
20#include "examples/peerconnection/client/peer_connection_client.h"
21#include "media/base/mediachannel.h"
22#include "media/base/videocommon.h"
Mirko Bonadeie0623852018-02-01 11:17:40 +010023#if defined(WEBRTC_WIN)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/win32.h"
Mirko Bonadeie0623852018-02-01 11:17:40 +010025#endif // WEBRTC_WIN
henrike@webrtc.org28e20752013-07-10 00:45:36 +000026
27class 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 protected:
36 virtual ~MainWndCallback() {}
37};
38
39// Pure virtual interface for the main window.
40class MainWindow {
41 public:
42 virtual ~MainWindow() {}
43
44 enum UI {
45 CONNECT_TO_SERVER,
46 LIST_PEERS,
47 STREAMING,
48 };
49
50 virtual void RegisterObserver(MainWndCallback* callback) = 0;
51
52 virtual bool IsWindow() = 0;
53 virtual void MessageBox(const char* caption, const char* text,
54 bool is_error) = 0;
55
56 virtual UI current_ui() = 0;
57
58 virtual void SwitchToConnectUI() = 0;
59 virtual void SwitchToPeerList(const Peers& peers) = 0;
60 virtual void SwitchToStreamingUI() = 0;
61
62 virtual void StartLocalRenderer(webrtc::VideoTrackInterface* local_video) = 0;
63 virtual void StopLocalRenderer() = 0;
jbauch70625e52015-12-09 14:18:14 -080064 virtual void StartRemoteRenderer(
65 webrtc::VideoTrackInterface* remote_video) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066 virtual void StopRemoteRenderer() = 0;
67
68 virtual void QueueUIThreadCallback(int msg_id, void* data) = 0;
69};
70
71#ifdef WIN32
72
73class MainWnd : public MainWindow {
74 public:
75 static const wchar_t kClassName[];
76
77 enum WindowMessages {
78 UI_THREAD_CALLBACK = WM_APP + 1,
79 };
80
kjellander@webrtc.org04025152014-07-01 16:28:13 +000081 MainWnd(const char* server, int port, bool auto_connect, bool auto_call);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082 ~MainWnd();
83
84 bool Create();
85 bool Destroy();
86 bool PreTranslateMessage(MSG* msg);
87
88 virtual void RegisterObserver(MainWndCallback* callback);
89 virtual bool IsWindow();
90 virtual void SwitchToConnectUI();
91 virtual void SwitchToPeerList(const Peers& peers);
92 virtual void SwitchToStreamingUI();
93 virtual void MessageBox(const char* caption, const char* text,
94 bool is_error);
95 virtual UI current_ui() { return ui_; }
96
97 virtual void StartLocalRenderer(webrtc::VideoTrackInterface* local_video);
98 virtual void StopLocalRenderer();
99 virtual void StartRemoteRenderer(webrtc::VideoTrackInterface* remote_video);
100 virtual void StopRemoteRenderer();
101
102 virtual void QueueUIThreadCallback(int msg_id, void* data);
103
104 HWND handle() const { return wnd_; }
105
nisseacd935b2016-11-11 03:55:13 -0800106 class VideoRenderer : public rtc::VideoSinkInterface<webrtc::VideoFrame> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107 public:
108 VideoRenderer(HWND wnd, int width, int height,
109 webrtc::VideoTrackInterface* track_to_render);
110 virtual ~VideoRenderer();
111
112 void Lock() {
113 ::EnterCriticalSection(&buffer_lock_);
114 }
115
116 void Unlock() {
117 ::LeaveCriticalSection(&buffer_lock_);
118 }
119
Niels Möller8f597622016-03-23 10:33:07 +0100120 // VideoSinkInterface implementation
nisseacd935b2016-11-11 03:55:13 -0800121 void OnFrame(const webrtc::VideoFrame& frame) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000122
123 const BITMAPINFO& bmi() const { return bmi_; }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200124 const uint8_t* image() const { return image_.get(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125
126 protected:
Niels Möller8f597622016-03-23 10:33:07 +0100127 void SetSize(int width, int height);
128
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129 enum {
130 SET_SIZE,
131 RENDER_FRAME,
132 };
133
134 HWND wnd_;
135 BITMAPINFO bmi_;
kwibergbfefb032016-05-01 14:53:46 -0700136 std::unique_ptr<uint8_t[]> image_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000137 CRITICAL_SECTION buffer_lock_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000138 rtc::scoped_refptr<webrtc::VideoTrackInterface> rendered_track_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139 };
140
141 // A little helper class to make sure we always to proper locking and
142 // unlocking when working with VideoRenderer buffers.
143 template <typename T>
144 class AutoLock {
145 public:
146 explicit AutoLock(T* obj) : obj_(obj) { obj_->Lock(); }
147 ~AutoLock() { obj_->Unlock(); }
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, ChildWindowID id, const wchar_t* class_name,
172 DWORD control_style, DWORD ex_style);
173 void CreateChildWindows();
174
175 void LayoutConnectUI(bool show);
176 void LayoutPeerListUI(bool show);
177
178 void HandleTabbing();
179
180 private:
kwibergbfefb032016-05-01 14:53:46 -0700181 std::unique_ptr<VideoRenderer> local_renderer_;
182 std::unique_ptr<VideoRenderer> remote_renderer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183 UI ui_;
184 HWND wnd_;
185 DWORD ui_thread_id_;
186 HWND edit1_;
187 HWND edit2_;
188 HWND label1_;
189 HWND label2_;
190 HWND button_;
191 HWND listbox_;
192 bool destroyed_;
193 void* nested_msg_;
194 MainWndCallback* callback_;
195 static ATOM wnd_class_;
kjellander@webrtc.org04025152014-07-01 16:28:13 +0000196 std::string server_;
197 std::string port_;
198 bool auto_connect_;
199 bool auto_call_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000200};
201#endif // WIN32
202
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200203#endif // EXAMPLES_PEERCONNECTION_CLIENT_MAIN_WND_H_