Chromium Code Reviews| 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/memory/ref_counted.h" | |
| 11 #include "base/memory/scoped_ptr.h" | |
| 12 #include "base/threading/thread.h" | |
| 13 #include "base/timer/timer.h" | |
| 14 #include "base/values.h" | |
| 15 #include "jingle/glue/thread_wrapper.h" | |
| 16 #include "remoting/host/host_extension_session.h" | |
| 17 #include "third_party/libjingle/source/talk/app/webrtc/peerconnectioninterface.h " | |
| 18 #include "third_party/webrtc/base/scoped_ref_ptr.h" | |
| 19 #include "third_party/webrtc/modules/desktop_capture/desktop_capturer.h" | |
| 20 | |
| 21 namespace base { | |
| 22 class SingleThreadTaskRunner; | |
| 23 class WaitableEvent; | |
| 24 } // namespace base | |
| 25 | |
| 26 namespace net { | |
| 27 class URLRequestContextGetter; | |
| 28 } // namespace net | |
| 29 | |
| 30 namespace webrtc { | |
| 31 class MediaStreamInterface; | |
| 32 } // namespace webrtc | |
| 33 | |
| 34 namespace remoting { | |
| 35 | |
| 36 namespace protocol { | |
| 37 struct NetworkSettings; | |
| 38 } // namespace protocol | |
| 39 | |
| 40 // A HostExtensionSession implementation that enables WebRTC support using | |
| 41 // the PeerConnection native API. | |
| 42 class CastExtensionSession : public HostExtensionSession, | |
| 43 public webrtc::PeerConnectionObserver { | |
| 44 public: | |
| 45 virtual ~CastExtensionSession(); | |
| 46 | |
| 47 // Creates and returns a CastExtensionSession object, after performing | |
| 48 // initialization steps on it. The caller must take ownership of the returned | |
| 49 // object. | |
| 50 static scoped_ptr<CastExtensionSession> Create( | |
| 51 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, | |
| 52 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, | |
| 53 const protocol::NetworkSettings& network_settings, | |
| 54 ClientSessionControl* client_session_control, | |
| 55 protocol::ClientStub* client_stub); | |
| 56 | |
| 57 // Called by webrtc::CreateSessionDescriptionObserver implementation. | |
| 58 void OnCreateSessionDescription(webrtc::SessionDescriptionInterface* desc); | |
| 59 void OnCreateSessionDescriptionFailure(const std::string& error); | |
| 60 | |
| 61 // HostExtensionSession interface. | |
| 62 virtual scoped_ptr<webrtc::DesktopCapturer> OnCreateVideoCapturer( | |
| 63 scoped_ptr<webrtc::DesktopCapturer> capturer) OVERRIDE; | |
| 64 virtual bool ModifiesVideoPipeline() const OVERRIDE; | |
| 65 virtual bool OnExtensionMessage( | |
| 66 ClientSessionControl* client_session_control, | |
| 67 protocol::ClientStub* client_stub, | |
| 68 const protocol::ExtensionMessage& message) OVERRIDE; | |
| 69 | |
| 70 // webrtc::PeerConnectionObserver interface. | |
| 71 virtual void OnError() OVERRIDE; | |
| 72 virtual void OnSignalingChange( | |
| 73 webrtc::PeerConnectionInterface::SignalingState new_state) OVERRIDE; | |
| 74 virtual void OnStateChange( | |
| 75 webrtc::PeerConnectionObserver::StateType state_changed) OVERRIDE; | |
| 76 virtual void OnAddStream(webrtc::MediaStreamInterface* stream) OVERRIDE; | |
| 77 virtual void OnRemoveStream(webrtc::MediaStreamInterface* stream) OVERRIDE; | |
| 78 virtual void OnDataChannel( | |
| 79 webrtc::DataChannelInterface* data_channel) OVERRIDE; | |
| 80 virtual void OnRenegotiationNeeded() OVERRIDE; | |
| 81 virtual void OnIceConnectionChange( | |
| 82 webrtc::PeerConnectionInterface::IceConnectionState new_state) OVERRIDE; | |
| 83 virtual void OnIceGatheringChange( | |
| 84 webrtc::PeerConnectionInterface::IceGatheringState new_state) OVERRIDE; | |
| 85 virtual void OnIceCandidate( | |
| 86 const webrtc::IceCandidateInterface* candidate) OVERRIDE; | |
| 87 virtual void OnIceComplete() OVERRIDE; | |
| 88 | |
| 89 private: | |
| 90 CastExtensionSession( | |
| 91 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, | |
| 92 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, | |
| 93 const protocol::NetworkSettings& network_settings, | |
| 94 ClientSessionControl* client_session_control, | |
| 95 protocol::ClientStub* client_stub); | |
| 96 | |
| 97 // Parses |message| for a Session Description and sets the remote | |
| 98 // description, returning true if successful. | |
| 99 bool ParseAndSetRemoteDescription(base::DictionaryValue* message); | |
| 100 | |
| 101 // Parses |message| for a PeerConnection ICE candidate and adds it to the | |
| 102 // Peer Connection, returning true if successful. | |
| 103 bool ParseAndAddICECandidate(base::DictionaryValue* message); | |
| 104 | |
| 105 // Sends a message to the client through |client_stub_|. This method must be | |
| 106 // called on the network thread. | |
| 107 // | |
| 108 // A protocol::ExtensionMessage consists of two string fields: type and data. | |
| 109 // | |
| 110 // The specifications for Cast Extension Messages are as follows: | |
|
Wez
2014/08/14 19:22:03
nit: This line adds nothing.
aiguha
2014/08/15 04:11:39
Done.
| |
| 111 // The type field must be |kExtensionMessageType|. | |
| 112 // The data field must be a JSON formatted string with two compulsory | |
| 113 // top level keys: |kTopLevelSubject| and |kTopLevelData|. | |
| 114 // Thus, the data field of any properly formed Cast Extension Message should | |
| 115 // look like: | |
| 116 // {subject: '...', chromoting_data: '...'} | |
|
Wez
2014/08/14 19:22:03
nit: Suggest just documenting what 'subject' and '
Wez
2014/08/14 19:22:03
Since the data portion is specific to this extensi
aiguha
2014/08/15 04:11:39
It's "chromoting_data" not just "data" because:
1.
aiguha
2014/08/15 04:11:39
I've made the comment clearer. Also added better e
| |
| 117 // | |
| 118 // The |subject| of a message describes the message to the receiving peer, so | |
| 119 // the peer can easily decide what to do next. The |subject| MUST be one of | |
|
Wez
2014/08/14 19:22:03
So the Subject is essentially the extension-specif
aiguha
2014/08/15 04:11:39
responded above
| |
| 120 // constants formatted as kSubject* defined in the .cc file. This set of | |
| 121 // subjects is identical between host and client, thus standardizing how they | |
| 122 // communicate WebRTC signaling and other control messages. | |
| 123 // The |data| of a message could be a simple string or another JSON-formatted | |
| 124 // string. | |
|
Wez
2014/08/14 19:22:03
Better to say that the type of 'data' depends on t
aiguha
2014/08/15 04:11:39
Done.
| |
| 125 bool SendMessageToClient(const std::string& subject, const std::string& data); | |
| 126 | |
| 127 // Creates the jingle wrapper for the current thread, sets send to allowed, | |
| 128 // and saves a pointer to the relevant thread pointer in ptr. If |event| | |
| 129 // is not NULL, signals the event on completion. | |
| 130 void EnsureTaskAndSetSend(rtc::Thread** ptr, | |
| 131 base::WaitableEvent* event = NULL); | |
| 132 | |
| 133 // Wraps each task runner in JingleThreadWrapper using EnsureTaskAndSetSend(), | |
| 134 // returning true if successful. Wrapping the task runners allows them to be | |
| 135 // shared with and used by the (about to be created) PeerConnectionFactory. | |
| 136 bool WrapTasksAndSave(); | |
| 137 | |
| 138 // Initializes PeerConnectionFactory and PeerConnection and sends a "ready" | |
| 139 // message to client. Returns true if these steps are performed successfully. | |
| 140 bool InitializePeerConnection(); | |
| 141 | |
| 142 // Constructs a CastVideoCapturerAdapter, a VideoSource, a VideoTrack and a | |
| 143 // MediaStream |stream_|, which it adds to the |peer_connection_|. Returns | |
| 144 // true if these steps are performed successfully. This method is called only | |
| 145 // when a PeerConnection offer is received from the client. | |
| 146 bool SetupVideoStream(scoped_ptr<webrtc::DesktopCapturer> desktop_capturer); | |
| 147 | |
| 148 // Polls a single stats report from the PeerConnection immediately. Called | |
| 149 // periodically using |stats_polling_timer_| after a PeerConnection has been | |
| 150 // established. | |
| 151 void PollPeerConnectionStats(); | |
| 152 | |
| 153 // Closes |peer_connection_|, releases |peer_connection_|, |stream_| and | |
| 154 // |peer_conn_factory_| and stops the worker thread. | |
| 155 void CleanupPeerConnection(); | |
| 156 | |
| 157 // Check if the connection is active. | |
| 158 bool connection_active() const; | |
| 159 | |
| 160 // TaskRunners that will be used to setup the PeerConnectionFactory's | |
| 161 // signalling thread and worker thread respectively. | |
| 162 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner_; | |
| 163 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner_; | |
| 164 | |
| 165 // Objects related to the WebRTC PeerConnection. | |
| 166 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_; | |
| 167 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> peer_conn_factory_; | |
| 168 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_; | |
| 169 | |
| 170 // Parameters passed to ChromiumPortAllocatorFactory on creation. | |
| 171 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | |
| 172 const protocol::NetworkSettings& network_settings_; | |
| 173 | |
| 174 // Interface to interact with ClientSession. | |
| 175 ClientSessionControl* client_session_control_; | |
| 176 | |
| 177 // Interface through which messages can be sent to the client. | |
| 178 protocol::ClientStub* client_stub_; | |
| 179 | |
| 180 // Used to track webrtc connection statistics. | |
| 181 rtc::scoped_refptr<webrtc::StatsObserver> stats_observer_; | |
| 182 | |
| 183 // Used to repeatedly poll stats from the |peer_connection_|. | |
| 184 base::RepeatingTimer<CastExtensionSession> stats_polling_timer_; | |
| 185 | |
| 186 // True if a PeerConnection offer from the client has been received. This | |
| 187 // necessarily means that the host is not the caller in this attempted | |
| 188 // peer connection. | |
| 189 bool received_offer_; | |
| 190 | |
| 191 // True if the webrtc::ScreenCapturer has been grabbed through the | |
| 192 // OnCreateVideoCapturer() callback. | |
| 193 bool has_grabbed_capturer_; | |
| 194 | |
| 195 // PeerConnection signaling and worker threads created from | |
| 196 // JingleThreadWrappers. Each is created by calling | |
| 197 // jingle_glue::EnsureForCurrentMessageLoop() and thus deletes itself | |
| 198 // automatically when the associated MessageLoop is destroyed. | |
| 199 rtc::Thread* signaling_thread_wrapper_; | |
| 200 rtc::Thread* worker_thread_wrapper_; | |
| 201 | |
| 202 // Worker thread that is wrapped to create |worker_thread_wrapper_|. | |
| 203 base::Thread worker_thread_; | |
| 204 | |
| 205 DISALLOW_COPY_AND_ASSIGN(CastExtensionSession); | |
| 206 }; | |
| 207 | |
| 208 } // namespace remoting | |
| 209 | |
| 210 #endif // REMOTING_HOST_CAST_EXTENSION_SESSION_H_ | |
| 211 | |
| OLD | NEW |