You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

213 lines
6.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2012, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef PEERCONNECTION_SAMPLES_CLIENT_MAIN_WND_H_
  28. #define PEERCONNECTION_SAMPLES_CLIENT_MAIN_WND_H_
  29. #pragma once
  30. #include <map>
  31. #include <string>
  32. #include "talk/app/webrtc/mediastreaminterface.h"
  33. #include "talk/base/win32.h"
  34. #include "talk/examples/peerconnection/client/peer_connection_client.h"
  35. #include "talk/media/base/mediachannel.h"
  36. #include "talk/media/base/videocommon.h"
  37. #include "talk/media/base/videoframe.h"
  38. #include "talk/media/base/videorenderer.h"
  39. class MainWndCallback {
  40. public:
  41. virtual void StartLogin(const std::string& server, int port) = 0;
  42. virtual void DisconnectFromServer() = 0;
  43. virtual void ConnectToPeer(int peer_id) = 0;
  44. virtual void DisconnectFromCurrentPeer() = 0;
  45. virtual void UIThreadCallback(int msg_id, void* data) = 0;
  46. virtual void Close() = 0;
  47. protected:
  48. virtual ~MainWndCallback() {}
  49. };
  50. // Pure virtual interface for the main window.
  51. class MainWindow {
  52. public:
  53. virtual ~MainWindow() {}
  54. enum UI {
  55. CONNECT_TO_SERVER,
  56. LIST_PEERS,
  57. STREAMING,
  58. };
  59. virtual void RegisterObserver(MainWndCallback* callback) = 0;
  60. virtual bool IsWindow() = 0;
  61. virtual void MessageBox(const char* caption, const char* text,
  62. bool is_error) = 0;
  63. virtual UI current_ui() = 0;
  64. virtual void SwitchToConnectUI() = 0;
  65. virtual void SwitchToPeerList(const Peers& peers) = 0;
  66. virtual void SwitchToStreamingUI() = 0;
  67. virtual void StartLocalRenderer(webrtc::VideoTrackInterface* local_video) = 0;
  68. virtual void StopLocalRenderer() = 0;
  69. virtual void StartRemoteRenderer(webrtc::VideoTrackInterface* remote_video) = 0;
  70. virtual void StopRemoteRenderer() = 0;
  71. virtual void QueueUIThreadCallback(int msg_id, void* data) = 0;
  72. };
  73. #ifdef WIN32
  74. class MainWnd : public MainWindow {
  75. public:
  76. static const wchar_t kClassName[];
  77. enum WindowMessages {
  78. UI_THREAD_CALLBACK = WM_APP + 1,
  79. };
  80. MainWnd();
  81. ~MainWnd();
  82. bool Create();
  83. bool Destroy();
  84. bool PreTranslateMessage(MSG* msg);
  85. virtual void RegisterObserver(MainWndCallback* callback);
  86. virtual bool IsWindow();
  87. virtual void SwitchToConnectUI();
  88. virtual void SwitchToPeerList(const Peers& peers);
  89. virtual void SwitchToStreamingUI();
  90. virtual void MessageBox(const char* caption, const char* text,
  91. bool is_error);
  92. virtual UI current_ui() { return ui_; }
  93. virtual void StartLocalRenderer(webrtc::VideoTrackInterface* local_video);
  94. virtual void StopLocalRenderer();
  95. virtual void StartRemoteRenderer(webrtc::VideoTrackInterface* remote_video);
  96. virtual void StopRemoteRenderer();
  97. virtual void QueueUIThreadCallback(int msg_id, void* data);
  98. HWND handle() const { return wnd_; }
  99. class VideoRenderer : public webrtc::VideoRendererInterface {
  100. public:
  101. VideoRenderer(HWND wnd, int width, int height,
  102. webrtc::VideoTrackInterface* track_to_render);
  103. virtual ~VideoRenderer();
  104. void Lock() {
  105. ::EnterCriticalSection(&buffer_lock_);
  106. }
  107. void Unlock() {
  108. ::LeaveCriticalSection(&buffer_lock_);
  109. }
  110. // VideoRendererInterface implementation
  111. virtual void SetSize(int width, int height);
  112. virtual void RenderFrame(const cricket::VideoFrame* frame);
  113. const BITMAPINFO& bmi() const { return bmi_; }
  114. const uint8* image() const { return image_.get(); }
  115. protected:
  116. enum {
  117. SET_SIZE,
  118. RENDER_FRAME,
  119. };
  120. HWND wnd_;
  121. BITMAPINFO bmi_;
  122. talk_base::scoped_array<uint8> image_;
  123. CRITICAL_SECTION buffer_lock_;
  124. talk_base::scoped_refptr<webrtc::VideoTrackInterface> rendered_track_;
  125. };
  126. // A little helper class to make sure we always to proper locking and
  127. // unlocking when working with VideoRenderer buffers.
  128. template <typename T>
  129. class AutoLock {
  130. public:
  131. explicit AutoLock(T* obj) : obj_(obj) { obj_->Lock(); }
  132. ~AutoLock() { obj_->Unlock(); }
  133. protected:
  134. T* obj_;
  135. };
  136. protected:
  137. enum ChildWindowID {
  138. EDIT_ID = 1,
  139. BUTTON_ID,
  140. LABEL1_ID,
  141. LABEL2_ID,
  142. LISTBOX_ID,
  143. };
  144. void OnPaint();
  145. void OnDestroyed();
  146. void OnDefaultAction();
  147. bool OnMessage(UINT msg, WPARAM wp, LPARAM lp, LRESULT* result);
  148. static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp);
  149. static bool RegisterWindowClass();
  150. void CreateChildWindow(HWND* wnd, ChildWindowID id, const wchar_t* class_name,
  151. DWORD control_style, DWORD ex_style);
  152. void CreateChildWindows();
  153. void LayoutConnectUI(bool show);
  154. void LayoutPeerListUI(bool show);
  155. void HandleTabbing();
  156. private:
  157. talk_base::scoped_ptr<VideoRenderer> local_renderer_;
  158. talk_base::scoped_ptr<VideoRenderer> remote_renderer_;
  159. UI ui_;
  160. HWND wnd_;
  161. DWORD ui_thread_id_;
  162. HWND edit1_;
  163. HWND edit2_;
  164. HWND label1_;
  165. HWND label2_;
  166. HWND button_;
  167. HWND listbox_;
  168. bool destroyed_;
  169. void* nested_msg_;
  170. MainWndCallback* callback_;
  171. static ATOM wnd_class_;
  172. };
  173. #endif // WIN32
  174. #endif // PEERCONNECTION_SAMPLES_CLIENT_MAIN_WND_H_