Compute can_use_lcd_text using property trees.
[chromium-blink-merge.git] / remoting / host / cast_extension_session.h
blob48d97a438706eaed8bb15c7c4410607af2ccfcb8
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.
5 #ifndef REMOTING_HOST_CAST_EXTENSION_SESSION_H_
6 #define REMOTING_HOST_CAST_EXTENSION_SESSION_H_
8 #include <string>
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"
21 namespace base {
22 class SingleThreadTaskRunner;
23 class WaitableEvent;
24 } // namespace base
26 namespace net {
27 class URLRequestContextGetter;
28 } // namespace net
30 namespace webrtc {
31 class MediaStreamInterface;
32 } // namespace webrtc
34 namespace remoting {
36 class CastCreateSessionDescriptionObserver;
38 namespace protocol {
39 struct NetworkSettings;
40 } // namespace protocol
42 // A HostExtensionSession implementation that enables WebRTC support using
43 // the PeerConnection native API.
44 class CastExtensionSession : public HostExtensionSession,
45 public webrtc::PeerConnectionObserver {
46 public:
47 ~CastExtensionSession() override;
49 // Creates and returns a CastExtensionSession object, after performing
50 // initialization steps on it. The caller must take ownership of the returned
51 // object.
52 static scoped_ptr<CastExtensionSession> Create(
53 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner,
54 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter,
55 const protocol::NetworkSettings& network_settings,
56 ClientSessionControl* client_session_control,
57 protocol::ClientStub* client_stub);
59 // Called by webrtc::CreateSessionDescriptionObserver implementation.
60 void OnCreateSessionDescription(webrtc::SessionDescriptionInterface* desc);
61 void OnCreateSessionDescriptionFailure(const std::string& error);
63 // HostExtensionSession interface.
64 void OnCreateVideoCapturer(
65 scoped_ptr<webrtc::DesktopCapturer>* capturer) override;
66 bool ModifiesVideoPipeline() const override;
67 bool OnExtensionMessage(ClientSessionControl* client_session_control,
68 protocol::ClientStub* client_stub,
69 const protocol::ExtensionMessage& message) override;
71 // webrtc::PeerConnectionObserver interface.
72 void OnSignalingChange(
73 webrtc::PeerConnectionInterface::SignalingState new_state) override;
74 void OnStateChange(
75 webrtc::PeerConnectionObserver::StateType state_changed) override;
76 void OnAddStream(webrtc::MediaStreamInterface* stream) override;
77 void OnRemoveStream(webrtc::MediaStreamInterface* stream) override;
78 void OnDataChannel(webrtc::DataChannelInterface* data_channel) override;
79 void OnRenegotiationNeeded() override;
80 void OnIceConnectionChange(
81 webrtc::PeerConnectionInterface::IceConnectionState new_state) override;
82 void OnIceGatheringChange(
83 webrtc::PeerConnectionInterface::IceGatheringState new_state) override;
84 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override;
85 void OnIceComplete() override;
87 private:
88 CastExtensionSession(
89 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner,
90 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter,
91 const protocol::NetworkSettings& network_settings,
92 ClientSessionControl* client_session_control,
93 protocol::ClientStub* client_stub);
95 // Parses |message| for a Session Description and sets the remote
96 // description, returning true if successful.
97 bool ParseAndSetRemoteDescription(base::DictionaryValue* message);
99 // Parses |message| for a PeerConnection ICE candidate and adds it to the
100 // Peer Connection, returning true if successful.
101 bool ParseAndAddICECandidate(base::DictionaryValue* message);
103 // Sends a message to the client through |client_stub_|. This method must be
104 // called on the network thread.
106 // A protocol::ExtensionMessage consists of two string fields: type and data.
108 // The type field must be |kExtensionMessageType|.
109 // The data field must be a JSON formatted string with two compulsory
110 // top level keys: |kTopLevelSubject| and |kTopLevelData|.
112 // The |subject| of a message describes the message to the receiving peer,
113 // effectively identifying the command the receiving peer should perform.
114 // The |subject| MUST be one of constants formatted as kSubject* defined in
115 // the .cc file. This set of subjects is identical between host and client,
116 // thus standardizing how they communicate.
117 // The |data| of a message depends on the |subject| of the message.
119 // Examples of what ExtensionMessage.data() could look like:
121 // Host Ready Message:
122 // Notifies the remote peer that we are ready to receive an offer.
124 // {
125 // "subject": "ready",
126 // "chromoting_data": "Host Ready to receive offers"
127 // }
129 // WebRTC Offer Message:
130 // Represents the offer received from the remote peer. The local
131 // peer would then respond with a webrtc_answer message.
132 // {
133 // "subject": "webrtc_offer",
134 // "chromoting_data": {
135 // "sdp" : "...",
136 // "type" : "offer"
137 // }
138 // }
140 // WebRTC Candidate Message:
141 // Represents an ICE candidate received from the remote peer. Each peer
142 // shares its local ICE candidates in this way, until a connection is
143 // established.
145 // {
146 // "subject": "webrtc_candidate",
147 // "chromoting_data": {
148 // "candidate" : "...",
149 // "sdpMid" : "...",
150 // "sdpMLineIndex" : "..."
151 // }
152 // }
154 bool SendMessageToClient(const std::string& subject, const std::string& data);
156 // Creates the jingle wrapper for the current thread, sets send to allowed,
157 // and saves a pointer to the relevant thread pointer in ptr. If |event|
158 // is not nullptr, signals the event on completion.
159 void EnsureTaskAndSetSend(rtc::Thread** ptr,
160 base::WaitableEvent* event = nullptr);
162 // Wraps each task runner in JingleThreadWrapper using EnsureTaskAndSetSend(),
163 // returning true if successful. Wrapping the task runners allows them to be
164 // shared with and used by the (about to be created) PeerConnectionFactory.
165 bool WrapTasksAndSave();
167 // Initializes PeerConnectionFactory and PeerConnection and sends a "ready"
168 // message to client. Returns true if these steps are performed successfully.
169 bool InitializePeerConnection();
171 // Constructs a CastVideoCapturerAdapter, a VideoSource, a VideoTrack and a
172 // MediaStream |stream_|, which it adds to the |peer_connection_|. Returns
173 // true if these steps are performed successfully. This method is called only
174 // when a PeerConnection offer is received from the client.
175 bool SetupVideoStream(scoped_ptr<webrtc::DesktopCapturer> desktop_capturer);
177 // Polls a single stats report from the PeerConnection immediately. Called
178 // periodically using |stats_polling_timer_| after a PeerConnection has been
179 // established.
180 void PollPeerConnectionStats();
182 // Closes |peer_connection_|, releases |peer_connection_|, |stream_| and
183 // |peer_conn_factory_| and stops the worker thread.
184 void CleanupPeerConnection();
186 // Check if the connection is active.
187 bool connection_active() const;
189 // TaskRunners that will be used to setup the PeerConnectionFactory's
190 // signalling thread and worker thread respectively.
191 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner_;
192 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner_;
194 // Objects related to the WebRTC PeerConnection.
195 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
196 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> peer_conn_factory_;
197 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_;
198 rtc::scoped_refptr<CastCreateSessionDescriptionObserver>
199 create_session_desc_observer_;
201 // Parameters passed to ChromiumPortAllocatorFactory on creation.
202 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
203 const protocol::NetworkSettings& network_settings_;
205 // Interface to interact with ClientSession.
206 ClientSessionControl* client_session_control_;
208 // Interface through which messages can be sent to the client.
209 protocol::ClientStub* client_stub_;
211 // Used to track webrtc connection statistics.
212 rtc::scoped_refptr<webrtc::StatsObserver> stats_observer_;
214 // Used to repeatedly poll stats from the |peer_connection_|.
215 base::RepeatingTimer<CastExtensionSession> stats_polling_timer_;
217 // True if a PeerConnection offer from the client has been received. This
218 // necessarily means that the host is not the caller in this attempted
219 // peer connection.
220 bool received_offer_;
222 // True if the webrtc::ScreenCapturer has been grabbed through the
223 // OnCreateVideoCapturer() callback.
224 bool has_grabbed_capturer_;
226 // PeerConnection signaling and worker threads created from
227 // JingleThreadWrappers. Each is created by calling
228 // jingle_glue::EnsureForCurrentMessageLoop() and thus deletes itself
229 // automatically when the associated MessageLoop is destroyed.
230 rtc::Thread* signaling_thread_wrapper_;
231 rtc::Thread* worker_thread_wrapper_;
233 // Worker thread that is wrapped to create |worker_thread_wrapper_|.
234 base::Thread worker_thread_;
236 DISALLOW_COPY_AND_ASSIGN(CastExtensionSession);
239 } // namespace remoting
241 #endif // REMOTING_HOST_CAST_EXTENSION_SESSION_H_