Update SplitString calls to new form
[chromium-blink-merge.git] / remoting / host / client_session.cc
blob18b3c66c098dee4c20d5fac8e994bf17c300fe82
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/command_line.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "remoting/base/capabilities.h"
13 #include "remoting/base/logging.h"
14 #include "remoting/codec/audio_encoder.h"
15 #include "remoting/codec/audio_encoder_opus.h"
16 #include "remoting/codec/audio_encoder_verbatim.h"
17 #include "remoting/codec/video_encoder.h"
18 #include "remoting/codec/video_encoder_verbatim.h"
19 #include "remoting/codec/video_encoder_vpx.h"
20 #include "remoting/host/audio_capturer.h"
21 #include "remoting/host/audio_pump.h"
22 #include "remoting/host/desktop_capturer_proxy.h"
23 #include "remoting/host/desktop_environment.h"
24 #include "remoting/host/host_extension_session.h"
25 #include "remoting/host/input_injector.h"
26 #include "remoting/host/mouse_shape_pump.h"
27 #include "remoting/host/screen_controls.h"
28 #include "remoting/host/screen_resolution.h"
29 #include "remoting/host/video_frame_pump.h"
30 #include "remoting/proto/control.pb.h"
31 #include "remoting/proto/event.pb.h"
32 #include "remoting/protocol/client_stub.h"
33 #include "remoting/protocol/clipboard_thread_proxy.h"
34 #include "remoting/protocol/pairing_registry.h"
35 #include "third_party/webrtc/modules/desktop_capture/desktop_capturer.h"
36 #include "third_party/webrtc/modules/desktop_capture/mouse_cursor_monitor.h"
38 // Default DPI to assume for old clients that use notifyClientDimensions.
39 const int kDefaultDPI = 96;
41 namespace remoting {
43 namespace {
45 // Name of command-line flag to disable use of I444 by default.
46 const char kDisableI444SwitchName[] = "disable-i444";
48 scoped_ptr<VideoEncoder> CreateVideoEncoder(
49 const protocol::SessionConfig& config) {
50 const protocol::ChannelConfig& video_config = config.video_config();
52 if (video_config.codec == protocol::ChannelConfig::CODEC_VP8) {
53 return VideoEncoderVpx::CreateForVP8().Pass();
54 } else if (video_config.codec == protocol::ChannelConfig::CODEC_VP9) {
55 return VideoEncoderVpx::CreateForVP9().Pass();
56 } else if (video_config.codec == protocol::ChannelConfig::CODEC_VERBATIM) {
57 return make_scoped_ptr(new VideoEncoderVerbatim());
60 NOTREACHED();
61 return nullptr;
64 scoped_ptr<AudioEncoder> CreateAudioEncoder(
65 const protocol::SessionConfig& config) {
66 const protocol::ChannelConfig& audio_config = config.audio_config();
68 if (audio_config.codec == protocol::ChannelConfig::CODEC_VERBATIM) {
69 return make_scoped_ptr(new AudioEncoderVerbatim());
70 } else if (audio_config.codec == protocol::ChannelConfig::CODEC_OPUS) {
71 return make_scoped_ptr(new AudioEncoderOpus());
74 NOTREACHED();
75 return nullptr;
78 } // namespace
80 ClientSession::ClientSession(
81 EventHandler* event_handler,
82 scoped_refptr<base::SingleThreadTaskRunner> audio_task_runner,
83 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner,
84 scoped_refptr<base::SingleThreadTaskRunner> video_capture_task_runner,
85 scoped_refptr<base::SingleThreadTaskRunner> video_encode_task_runner,
86 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner,
87 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
88 scoped_ptr<protocol::ConnectionToClient> connection,
89 DesktopEnvironmentFactory* desktop_environment_factory,
90 const base::TimeDelta& max_duration,
91 scoped_refptr<protocol::PairingRegistry> pairing_registry,
92 const std::vector<HostExtension*>& extensions)
93 : event_handler_(event_handler),
94 connection_(connection.Pass()),
95 client_jid_(connection_->session()->jid()),
96 desktop_environment_factory_(desktop_environment_factory),
97 input_tracker_(&host_input_filter_),
98 remote_input_filter_(&input_tracker_),
99 mouse_clamping_filter_(&remote_input_filter_),
100 disable_input_filter_(mouse_clamping_filter_.input_filter()),
101 disable_clipboard_filter_(clipboard_echo_filter_.host_filter()),
102 client_clipboard_factory_(clipboard_echo_filter_.client_filter()),
103 max_duration_(max_duration),
104 audio_task_runner_(audio_task_runner),
105 input_task_runner_(input_task_runner),
106 video_capture_task_runner_(video_capture_task_runner),
107 video_encode_task_runner_(video_encode_task_runner),
108 network_task_runner_(network_task_runner),
109 ui_task_runner_(ui_task_runner),
110 pairing_registry_(pairing_registry),
111 is_authenticated_(false),
112 pause_video_(false),
113 lossless_video_encode_(false),
114 // Note that |lossless_video_color_| defaults to true, but actually only
115 // controls VP9 video stream color quality.
116 lossless_video_color_(!base::CommandLine::ForCurrentProcess()->HasSwitch(
117 kDisableI444SwitchName)),
118 weak_factory_(this) {
119 connection_->SetEventHandler(this);
121 // Create a manager for the configured extensions, if any.
122 extension_manager_.reset(new HostExtensionSessionManager(extensions, this));
124 #if defined(OS_WIN)
125 // LocalInputMonitorWin filters out an echo of the injected input before it
126 // reaches |remote_input_filter_|.
127 remote_input_filter_.SetExpectLocalEcho(false);
128 #endif // defined(OS_WIN)
131 ClientSession::~ClientSession() {
132 DCHECK(CalledOnValidThread());
133 DCHECK(!audio_pump_);
134 DCHECK(!desktop_environment_);
135 DCHECK(!input_injector_);
136 DCHECK(!screen_controls_);
137 DCHECK(!video_frame_pump_);
139 connection_.reset();
142 void ClientSession::NotifyClientResolution(
143 const protocol::ClientResolution& resolution) {
144 DCHECK(CalledOnValidThread());
146 // TODO(sergeyu): Move these checks to protocol layer.
147 if (!resolution.has_dips_width() || !resolution.has_dips_height() ||
148 resolution.dips_width() < 0 || resolution.dips_height() < 0 ||
149 resolution.width() <= 0 || resolution.height() <= 0) {
150 LOG(ERROR) << "Received invalid ClientResolution message.";
151 return;
154 VLOG(1) << "Received ClientResolution (dips_width="
155 << resolution.dips_width() << ", dips_height="
156 << resolution.dips_height() << ")";
158 if (!screen_controls_)
159 return;
161 ScreenResolution client_resolution(
162 webrtc::DesktopSize(resolution.dips_width(), resolution.dips_height()),
163 webrtc::DesktopVector(kDefaultDPI, kDefaultDPI));
165 // Try to match the client's resolution.
166 screen_controls_->SetScreenResolution(client_resolution);
169 void ClientSession::ControlVideo(const protocol::VideoControl& video_control) {
170 DCHECK(CalledOnValidThread());
172 // Note that |video_frame_pump_| may be null, depending upon whether
173 // extensions choose to wrap or "steal" the video capturer or encoder.
174 if (video_control.has_enable()) {
175 VLOG(1) << "Received VideoControl (enable="
176 << video_control.enable() << ")";
177 pause_video_ = !video_control.enable();
178 if (video_frame_pump_)
179 video_frame_pump_->Pause(pause_video_);
181 if (video_control.has_lossless_encode()) {
182 VLOG(1) << "Received VideoControl (lossless_encode="
183 << video_control.lossless_encode() << ")";
184 lossless_video_encode_ = video_control.lossless_encode();
185 if (video_frame_pump_)
186 video_frame_pump_->SetLosslessEncode(lossless_video_encode_);
188 if (video_control.has_lossless_color()) {
189 VLOG(1) << "Received VideoControl (lossless_color="
190 << video_control.lossless_color() << ")";
191 lossless_video_color_ = video_control.lossless_color();
192 if (video_frame_pump_)
193 video_frame_pump_->SetLosslessColor(lossless_video_color_);
197 void ClientSession::ControlAudio(const protocol::AudioControl& audio_control) {
198 DCHECK(CalledOnValidThread());
200 if (audio_control.has_enable()) {
201 VLOG(1) << "Received AudioControl (enable="
202 << audio_control.enable() << ")";
203 if (audio_pump_)
204 audio_pump_->Pause(!audio_control.enable());
208 void ClientSession::SetCapabilities(
209 const protocol::Capabilities& capabilities) {
210 DCHECK(CalledOnValidThread());
212 // Ignore all the messages but the 1st one.
213 if (client_capabilities_) {
214 LOG(WARNING) << "protocol::Capabilities has been received already.";
215 return;
218 // Compute the set of capabilities supported by both client and host.
219 client_capabilities_ = make_scoped_ptr(new std::string());
220 if (capabilities.has_capabilities())
221 *client_capabilities_ = capabilities.capabilities();
222 capabilities_ = IntersectCapabilities(*client_capabilities_,
223 host_capabilities_);
224 extension_manager_->OnNegotiatedCapabilities(
225 connection_->client_stub(), capabilities_);
227 VLOG(1) << "Client capabilities: " << *client_capabilities_;
229 // Calculate the set of capabilities enabled by both client and host and
230 // pass it to the desktop environment if it is available.
231 desktop_environment_->SetCapabilities(capabilities_);
234 void ClientSession::RequestPairing(
235 const protocol::PairingRequest& pairing_request) {
236 if (pairing_registry_.get() && pairing_request.has_client_name()) {
237 protocol::PairingRegistry::Pairing pairing =
238 pairing_registry_->CreatePairing(pairing_request.client_name());
239 protocol::PairingResponse pairing_response;
240 pairing_response.set_client_id(pairing.client_id());
241 pairing_response.set_shared_secret(pairing.shared_secret());
242 connection_->client_stub()->SetPairingResponse(pairing_response);
246 void ClientSession::DeliverClientMessage(
247 const protocol::ExtensionMessage& message) {
248 if (message.has_type()) {
249 if (message.type() == "test-echo") {
250 protocol::ExtensionMessage reply;
251 reply.set_type("test-echo-reply");
252 if (message.has_data())
253 reply.set_data(message.data().substr(0, 16));
254 connection_->client_stub()->DeliverHostMessage(reply);
255 return;
256 } else if (message.type() == "gnubby-auth") {
257 if (gnubby_auth_handler_) {
258 gnubby_auth_handler_->DeliverClientMessage(message.data());
259 } else {
260 HOST_LOG << "gnubby auth is not enabled";
262 return;
263 } else {
264 if (extension_manager_->OnExtensionMessage(message))
265 return;
267 DLOG(INFO) << "Unexpected message received: "
268 << message.type() << ": " << message.data();
273 void ClientSession::OnConnectionAuthenticating(
274 protocol::ConnectionToClient* connection) {
275 event_handler_->OnSessionAuthenticating(this);
278 void ClientSession::OnConnectionAuthenticated(
279 protocol::ConnectionToClient* connection) {
280 DCHECK(CalledOnValidThread());
281 DCHECK_EQ(connection_.get(), connection);
282 DCHECK(!audio_pump_);
283 DCHECK(!desktop_environment_);
284 DCHECK(!input_injector_);
285 DCHECK(!screen_controls_);
286 DCHECK(!video_frame_pump_);
288 is_authenticated_ = true;
290 if (max_duration_ > base::TimeDelta()) {
291 // TODO(simonmorris): Let Disconnect() tell the client that the
292 // disconnection was caused by the session exceeding its maximum duration.
293 max_duration_timer_.Start(FROM_HERE, max_duration_,
294 this, &ClientSession::DisconnectSession);
297 // Disconnect the session if the connection was rejected by the host.
298 if (!event_handler_->OnSessionAuthenticated(this)) {
299 DisconnectSession();
300 return;
303 // Create the desktop environment. Drop the connection if it could not be
304 // created for any reason (for instance the curtain could not initialize).
305 desktop_environment_ =
306 desktop_environment_factory_->Create(weak_factory_.GetWeakPtr());
307 if (!desktop_environment_) {
308 DisconnectSession();
309 return;
312 // Connect host stub.
313 connection_->set_host_stub(this);
315 // Connect video stub.
316 mouse_clamping_filter_.set_video_stub(connection_->video_stub());
318 // Collate the set of capabilities to offer the client, if it supports them.
319 host_capabilities_ = desktop_environment_->GetCapabilities();
320 if (!host_capabilities_.empty())
321 host_capabilities_.append(" ");
322 host_capabilities_.append(extension_manager_->GetCapabilities());
324 // Create the object that controls the screen resolution.
325 screen_controls_ = desktop_environment_->CreateScreenControls();
327 // Create the event executor.
328 input_injector_ = desktop_environment_->CreateInputInjector();
330 // Connect the host input stubs.
331 connection_->set_input_stub(&disable_input_filter_);
332 host_input_filter_.set_input_stub(input_injector_.get());
334 // Connect the clipboard stubs.
335 connection_->set_clipboard_stub(&disable_clipboard_filter_);
336 clipboard_echo_filter_.set_host_stub(input_injector_.get());
337 clipboard_echo_filter_.set_client_stub(connection_->client_stub());
339 // Create a GnubbyAuthHandler to proxy gnubbyd messages.
340 gnubby_auth_handler_ = desktop_environment_->CreateGnubbyAuthHandler(
341 connection_->client_stub());
344 void ClientSession::OnConnectionChannelsConnected(
345 protocol::ConnectionToClient* connection) {
346 DCHECK(CalledOnValidThread());
347 DCHECK_EQ(connection_.get(), connection);
349 // Negotiate capabilities with the client.
350 VLOG(1) << "Host capabilities: " << host_capabilities_;
351 protocol::Capabilities capabilities;
352 capabilities.set_capabilities(host_capabilities_);
353 connection_->client_stub()->SetCapabilities(capabilities);
355 // Start the event executor.
356 input_injector_->Start(CreateClipboardProxy());
357 SetDisableInputs(false);
359 // Start recording video.
360 ResetVideoPipeline();
362 // Create an AudioPump if audio is enabled, to pump audio samples.
363 if (connection_->session()->config().is_audio_enabled()) {
364 scoped_ptr<AudioEncoder> audio_encoder =
365 CreateAudioEncoder(connection_->session()->config());
366 audio_pump_.reset(new AudioPump(
367 audio_task_runner_, desktop_environment_->CreateAudioCapturer(),
368 audio_encoder.Pass(), connection_->audio_stub()));
371 // Notify the event handler that all our channels are now connected.
372 event_handler_->OnSessionChannelsConnected(this);
375 void ClientSession::OnConnectionClosed(
376 protocol::ConnectionToClient* connection,
377 protocol::ErrorCode error) {
378 DCHECK(CalledOnValidThread());
379 DCHECK_EQ(connection_.get(), connection);
381 HOST_LOG << "Client disconnected: " << client_jid_ << "; error = " << error;
383 // Ignore any further callbacks.
384 weak_factory_.InvalidateWeakPtrs();
386 // If the client never authenticated then the session failed.
387 if (!is_authenticated_)
388 event_handler_->OnSessionAuthenticationFailed(this);
390 // Ensure that any pressed keys or buttons are released.
391 input_tracker_.ReleaseAll();
393 // Stop components access the client, audio or video stubs, which are no
394 // longer valid once ConnectionToClient calls OnConnectionClosed().
395 audio_pump_.reset();
396 video_frame_pump_.reset();
397 mouse_shape_pump_.reset();
398 client_clipboard_factory_.InvalidateWeakPtrs();
399 input_injector_.reset();
400 screen_controls_.reset();
401 desktop_environment_.reset();
403 // Notify the ChromotingHost that this client is disconnected.
404 event_handler_->OnSessionClosed(this);
407 void ClientSession::OnEventTimestamp(protocol::ConnectionToClient* connection,
408 int64 timestamp) {
409 DCHECK(CalledOnValidThread());
410 DCHECK_EQ(connection_.get(), connection);
412 if (video_frame_pump_.get())
413 video_frame_pump_->SetLatestEventTimestamp(timestamp);
416 void ClientSession::OnRouteChange(
417 protocol::ConnectionToClient* connection,
418 const std::string& channel_name,
419 const protocol::TransportRoute& route) {
420 DCHECK(CalledOnValidThread());
421 DCHECK_EQ(connection_.get(), connection);
422 event_handler_->OnSessionRouteChange(this, channel_name, route);
425 const std::string& ClientSession::client_jid() const {
426 return client_jid_;
429 void ClientSession::DisconnectSession() {
430 DCHECK(CalledOnValidThread());
431 DCHECK(connection_.get());
433 max_duration_timer_.Stop();
435 // This triggers OnConnectionClosed(), and the session may be destroyed
436 // as the result, so this call must be the last in this method.
437 connection_->Disconnect();
440 void ClientSession::OnLocalMouseMoved(const webrtc::DesktopVector& position) {
441 DCHECK(CalledOnValidThread());
442 remote_input_filter_.LocalMouseMoved(position);
445 void ClientSession::SetDisableInputs(bool disable_inputs) {
446 DCHECK(CalledOnValidThread());
448 if (disable_inputs)
449 input_tracker_.ReleaseAll();
451 disable_input_filter_.set_enabled(!disable_inputs);
452 disable_clipboard_filter_.set_enabled(!disable_inputs);
455 void ClientSession::ResetVideoPipeline() {
456 DCHECK(CalledOnValidThread());
458 mouse_shape_pump_.reset();
459 connection_->set_video_feedback_stub(nullptr);
460 video_frame_pump_.reset();
462 // Create VideoEncoder and DesktopCapturer to match the session's video
463 // channel configuration.
464 scoped_ptr<webrtc::DesktopCapturer> video_capturer =
465 desktop_environment_->CreateVideoCapturer();
466 extension_manager_->OnCreateVideoCapturer(&video_capturer);
467 scoped_ptr<VideoEncoder> video_encoder =
468 CreateVideoEncoder(connection_->session()->config());
469 extension_manager_->OnCreateVideoEncoder(&video_encoder);
471 // Don't start the VideoFramePump if either capturer or encoder are missing.
472 if (!video_capturer || !video_encoder)
473 return;
475 // Create MouseShapePump to send mouse cursor shape.
476 mouse_shape_pump_.reset(
477 new MouseShapePump(video_capture_task_runner_,
478 desktop_environment_->CreateMouseCursorMonitor(),
479 connection_->client_stub()));
481 // Create a VideoFramePump to pump frames from the capturer to the client.'
483 // TODO(sergeyu): Move DesktopCapturerProxy creation to DesktopEnvironment.
484 // When using IpcDesktopCapturer the capture thread is not useful.
485 scoped_ptr<DesktopCapturerProxy> capturer_proxy(new DesktopCapturerProxy(
486 video_capture_task_runner_, video_capturer.Pass()));
487 video_frame_pump_.reset(
488 new VideoFramePump(video_encode_task_runner_, capturer_proxy.Pass(),
489 video_encoder.Pass(), &mouse_clamping_filter_));
491 // Apply video-control parameters to the new scheduler.
492 video_frame_pump_->SetLosslessEncode(lossless_video_encode_);
493 video_frame_pump_->SetLosslessColor(lossless_video_color_);
495 // Pause capturing if necessary.
496 video_frame_pump_->Pause(pause_video_);
498 connection_->set_video_feedback_stub(
499 video_frame_pump_->video_feedback_stub());
502 void ClientSession::SetGnubbyAuthHandlerForTesting(
503 GnubbyAuthHandler* gnubby_auth_handler) {
504 DCHECK(CalledOnValidThread());
505 gnubby_auth_handler_.reset(gnubby_auth_handler);
508 scoped_ptr<protocol::ClipboardStub> ClientSession::CreateClipboardProxy() {
509 DCHECK(CalledOnValidThread());
511 return make_scoped_ptr(
512 new protocol::ClipboardThreadProxy(client_clipboard_factory_.GetWeakPtr(),
513 base::ThreadTaskRunnerHandle::Get()));
516 } // namespace remoting