OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef REMOTING_HOST_CAST_EXTENSION_SESSION_H_ |
| 6 #define REMOTING_HOST_CAST_EXTENSION_SESSION_H_ |
| 7 |
| 8 #include <string> |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/timer/timer.h" |
| 13 #include "base/values.h" |
| 14 #include "jingle/glue/thread_wrapper.h" |
| 15 #include "remoting/host/host_extension_session.h" |
| 16 #include "remoting/protocol/client_stub.h" |
| 17 #include "third_party/libjingle/source/talk/app/webrtc/mediastreaminterface.h" |
| 18 #include "third_party/libjingle/source/talk/app/webrtc/peerconnectioninterface.h
" |
| 19 #include "third_party/libjingle/source/talk/base/scoped_ref_ptr.h" |
| 20 #include "third_party/libjingle/source/talk/media/base/videocapturer.h" |
| 21 #include "third_party/webrtc/modules/desktop_capture/screen_capturer.h" |
| 22 |
| 23 namespace base { |
| 24 class SingleThreadTaskRunner; |
| 25 class WaitableEvent; |
| 26 } // namespace base |
| 27 |
| 28 namespace net { |
| 29 class URLRequestContextGetter; |
| 30 } // namespace net |
| 31 |
| 32 namespace remoting { |
| 33 |
| 34 namespace protocol { |
| 35 struct NetworkSettings; |
| 36 } // namespace protocol |
| 37 |
| 38 // A class that extends HostExtensionSession to enable WebRTC support using |
| 39 // the PeerConnection native APIs. |
| 40 class CastExtensionSession : public HostExtensionSession, |
| 41 public webrtc::PeerConnectionObserver, |
| 42 public webrtc::ScreenCapturer::MouseShapeObserver { |
| 43 public: |
| 44 virtual ~CastExtensionSession(); |
| 45 // Creates and returns a CastExtensionSession object, after performing |
| 46 // initialization steps on it. The caller must take ownership of the returned |
| 47 // object. |
| 48 static CastExtensionSession* Create( |
| 49 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner, |
| 50 scoped_refptr<base::SingleThreadTaskRunner> video_capture_task_runner, |
| 51 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, |
| 52 const protocol::NetworkSettings& network_settings, |
| 53 ClientSession* client_session); |
| 54 |
| 55 // HostExtensionSession implementation. |
| 56 virtual bool OnExtensionMessage( |
| 57 ClientSession* client_session, |
| 58 const protocol::ExtensionMessage& message) OVERRIDE; |
| 59 |
| 60 // PeerConnectionObserver implementation. |
| 61 virtual void OnError() OVERRIDE; |
| 62 virtual void OnSignalingChange( |
| 63 webrtc::PeerConnectionInterface::SignalingState new_state) OVERRIDE; |
| 64 virtual void OnStateChange( |
| 65 webrtc::PeerConnectionObserver::StateType state_changed) OVERRIDE; |
| 66 virtual void OnAddStream(webrtc::MediaStreamInterface* stream) OVERRIDE; |
| 67 virtual void OnRemoveStream(webrtc::MediaStreamInterface* stream) OVERRIDE; |
| 68 virtual void OnDataChannel( |
| 69 webrtc::DataChannelInterface* data_channel) OVERRIDE; |
| 70 virtual void OnRenegotiationNeeded() OVERRIDE; |
| 71 virtual void OnIceConnectionChange( |
| 72 webrtc::PeerConnectionInterface::IceConnectionState new_state) OVERRIDE; |
| 73 virtual void OnIceGatheringChange( |
| 74 webrtc::PeerConnectionInterface::IceGatheringState new_state) OVERRIDE; |
| 75 virtual void OnIceCandidate( |
| 76 const webrtc::IceCandidateInterface* candidate) OVERRIDE; |
| 77 virtual void OnIceComplete() OVERRIDE; |
| 78 |
| 79 // MouseShapeObserver implementation. |
| 80 virtual void OnCursorShapeChanged( |
| 81 webrtc::MouseCursorShape* cursor_shape) OVERRIDE; |
| 82 |
| 83 // Called by webrtc::CreateSessionDescriptionObserver implementation. |
| 84 void OnSuccess(webrtc::SessionDescriptionInterface* desc); |
| 85 void OnFailure(const std::string& error); |
| 86 |
| 87 private: |
| 88 CastExtensionSession( |
| 89 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner, |
| 90 scoped_refptr<base::SingleThreadTaskRunner> video_capture_task_runner, |
| 91 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, |
| 92 const protocol::NetworkSettings& network_settings, |
| 93 ClientSession* client_session); |
| 94 |
| 95 // Sends messages to client using the ClientStub through |client_session_|. |
| 96 // |data| should be json formatted. This method must be called on the |
| 97 // network thread. |
| 98 bool SendMessageToClient(const char* subject, const std::string& data); |
| 99 |
| 100 // Sends the new |cursor_shape| using the ClientStub through |
| 101 // |client_session_|. |
| 102 void SendCursorShape(scoped_ptr<protocol::CursorShapeInfo> cursor_shape); |
| 103 |
| 104 // Creates the jingle wrapper for the current thread, sets send to allowed, |
| 105 // and saves a pointer to the relevant thread pointer in ptr. If |event| |
| 106 // is not NULL, signals the event on completion. |
| 107 void EnsureTaskAndSetSend(talk_base::Thread** ptr, |
| 108 base::WaitableEvent* event = NULL); |
| 109 |
| 110 // Wraps each task runner in JingleThreadWrapper using EnsureTaskAndSetSend(), |
| 111 // returning true if successful. Wrapping the task runners allows them to be |
| 112 // shared with and used by the (about to be created) PeerConnectionFactory. |
| 113 bool WrapTasksAndSave(); |
| 114 |
| 115 // Initializes PeerConnectionFactory and PeerConnection and sends a "ready" |
| 116 // message to client. Returns true if these steps are performed successfully. |
| 117 bool InitializePeerConnection(); |
| 118 |
| 119 // Requests a ScreenCapturer from |client_session_|, sets |this| as the |
| 120 // MouseShapeObserver, constructs a VideoSource, a VideoTrack and a |
| 121 // MediaStream |stream_|, which it adds to the |peer_connection_|. It returns |
| 122 // true if these steps are performed successfully. This method is called when |
| 123 // only when a PeerConnection offer is received from the client. |
| 124 bool InitializeAndAddMediaStream(); |
| 125 |
| 126 // Polls a single stats report from the PeerConnection immediately. Called |
| 127 // periodically using |stats_polling_timer_| after a PeerConnection has been |
| 128 // established. |
| 129 void PollPeerConnectionStats(); |
| 130 |
| 131 // Explicitly sets |peer_conn_factory_|, |peer_connection_| and |stream_| to |
| 132 // NULL. |
| 133 void DeletePeerConnection(); |
| 134 |
| 135 // Check if the connection is active. |
| 136 bool connection_active() const; |
| 137 |
| 138 // TaskRunners that will be used to setup the PeerConnectionFactory's |
| 139 // signalling and worker threads. |
| 140 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_; |
| 141 scoped_refptr<base::SingleThreadTaskRunner> capture_task_runner_; |
| 142 |
| 143 // Objects related to the WebRTC PeerConnection. |
| 144 talk_base::scoped_refptr<webrtc::PeerConnectionFactoryInterface> |
| 145 peer_conn_factory_; |
| 146 talk_base::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_; |
| 147 talk_base::scoped_refptr<webrtc::MediaStreamInterface> stream_; |
| 148 |
| 149 // Jingle Thread wrappers for talk_base::Thread, to be used with |
| 150 // PeerConnection API as well as the VideoScheduler for networking. |
| 151 // Created by calling jingle_glue::EnsureForCurrentMessageLoop() and thus |
| 152 // deletes itself automatically when the associated MessageLoop is destroyed. |
| 153 talk_base::Thread* network_thread_wrapper_; |
| 154 |
| 155 // Jingle Thread wrappers for talk_base::Thread, to be used with |
| 156 // PeerConnection API as well as the VideoScheduler for capturing/as worker. |
| 157 // Deletion of this object is handled same as above. |
| 158 talk_base::Thread* capture_thread_wrapper_; |
| 159 |
| 160 // Parameters passed to ChromiumPortAllocatorFactory on creation. |
| 161 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
| 162 const protocol::NetworkSettings& network_settings_; |
| 163 |
| 164 // Client Session that owns this CastExtensionSession. |
| 165 ClientSession* client_session_; |
| 166 |
| 167 // Used to track webrtc connection statistics. |
| 168 talk_base::scoped_refptr<webrtc::StatsObserver> stats_observer_; |
| 169 |
| 170 // Used to repeatedly poll stats from the |peer_connection_|. |
| 171 base::RepeatingTimer<CastExtensionSession> stats_polling_timer_; |
| 172 |
| 173 DISALLOW_COPY_AND_ASSIGN(CastExtensionSession); |
| 174 }; |
| 175 |
| 176 } // namespace remoting |
| 177 |
| 178 #endif // REMOTING_HOST_CAST_EXTENSION_SESSION_H_ |
| 179 |
OLD | NEW |