blob: 6d39b38aa9d0671c464398be300b53b658334a83 [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
18#include "talk/app/webrtc/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"
24#include "webrtc/media/base/videorenderer.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
105 class VideoRenderer : public webrtc::VideoRendererInterface {
106 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
119 // VideoRendererInterface implementation
120 virtual void SetSize(int width, int height);
121 virtual void RenderFrame(const cricket::VideoFrame* frame);
122
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:
127 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_