blob: 898fea9d9204214f19d82000924f8b1bf514b2e4 [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
Steve Anton10542f22019-01-11 09:11:00 -080018#include "api/media_stream_interface.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "api/video/video_frame.h"
20#include "examples/peerconnection/client/peer_connection_client.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "media/base/media_channel.h"
22#include "media/base/video_common.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;
Yves Gerey665174f2018-06-19 15:03:05 +020035
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036 protected:
37 virtual ~MainWndCallback() {}
38};
39
40// Pure virtual interface for the main window.
41class MainWindow {
42 public:
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;
Yves Gerey665174f2018-06-19 15:03:05 +020054 virtual void MessageBox(const char* caption,
55 const char* text,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056 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;
jbauch70625e52015-12-09 14:18:14 -080066 virtual void StartRemoteRenderer(
67 webrtc::VideoTrackInterface* remote_video) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068 virtual void StopRemoteRenderer() = 0;
69
70 virtual void QueueUIThreadCallback(int msg_id, void* data) = 0;
71};
72
73#ifdef WIN32
74
75class MainWnd : public MainWindow {
76 public:
77 static const wchar_t kClassName[];
78
79 enum WindowMessages {
80 UI_THREAD_CALLBACK = WM_APP + 1,
81 };
82
kjellander@webrtc.org04025152014-07-01 16:28:13 +000083 MainWnd(const char* server, int port, bool auto_connect, bool auto_call);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084 ~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();
Yves Gerey665174f2018-06-19 15:03:05 +020095 virtual void MessageBox(const char* caption, const char* text, bool is_error);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096 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
105 HWND handle() const { return wnd_; }
106
nisseacd935b2016-11-11 03:55:13 -0800107 class VideoRenderer : public rtc::VideoSinkInterface<webrtc::VideoFrame> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000108 public:
Yves Gerey665174f2018-06-19 15:03:05 +0200109 VideoRenderer(HWND wnd,
110 int width,
111 int height,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000112 webrtc::VideoTrackInterface* track_to_render);
113 virtual ~VideoRenderer();
114
Yves Gerey665174f2018-06-19 15:03:05 +0200115 void Lock() { ::EnterCriticalSection(&buffer_lock_); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116
Yves Gerey665174f2018-06-19 15:03:05 +0200117 void Unlock() { ::LeaveCriticalSection(&buffer_lock_); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118
Niels Möller8f597622016-03-23 10:33:07 +0100119 // VideoSinkInterface implementation
nisseacd935b2016-11-11 03:55:13 -0800120 void OnFrame(const webrtc::VideoFrame& frame) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000121
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:
Niels Möller8f597622016-03-23 10:33:07 +0100126 void SetSize(int width, int height);
127
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128 enum {
129 SET_SIZE,
130 RENDER_FRAME,
131 };
132
133 HWND wnd_;
134 BITMAPINFO bmi_;
kwibergbfefb032016-05-01 14:53:46 -0700135 std::unique_ptr<uint8_t[]> image_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136 CRITICAL_SECTION buffer_lock_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000137 rtc::scoped_refptr<webrtc::VideoTrackInterface> rendered_track_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000138 };
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:
145 explicit AutoLock(T* obj) : obj_(obj) { obj_->Lock(); }
146 ~AutoLock() { obj_->Unlock(); }
Yves Gerey665174f2018-06-19 15:03:05 +0200147
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148 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
Yves Gerey665174f2018-06-19 15:03:05 +0200171 void CreateChildWindow(HWND* wnd,
172 ChildWindowID id,
173 const wchar_t* class_name,
174 DWORD control_style,
175 DWORD ex_style);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176 void CreateChildWindows();
177
178 void LayoutConnectUI(bool show);
179 void LayoutPeerListUI(bool show);
180
181 void HandleTabbing();
182
183 private:
kwibergbfefb032016-05-01 14:53:46 -0700184 std::unique_ptr<VideoRenderer> local_renderer_;
185 std::unique_ptr<VideoRenderer> remote_renderer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000186 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_;
kjellander@webrtc.org04025152014-07-01 16:28:13 +0000199 std::string server_;
200 std::string port_;
201 bool auto_connect_;
202 bool auto_call_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000203};
204#endif // WIN32
205
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200206#endif // EXAMPLES_PEERCONNECTION_CLIENT_MAIN_WND_H_