Remove dependency on content from remoting_host.
[chromium-blink-merge.git] / remoting / host / client_session.cc
blobef6f5a6a4c7bb03645fc4a2197f389f85a294d5d
1 // Copyright (c) 2012 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 #include "remoting/host/client_session.h"
7 #include <algorithm>
9 #include "base/message_loop/message_loop_proxy.h"
10 #include "remoting/base/capabilities.h"
11 #include "remoting/base/logging.h"
12 #include "remoting/codec/audio_encoder.h"
13 #include "remoting/codec/audio_encoder_opus.h"
14 #include "remoting/codec/audio_encoder_verbatim.h"
15 #include "remoting/codec/video_encoder.h"
16 #include "remoting/codec/video_encoder_verbatim.h"
17 #include "remoting/codec/video_encoder_vpx.h"
18 #include "remoting/host/audio_capturer.h"
19 #include "remoting/host/audio_pump.h"
20 #include "remoting/host/desktop_capturer_proxy.h"
21 #include "remoting/host/desktop_environment.h"
22 #include "remoting/host/host_extension_session.h"
23 #include "remoting/host/input_injector.h"
24 #include "remoting/host/mouse_shape_pump.h"
25 #include "remoting/host/screen_controls.h"
26 #include "remoting/host/screen_resolution.h"
27 #include "remoting/host/video_frame_pump.h"
28 #include "remoting/proto/control.pb.h"
29 #include "remoting/proto/event.pb.h"
30 #include "remoting/protocol/client_stub.h"
31 #include "remoting/protocol/clipboard_thread_proxy.h"
32 #include "remoting/protocol/pairing_registry.h"
33 #include "third_party/webrtc/modules/desktop_capture/desktop_capturer.h"
34 #include "third_party/webrtc/modules/desktop_capture/mouse_cursor_monitor.h"
36 // Default DPI to assume for old clients that use notifyClientDimensions.
37 const int kDefaultDPI = 96;
39 namespace remoting {
41 namespace {
43 scoped_ptr<VideoEncoder> CreateVideoEncoder(
44 const protocol::SessionConfig& config) {
45 const protocol::ChannelConfig& video_config = config.video_config();
47 if (video_config.codec == protocol::ChannelConfig::CODEC_VP8) {
48 return VideoEncoderVpx::CreateForVP8().Pass();
49 } else if (video_config.codec == protocol::ChannelConfig::CODEC_VP9) {
50 return VideoEncoderVpx::CreateForVP9().Pass();
51 } else if (video_config.codec == protocol::ChannelConfig::CODEC_VERBATIM) {
52 return make_scoped_ptr(new VideoEncoderVerbatim());
55 NOTREACHED();
56 return nullptr;
59 scoped_ptr<AudioEncoder> CreateAudioEncoder(
60 const protocol::SessionConfig& config) {
61 const protocol::ChannelConfig& audio_config = config.audio_config();
63 if (audio_config.codec == protocol::ChannelConfig::CODEC_VERBATIM) {
64 return make_scoped_ptr(new AudioEncoderVerbatim());
65 } else if (audio_config.codec == protocol::ChannelConfig::CODEC_OPUS) {
66 return make_scoped_ptr(new AudioEncoderOpus());
69 NOTREACHED();
70 return nullptr;
73 } // namespace
75 ClientSession::ClientSession(
76 EventHandler* event_handler,
77 scoped_refptr<base::SingleThreadTaskRunner> audio_task_runner,
78 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner,
79 scoped_refptr<base::SingleThreadTaskRunner> video_capture_task_runner,
80 scoped_refptr<base::SingleThreadTaskRunner> video_encode_task_runner,
81 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner,
82 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
83 scoped_ptr<protocol::ConnectionToClient> connection,
84 DesktopEnvironmentFactory* desktop_environment_factory,
85 const base::TimeDelta& max_duration,
86 scoped_refptr<protocol::PairingRegistry> pairing_registry,
87 const std::vector<HostExtension*>& extensions)
88 : event_handler_(event_handler),
89 connection_(connection.Pass()),
90 client_jid_(connection_->session()->jid()),
91 desktop_environment_factory_(desktop_environment_factory),
92 input_tracker_(&host_input_filter_),
93 remote_input_filter_(&input_tracker_),
94 mouse_clamping_filter_(&remote_input_filter_),
95 disable_input_filter_(mouse_clamping_filter_.input_filter()),
96 disable_clipboard_filter_(clipboard_echo_filter_.host_filter()),
97 client_clipboard_factory_(clipboard_echo_filter_.client_filter()),
98 max_duration_(max_duration),
99 audio_task_runner_(audio_task_runner),
100 input_task_runner_(input_task_runner),
101 video_capture_task_runner_(video_capture_task_runner),
102 video_encode_task_runner_(video_encode_task_runner),
103 network_task_runner_(network_task_runner),
104 ui_task_runner_(ui_task_runner),
105 pairing_registry_(pairing_registry),
106 is_authenticated_(false),
107 pause_video_(false),
108 lossless_video_encode_(false),
109 lossless_video_color_(false),
110 weak_factory_(this) {
111 connection_->SetEventHandler(this);
113 // Create a manager for the configured extensions, if any.
114 extension_manager_.reset(new HostExtensionSessionManager(extensions, this));
116 #if defined(OS_WIN)
117 // LocalInputMonitorWin filters out an echo of the injected input before it
118 // reaches |remote_input_filter_|.
119 remote_input_filter_.SetExpectLocalEcho(false);
120 #endif // defined(OS_WIN)
123 ClientSession::~ClientSession() {
124 DCHECK(CalledOnValidThread());
125 DCHECK(!audio_pump_);
126 DCHECK(!desktop_environment_);
127 DCHECK(!input_injector_);
128 DCHECK(!screen_controls_);
129 DCHECK(!video_frame_pump_);
131 connection_.reset();
134 void ClientSession::NotifyClientResolution(
135 const protocol::ClientResolution& resolution) {
136 DCHECK(CalledOnValidThread());
138 // TODO(sergeyu): Move these checks to protocol layer.
139 if (!resolution.has_dips_width() || !resolution.has_dips_height() ||
140 resolution.dips_width() < 0 || resolution.dips_height() < 0 ||
141 resolution.width() <= 0 || resolution.height() <= 0) {
142 LOG(ERROR) << "Received invalid ClientResolution message.";
143 return;
146 VLOG(1) << "Received ClientResolution (dips_width="
147 << resolution.dips_width() << ", dips_height="
148 << resolution.dips_height() << ")";
150 if (!screen_controls_)
151 return;
153 ScreenResolution client_resolution(
154 webrtc::DesktopSize(resolution.dips_width(), resolution.dips_height()),
155 webrtc::DesktopVector(kDefaultDPI, kDefaultDPI));
157 // Try to match the client's resolution.
158 screen_controls_->SetScreenResolution(client_resolution);
161 void ClientSession::ControlVideo(const protocol::VideoControl& video_control) {
162 DCHECK(CalledOnValidThread());
164 // Note that |video_frame_pump_| may be null, depending upon whether
165 // extensions choose to wrap or "steal" the video capturer or encoder.
166 if (video_control.has_enable()) {
167 VLOG(1) << "Received VideoControl (enable="
168 << video_control.enable() << ")";
169 pause_video_ = !video_control.enable();
170 if (video_frame_pump_)
171 video_frame_pump_->Pause(pause_video_);
173 if (video_control.has_lossless_encode()) {
174 VLOG(1) << "Received VideoControl (lossless_encode="
175 << video_control.lossless_encode() << ")";
176 lossless_video_encode_ = video_control.lossless_encode();
177 if (video_frame_pump_)
178 video_frame_pump_->SetLosslessEncode(lossless_video_encode_);
180 if (video_control.has_lossless_color()) {
181 VLOG(1) << "Received VideoControl (lossless_color="
182 << video_control.lossless_color() << ")";
183 lossless_video_color_ = video_control.lossless_color();
184 if (video_frame_pump_)
185 video_frame_pump_->SetLosslessColor(lossless_video_color_);
189 void ClientSession::ControlAudio(const protocol::AudioControl& audio_control) {
190 DCHECK(CalledOnValidThread());
192 if (audio_control.has_enable()) {
193 VLOG(1) << "Received AudioControl (enable="
194 << audio_control.enable() << ")";
195 if (audio_pump_)
196 audio_pump_->Pause(!audio_control.enable());
200 void ClientSession::SetCapabilities(
201 const protocol::Capabilities& capabilities) {
202 DCHECK(CalledOnValidThread());
204 // Ignore all the messages but the 1st one.
205 if (client_capabilities_) {
206 LOG(WARNING) << "protocol::Capabilities has been received already.";
207 return;
210 // Compute the set of capabilities supported by both client and host.
211 client_capabilities_ = make_scoped_ptr(new std::string());
212 if (capabilities.has_capabilities())
213 *client_capabilities_ = capabilities.capabilities();
214 capabilities_ = IntersectCapabilities(*client_capabilities_,
215 host_capabilities_);
216 extension_manager_->OnNegotiatedCapabilities(
217 connection_->client_stub(), capabilities_);
219 VLOG(1) << "Client capabilities: " << *client_capabilities_;
221 // Calculate the set of capabilities enabled by both client and host and
222 // pass it to the desktop environment if it is available.
223 desktop_environment_->SetCapabilities(capabilities_);
226 void ClientSession::RequestPairing(
227 const protocol::PairingRequest& pairing_request) {
228 if (pairing_registry_.get() && pairing_request.has_client_name()) {
229 protocol::PairingRegistry::Pairing pairing =
230 pairing_registry_->CreatePairing(pairing_request.client_name());
231 protocol::PairingResponse pairing_response;
232 pairing_response.set_client_id(pairing.client_id());
233 pairing_response.set_shared_secret(pairing.shared_secret());
234 connection_->client_stub()->SetPairingResponse(pairing_response);
238 void ClientSession::DeliverClientMessage(
239 const protocol::ExtensionMessage& message) {
240 if (message.has_type()) {
241 if (message.type() == "test-echo") {
242 protocol::ExtensionMessage reply;
243 reply.set_type("test-echo-reply");
244 if (message.has_data())
245 reply.set_data(message.data().substr(0, 16));
246 connection_->client_stub()->DeliverHostMessage(reply);
247 return;
248 } else if (message.type() == "gnubby-auth") {
249 if (gnubby_auth_handler_) {
250 gnubby_auth_handler_->DeliverClientMessage(message.data());
251 } else {
252 HOST_LOG << "gnubby auth is not enabled";
254 return;
255 } else {
256 if (extension_manager_->OnExtensionMessage(message))
257 return;
259 DLOG(INFO) << "Unexpected message received: "
260 << message.type() << ": " << message.data();
265 void ClientSession::OnConnectionAuthenticating(
266 protocol::ConnectionToClient* connection) {
267 event_handler_->OnSessionAuthenticating(this);
270 void ClientSession::OnConnectionAuthenticated(
271 protocol::ConnectionToClient* connection) {
272 DCHECK(CalledOnValidThread());
273 DCHECK_EQ(connection_.get(), connection);
274 DCHECK(!audio_pump_);
275 DCHECK(!desktop_environment_);
276 DCHECK(!input_injector_);
277 DCHECK(!screen_controls_);
278 DCHECK(!video_frame_pump_);
280 is_authenticated_ = true;
282 if (max_duration_ > base::TimeDelta()) {
283 // TODO(simonmorris): Let Disconnect() tell the client that the
284 // disconnection was caused by the session exceeding its maximum duration.
285 max_duration_timer_.Start(FROM_HERE, max_duration_,
286 this, &ClientSession::DisconnectSession);
289 // Disconnect the session if the connection was rejected by the host.
290 if (!event_handler_->OnSessionAuthenticated(this)) {
291 DisconnectSession();
292 return;
295 // Create the desktop environment. Drop the connection if it could not be
296 // created for any reason (for instance the curtain could not initialize).
297 desktop_environment_ =
298 desktop_environment_factory_->Create(weak_factory_.GetWeakPtr());
299 if (!desktop_environment_) {
300 DisconnectSession();
301 return;
304 // Connect host stub.
305 connection_->set_host_stub(this);
307 // Connect video stub.
308 mouse_clamping_filter_.set_video_stub(connection_->video_stub());
310 // Collate the set of capabilities to offer the client, if it supports them.
311 host_capabilities_ = desktop_environment_->GetCapabilities();
312 if (!host_capabilities_.empty())
313 host_capabilities_.append(" ");
314 host_capabilities_.append(extension_manager_->GetCapabilities());
316 // Create the object that controls the screen resolution.
317 screen_controls_ = desktop_environment_->CreateScreenControls();
319 // Create the event executor.
320 input_injector_ = desktop_environment_->CreateInputInjector();
322 // Connect the host input stubs.
323 connection_->set_input_stub(&disable_input_filter_);
324 host_input_filter_.set_input_stub(input_injector_.get());
326 // Connect the clipboard stubs.
327 connection_->set_clipboard_stub(&disable_clipboard_filter_);
328 clipboard_echo_filter_.set_host_stub(input_injector_.get());
329 clipboard_echo_filter_.set_client_stub(connection_->client_stub());
331 // Create a GnubbyAuthHandler to proxy gnubbyd messages.
332 gnubby_auth_handler_ = desktop_environment_->CreateGnubbyAuthHandler(
333 connection_->client_stub());
336 void ClientSession::OnConnectionChannelsConnected(
337 protocol::ConnectionToClient* connection) {
338 DCHECK(CalledOnValidThread());
339 DCHECK_EQ(connection_.get(), connection);
341 // Negotiate capabilities with the client.
342 VLOG(1) << "Host capabilities: " << host_capabilities_;
343 protocol::Capabilities capabilities;
344 capabilities.set_capabilities(host_capabilities_);
345 connection_->client_stub()->SetCapabilities(capabilities);
347 // Start the event executor.
348 input_injector_->Start(CreateClipboardProxy());
349 SetDisableInputs(false);
351 // Start recording video.
352 ResetVideoPipeline();
354 // Create an AudioPump if audio is enabled, to pump audio samples.
355 if (connection_->session()->config().is_audio_enabled()) {
356 scoped_ptr<AudioEncoder> audio_encoder =
357 CreateAudioEncoder(connection_->session()->config());
358 audio_pump_.reset(new AudioPump(
359 audio_task_runner_, desktop_environment_->CreateAudioCapturer(),
360 audio_encoder.Pass(), connection_->audio_stub()));
363 // Notify the event handler that all our channels are now connected.
364 event_handler_->OnSessionChannelsConnected(this);
367 void ClientSession::OnConnectionClosed(
368 protocol::ConnectionToClient* connection,
369 protocol::ErrorCode error) {
370 DCHECK(CalledOnValidThread());
371 DCHECK_EQ(connection_.get(), connection);
373 // Ignore any further callbacks.
374 weak_factory_.InvalidateWeakPtrs();
376 // If the client never authenticated then the session failed.
377 if (!is_authenticated_)
378 event_handler_->OnSessionAuthenticationFailed(this);
380 // Ensure that any pressed keys or buttons are released.
381 input_tracker_.ReleaseAll();
383 // Stop components access the client, audio or video stubs, which are no
384 // longer valid once ConnectionToClient calls OnConnectionClosed().
385 audio_pump_.reset();
386 video_frame_pump_.reset();
387 mouse_shape_pump_.reset();
388 client_clipboard_factory_.InvalidateWeakPtrs();
389 input_injector_.reset();
390 screen_controls_.reset();
391 desktop_environment_.reset();
393 // Notify the ChromotingHost that this client is disconnected.
394 // TODO(sergeyu): Log failure reason?
395 event_handler_->OnSessionClosed(this);
398 void ClientSession::OnEventTimestamp(protocol::ConnectionToClient* connection,
399 int64 timestamp) {
400 DCHECK(CalledOnValidThread());
401 DCHECK_EQ(connection_.get(), connection);
403 if (video_frame_pump_.get())
404 video_frame_pump_->SetLatestEventTimestamp(timestamp);
407 void ClientSession::OnRouteChange(
408 protocol::ConnectionToClient* connection,
409 const std::string& channel_name,
410 const protocol::TransportRoute& route) {
411 DCHECK(CalledOnValidThread());
412 DCHECK_EQ(connection_.get(), connection);
413 event_handler_->OnSessionRouteChange(this, channel_name, route);
416 const std::string& ClientSession::client_jid() const {
417 return client_jid_;
420 void ClientSession::DisconnectSession() {
421 DCHECK(CalledOnValidThread());
422 DCHECK(connection_.get());
424 max_duration_timer_.Stop();
426 // This triggers OnConnectionClosed(), and the session may be destroyed
427 // as the result, so this call must be the last in this method.
428 connection_->Disconnect();
431 void ClientSession::OnLocalMouseMoved(const webrtc::DesktopVector& position) {
432 DCHECK(CalledOnValidThread());
433 remote_input_filter_.LocalMouseMoved(position);
436 void ClientSession::SetDisableInputs(bool disable_inputs) {
437 DCHECK(CalledOnValidThread());
439 if (disable_inputs)
440 input_tracker_.ReleaseAll();
442 disable_input_filter_.set_enabled(!disable_inputs);
443 disable_clipboard_filter_.set_enabled(!disable_inputs);
446 void ClientSession::ResetVideoPipeline() {
447 DCHECK(CalledOnValidThread());
449 mouse_shape_pump_.reset();
450 video_frame_pump_.reset();
452 // Create VideoEncoder and DesktopCapturer to match the session's video
453 // channel configuration.
454 scoped_ptr<webrtc::DesktopCapturer> video_capturer =
455 desktop_environment_->CreateVideoCapturer();
456 extension_manager_->OnCreateVideoCapturer(&video_capturer);
457 scoped_ptr<VideoEncoder> video_encoder =
458 CreateVideoEncoder(connection_->session()->config());
459 extension_manager_->OnCreateVideoEncoder(&video_encoder);
461 // Don't start the VideoFramePump if either capturer or encoder are missing.
462 if (!video_capturer || !video_encoder)
463 return;
465 // Create MouseShapePump to send mouse cursor shape.
466 mouse_shape_pump_.reset(
467 new MouseShapePump(video_capture_task_runner_,
468 desktop_environment_->CreateMouseCursorMonitor(),
469 connection_->client_stub()));
471 // Create a VideoFramePump to pump frames from the capturer to the client.'
473 // TODO(sergeyu): Move DesktopCapturerProxy creation to DesktopEnvironment.
474 // When using IpcDesktopCapturer the capture thread is not useful.
475 scoped_ptr<DesktopCapturerProxy> capturer_proxy(new DesktopCapturerProxy(
476 video_capture_task_runner_, video_capturer.Pass()));
477 video_frame_pump_.reset(
478 new VideoFramePump(video_encode_task_runner_, capturer_proxy.Pass(),
479 video_encoder.Pass(), &mouse_clamping_filter_));
481 // Apply video-control parameters to the new scheduler.
482 video_frame_pump_->SetLosslessEncode(lossless_video_encode_);
483 video_frame_pump_->SetLosslessColor(lossless_video_color_);
485 // Pause capturing if necessary.
486 video_frame_pump_->Pause(pause_video_);
489 void ClientSession::SetGnubbyAuthHandlerForTesting(
490 GnubbyAuthHandler* gnubby_auth_handler) {
491 DCHECK(CalledOnValidThread());
492 gnubby_auth_handler_.reset(gnubby_auth_handler);
495 scoped_ptr<protocol::ClipboardStub> ClientSession::CreateClipboardProxy() {
496 DCHECK(CalledOnValidThread());
498 return make_scoped_ptr(
499 new protocol::ClipboardThreadProxy(client_clipboard_factory_.GetWeakPtr(),
500 base::MessageLoopProxy::current()));
503 } // namespace remoting