Remove some NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED
[chromium-blink-merge.git] / remoting / host / client_session.cc
blob2661e0e30339b715d2f952122f1672f2930bb38e
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/input_injector.h"
22 #include "remoting/host/screen_controls.h"
23 #include "remoting/host/screen_resolution.h"
24 #include "remoting/host/video_scheduler.h"
25 #include "remoting/proto/control.pb.h"
26 #include "remoting/proto/event.pb.h"
27 #include "remoting/protocol/client_stub.h"
28 #include "remoting/protocol/clipboard_thread_proxy.h"
29 #include "remoting/protocol/pairing_registry.h"
30 #include "third_party/webrtc/modules/desktop_capture/screen_capturer.h"
32 // Default DPI to assume for old clients that use notifyClientDimensions.
33 const int kDefaultDPI = 96;
35 namespace remoting {
37 ClientSession::ClientSession(
38 EventHandler* event_handler,
39 scoped_refptr<base::SingleThreadTaskRunner> audio_task_runner,
40 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner,
41 scoped_refptr<base::SingleThreadTaskRunner> video_capture_task_runner,
42 scoped_refptr<base::SingleThreadTaskRunner> video_encode_task_runner,
43 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner,
44 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
45 scoped_ptr<protocol::ConnectionToClient> connection,
46 DesktopEnvironmentFactory* desktop_environment_factory,
47 const base::TimeDelta& max_duration,
48 scoped_refptr<protocol::PairingRegistry> pairing_registry)
49 : event_handler_(event_handler),
50 connection_(connection.Pass()),
51 client_jid_(connection_->session()->jid()),
52 control_factory_(this),
53 desktop_environment_factory_(desktop_environment_factory),
54 input_tracker_(&host_input_filter_),
55 remote_input_filter_(&input_tracker_),
56 mouse_clamping_filter_(&remote_input_filter_),
57 disable_input_filter_(mouse_clamping_filter_.input_filter()),
58 disable_clipboard_filter_(clipboard_echo_filter_.host_filter()),
59 auth_input_filter_(&disable_input_filter_),
60 auth_clipboard_filter_(&disable_clipboard_filter_),
61 client_clipboard_factory_(clipboard_echo_filter_.client_filter()),
62 max_duration_(max_duration),
63 audio_task_runner_(audio_task_runner),
64 input_task_runner_(input_task_runner),
65 video_capture_task_runner_(video_capture_task_runner),
66 video_encode_task_runner_(video_encode_task_runner),
67 network_task_runner_(network_task_runner),
68 ui_task_runner_(ui_task_runner),
69 pairing_registry_(pairing_registry) {
70 connection_->SetEventHandler(this);
72 // TODO(sergeyu): Currently ConnectionToClient expects stubs to be
73 // set before channels are connected. Make it possible to set stubs
74 // later and set them only when connection is authenticated.
75 connection_->set_clipboard_stub(&auth_clipboard_filter_);
76 connection_->set_host_stub(this);
77 connection_->set_input_stub(&auth_input_filter_);
79 // |auth_*_filter_|'s states reflect whether the session is authenticated.
80 auth_input_filter_.set_enabled(false);
81 auth_clipboard_filter_.set_enabled(false);
83 #if defined(OS_WIN)
84 // LocalInputMonitorWin filters out an echo of the injected input before it
85 // reaches |remote_input_filter_|.
86 remote_input_filter_.SetExpectLocalEcho(false);
87 #endif // defined(OS_WIN)
90 ClientSession::~ClientSession() {
91 DCHECK(CalledOnValidThread());
92 DCHECK(!audio_scheduler_.get());
93 DCHECK(!desktop_environment_);
94 DCHECK(!input_injector_);
95 DCHECK(!screen_controls_);
96 DCHECK(!video_scheduler_.get());
98 connection_.reset();
101 void ClientSession::NotifyClientResolution(
102 const protocol::ClientResolution& resolution) {
103 DCHECK(CalledOnValidThread());
105 // TODO(sergeyu): Move these checks to protocol layer.
106 if (!resolution.has_dips_width() || !resolution.has_dips_height() ||
107 resolution.dips_width() < 0 || resolution.dips_height() < 0 ||
108 resolution.width() <= 0 || resolution.height() <= 0) {
109 LOG(ERROR) << "Received invalid ClientResolution message.";
110 return;
113 VLOG(1) << "Received ClientResolution (dips_width="
114 << resolution.dips_width() << ", dips_height="
115 << resolution.dips_height() << ")";
117 if (!screen_controls_)
118 return;
120 ScreenResolution client_resolution(
121 webrtc::DesktopSize(resolution.dips_width(), resolution.dips_height()),
122 webrtc::DesktopVector(kDefaultDPI, kDefaultDPI));
124 // Try to match the client's resolution.
125 screen_controls_->SetScreenResolution(client_resolution);
128 void ClientSession::ControlVideo(const protocol::VideoControl& video_control) {
129 DCHECK(CalledOnValidThread());
131 if (video_control.has_enable()) {
132 VLOG(1) << "Received VideoControl (enable="
133 << video_control.enable() << ")";
134 video_scheduler_->Pause(!video_control.enable());
138 void ClientSession::ControlAudio(const protocol::AudioControl& audio_control) {
139 DCHECK(CalledOnValidThread());
141 if (audio_control.has_enable()) {
142 VLOG(1) << "Received AudioControl (enable="
143 << audio_control.enable() << ")";
144 if (audio_scheduler_.get())
145 audio_scheduler_->Pause(!audio_control.enable());
149 void ClientSession::SetCapabilities(
150 const protocol::Capabilities& capabilities) {
151 DCHECK(CalledOnValidThread());
153 // The client should not send protocol::Capabilities if it is not supported by
154 // the config channel.
155 if (!connection_->session()->config().SupportsCapabilities()) {
156 LOG(ERROR) << "Unexpected protocol::Capabilities has been received.";
157 return;
160 // Ignore all the messages but the 1st one.
161 if (client_capabilities_) {
162 LOG(WARNING) << "protocol::Capabilities has been received already.";
163 return;
166 client_capabilities_ = make_scoped_ptr(new std::string());
167 if (capabilities.has_capabilities())
168 *client_capabilities_ = capabilities.capabilities();
170 VLOG(1) << "Client capabilities: " << *client_capabilities_;
172 // Calculate the set of capabilities enabled by both client and host and
173 // pass it to the desktop environment if it is available.
174 desktop_environment_->SetCapabilities(
175 IntersectCapabilities(*client_capabilities_, host_capabilities_));
178 void ClientSession::RequestPairing(
179 const protocol::PairingRequest& pairing_request) {
180 if (pairing_registry_ && pairing_request.has_client_name()) {
181 protocol::PairingRegistry::Pairing pairing =
182 pairing_registry_->CreatePairing(pairing_request.client_name());
183 protocol::PairingResponse pairing_response;
184 pairing_response.set_client_id(pairing.client_id());
185 pairing_response.set_shared_secret(pairing.shared_secret());
186 connection_->client_stub()->SetPairingResponse(pairing_response);
190 void ClientSession::DeliverClientMessage(
191 const protocol::ExtensionMessage& message) {
192 if (message.has_type()) {
193 if (message.type() == "test-echo") {
194 protocol::ExtensionMessage reply;
195 reply.set_type("test-echo-reply");
196 if (message.has_data())
197 reply.set_data(message.data().substr(0, 16));
198 connection_->client_stub()->DeliverHostMessage(reply);
199 return;
200 } else if (message.type() == "gnubby-auth") {
201 if (gnubby_auth_handler_) {
202 gnubby_auth_handler_->DeliverClientMessage(message.data());
203 } else {
204 HOST_LOG << "gnubby auth is not enabled";
206 return;
209 HOST_LOG << "Unexpected message received: "
210 << message.type() << ": " << message.data();
213 void ClientSession::OnConnectionAuthenticating(
214 protocol::ConnectionToClient* connection) {
215 event_handler_->OnSessionAuthenticating(this);
218 void ClientSession::OnConnectionAuthenticated(
219 protocol::ConnectionToClient* connection) {
220 DCHECK(CalledOnValidThread());
221 DCHECK_EQ(connection_.get(), connection);
222 DCHECK(!audio_scheduler_.get());
223 DCHECK(!desktop_environment_);
224 DCHECK(!input_injector_);
225 DCHECK(!screen_controls_);
226 DCHECK(!video_scheduler_.get());
228 auth_input_filter_.set_enabled(true);
229 auth_clipboard_filter_.set_enabled(true);
231 clipboard_echo_filter_.set_client_stub(connection_->client_stub());
232 mouse_clamping_filter_.set_video_stub(connection_->video_stub());
234 if (max_duration_ > base::TimeDelta()) {
235 // TODO(simonmorris): Let Disconnect() tell the client that the
236 // disconnection was caused by the session exceeding its maximum duration.
237 max_duration_timer_.Start(FROM_HERE, max_duration_,
238 this, &ClientSession::DisconnectSession);
241 // Disconnect the session if the connection was rejected by the host.
242 if (!event_handler_->OnSessionAuthenticated(this)) {
243 DisconnectSession();
244 return;
247 // Create the desktop environment. Drop the connection if it could not be
248 // created for any reason (for instance the curtain could not initialize).
249 desktop_environment_ =
250 desktop_environment_factory_->Create(control_factory_.GetWeakPtr());
251 if (!desktop_environment_) {
252 DisconnectSession();
253 return;
256 host_capabilities_ = desktop_environment_->GetCapabilities();
258 // Ignore protocol::Capabilities messages from the client if it does not
259 // support any capabilities.
260 if (!connection_->session()->config().SupportsCapabilities()) {
261 VLOG(1) << "The client does not support any capabilities.";
263 client_capabilities_ = make_scoped_ptr(new std::string());
264 desktop_environment_->SetCapabilities(*client_capabilities_);
267 // Create the object that controls the screen resolution.
268 screen_controls_ = desktop_environment_->CreateScreenControls();
270 // Create the event executor.
271 input_injector_ = desktop_environment_->CreateInputInjector();
273 // Connect the host clipboard and input stubs.
274 host_input_filter_.set_input_stub(input_injector_.get());
275 clipboard_echo_filter_.set_host_stub(input_injector_.get());
277 // Create a VideoEncoder based on the session's video channel configuration.
278 scoped_ptr<VideoEncoder> video_encoder =
279 CreateVideoEncoder(connection_->session()->config());
281 // Create a VideoScheduler to pump frames from the capturer to the client.
282 video_scheduler_ = new VideoScheduler(
283 video_capture_task_runner_,
284 video_encode_task_runner_,
285 network_task_runner_,
286 desktop_environment_->CreateVideoCapturer(),
287 video_encoder.Pass(),
288 connection_->client_stub(),
289 &mouse_clamping_filter_);
291 // Create an AudioScheduler if audio is enabled, to pump audio samples.
292 if (connection_->session()->config().is_audio_enabled()) {
293 scoped_ptr<AudioEncoder> audio_encoder =
294 CreateAudioEncoder(connection_->session()->config());
295 audio_scheduler_ = new AudioScheduler(
296 audio_task_runner_,
297 network_task_runner_,
298 desktop_environment_->CreateAudioCapturer(),
299 audio_encoder.Pass(),
300 connection_->audio_stub());
303 // Create a GnubbyAuthHandler to proxy gnubbyd messages.
304 gnubby_auth_handler_ = desktop_environment_->CreateGnubbyAuthHandler(
305 connection_->client_stub());
308 void ClientSession::OnConnectionChannelsConnected(
309 protocol::ConnectionToClient* connection) {
310 DCHECK(CalledOnValidThread());
311 DCHECK_EQ(connection_.get(), connection);
313 // Negotiate capabilities with the client.
314 if (connection_->session()->config().SupportsCapabilities()) {
315 VLOG(1) << "Host capabilities: " << host_capabilities_;
317 protocol::Capabilities capabilities;
318 capabilities.set_capabilities(host_capabilities_);
319 connection_->client_stub()->SetCapabilities(capabilities);
322 // Start the event executor.
323 input_injector_->Start(CreateClipboardProxy());
324 SetDisableInputs(false);
326 // Start capturing the screen.
327 video_scheduler_->Start();
329 // Start recording audio.
330 if (connection_->session()->config().is_audio_enabled())
331 audio_scheduler_->Start();
333 // Notify the event handler that all our channels are now connected.
334 event_handler_->OnSessionChannelsConnected(this);
337 void ClientSession::OnConnectionClosed(
338 protocol::ConnectionToClient* connection,
339 protocol::ErrorCode error) {
340 DCHECK(CalledOnValidThread());
341 DCHECK_EQ(connection_.get(), connection);
343 // Ignore any further callbacks.
344 control_factory_.InvalidateWeakPtrs();
346 // If the client never authenticated then the session failed.
347 if (!auth_input_filter_.enabled())
348 event_handler_->OnSessionAuthenticationFailed(this);
350 // Block any further input events from the client.
351 // TODO(wez): Fix ChromotingHost::OnSessionClosed not to check our
352 // is_authenticated(), so that we can disable |auth_*_filter_| here.
353 disable_input_filter_.set_enabled(false);
354 disable_clipboard_filter_.set_enabled(false);
356 // Ensure that any pressed keys or buttons are released.
357 input_tracker_.ReleaseAll();
359 // Stop components access the client, audio or video stubs, which are no
360 // longer valid once ConnectionToClient calls OnConnectionClosed().
361 if (audio_scheduler_.get()) {
362 audio_scheduler_->Stop();
363 audio_scheduler_ = NULL;
365 if (video_scheduler_.get()) {
366 video_scheduler_->Stop();
367 video_scheduler_ = NULL;
370 client_clipboard_factory_.InvalidateWeakPtrs();
371 input_injector_.reset();
372 screen_controls_.reset();
373 desktop_environment_.reset();
375 // Notify the ChromotingHost that this client is disconnected.
376 // TODO(sergeyu): Log failure reason?
377 event_handler_->OnSessionClosed(this);
380 void ClientSession::OnSequenceNumberUpdated(
381 protocol::ConnectionToClient* connection, int64 sequence_number) {
382 DCHECK(CalledOnValidThread());
383 DCHECK_EQ(connection_.get(), connection);
385 if (video_scheduler_.get())
386 video_scheduler_->UpdateSequenceNumber(sequence_number);
388 event_handler_->OnSessionSequenceNumber(this, sequence_number);
391 void ClientSession::OnRouteChange(
392 protocol::ConnectionToClient* connection,
393 const std::string& channel_name,
394 const protocol::TransportRoute& route) {
395 DCHECK(CalledOnValidThread());
396 DCHECK_EQ(connection_.get(), connection);
397 event_handler_->OnSessionRouteChange(this, channel_name, route);
400 const std::string& ClientSession::client_jid() const {
401 return client_jid_;
404 void ClientSession::DisconnectSession() {
405 DCHECK(CalledOnValidThread());
406 DCHECK(connection_.get());
408 max_duration_timer_.Stop();
410 // This triggers OnConnectionClosed(), and the session may be destroyed
411 // as the result, so this call must be the last in this method.
412 connection_->Disconnect();
415 void ClientSession::OnLocalMouseMoved(const webrtc::DesktopVector& position) {
416 DCHECK(CalledOnValidThread());
417 remote_input_filter_.LocalMouseMoved(position);
420 void ClientSession::SetDisableInputs(bool disable_inputs) {
421 DCHECK(CalledOnValidThread());
423 if (disable_inputs)
424 input_tracker_.ReleaseAll();
426 disable_input_filter_.set_enabled(!disable_inputs);
427 disable_clipboard_filter_.set_enabled(!disable_inputs);
430 void ClientSession::SetGnubbyAuthHandlerForTesting(
431 GnubbyAuthHandler* gnubby_auth_handler) {
432 gnubby_auth_handler_.reset(gnubby_auth_handler);
435 scoped_ptr<protocol::ClipboardStub> ClientSession::CreateClipboardProxy() {
436 DCHECK(CalledOnValidThread());
438 return scoped_ptr<protocol::ClipboardStub>(
439 new protocol::ClipboardThreadProxy(
440 client_clipboard_factory_.GetWeakPtr(),
441 base::MessageLoopProxy::current()));
444 // TODO(sergeyu): Move this to SessionManager?
445 // static
446 scoped_ptr<VideoEncoder> ClientSession::CreateVideoEncoder(
447 const protocol::SessionConfig& config) {
448 const protocol::ChannelConfig& video_config = config.video_config();
450 if (video_config.codec == protocol::ChannelConfig::CODEC_VP8) {
451 return remoting::VideoEncoderVpx::CreateForVP8().PassAs<VideoEncoder>();
452 } else if (video_config.codec == protocol::ChannelConfig::CODEC_VP9) {
453 return remoting::VideoEncoderVpx::CreateForVP9().PassAs<VideoEncoder>();
456 NOTREACHED();
457 return scoped_ptr<VideoEncoder>();
460 // static
461 scoped_ptr<AudioEncoder> ClientSession::CreateAudioEncoder(
462 const protocol::SessionConfig& config) {
463 const protocol::ChannelConfig& audio_config = config.audio_config();
465 if (audio_config.codec == protocol::ChannelConfig::CODEC_VERBATIM) {
466 return scoped_ptr<AudioEncoder>(new AudioEncoderVerbatim());
467 } else if (audio_config.codec == protocol::ChannelConfig::CODEC_OPUS) {
468 return scoped_ptr<AudioEncoder>(new AudioEncoderOpus());
471 NOTREACHED();
472 return scoped_ptr<AudioEncoder>();
475 } // namespace remoting