blob: 543b7ce573560996cfc20a5771eebcf1236d46f9 [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>
kwibergbfefb032016-05-01 14:53:46 -070016#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000017#include <string>
18
Henrik Kjellander15583c12016-02-10 10:53:12 +010019#include "webrtc/api/mediastreaminterface.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000020#include "webrtc/base/win32.h"
kjellandera96e2d72016-02-04 23:52:28 -080021#include "webrtc/examples/peerconnection/client/peer_connection_client.h"
22#include "webrtc/media/base/mediachannel.h"
23#include "webrtc/media/base/videocommon.h"
nisseacd935b2016-11-11 03:55:13 -080024#include "webrtc/video_frame.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;
jbauch70625e52015-12-09 14:18:14 -080063 virtual void StartRemoteRenderer(
64 webrtc::VideoTrackInterface* remote_video) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065 virtual void StopRemoteRenderer() = 0;
66
67 virtual void QueueUIThreadCallback(int msg_id, void* data) = 0;
68};
69
70#ifdef WIN32
71
72class MainWnd : public MainWindow {
73 public:
74 static const wchar_t kClassName[];
75
76 enum WindowMessages {
77 UI_THREAD_CALLBACK = WM_APP + 1,
78 };
79
kjellander@webrtc.org04025152014-07-01 16:28:13 +000080 MainWnd(const char* server, int port, bool auto_connect, bool auto_call);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081 ~MainWnd();
82
83 bool Create();
84 bool Destroy();
85 bool PreTranslateMessage(MSG* msg);
86
87 virtual void RegisterObserver(MainWndCallback* callback);
88 virtual bool IsWindow();
89 virtual void SwitchToConnectUI();
90 virtual void SwitchToPeerList(const Peers& peers);
91 virtual void SwitchToStreamingUI();
92 virtual void MessageBox(const char* caption, const char* text,
93 bool is_error);
94 virtual UI current_ui() { return ui_; }
95
96 virtual void StartLocalRenderer(webrtc::VideoTrackInterface* local_video);
97 virtual void StopLocalRenderer();
98 virtual void StartRemoteRenderer(webrtc::VideoTrackInterface* remote_video);
99 virtual void StopRemoteRenderer();
100
101 virtual void QueueUIThreadCallback(int msg_id, void* data);
102
103 HWND handle() const { return wnd_; }
104
nisseacd935b2016-11-11 03:55:13 -0800105 class VideoRenderer : public rtc::VideoSinkInterface<webrtc::VideoFrame> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106 public:
107 VideoRenderer(HWND wnd, int width, int height,
108 webrtc::VideoTrackInterface* track_to_render);
109 virtual ~VideoRenderer();
110
111 void Lock() {
112 ::EnterCriticalSection(&buffer_lock_);
113 }
114
115 void Unlock() {
116 ::LeaveCriticalSection(&buffer_lock_);
117 }
118
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(); }
147 protected:
148 T* obj_;
149 };
150
151 protected:
152 enum ChildWindowID {
153 EDIT_ID = 1,
154 BUTTON_ID,
155 LABEL1_ID,
156 LABEL2_ID,
157 LISTBOX_ID,
158 };
159
160 void OnPaint();
161 void OnDestroyed();
162
163 void OnDefaultAction();
164
165 bool OnMessage(UINT msg, WPARAM wp, LPARAM lp, LRESULT* result);
166
167 static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp);
168 static bool RegisterWindowClass();
169
170 void CreateChildWindow(HWND* wnd, ChildWindowID id, const wchar_t* class_name,
171 DWORD control_style, DWORD ex_style);
172 void CreateChildWindows();
173
174 void LayoutConnectUI(bool show);
175 void LayoutPeerListUI(bool show);
176
177 void HandleTabbing();
178
179 private:
kwibergbfefb032016-05-01 14:53:46 -0700180 std::unique_ptr<VideoRenderer> local_renderer_;
181 std::unique_ptr<VideoRenderer> remote_renderer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182 UI ui_;
183 HWND wnd_;
184 DWORD ui_thread_id_;
185 HWND edit1_;
186 HWND edit2_;
187 HWND label1_;
188 HWND label2_;
189 HWND button_;
190 HWND listbox_;
191 bool destroyed_;
192 void* nested_msg_;
193 MainWndCallback* callback_;
194 static ATOM wnd_class_;
kjellander@webrtc.org04025152014-07-01 16:28:13 +0000195 std::string server_;
196 std::string port_;
197 bool auto_connect_;
198 bool auto_call_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000199};
200#endif // WIN32
201
jbauch70625e52015-12-09 14:18:14 -0800202#endif // WEBRTC_EXAMPLES_PEERCONNECTION_CLIENT_MAIN_WND_H_