ozone: DCHECK page_flip_request instead of page_flip_request_
[chromium-blink-merge.git] / remoting / host / client_session.cc
blob2b72eca8c7bac1f7cb543161367d20aebe8d87bc
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 enable VP9 to use I444 by default.
46 const char kEnableI444SwitchName[] = "enable-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 lossless_video_color_(base::CommandLine::ForCurrentProcess()->HasSwitch(
115 kEnableI444SwitchName)),
116 weak_factory_(this) {
117 connection_->SetEventHandler(this);
119 // Create a manager for the configured extensions, if any.
120 extension_manager_.reset(new HostExtensionSessionManager(extensions, this));
122 #if defined(OS_WIN)
123 // LocalInputMonitorWin filters out an echo of the injected input before it
124 // reaches |remote_input_filter_|.
125 remote_input_filter_.SetExpectLocalEcho(false);
126 #endif // defined(OS_WIN)
129 ClientSession::~ClientSession() {
130 DCHECK(CalledOnValidThread());
131 DCHECK(!audio_pump_);
132 DCHECK(!desktop_environment_);
133 DCHECK(!input_injector_);
134 DCHECK(!screen_controls_);
135 DCHECK(!video_frame_pump_);
137 connection_.reset();
140 void ClientSession::NotifyClientResolution(
141 const protocol::ClientResolution& resolution) {
142 DCHECK(CalledOnValidThread());
144 // TODO(sergeyu): Move these checks to protocol layer.
145 if (!resolution.has_dips_width() || !resolution.has_dips_height() ||
146 resolution.dips_width() < 0 || resolution.dips_height() < 0 ||
147 resolution.width() <= 0 || resolution.height() <= 0) {
148 LOG(ERROR) << "Received invalid ClientResolution message.";
149 return;
152 VLOG(1) << "Received ClientResolution (dips_width="
153 << resolution.dips_width() << ", dips_height="
154 << resolution.dips_height() << ")";
156 if (!screen_controls_)
157 return;
159 ScreenResolution client_resolution(
160 webrtc::DesktopSize(resolution.dips_width(), resolution.dips_height()),
161 webrtc::DesktopVector(kDefaultDPI, kDefaultDPI));
163 // Try to match the client's resolution.
164 screen_controls_->SetScreenResolution(client_resolution);
167 void ClientSession::ControlVideo(const protocol::VideoControl& video_control) {
168 DCHECK(CalledOnValidThread());
170 // Note that |video_frame_pump_| may be null, depending upon whether
171 // extensions choose to wrap or "steal" the video capturer or encoder.
172 if (video_control.has_enable()) {
173 VLOG(1) << "Received VideoControl (enable="
174 << video_control.enable() << ")";
175 pause_video_ = !video_control.enable();
176 if (video_frame_pump_)
177 video_frame_pump_->Pause(pause_video_);
179 if (video_control.has_lossless_encode()) {
180 VLOG(1) << "Received VideoControl (lossless_encode="
181 << video_control.lossless_encode() << ")";
182 lossless_video_encode_ = video_control.lossless_encode();
183 if (video_frame_pump_)
184 video_frame_pump_->SetLosslessEncode(lossless_video_encode_);
186 if (video_control.has_lossless_color()) {
187 VLOG(1) << "Received VideoControl (lossless_color="
188 << video_control.lossless_color() << ")";
189 lossless_video_color_ = video_control.lossless_color();
190 if (video_frame_pump_)
191 video_frame_pump_->SetLosslessColor(lossless_video_color_);
195 void ClientSession::ControlAudio(const protocol::AudioControl& audio_control) {
196 DCHECK(CalledOnValidThread());
198 if (audio_control.has_enable()) {
199 VLOG(1) << "Received AudioControl (enable="
200 << audio_control.enable() << ")";
201 if (audio_pump_)
202 audio_pump_->Pause(!audio_control.enable());
206 void ClientSession::SetCapabilities(
207 const protocol::Capabilities& capabilities) {
208 DCHECK(CalledOnValidThread());
210 // Ignore all the messages but the 1st one.
211 if (client_capabilities_) {
212 LOG(WARNING) << "protocol::Capabilities has been received already.";
213 return;
216 // Compute the set of capabilities supported by both client and host.
217 client_capabilities_ = make_scoped_ptr(new std::string());
218 if (capabilities.has_capabilities())
219 *client_capabilities_ = capabilities.capabilities();
220 capabilities_ = IntersectCapabilities(*client_capabilities_,
221 host_capabilities_);
222 extension_manager_->OnNegotiatedCapabilities(
223 connection_->client_stub(), capabilities_);
225 VLOG(1) << "Client capabilities: " << *client_capabilities_;
227 // Calculate the set of capabilities enabled by both client and host and
228 // pass it to the desktop environment if it is available.
229 desktop_environment_->SetCapabilities(capabilities_);
232 void ClientSession::RequestPairing(
233 const protocol::PairingRequest& pairing_request) {
234 if (pairing_registry_.get() && pairing_request.has_client_name()) {
235 protocol::PairingRegistry::Pairing pairing =
236 pairing_registry_->CreatePairing(pairing_request.client_name());
237 protocol::PairingResponse pairing_response;
238 pairing_response.set_client_id(pairing.client_id());
239 pairing_response.set_shared_secret(pairing.shared_secret());
240 connection_->client_stub()->SetPairingResponse(pairing_response);
244 void ClientSession::DeliverClientMessage(
245 const protocol::ExtensionMessage& message) {
246 if (message.has_type()) {
247 if (message.type() == "test-echo") {
248 protocol::ExtensionMessage reply;
249 reply.set_type("test-echo-reply");
250 if (message.has_data())
251 reply.set_data(message.data().substr(0, 16));
252 connection_->client_stub()->DeliverHostMessage(reply);
253 return;
254 } else if (message.type() == "gnubby-auth") {
255 if (gnubby_auth_handler_) {
256 gnubby_auth_handler_->DeliverClientMessage(message.data());
257 } else {
258 HOST_LOG << "gnubby auth is not enabled";
260 return;
261 } else {
262 if (extension_manager_->OnExtensionMessage(message))
263 return;
265 DLOG(INFO) << "Unexpected message received: "
266 << message.type() << ": " << message.data();
271 void ClientSession::OnConnectionAuthenticating(
272 protocol::ConnectionToClient* connection) {
273 event_handler_->OnSessionAuthenticating(this);
276 void ClientSession::OnConnectionAuthenticated(
277 protocol::ConnectionToClient* connection) {
278 DCHECK(CalledOnValidThread());
279 DCHECK_EQ(connection_.get(), connection);
280 DCHECK(!audio_pump_);
281 DCHECK(!desktop_environment_);
282 DCHECK(!input_injector_);
283 DCHECK(!screen_controls_);
284 DCHECK(!video_frame_pump_);
286 is_authenticated_ = true;
288 if (max_duration_ > base::TimeDelta()) {
289 // TODO(simonmorris): Let Disconnect() tell the client that the
290 // disconnection was caused by the session exceeding its maximum duration.
291 max_duration_timer_.Start(FROM_HERE, max_duration_,
292 this, &ClientSession::DisconnectSession);
295 // Disconnect the session if the connection was rejected by the host.
296 if (!event_handler_->OnSessionAuthenticated(this)) {
297 DisconnectSession();
298 return;
301 // Create the desktop environment. Drop the connection if it could not be
302 // created for any reason (for instance the curtain could not initialize).
303 desktop_environment_ =
304 desktop_environment_factory_->Create(weak_factory_.GetWeakPtr());
305 if (!desktop_environment_) {
306 DisconnectSession();
307 return;
310 // Connect host stub.
311 connection_->set_host_stub(this);
313 // Connect video stub.
314 mouse_clamping_filter_.set_video_stub(connection_->video_stub());
316 // Collate the set of capabilities to offer the client, if it supports them.
317 host_capabilities_ = desktop_environment_->GetCapabilities();
318 if (!host_capabilities_.empty())
319 host_capabilities_.append(" ");
320 host_capabilities_.append(extension_manager_->GetCapabilities());
322 // Create the object that controls the screen resolution.
323 screen_controls_ = desktop_environment_->CreateScreenControls();
325 // Create the event executor.
326 input_injector_ = desktop_environment_->CreateInputInjector();
328 // Connect the host input stubs.
329 connection_->set_input_stub(&disable_input_filter_);
330 host_input_filter_.set_input_stub(input_injector_.get());
332 // Connect the clipboard stubs.
333 connection_->set_clipboard_stub(&disable_clipboard_filter_);
334 clipboard_echo_filter_.set_host_stub(input_injector_.get());
335 clipboard_echo_filter_.set_client_stub(connection_->client_stub());
337 // Create a GnubbyAuthHandler to proxy gnubbyd messages.
338 gnubby_auth_handler_ = desktop_environment_->CreateGnubbyAuthHandler(
339 connection_->client_stub());
342 void ClientSession::OnConnectionChannelsConnected(
343 protocol::ConnectionToClient* connection) {
344 DCHECK(CalledOnValidThread());
345 DCHECK_EQ(connection_.get(), connection);
347 // Negotiate capabilities with the client.
348 VLOG(1) << "Host capabilities: " << host_capabilities_;
349 protocol::Capabilities capabilities;
350 capabilities.set_capabilities(host_capabilities_);
351 connection_->client_stub()->SetCapabilities(capabilities);
353 // Start the event executor.
354 input_injector_->Start(CreateClipboardProxy());
355 SetDisableInputs(false);
357 // Start recording video.
358 ResetVideoPipeline();
360 // Create an AudioPump if audio is enabled, to pump audio samples.
361 if (connection_->session()->config().is_audio_enabled()) {
362 scoped_ptr<AudioEncoder> audio_encoder =
363 CreateAudioEncoder(connection_->session()->config());
364 audio_pump_.reset(new AudioPump(
365 audio_task_runner_, desktop_environment_->CreateAudioCapturer(),
366 audio_encoder.Pass(), connection_->audio_stub()));
369 // Notify the event handler that all our channels are now connected.
370 event_handler_->OnSessionChannelsConnected(this);
373 void ClientSession::OnConnectionClosed(
374 protocol::ConnectionToClient* connection,
375 protocol::ErrorCode error) {
376 DCHECK(CalledOnValidThread());
377 DCHECK_EQ(connection_.get(), connection);
379 HOST_LOG << "Client disconnected: " << client_jid_ << "; error = " << error;
381 // Ignore any further callbacks.
382 weak_factory_.InvalidateWeakPtrs();
384 // If the client never authenticated then the session failed.
385 if (!is_authenticated_)
386 event_handler_->OnSessionAuthenticationFailed(this);
388 // Ensure that any pressed keys or buttons are released.
389 input_tracker_.ReleaseAll();
391 // Stop components access the client, audio or video stubs, which are no
392 // longer valid once ConnectionToClient calls OnConnectionClosed().
393 audio_pump_.reset();
394 video_frame_pump_.reset();
395 mouse_shape_pump_.reset();
396 client_clipboard_factory_.InvalidateWeakPtrs();
397 input_injector_.reset();
398 screen_controls_.reset();
399 desktop_environment_.reset();
401 // Notify the ChromotingHost that this client is disconnected.
402 event_handler_->OnSessionClosed(this);
405 void ClientSession::OnEventTimestamp(protocol::ConnectionToClient* connection,
406 int64 timestamp) {
407 DCHECK(CalledOnValidThread());
408 DCHECK_EQ(connection_.get(), connection);
410 if (video_frame_pump_.get())
411 video_frame_pump_->SetLatestEventTimestamp(timestamp);
414 void ClientSession::OnRouteChange(
415 protocol::ConnectionToClient* connection,
416 const std::string& channel_name,
417 const protocol::TransportRoute& route) {
418 DCHECK(CalledOnValidThread());
419 DCHECK_EQ(connection_.get(), connection);
420 event_handler_->OnSessionRouteChange(this, channel_name, route);
423 const std::string& ClientSession::client_jid() const {
424 return client_jid_;
427 void ClientSession::DisconnectSession() {
428 DCHECK(CalledOnValidThread());
429 DCHECK(connection_.get());
431 max_duration_timer_.Stop();
433 // This triggers OnConnectionClosed(), and the session may be destroyed
434 // as the result, so this call must be the last in this method.
435 connection_->Disconnect();
438 void ClientSession::OnLocalMouseMoved(const webrtc::DesktopVector& position) {
439 DCHECK(CalledOnValidThread());
440 remote_input_filter_.LocalMouseMoved(position);
443 void ClientSession::SetDisableInputs(bool disable_inputs) {
444 DCHECK(CalledOnValidThread());
446 if (disable_inputs)
447 input_tracker_.ReleaseAll();
449 disable_input_filter_.set_enabled(!disable_inputs);
450 disable_clipboard_filter_.set_enabled(!disable_inputs);
453 void ClientSession::ResetVideoPipeline() {
454 DCHECK(CalledOnValidThread());
456 mouse_shape_pump_.reset();
457 connection_->set_video_feedback_stub(nullptr);
458 video_frame_pump_.reset();
460 // Create VideoEncoder and DesktopCapturer to match the session's video
461 // channel configuration.
462 scoped_ptr<webrtc::DesktopCapturer> video_capturer =
463 desktop_environment_->CreateVideoCapturer();
464 extension_manager_->OnCreateVideoCapturer(&video_capturer);
465 scoped_ptr<VideoEncoder> video_encoder =
466 CreateVideoEncoder(connection_->session()->config());
467 extension_manager_->OnCreateVideoEncoder(&video_encoder);
469 // Don't start the VideoFramePump if either capturer or encoder are missing.
470 if (!video_capturer || !video_encoder)
471 return;
473 // Create MouseShapePump to send mouse cursor shape.
474 mouse_shape_pump_.reset(
475 new MouseShapePump(video_capture_task_runner_,
476 desktop_environment_->CreateMouseCursorMonitor(),
477 connection_->client_stub()));
479 // Create a VideoFramePump to pump frames from the capturer to the client.'
481 // TODO(sergeyu): Move DesktopCapturerProxy creation to DesktopEnvironment.
482 // When using IpcDesktopCapturer the capture thread is not useful.
483 scoped_ptr<DesktopCapturerProxy> capturer_proxy(new DesktopCapturerProxy(
484 video_capture_task_runner_, video_capturer.Pass()));
485 video_frame_pump_.reset(
486 new VideoFramePump(video_encode_task_runner_, capturer_proxy.Pass(),
487 video_encoder.Pass(), &mouse_clamping_filter_));
489 // Apply video-control parameters to the new scheduler.
490 video_frame_pump_->SetLosslessEncode(lossless_video_encode_);
491 video_frame_pump_->SetLosslessColor(lossless_video_color_);
493 // Pause capturing if necessary.
494 video_frame_pump_->Pause(pause_video_);
496 connection_->set_video_feedback_stub(
497 video_frame_pump_->video_feedback_stub());
500 void ClientSession::SetGnubbyAuthHandlerForTesting(
501 GnubbyAuthHandler* gnubby_auth_handler) {
502 DCHECK(CalledOnValidThread());
503 gnubby_auth_handler_.reset(gnubby_auth_handler);
506 scoped_ptr<protocol::ClipboardStub> ClientSession::CreateClipboardProxy() {
507 DCHECK(CalledOnValidThread());
509 return make_scoped_ptr(
510 new protocol::ClipboardThreadProxy(client_clipboard_factory_.GetWeakPtr(),
511 base::ThreadTaskRunnerHandle::Get()));
514 } // namespace remoting