Battery Status API: add UMA logging for Linux.
[chromium-blink-merge.git] / remoting / host / client_session.cc
blobd741e1cdda881d42aeeab43bcd79a6d64fb81840
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_scheduler.h"
20 #include "remoting/host/desktop_environment.h"
21 #include "remoting/host/host_extension_session.h"
22 #include "remoting/host/input_injector.h"
23 #include "remoting/host/screen_controls.h"
24 #include "remoting/host/screen_resolution.h"
25 #include "remoting/host/video_scheduler.h"
26 #include "remoting/proto/control.pb.h"
27 #include "remoting/proto/event.pb.h"
28 #include "remoting/protocol/client_stub.h"
29 #include "remoting/protocol/clipboard_thread_proxy.h"
30 #include "remoting/protocol/pairing_registry.h"
31 #include "third_party/webrtc/modules/desktop_capture/desktop_capturer.h"
33 // Default DPI to assume for old clients that use notifyClientDimensions.
34 const int kDefaultDPI = 96;
36 namespace remoting {
38 ClientSession::ClientSession(
39 EventHandler* event_handler,
40 scoped_refptr<base::SingleThreadTaskRunner> audio_task_runner,
41 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner,
42 scoped_refptr<base::SingleThreadTaskRunner> video_capture_task_runner,
43 scoped_refptr<base::SingleThreadTaskRunner> video_encode_task_runner,
44 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner,
45 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
46 scoped_ptr<protocol::ConnectionToClient> connection,
47 DesktopEnvironmentFactory* desktop_environment_factory,
48 const base::TimeDelta& max_duration,
49 scoped_refptr<protocol::PairingRegistry> pairing_registry,
50 const std::vector<HostExtension*>& extensions)
51 : event_handler_(event_handler),
52 connection_(connection.Pass()),
53 client_jid_(connection_->session()->jid()),
54 control_factory_(this),
55 desktop_environment_factory_(desktop_environment_factory),
56 input_tracker_(&host_input_filter_),
57 remote_input_filter_(&input_tracker_),
58 mouse_clamping_filter_(&remote_input_filter_),
59 disable_input_filter_(mouse_clamping_filter_.input_filter()),
60 disable_clipboard_filter_(clipboard_echo_filter_.host_filter()),
61 auth_input_filter_(&disable_input_filter_),
62 auth_clipboard_filter_(&disable_clipboard_filter_),
63 client_clipboard_factory_(clipboard_echo_filter_.client_filter()),
64 max_duration_(max_duration),
65 audio_task_runner_(audio_task_runner),
66 input_task_runner_(input_task_runner),
67 video_capture_task_runner_(video_capture_task_runner),
68 video_encode_task_runner_(video_encode_task_runner),
69 network_task_runner_(network_task_runner),
70 ui_task_runner_(ui_task_runner),
71 pairing_registry_(pairing_registry),
72 pause_video_(false),
73 lossless_video_encode_(false),
74 lossless_video_color_(false) {
75 connection_->SetEventHandler(this);
77 // TODO(sergeyu): Currently ConnectionToClient expects stubs to be
78 // set before channels are connected. Make it possible to set stubs
79 // later and set them only when connection is authenticated.
80 connection_->set_clipboard_stub(&auth_clipboard_filter_);
81 connection_->set_host_stub(this);
82 connection_->set_input_stub(&auth_input_filter_);
84 // |auth_*_filter_|'s states reflect whether the session is authenticated.
85 auth_input_filter_.set_enabled(false);
86 auth_clipboard_filter_.set_enabled(false);
88 // Create a manager for the configured extensions, if any.
89 extension_manager_.reset(new HostExtensionSessionManager(extensions, this));
91 #if defined(OS_WIN)
92 // LocalInputMonitorWin filters out an echo of the injected input before it
93 // reaches |remote_input_filter_|.
94 remote_input_filter_.SetExpectLocalEcho(false);
95 #endif // defined(OS_WIN)
98 ClientSession::~ClientSession() {
99 DCHECK(CalledOnValidThread());
100 DCHECK(!audio_scheduler_.get());
101 DCHECK(!desktop_environment_);
102 DCHECK(!input_injector_);
103 DCHECK(!screen_controls_);
104 DCHECK(!video_scheduler_.get());
106 connection_.reset();
109 void ClientSession::NotifyClientResolution(
110 const protocol::ClientResolution& resolution) {
111 DCHECK(CalledOnValidThread());
113 // TODO(sergeyu): Move these checks to protocol layer.
114 if (!resolution.has_dips_width() || !resolution.has_dips_height() ||
115 resolution.dips_width() < 0 || resolution.dips_height() < 0 ||
116 resolution.width() <= 0 || resolution.height() <= 0) {
117 LOG(ERROR) << "Received invalid ClientResolution message.";
118 return;
121 VLOG(1) << "Received ClientResolution (dips_width="
122 << resolution.dips_width() << ", dips_height="
123 << resolution.dips_height() << ")";
125 if (!screen_controls_)
126 return;
128 ScreenResolution client_resolution(
129 webrtc::DesktopSize(resolution.dips_width(), resolution.dips_height()),
130 webrtc::DesktopVector(kDefaultDPI, kDefaultDPI));
132 // Try to match the client's resolution.
133 screen_controls_->SetScreenResolution(client_resolution);
136 void ClientSession::ControlVideo(const protocol::VideoControl& video_control) {
137 DCHECK(CalledOnValidThread());
139 // Note that |video_scheduler_| may be NULL, depending upon whether extensions
140 // choose to wrap or "steal" the video capturer or encoder.
141 if (video_control.has_enable()) {
142 VLOG(1) << "Received VideoControl (enable="
143 << video_control.enable() << ")";
144 pause_video_ = !video_control.enable();
145 if (video_scheduler_.get())
146 video_scheduler_->Pause(pause_video_);
148 if (video_control.has_lossless_encode()) {
149 VLOG(1) << "Received VideoControl (lossless_encode="
150 << video_control.lossless_encode() << ")";
151 lossless_video_encode_ = video_control.lossless_encode();
152 if (video_scheduler_.get())
153 video_scheduler_->SetLosslessEncode(lossless_video_encode_);
155 if (video_control.has_lossless_color()) {
156 VLOG(1) << "Received VideoControl (lossless_color="
157 << video_control.lossless_color() << ")";
158 lossless_video_color_ = video_control.lossless_color();
159 if (video_scheduler_.get())
160 video_scheduler_->SetLosslessColor(lossless_video_color_);
164 void ClientSession::ControlAudio(const protocol::AudioControl& audio_control) {
165 DCHECK(CalledOnValidThread());
167 if (audio_control.has_enable()) {
168 VLOG(1) << "Received AudioControl (enable="
169 << audio_control.enable() << ")";
170 if (audio_scheduler_.get())
171 audio_scheduler_->Pause(!audio_control.enable());
175 void ClientSession::SetCapabilities(
176 const protocol::Capabilities& capabilities) {
177 DCHECK(CalledOnValidThread());
179 // The client should not send protocol::Capabilities if it is not supported by
180 // the config channel.
181 if (!connection_->session()->config().SupportsCapabilities()) {
182 LOG(ERROR) << "Unexpected protocol::Capabilities has been received.";
183 return;
186 // Ignore all the messages but the 1st one.
187 if (client_capabilities_) {
188 LOG(WARNING) << "protocol::Capabilities has been received already.";
189 return;
192 // Compute the set of capabilities supported by both client and host.
193 client_capabilities_ = make_scoped_ptr(new std::string());
194 if (capabilities.has_capabilities())
195 *client_capabilities_ = capabilities.capabilities();
196 capabilities_ = IntersectCapabilities(*client_capabilities_,
197 host_capabilities_);
198 extension_manager_->OnNegotiatedCapabilities(
199 connection_->client_stub(), capabilities_);
201 VLOG(1) << "Client capabilities: " << *client_capabilities_;
203 // Calculate the set of capabilities enabled by both client and host and
204 // pass it to the desktop environment if it is available.
205 desktop_environment_->SetCapabilities(capabilities_);
208 void ClientSession::RequestPairing(
209 const protocol::PairingRequest& pairing_request) {
210 if (pairing_registry_.get() && pairing_request.has_client_name()) {
211 protocol::PairingRegistry::Pairing pairing =
212 pairing_registry_->CreatePairing(pairing_request.client_name());
213 protocol::PairingResponse pairing_response;
214 pairing_response.set_client_id(pairing.client_id());
215 pairing_response.set_shared_secret(pairing.shared_secret());
216 connection_->client_stub()->SetPairingResponse(pairing_response);
220 void ClientSession::DeliverClientMessage(
221 const protocol::ExtensionMessage& message) {
222 if (message.has_type()) {
223 if (message.type() == "test-echo") {
224 protocol::ExtensionMessage reply;
225 reply.set_type("test-echo-reply");
226 if (message.has_data())
227 reply.set_data(message.data().substr(0, 16));
228 connection_->client_stub()->DeliverHostMessage(reply);
229 return;
230 } else if (message.type() == "gnubby-auth") {
231 if (gnubby_auth_handler_) {
232 gnubby_auth_handler_->DeliverClientMessage(message.data());
233 } else {
234 HOST_LOG << "gnubby auth is not enabled";
236 return;
237 } else {
238 extension_manager_->OnExtensionMessage(message);
239 return;
242 HOST_LOG << "Unexpected message received: "
243 << message.type() << ": " << message.data();
246 void ClientSession::OnConnectionAuthenticating(
247 protocol::ConnectionToClient* connection) {
248 event_handler_->OnSessionAuthenticating(this);
251 void ClientSession::OnConnectionAuthenticated(
252 protocol::ConnectionToClient* connection) {
253 DCHECK(CalledOnValidThread());
254 DCHECK_EQ(connection_.get(), connection);
255 DCHECK(!audio_scheduler_.get());
256 DCHECK(!desktop_environment_);
257 DCHECK(!input_injector_);
258 DCHECK(!screen_controls_);
259 DCHECK(!video_scheduler_.get());
261 auth_input_filter_.set_enabled(true);
262 auth_clipboard_filter_.set_enabled(true);
264 clipboard_echo_filter_.set_client_stub(connection_->client_stub());
265 mouse_clamping_filter_.set_video_stub(connection_->video_stub());
267 if (max_duration_ > base::TimeDelta()) {
268 // TODO(simonmorris): Let Disconnect() tell the client that the
269 // disconnection was caused by the session exceeding its maximum duration.
270 max_duration_timer_.Start(FROM_HERE, max_duration_,
271 this, &ClientSession::DisconnectSession);
274 // Disconnect the session if the connection was rejected by the host.
275 if (!event_handler_->OnSessionAuthenticated(this)) {
276 DisconnectSession();
277 return;
280 // Create the desktop environment. Drop the connection if it could not be
281 // created for any reason (for instance the curtain could not initialize).
282 desktop_environment_ =
283 desktop_environment_factory_->Create(control_factory_.GetWeakPtr());
284 if (!desktop_environment_) {
285 DisconnectSession();
286 return;
289 // Collate the set of capabilities to offer the client, if it supports them.
290 if (connection_->session()->config().SupportsCapabilities()) {
291 host_capabilities_ = desktop_environment_->GetCapabilities();
292 if (!host_capabilities_.empty()) {
293 host_capabilities_.append(" ");
295 host_capabilities_.append(extension_manager_->GetCapabilities());
296 } else {
297 VLOG(1) << "The client does not support any capabilities.";
298 desktop_environment_->SetCapabilities(std::string());
301 // Create the object that controls the screen resolution.
302 screen_controls_ = desktop_environment_->CreateScreenControls();
304 // Create the event executor.
305 input_injector_ = desktop_environment_->CreateInputInjector();
307 // Connect the host clipboard and input stubs.
308 host_input_filter_.set_input_stub(input_injector_.get());
309 clipboard_echo_filter_.set_host_stub(input_injector_.get());
311 // Create an AudioScheduler if audio is enabled, to pump audio samples.
312 if (connection_->session()->config().is_audio_enabled()) {
313 scoped_ptr<AudioEncoder> audio_encoder =
314 CreateAudioEncoder(connection_->session()->config());
315 audio_scheduler_ = new AudioScheduler(
316 audio_task_runner_,
317 network_task_runner_,
318 desktop_environment_->CreateAudioCapturer(),
319 audio_encoder.Pass(),
320 connection_->audio_stub());
323 // Create a GnubbyAuthHandler to proxy gnubbyd messages.
324 gnubby_auth_handler_ = desktop_environment_->CreateGnubbyAuthHandler(
325 connection_->client_stub());
328 void ClientSession::OnConnectionChannelsConnected(
329 protocol::ConnectionToClient* connection) {
330 DCHECK(CalledOnValidThread());
331 DCHECK_EQ(connection_.get(), connection);
333 // Negotiate capabilities with the client.
334 if (connection_->session()->config().SupportsCapabilities()) {
335 VLOG(1) << "Host capabilities: " << host_capabilities_;
337 protocol::Capabilities capabilities;
338 capabilities.set_capabilities(host_capabilities_);
339 connection_->client_stub()->SetCapabilities(capabilities);
342 // Start the event executor.
343 input_injector_->Start(CreateClipboardProxy());
344 SetDisableInputs(false);
346 // Start recording video.
347 ResetVideoPipeline();
349 // Start recording audio.
350 if (connection_->session()->config().is_audio_enabled())
351 audio_scheduler_->Start();
353 // Notify the event handler that all our channels are now connected.
354 event_handler_->OnSessionChannelsConnected(this);
357 void ClientSession::OnConnectionClosed(
358 protocol::ConnectionToClient* connection,
359 protocol::ErrorCode error) {
360 DCHECK(CalledOnValidThread());
361 DCHECK_EQ(connection_.get(), connection);
363 // Ignore any further callbacks.
364 control_factory_.InvalidateWeakPtrs();
366 // If the client never authenticated then the session failed.
367 if (!auth_input_filter_.enabled())
368 event_handler_->OnSessionAuthenticationFailed(this);
370 // Block any further input events from the client.
371 // TODO(wez): Fix ChromotingHost::OnSessionClosed not to check our
372 // is_authenticated(), so that we can disable |auth_*_filter_| here.
373 disable_input_filter_.set_enabled(false);
374 disable_clipboard_filter_.set_enabled(false);
376 // Ensure that any pressed keys or buttons are released.
377 input_tracker_.ReleaseAll();
379 // Stop components access the client, audio or video stubs, which are no
380 // longer valid once ConnectionToClient calls OnConnectionClosed().
381 if (audio_scheduler_.get()) {
382 audio_scheduler_->Stop();
383 audio_scheduler_ = NULL;
385 if (video_scheduler_.get()) {
386 video_scheduler_->Stop();
387 video_scheduler_ = NULL;
390 client_clipboard_factory_.InvalidateWeakPtrs();
391 input_injector_.reset();
392 screen_controls_.reset();
393 desktop_environment_.reset();
395 // Notify the ChromotingHost that this client is disconnected.
396 // TODO(sergeyu): Log failure reason?
397 event_handler_->OnSessionClosed(this);
400 void ClientSession::OnSequenceNumberUpdated(
401 protocol::ConnectionToClient* connection, int64 sequence_number) {
402 DCHECK(CalledOnValidThread());
403 DCHECK_EQ(connection_.get(), connection);
405 if (video_scheduler_.get())
406 video_scheduler_->UpdateSequenceNumber(sequence_number);
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 {
419 return client_jid_;
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());
441 if (disable_inputs)
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 if (video_scheduler_.get()) {
452 video_scheduler_->Stop();
453 video_scheduler_ = NULL;
456 // Create VideoEncoder and DesktopCapturer to match the session's video
457 // channel configuration.
458 scoped_ptr<webrtc::DesktopCapturer> video_capturer =
459 extension_manager_->OnCreateVideoCapturer(
460 desktop_environment_->CreateVideoCapturer());
461 scoped_ptr<VideoEncoder> video_encoder =
462 extension_manager_->OnCreateVideoEncoder(
463 CreateVideoEncoder(connection_->session()->config()));
465 // Don't start the VideoScheduler if either capturer or encoder are missing.
466 if (!video_capturer || !video_encoder)
467 return;
469 // Create a VideoScheduler to pump frames from the capturer to the client.
470 video_scheduler_ = new VideoScheduler(
471 video_capture_task_runner_,
472 video_encode_task_runner_,
473 network_task_runner_,
474 video_capturer.Pass(),
475 desktop_environment_->CreateMouseCursorMonitor(),
476 video_encoder.Pass(),
477 connection_->client_stub(),
478 &mouse_clamping_filter_);
480 // Apply video-control parameters to the new scheduler.
481 video_scheduler_->Pause(pause_video_);
482 video_scheduler_->SetLosslessEncode(lossless_video_encode_);
483 video_scheduler_->SetLosslessColor(lossless_video_color_);
485 // Start capturing the screen.
486 video_scheduler_->Start();
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 scoped_ptr<protocol::ClipboardStub>(
499 new protocol::ClipboardThreadProxy(
500 client_clipboard_factory_.GetWeakPtr(),
501 base::MessageLoopProxy::current()));
504 // TODO(sergeyu): Move this to SessionManager?
505 // static
506 scoped_ptr<VideoEncoder> ClientSession::CreateVideoEncoder(
507 const protocol::SessionConfig& config) {
508 const protocol::ChannelConfig& video_config = config.video_config();
510 if (video_config.codec == protocol::ChannelConfig::CODEC_VP8) {
511 return remoting::VideoEncoderVpx::CreateForVP8().PassAs<VideoEncoder>();
512 } else if (video_config.codec == protocol::ChannelConfig::CODEC_VP9) {
513 return remoting::VideoEncoderVpx::CreateForVP9().PassAs<VideoEncoder>();
514 } else if (video_config.codec == protocol::ChannelConfig::CODEC_VERBATIM) {
515 return scoped_ptr<VideoEncoder>(new remoting::VideoEncoderVerbatim());
518 NOTREACHED();
519 return scoped_ptr<VideoEncoder>();
522 // static
523 scoped_ptr<AudioEncoder> ClientSession::CreateAudioEncoder(
524 const protocol::SessionConfig& config) {
525 const protocol::ChannelConfig& audio_config = config.audio_config();
527 if (audio_config.codec == protocol::ChannelConfig::CODEC_VERBATIM) {
528 return scoped_ptr<AudioEncoder>(new AudioEncoderVerbatim());
529 } else if (audio_config.codec == protocol::ChannelConfig::CODEC_OPUS) {
530 return scoped_ptr<AudioEncoder>(new AudioEncoderOpus());
533 NOTREACHED();
534 return scoped_ptr<AudioEncoder>();
537 } // namespace remoting