main_wnd.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright 2012 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * 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.
  9. */
  10. #ifndef EXAMPLES_PEERCONNECTION_CLIENT_MAIN_WND_H_
  11. #define EXAMPLES_PEERCONNECTION_CLIENT_MAIN_WND_H_
  12. #include <map>
  13. #include <memory>
  14. #include <string>
  15. #include "api/media_stream_interface.h"
  16. #include "api/video/video_frame.h"
  17. #include "examples/peerconnection/client/peer_connection_client.h"
  18. #include "media/base/media_channel.h"
  19. #include "media/base/video_common.h"
  20. #if defined(WEBRTC_WIN)
  21. #include "rtc_base/win32.h"
  22. #endif // WEBRTC_WIN
  23. class MainWndCallback {
  24. public:
  25. virtual void StartLogin(const std::string& server, int port) = 0;
  26. virtual void DisconnectFromServer() = 0;
  27. virtual void ConnectToPeer(int peer_id) = 0;
  28. virtual void DisconnectFromCurrentPeer() = 0;
  29. virtual void UIThreadCallback(int msg_id, void* data) = 0;
  30. virtual void Close() = 0;
  31. protected:
  32. virtual ~MainWndCallback() {}
  33. };
  34. // Pure virtual interface for the main window.
  35. class MainWindow {
  36. public:
  37. virtual ~MainWindow() {}
  38. enum UI {
  39. CONNECT_TO_SERVER,
  40. LIST_PEERS,
  41. STREAMING,
  42. };
  43. virtual void RegisterObserver(MainWndCallback* callback) = 0;
  44. virtual bool IsWindow() = 0;
  45. virtual void MessageBox(const char* caption,
  46. const char* text,
  47. bool is_error) = 0;
  48. virtual UI current_ui() = 0;
  49. virtual void SwitchToConnectUI() = 0;
  50. virtual void SwitchToPeerList(const Peers& peers) = 0;
  51. virtual void SwitchToStreamingUI() = 0;
  52. virtual void StartLocalRenderer(webrtc::VideoTrackInterface* local_video) = 0;
  53. virtual void StopLocalRenderer() = 0;
  54. virtual void StartRemoteRenderer(
  55. webrtc::VideoTrackInterface* remote_video) = 0;
  56. virtual void StopRemoteRenderer() = 0;
  57. virtual void QueueUIThreadCallback(int msg_id, void* data) = 0;
  58. };
  59. #ifdef WIN32
  60. class MainWnd : public MainWindow {
  61. public:
  62. static const wchar_t kClassName[];
  63. enum WindowMessages {
  64. UI_THREAD_CALLBACK = WM_APP + 1,
  65. };
  66. MainWnd(const char* server, int port, bool auto_connect, bool auto_call);
  67. ~MainWnd();
  68. bool Create();
  69. bool Destroy();
  70. bool PreTranslateMessage(MSG* msg);
  71. virtual void RegisterObserver(MainWndCallback* callback);
  72. virtual bool IsWindow();
  73. virtual void SwitchToConnectUI();
  74. virtual void SwitchToPeerList(const Peers& peers);
  75. virtual void SwitchToStreamingUI();
  76. virtual void MessageBox(const char* caption, const char* text, bool is_error);
  77. virtual UI current_ui() { return ui_; }
  78. virtual void StartLocalRenderer(webrtc::VideoTrackInterface* local_video);
  79. virtual void StopLocalRenderer();
  80. virtual void StartRemoteRenderer(webrtc::VideoTrackInterface* remote_video);
  81. virtual void StopRemoteRenderer();
  82. virtual void QueueUIThreadCallback(int msg_id, void* data);
  83. HWND handle() const { return wnd_; }
  84. class VideoRenderer : public rtc::VideoSinkInterface<webrtc::VideoFrame> {
  85. public:
  86. VideoRenderer(HWND wnd,
  87. int width,
  88. int height,
  89. webrtc::VideoTrackInterface* track_to_render);
  90. virtual ~VideoRenderer();
  91. void Lock() { ::EnterCriticalSection(&buffer_lock_); }
  92. void Unlock() { ::LeaveCriticalSection(&buffer_lock_); }
  93. // VideoSinkInterface implementation
  94. void OnFrame(const webrtc::VideoFrame& frame) override;
  95. const BITMAPINFO& bmi() const { return bmi_; }
  96. const uint8_t* image() const { return image_.get(); }
  97. protected:
  98. void SetSize(int width, int height);
  99. enum {
  100. SET_SIZE,
  101. RENDER_FRAME,
  102. };
  103. HWND wnd_;
  104. BITMAPINFO bmi_;
  105. std::unique_ptr<uint8_t[]> image_;
  106. CRITICAL_SECTION buffer_lock_;
  107. rtc::scoped_refptr<webrtc::VideoTrackInterface> rendered_track_;
  108. };
  109. // A little helper class to make sure we always to proper locking and
  110. // unlocking when working with VideoRenderer buffers.
  111. template <typename T>
  112. class AutoLock {
  113. public:
  114. explicit AutoLock(T* obj) : obj_(obj) { obj_->Lock(); }
  115. ~AutoLock() { obj_->Unlock(); }
  116. protected:
  117. T* obj_;
  118. };
  119. protected:
  120. enum ChildWindowID {
  121. EDIT_ID = 1,
  122. BUTTON_ID,
  123. LABEL1_ID,
  124. LABEL2_ID,
  125. LISTBOX_ID,
  126. };
  127. void OnPaint();
  128. void OnDestroyed();
  129. void OnDefaultAction();
  130. bool OnMessage(UINT msg, WPARAM wp, LPARAM lp, LRESULT* result);
  131. static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp);
  132. static bool RegisterWindowClass();
  133. void CreateChildWindow(HWND* wnd,
  134. ChildWindowID id,
  135. const wchar_t* class_name,
  136. DWORD control_style,
  137. DWORD ex_style);
  138. void CreateChildWindows();
  139. void LayoutConnectUI(bool show);
  140. void LayoutPeerListUI(bool show);
  141. void HandleTabbing();
  142. private:
  143. std::unique_ptr<VideoRenderer> local_renderer_;
  144. std::unique_ptr<VideoRenderer> remote_renderer_;
  145. UI ui_;
  146. HWND wnd_;
  147. DWORD ui_thread_id_;
  148. HWND edit1_;
  149. HWND edit2_;
  150. HWND label1_;
  151. HWND label2_;
  152. HWND button_;
  153. HWND listbox_;
  154. bool destroyed_;
  155. void* nested_msg_;
  156. MainWndCallback* callback_;
  157. static ATOM wnd_class_;
  158. std::string server_;
  159. std::string port_;
  160. bool auto_connect_;
  161. bool auto_call_;
  162. };
  163. #endif // WIN32
  164. #endif // EXAMPLES_PEERCONNECTION_CLIENT_MAIN_WND_H_