blob: 80db2a5adcf5f2e06ed68f295391e2ff2ff3ca94 [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
jbauch70625e52015-12-09 14:18:14 -080011#ifndef WEBRTC_EXAMPLES_PEERCONNECTION_CLIENT_MAIN_WND_H_
12#define WEBRTC_EXAMPLES_PEERCONNECTION_CLIENT_MAIN_WND_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000013#pragma once
14
15#include <map>
16#include <string>
17
Henrik Kjellander15583c12016-02-10 10:53:12 +010018#include "webrtc/api/mediastreaminterface.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000019#include "webrtc/base/win32.h"
kjellandera96e2d72016-02-04 23:52:28 -080020#include "webrtc/examples/peerconnection/client/peer_connection_client.h"
21#include "webrtc/media/base/mediachannel.h"
22#include "webrtc/media/base/videocommon.h"
23#include "webrtc/media/base/videoframe.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000024
25class MainWndCallback {
26 public:
27 virtual void StartLogin(const std::string& server, int port) = 0;
28 virtual void DisconnectFromServer() = 0;
29 virtual void ConnectToPeer(int peer_id) = 0;
30 virtual void DisconnectFromCurrentPeer() = 0;
31 virtual void UIThreadCallback(int msg_id, void* data) = 0;
32 virtual void Close() = 0;
33 protected:
34 virtual ~MainWndCallback() {}
35};
36
37// Pure virtual interface for the main window.
38class MainWindow {
39 public:
40 virtual ~MainWindow() {}
41
42 enum UI {
43 CONNECT_TO_SERVER,
44 LIST_PEERS,
45 STREAMING,
46 };
47
48 virtual void RegisterObserver(MainWndCallback* callback) = 0;
49
50 virtual bool IsWindow() = 0;
51 virtual void MessageBox(const char* caption, const char* text,
52 bool is_error) = 0;
53
54 virtual UI current_ui() = 0;
55
56 virtual void SwitchToConnectUI() = 0;
57 virtual void SwitchToPeerList(const Peers& peers) = 0;
58 virtual void SwitchToStreamingUI() = 0;
59
60 virtual void StartLocalRenderer(webrtc::VideoTrackInterface* local_video) = 0;
61 virtual void StopLocalRenderer() = 0;
jbauch70625e52015-12-09 14:18:14 -080062 virtual void StartRemoteRenderer(
63 webrtc::VideoTrackInterface* remote_video) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064 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
Niels Möller8f597622016-03-23 10:33:07 +0100104 class VideoRenderer : public rtc::VideoSinkInterface<cricket::VideoFrame> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105 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
Niels Möller8f597622016-03-23 10:33:07 +0100118 // VideoSinkInterface implementation
119 void OnFrame(const cricket::VideoFrame& frame) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000120
121 const BITMAPINFO& bmi() const { return bmi_; }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200122 const uint8_t* image() const { return image_.get(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123
124 protected:
Niels Möller8f597622016-03-23 10:33:07 +0100125 void SetSize(int width, int height);
126
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127 enum {
128 SET_SIZE,
129 RENDER_FRAME,
130 };
131
132 HWND wnd_;
133 BITMAPINFO bmi_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200134 rtc::scoped_ptr<uint8_t[]> image_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135 CRITICAL_SECTION buffer_lock_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000136 rtc::scoped_refptr<webrtc::VideoTrackInterface> rendered_track_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000137 };
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:
144 explicit AutoLock(T* obj) : obj_(obj) { obj_->Lock(); }
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:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000179 rtc::scoped_ptr<VideoRenderer> local_renderer_;
180 rtc::scoped_ptr<VideoRenderer> remote_renderer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181 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_;
kjellander@webrtc.org04025152014-07-01 16:28:13 +0000194 std::string server_;
195 std::string port_;
196 bool auto_connect_;
197 bool auto_call_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198};
199#endif // WIN32
200
jbauch70625e52015-12-09 14:18:14 -0800201#endif // WEBRTC_EXAMPLES_PEERCONNECTION_CLIENT_MAIN_WND_H_