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"
9 #include "base/single_thread_task_runner.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "remoting/base/capabilities.h"
12 #include "remoting/base/logging.h"
13 #include "remoting/codec/audio_encoder.h"
14 #include "remoting/codec/audio_encoder_opus.h"
15 #include "remoting/codec/audio_encoder_verbatim.h"
16 #include "remoting/codec/video_encoder.h"
17 #include "remoting/codec/video_encoder_verbatim.h"
18 #include "remoting/codec/video_encoder_vpx.h"
19 #include "remoting/host/audio_capturer.h"
20 #include "remoting/host/audio_pump.h"
21 #include "remoting/host/desktop_capturer_proxy.h"
22 #include "remoting/host/desktop_environment.h"
23 #include "remoting/host/host_extension_session.h"
24 #include "remoting/host/input_injector.h"
25 #include "remoting/host/mouse_shape_pump.h"
26 #include "remoting/host/screen_controls.h"
27 #include "remoting/host/screen_resolution.h"
28 #include "remoting/host/video_frame_pump.h"
29 #include "remoting/proto/control.pb.h"
30 #include "remoting/proto/event.pb.h"
31 #include "remoting/protocol/client_stub.h"
32 #include "remoting/protocol/clipboard_thread_proxy.h"
33 #include "remoting/protocol/pairing_registry.h"
34 #include "third_party/webrtc/modules/desktop_capture/desktop_capturer.h"
35 #include "third_party/webrtc/modules/desktop_capture/mouse_cursor_monitor.h"
37 // Default DPI to assume for old clients that use notifyClientDimensions.
38 const int kDefaultDPI
= 96;
44 scoped_ptr
<VideoEncoder
> CreateVideoEncoder(
45 const protocol::SessionConfig
& config
) {
46 const protocol::ChannelConfig
& video_config
= config
.video_config();
48 if (video_config
.codec
== protocol::ChannelConfig::CODEC_VP8
) {
49 return VideoEncoderVpx::CreateForVP8().Pass();
50 } else if (video_config
.codec
== protocol::ChannelConfig::CODEC_VP9
) {
51 return VideoEncoderVpx::CreateForVP9().Pass();
52 } else if (video_config
.codec
== protocol::ChannelConfig::CODEC_VERBATIM
) {
53 return make_scoped_ptr(new VideoEncoderVerbatim());
60 scoped_ptr
<AudioEncoder
> CreateAudioEncoder(
61 const protocol::SessionConfig
& config
) {
62 const protocol::ChannelConfig
& audio_config
= config
.audio_config();
64 if (audio_config
.codec
== protocol::ChannelConfig::CODEC_VERBATIM
) {
65 return make_scoped_ptr(new AudioEncoderVerbatim());
66 } else if (audio_config
.codec
== protocol::ChannelConfig::CODEC_OPUS
) {
67 return make_scoped_ptr(new AudioEncoderOpus());
76 ClientSession::ClientSession(
77 EventHandler
* event_handler
,
78 scoped_refptr
<base::SingleThreadTaskRunner
> audio_task_runner
,
79 scoped_refptr
<base::SingleThreadTaskRunner
> input_task_runner
,
80 scoped_refptr
<base::SingleThreadTaskRunner
> video_capture_task_runner
,
81 scoped_refptr
<base::SingleThreadTaskRunner
> video_encode_task_runner
,
82 scoped_refptr
<base::SingleThreadTaskRunner
> network_task_runner
,
83 scoped_refptr
<base::SingleThreadTaskRunner
> ui_task_runner
,
84 scoped_ptr
<protocol::ConnectionToClient
> connection
,
85 DesktopEnvironmentFactory
* desktop_environment_factory
,
86 const base::TimeDelta
& max_duration
,
87 scoped_refptr
<protocol::PairingRegistry
> pairing_registry
,
88 const std::vector
<HostExtension
*>& extensions
)
89 : event_handler_(event_handler
),
90 connection_(connection
.Pass()),
91 client_jid_(connection_
->session()->jid()),
92 desktop_environment_factory_(desktop_environment_factory
),
93 input_tracker_(&host_input_filter_
),
94 remote_input_filter_(&input_tracker_
),
95 mouse_clamping_filter_(&remote_input_filter_
),
96 disable_input_filter_(mouse_clamping_filter_
.input_filter()),
97 disable_clipboard_filter_(clipboard_echo_filter_
.host_filter()),
98 client_clipboard_factory_(clipboard_echo_filter_
.client_filter()),
99 max_duration_(max_duration
),
100 audio_task_runner_(audio_task_runner
),
101 input_task_runner_(input_task_runner
),
102 video_capture_task_runner_(video_capture_task_runner
),
103 video_encode_task_runner_(video_encode_task_runner
),
104 network_task_runner_(network_task_runner
),
105 ui_task_runner_(ui_task_runner
),
106 pairing_registry_(pairing_registry
),
107 is_authenticated_(false),
109 lossless_video_encode_(false),
110 lossless_video_color_(false),
111 weak_factory_(this) {
112 connection_
->SetEventHandler(this);
114 // Create a manager for the configured extensions, if any.
115 extension_manager_
.reset(new HostExtensionSessionManager(extensions
, this));
118 // LocalInputMonitorWin filters out an echo of the injected input before it
119 // reaches |remote_input_filter_|.
120 remote_input_filter_
.SetExpectLocalEcho(false);
121 #endif // defined(OS_WIN)
124 ClientSession::~ClientSession() {
125 DCHECK(CalledOnValidThread());
126 DCHECK(!audio_pump_
);
127 DCHECK(!desktop_environment_
);
128 DCHECK(!input_injector_
);
129 DCHECK(!screen_controls_
);
130 DCHECK(!video_frame_pump_
);
135 void ClientSession::NotifyClientResolution(
136 const protocol::ClientResolution
& resolution
) {
137 DCHECK(CalledOnValidThread());
139 // TODO(sergeyu): Move these checks to protocol layer.
140 if (!resolution
.has_dips_width() || !resolution
.has_dips_height() ||
141 resolution
.dips_width() < 0 || resolution
.dips_height() < 0 ||
142 resolution
.width() <= 0 || resolution
.height() <= 0) {
143 LOG(ERROR
) << "Received invalid ClientResolution message.";
147 VLOG(1) << "Received ClientResolution (dips_width="
148 << resolution
.dips_width() << ", dips_height="
149 << resolution
.dips_height() << ")";
151 if (!screen_controls_
)
154 ScreenResolution
client_resolution(
155 webrtc::DesktopSize(resolution
.dips_width(), resolution
.dips_height()),
156 webrtc::DesktopVector(kDefaultDPI
, kDefaultDPI
));
158 // Try to match the client's resolution.
159 screen_controls_
->SetScreenResolution(client_resolution
);
162 void ClientSession::ControlVideo(const protocol::VideoControl
& video_control
) {
163 DCHECK(CalledOnValidThread());
165 // Note that |video_frame_pump_| may be null, depending upon whether
166 // extensions choose to wrap or "steal" the video capturer or encoder.
167 if (video_control
.has_enable()) {
168 VLOG(1) << "Received VideoControl (enable="
169 << video_control
.enable() << ")";
170 pause_video_
= !video_control
.enable();
171 if (video_frame_pump_
)
172 video_frame_pump_
->Pause(pause_video_
);
174 if (video_control
.has_lossless_encode()) {
175 VLOG(1) << "Received VideoControl (lossless_encode="
176 << video_control
.lossless_encode() << ")";
177 lossless_video_encode_
= video_control
.lossless_encode();
178 if (video_frame_pump_
)
179 video_frame_pump_
->SetLosslessEncode(lossless_video_encode_
);
181 if (video_control
.has_lossless_color()) {
182 VLOG(1) << "Received VideoControl (lossless_color="
183 << video_control
.lossless_color() << ")";
184 lossless_video_color_
= video_control
.lossless_color();
185 if (video_frame_pump_
)
186 video_frame_pump_
->SetLosslessColor(lossless_video_color_
);
190 void ClientSession::ControlAudio(const protocol::AudioControl
& audio_control
) {
191 DCHECK(CalledOnValidThread());
193 if (audio_control
.has_enable()) {
194 VLOG(1) << "Received AudioControl (enable="
195 << audio_control
.enable() << ")";
197 audio_pump_
->Pause(!audio_control
.enable());
201 void ClientSession::SetCapabilities(
202 const protocol::Capabilities
& capabilities
) {
203 DCHECK(CalledOnValidThread());
205 // Ignore all the messages but the 1st one.
206 if (client_capabilities_
) {
207 LOG(WARNING
) << "protocol::Capabilities has been received already.";
211 // Compute the set of capabilities supported by both client and host.
212 client_capabilities_
= make_scoped_ptr(new std::string());
213 if (capabilities
.has_capabilities())
214 *client_capabilities_
= capabilities
.capabilities();
215 capabilities_
= IntersectCapabilities(*client_capabilities_
,
217 extension_manager_
->OnNegotiatedCapabilities(
218 connection_
->client_stub(), capabilities_
);
220 VLOG(1) << "Client capabilities: " << *client_capabilities_
;
222 // Calculate the set of capabilities enabled by both client and host and
223 // pass it to the desktop environment if it is available.
224 desktop_environment_
->SetCapabilities(capabilities_
);
227 void ClientSession::RequestPairing(
228 const protocol::PairingRequest
& pairing_request
) {
229 if (pairing_registry_
.get() && pairing_request
.has_client_name()) {
230 protocol::PairingRegistry::Pairing pairing
=
231 pairing_registry_
->CreatePairing(pairing_request
.client_name());
232 protocol::PairingResponse pairing_response
;
233 pairing_response
.set_client_id(pairing
.client_id());
234 pairing_response
.set_shared_secret(pairing
.shared_secret());
235 connection_
->client_stub()->SetPairingResponse(pairing_response
);
239 void ClientSession::DeliverClientMessage(
240 const protocol::ExtensionMessage
& message
) {
241 if (message
.has_type()) {
242 if (message
.type() == "test-echo") {
243 protocol::ExtensionMessage reply
;
244 reply
.set_type("test-echo-reply");
245 if (message
.has_data())
246 reply
.set_data(message
.data().substr(0, 16));
247 connection_
->client_stub()->DeliverHostMessage(reply
);
249 } else if (message
.type() == "gnubby-auth") {
250 if (gnubby_auth_handler_
) {
251 gnubby_auth_handler_
->DeliverClientMessage(message
.data());
253 HOST_LOG
<< "gnubby auth is not enabled";
257 if (extension_manager_
->OnExtensionMessage(message
))
260 DLOG(INFO
) << "Unexpected message received: "
261 << message
.type() << ": " << message
.data();
266 void ClientSession::OnConnectionAuthenticating(
267 protocol::ConnectionToClient
* connection
) {
268 event_handler_
->OnSessionAuthenticating(this);
271 void ClientSession::OnConnectionAuthenticated(
272 protocol::ConnectionToClient
* connection
) {
273 DCHECK(CalledOnValidThread());
274 DCHECK_EQ(connection_
.get(), connection
);
275 DCHECK(!audio_pump_
);
276 DCHECK(!desktop_environment_
);
277 DCHECK(!input_injector_
);
278 DCHECK(!screen_controls_
);
279 DCHECK(!video_frame_pump_
);
281 is_authenticated_
= true;
283 if (max_duration_
> base::TimeDelta()) {
284 // TODO(simonmorris): Let Disconnect() tell the client that the
285 // disconnection was caused by the session exceeding its maximum duration.
286 max_duration_timer_
.Start(FROM_HERE
, max_duration_
,
287 this, &ClientSession::DisconnectSession
);
290 // Disconnect the session if the connection was rejected by the host.
291 if (!event_handler_
->OnSessionAuthenticated(this)) {
296 // Create the desktop environment. Drop the connection if it could not be
297 // created for any reason (for instance the curtain could not initialize).
298 desktop_environment_
=
299 desktop_environment_factory_
->Create(weak_factory_
.GetWeakPtr());
300 if (!desktop_environment_
) {
305 // Connect host stub.
306 connection_
->set_host_stub(this);
308 // Connect video stub.
309 mouse_clamping_filter_
.set_video_stub(connection_
->video_stub());
311 // Collate the set of capabilities to offer the client, if it supports them.
312 host_capabilities_
= desktop_environment_
->GetCapabilities();
313 if (!host_capabilities_
.empty())
314 host_capabilities_
.append(" ");
315 host_capabilities_
.append(extension_manager_
->GetCapabilities());
317 // Create the object that controls the screen resolution.
318 screen_controls_
= desktop_environment_
->CreateScreenControls();
320 // Create the event executor.
321 input_injector_
= desktop_environment_
->CreateInputInjector();
323 // Connect the host input stubs.
324 connection_
->set_input_stub(&disable_input_filter_
);
325 host_input_filter_
.set_input_stub(input_injector_
.get());
327 // Connect the clipboard stubs.
328 connection_
->set_clipboard_stub(&disable_clipboard_filter_
);
329 clipboard_echo_filter_
.set_host_stub(input_injector_
.get());
330 clipboard_echo_filter_
.set_client_stub(connection_
->client_stub());
332 // Create a GnubbyAuthHandler to proxy gnubbyd messages.
333 gnubby_auth_handler_
= desktop_environment_
->CreateGnubbyAuthHandler(
334 connection_
->client_stub());
337 void ClientSession::OnConnectionChannelsConnected(
338 protocol::ConnectionToClient
* connection
) {
339 DCHECK(CalledOnValidThread());
340 DCHECK_EQ(connection_
.get(), connection
);
342 // Negotiate capabilities with the client.
343 VLOG(1) << "Host capabilities: " << host_capabilities_
;
344 protocol::Capabilities capabilities
;
345 capabilities
.set_capabilities(host_capabilities_
);
346 connection_
->client_stub()->SetCapabilities(capabilities
);
348 // Start the event executor.
349 input_injector_
->Start(CreateClipboardProxy());
350 SetDisableInputs(false);
352 // Start recording video.
353 ResetVideoPipeline();
355 // Create an AudioPump if audio is enabled, to pump audio samples.
356 if (connection_
->session()->config().is_audio_enabled()) {
357 scoped_ptr
<AudioEncoder
> audio_encoder
=
358 CreateAudioEncoder(connection_
->session()->config());
359 audio_pump_
.reset(new AudioPump(
360 audio_task_runner_
, desktop_environment_
->CreateAudioCapturer(),
361 audio_encoder
.Pass(), connection_
->audio_stub()));
364 // Notify the event handler that all our channels are now connected.
365 event_handler_
->OnSessionChannelsConnected(this);
368 void ClientSession::OnConnectionClosed(
369 protocol::ConnectionToClient
* connection
,
370 protocol::ErrorCode error
) {
371 DCHECK(CalledOnValidThread());
372 DCHECK_EQ(connection_
.get(), connection
);
374 HOST_LOG
<< "Client disconnected: " << client_jid_
<< "; error = " << error
;
376 // Ignore any further callbacks.
377 weak_factory_
.InvalidateWeakPtrs();
379 // If the client never authenticated then the session failed.
380 if (!is_authenticated_
)
381 event_handler_
->OnSessionAuthenticationFailed(this);
383 // Ensure that any pressed keys or buttons are released.
384 input_tracker_
.ReleaseAll();
386 // Stop components access the client, audio or video stubs, which are no
387 // longer valid once ConnectionToClient calls OnConnectionClosed().
389 video_frame_pump_
.reset();
390 mouse_shape_pump_
.reset();
391 client_clipboard_factory_
.InvalidateWeakPtrs();
392 input_injector_
.reset();
393 screen_controls_
.reset();
394 desktop_environment_
.reset();
396 // Notify the ChromotingHost that this client is disconnected.
397 event_handler_
->OnSessionClosed(this);
400 void ClientSession::OnEventTimestamp(protocol::ConnectionToClient
* connection
,
402 DCHECK(CalledOnValidThread());
403 DCHECK_EQ(connection_
.get(), connection
);
405 if (video_frame_pump_
.get())
406 video_frame_pump_
->SetLatestEventTimestamp(timestamp
);
409 void ClientSession::OnRouteChange(
410 protocol::ConnectionToClient
* connection
,
411 const std::string
& channel_name
,
412 const protocol::TransportRoute
& route
) {
413 DCHECK(CalledOnValidThread());
414 DCHECK_EQ(connection_
.get(), connection
);
415 event_handler_
->OnSessionRouteChange(this, channel_name
, route
);
418 const std::string
& ClientSession::client_jid() const {
422 void ClientSession::DisconnectSession() {
423 DCHECK(CalledOnValidThread());
424 DCHECK(connection_
.get());
426 max_duration_timer_
.Stop();
428 // This triggers OnConnectionClosed(), and the session may be destroyed
429 // as the result, so this call must be the last in this method.
430 connection_
->Disconnect();
433 void ClientSession::OnLocalMouseMoved(const webrtc::DesktopVector
& position
) {
434 DCHECK(CalledOnValidThread());
435 remote_input_filter_
.LocalMouseMoved(position
);
438 void ClientSession::SetDisableInputs(bool disable_inputs
) {
439 DCHECK(CalledOnValidThread());
442 input_tracker_
.ReleaseAll();
444 disable_input_filter_
.set_enabled(!disable_inputs
);
445 disable_clipboard_filter_
.set_enabled(!disable_inputs
);
448 void ClientSession::ResetVideoPipeline() {
449 DCHECK(CalledOnValidThread());
451 mouse_shape_pump_
.reset();
452 connection_
->set_video_feedback_stub(nullptr);
453 video_frame_pump_
.reset();
455 // Create VideoEncoder and DesktopCapturer to match the session's video
456 // channel configuration.
457 scoped_ptr
<webrtc::DesktopCapturer
> video_capturer
=
458 desktop_environment_
->CreateVideoCapturer();
459 extension_manager_
->OnCreateVideoCapturer(&video_capturer
);
460 scoped_ptr
<VideoEncoder
> video_encoder
=
461 CreateVideoEncoder(connection_
->session()->config());
462 extension_manager_
->OnCreateVideoEncoder(&video_encoder
);
464 // Don't start the VideoFramePump if either capturer or encoder are missing.
465 if (!video_capturer
|| !video_encoder
)
468 // Create MouseShapePump to send mouse cursor shape.
469 mouse_shape_pump_
.reset(
470 new MouseShapePump(video_capture_task_runner_
,
471 desktop_environment_
->CreateMouseCursorMonitor(),
472 connection_
->client_stub()));
474 // Create a VideoFramePump to pump frames from the capturer to the client.'
476 // TODO(sergeyu): Move DesktopCapturerProxy creation to DesktopEnvironment.
477 // When using IpcDesktopCapturer the capture thread is not useful.
478 scoped_ptr
<DesktopCapturerProxy
> capturer_proxy(new DesktopCapturerProxy(
479 video_capture_task_runner_
, video_capturer
.Pass()));
480 video_frame_pump_
.reset(
481 new VideoFramePump(video_encode_task_runner_
, capturer_proxy
.Pass(),
482 video_encoder
.Pass(), &mouse_clamping_filter_
));
484 // Apply video-control parameters to the new scheduler.
485 video_frame_pump_
->SetLosslessEncode(lossless_video_encode_
);
486 video_frame_pump_
->SetLosslessColor(lossless_video_color_
);
488 // Pause capturing if necessary.
489 video_frame_pump_
->Pause(pause_video_
);
491 connection_
->set_video_feedback_stub(
492 video_frame_pump_
->video_feedback_stub());
495 void ClientSession::SetGnubbyAuthHandlerForTesting(
496 GnubbyAuthHandler
* gnubby_auth_handler
) {
497 DCHECK(CalledOnValidThread());
498 gnubby_auth_handler_
.reset(gnubby_auth_handler
);
501 scoped_ptr
<protocol::ClipboardStub
> ClientSession::CreateClipboardProxy() {
502 DCHECK(CalledOnValidThread());
504 return make_scoped_ptr(
505 new protocol::ClipboardThreadProxy(client_clipboard_factory_
.GetWeakPtr(),
506 base::ThreadTaskRunnerHandle::Get()));
509 } // namespace remoting