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/message_loop/message_loop_proxy.h"
10 #include "remoting/base/capabilities.h"
11 #include "remoting/codec/audio_encoder.h"
12 #include "remoting/codec/audio_encoder_opus.h"
13 #include "remoting/codec/audio_encoder_verbatim.h"
14 #include "remoting/codec/video_encoder.h"
15 #include "remoting/codec/video_encoder_verbatim.h"
16 #include "remoting/codec/video_encoder_vpx.h"
17 #include "remoting/host/audio_capturer.h"
18 #include "remoting/host/audio_scheduler.h"
19 #include "remoting/host/desktop_environment.h"
20 #include "remoting/host/input_injector.h"
21 #include "remoting/host/screen_controls.h"
22 #include "remoting/host/screen_resolution.h"
23 #include "remoting/host/video_scheduler.h"
24 #include "remoting/proto/control.pb.h"
25 #include "remoting/proto/event.pb.h"
26 #include "remoting/protocol/client_stub.h"
27 #include "remoting/protocol/clipboard_thread_proxy.h"
28 #include "remoting/protocol/pairing_registry.h"
29 #include "third_party/webrtc/modules/desktop_capture/screen_capturer.h"
31 // Default DPI to assume for old clients that use notifyClientDimensions.
32 const int kDefaultDPI = 96;
36 ClientSession::ClientSession(
37 EventHandler* event_handler,
38 scoped_refptr<base::SingleThreadTaskRunner> audio_task_runner,
39 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner,
40 scoped_refptr<base::SingleThreadTaskRunner> video_capture_task_runner,
41 scoped_refptr<base::SingleThreadTaskRunner> video_encode_task_runner,
42 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner,
43 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
44 scoped_ptr<protocol::ConnectionToClient> connection,
45 DesktopEnvironmentFactory* desktop_environment_factory,
46 const base::TimeDelta& max_duration,
47 scoped_refptr<protocol::PairingRegistry> pairing_registry)
48 : event_handler_(event_handler),
49 connection_(connection.Pass()),
50 client_jid_(connection_->session()->jid()),
51 control_factory_(this),
52 desktop_environment_factory_(desktop_environment_factory),
53 input_tracker_(&host_input_filter_),
54 remote_input_filter_(&input_tracker_),
55 mouse_clamping_filter_(&remote_input_filter_),
56 disable_input_filter_(mouse_clamping_filter_.input_filter()),
57 disable_clipboard_filter_(clipboard_echo_filter_.host_filter()),
58 auth_input_filter_(&disable_input_filter_),
59 auth_clipboard_filter_(&disable_clipboard_filter_),
60 client_clipboard_factory_(clipboard_echo_filter_.client_filter()),
61 max_duration_(max_duration),
62 audio_task_runner_(audio_task_runner),
63 input_task_runner_(input_task_runner),
64 video_capture_task_runner_(video_capture_task_runner),
65 video_encode_task_runner_(video_encode_task_runner),
66 network_task_runner_(network_task_runner),
67 ui_task_runner_(ui_task_runner),
68 pairing_registry_(pairing_registry) {
69 connection_->SetEventHandler(this);
71 // TODO(sergeyu): Currently ConnectionToClient expects stubs to be
72 // set before channels are connected. Make it possible to set stubs
73 // later and set them only when connection is authenticated.
74 connection_->set_clipboard_stub(&auth_clipboard_filter_);
75 connection_->set_host_stub(this);
76 connection_->set_input_stub(&auth_input_filter_);
78 // |auth_*_filter_|'s states reflect whether the session is authenticated.
79 auth_input_filter_.set_enabled(false);
80 auth_clipboard_filter_.set_enabled(false);
83 // LocalInputMonitorWin filters out an echo of the injected input before it
84 // reaches |remote_input_filter_|.
85 remote_input_filter_.SetExpectLocalEcho(false);
86 #endif // defined(OS_WIN)
89 ClientSession::~ClientSession() {
90 DCHECK(CalledOnValidThread());
91 DCHECK(!audio_scheduler_.get());
92 DCHECK(!desktop_environment_);
93 DCHECK(!input_injector_);
94 DCHECK(!screen_controls_);
95 DCHECK(!video_scheduler_.get());
100 void ClientSession::NotifyClientResolution(
101 const protocol::ClientResolution& resolution) {
102 DCHECK(CalledOnValidThread());
104 // TODO(sergeyu): Move these checks to protocol layer.
105 if (!resolution.has_dips_width() || !resolution.has_dips_height() ||
106 resolution.dips_width() < 0 || resolution.dips_height() < 0 ||
107 resolution.width() <= 0 || resolution.height() <= 0) {
108 LOG(ERROR) << "Received invalid ClientResolution message.";
112 VLOG(1) << "Received ClientResolution (dips_width="
113 << resolution.dips_width() << ", dips_height="
114 << resolution.dips_height() << ")";
116 if (!screen_controls_)
119 ScreenResolution client_resolution(
120 webrtc::DesktopSize(resolution.dips_width(), resolution.dips_height()),
121 webrtc::DesktopVector(kDefaultDPI, kDefaultDPI));
123 // Try to match the client's resolution.
124 screen_controls_->SetScreenResolution(client_resolution);
127 void ClientSession::ControlVideo(const protocol::VideoControl& video_control) {
128 DCHECK(CalledOnValidThread());
130 if (video_control.has_enable()) {
131 VLOG(1) << "Received VideoControl (enable="
132 << video_control.enable() << ")";
133 video_scheduler_->Pause(!video_control.enable());
137 void ClientSession::ControlAudio(const protocol::AudioControl& audio_control) {
138 DCHECK(CalledOnValidThread());
140 if (audio_control.has_enable()) {
141 VLOG(1) << "Received AudioControl (enable="
142 << audio_control.enable() << ")";
143 if (audio_scheduler_.get())
144 audio_scheduler_->Pause(!audio_control.enable());
148 void ClientSession::SetCapabilities(
149 const protocol::Capabilities& capabilities) {
150 DCHECK(CalledOnValidThread());
152 // The client should not send protocol::Capabilities if it is not supported by
153 // the config channel.
154 if (!connection_->session()->config().SupportsCapabilities()) {
155 LOG(ERROR) << "Unexpected protocol::Capabilities has been received.";
159 // Ignore all the messages but the 1st one.
160 if (client_capabilities_) {
161 LOG(WARNING) << "protocol::Capabilities has been received already.";
165 client_capabilities_ = make_scoped_ptr(new std::string());
166 if (capabilities.has_capabilities())
167 *client_capabilities_ = capabilities.capabilities();
169 VLOG(1) << "Client capabilities: " << *client_capabilities_;
171 // Calculate the set of capabilities enabled by both client and host and
172 // pass it to the desktop environment if it is available.
173 desktop_environment_->SetCapabilities(
174 IntersectCapabilities(*client_capabilities_, host_capabilities_));
177 void ClientSession::RequestPairing(
178 const protocol::PairingRequest& pairing_request) {
179 if (pairing_registry_ && pairing_request.has_client_name()) {
180 protocol::PairingRegistry::Pairing pairing =
181 pairing_registry_->CreatePairing(pairing_request.client_name());
182 protocol::PairingResponse pairing_response;
183 pairing_response.set_client_id(pairing.client_id());
184 pairing_response.set_shared_secret(pairing.shared_secret());
185 connection_->client_stub()->SetPairingResponse(pairing_response);
189 void ClientSession::DeliverClientMessage(
190 const protocol::ExtensionMessage& message) {
191 if (message.has_type()) {
192 if (message.type() == "test-echo") {
193 protocol::ExtensionMessage reply;
194 reply.set_type("test-echo-reply");
195 if (message.has_data())
196 reply.set_data(message.data().substr(0, 16));
197 connection_->client_stub()->DeliverHostMessage(reply);
201 // No messages are currently supported.
202 LOG(INFO) << "Unexpected message received: "
203 << message.type() << ": " << message.data();
206 void ClientSession::OnConnectionAuthenticated(
207 protocol::ConnectionToClient* connection) {
208 DCHECK(CalledOnValidThread());
209 DCHECK_EQ(connection_.get(), connection);
210 DCHECK(!audio_scheduler_.get());
211 DCHECK(!desktop_environment_);
212 DCHECK(!input_injector_);
213 DCHECK(!screen_controls_);
214 DCHECK(!video_scheduler_.get());
216 auth_input_filter_.set_enabled(true);
217 auth_clipboard_filter_.set_enabled(true);
219 clipboard_echo_filter_.set_client_stub(connection_->client_stub());
220 mouse_clamping_filter_.set_video_stub(connection_->video_stub());
222 if (max_duration_ > base::TimeDelta()) {
223 // TODO(simonmorris): Let Disconnect() tell the client that the
224 // disconnection was caused by the session exceeding its maximum duration.
225 max_duration_timer_.Start(FROM_HERE, max_duration_,
226 this, &ClientSession::DisconnectSession);
229 // Disconnect the session if the connection was rejected by the host.
230 if (!event_handler_->OnSessionAuthenticated(this)) {
235 // Create the desktop environment. Drop the connection if it could not be
236 // created for any reason (for instance the curtain could not initialize).
237 desktop_environment_ =
238 desktop_environment_factory_->Create(control_factory_.GetWeakPtr());
239 if (!desktop_environment_) {
244 host_capabilities_ = desktop_environment_->GetCapabilities();
246 // Ignore protocol::Capabilities messages from the client if it does not
247 // support any capabilities.
248 if (!connection_->session()->config().SupportsCapabilities()) {
249 VLOG(1) << "The client does not support any capabilities.";
251 client_capabilities_ = make_scoped_ptr(new std::string());
252 desktop_environment_->SetCapabilities(*client_capabilities_);
255 // Create the object that controls the screen resolution.
256 screen_controls_ = desktop_environment_->CreateScreenControls();
258 // Create the event executor.
259 input_injector_ = desktop_environment_->CreateInputInjector();
261 // Connect the host clipboard and input stubs.
262 host_input_filter_.set_input_stub(input_injector_.get());
263 clipboard_echo_filter_.set_host_stub(input_injector_.get());
265 // Create a VideoEncoder based on the session's video channel configuration.
266 scoped_ptr<VideoEncoder> video_encoder =
267 CreateVideoEncoder(connection_->session()->config());
269 // Create a VideoScheduler to pump frames from the capturer to the client.
270 video_scheduler_ = new VideoScheduler(
271 video_capture_task_runner_,
272 video_encode_task_runner_,
273 network_task_runner_,
274 desktop_environment_->CreateVideoCapturer(),
275 video_encoder.Pass(),
276 connection_->client_stub(),
277 &mouse_clamping_filter_);
279 // Create an AudioScheduler if audio is enabled, to pump audio samples.
280 if (connection_->session()->config().is_audio_enabled()) {
281 scoped_ptr<AudioEncoder> audio_encoder =
282 CreateAudioEncoder(connection_->session()->config());
283 audio_scheduler_ = new AudioScheduler(
285 network_task_runner_,
286 desktop_environment_->CreateAudioCapturer(),
287 audio_encoder.Pass(),
288 connection_->audio_stub());
292 void ClientSession::OnConnectionChannelsConnected(
293 protocol::ConnectionToClient* connection) {
294 DCHECK(CalledOnValidThread());
295 DCHECK_EQ(connection_.get(), connection);
297 // Negotiate capabilities with the client.
298 if (connection_->session()->config().SupportsCapabilities()) {
299 VLOG(1) << "Host capabilities: " << host_capabilities_;
301 protocol::Capabilities capabilities;
302 capabilities.set_capabilities(host_capabilities_);
303 connection_->client_stub()->SetCapabilities(capabilities);
306 // Start the event executor.
307 input_injector_->Start(CreateClipboardProxy());
308 SetDisableInputs(false);
310 // Start capturing the screen.
311 video_scheduler_->Start();
313 // Start recording audio.
314 if (connection_->session()->config().is_audio_enabled())
315 audio_scheduler_->Start();
317 // Notify the event handler that all our channels are now connected.
318 event_handler_->OnSessionChannelsConnected(this);
321 void ClientSession::OnConnectionClosed(
322 protocol::ConnectionToClient* connection,
323 protocol::ErrorCode error) {
324 DCHECK(CalledOnValidThread());
325 DCHECK_EQ(connection_.get(), connection);
327 // Ignore any further callbacks.
328 control_factory_.InvalidateWeakPtrs();
330 // If the client never authenticated then the session failed.
331 if (!auth_input_filter_.enabled())
332 event_handler_->OnSessionAuthenticationFailed(this);
334 // Block any further input events from the client.
335 // TODO(wez): Fix ChromotingHost::OnSessionClosed not to check our
336 // is_authenticated(), so that we can disable |auth_*_filter_| here.
337 disable_input_filter_.set_enabled(false);
338 disable_clipboard_filter_.set_enabled(false);
340 // Ensure that any pressed keys or buttons are released.
341 input_tracker_.ReleaseAll();
343 // Stop components access the client, audio or video stubs, which are no
344 // longer valid once ConnectionToClient calls OnConnectionClosed().
345 if (audio_scheduler_.get()) {
346 audio_scheduler_->Stop();
347 audio_scheduler_ = NULL;
349 if (video_scheduler_.get()) {
350 video_scheduler_->Stop();
351 video_scheduler_ = NULL;
354 client_clipboard_factory_.InvalidateWeakPtrs();
355 input_injector_.reset();
356 screen_controls_.reset();
357 desktop_environment_.reset();
359 // Notify the ChromotingHost that this client is disconnected.
360 // TODO(sergeyu): Log failure reason?
361 event_handler_->OnSessionClosed(this);
364 void ClientSession::OnSequenceNumberUpdated(
365 protocol::ConnectionToClient* connection, int64 sequence_number) {
366 DCHECK(CalledOnValidThread());
367 DCHECK_EQ(connection_.get(), connection);
369 if (video_scheduler_.get())
370 video_scheduler_->UpdateSequenceNumber(sequence_number);
372 event_handler_->OnSessionSequenceNumber(this, sequence_number);
375 void ClientSession::OnRouteChange(
376 protocol::ConnectionToClient* connection,
377 const std::string& channel_name,
378 const protocol::TransportRoute& route) {
379 DCHECK(CalledOnValidThread());
380 DCHECK_EQ(connection_.get(), connection);
381 event_handler_->OnSessionRouteChange(this, channel_name, route);
384 const std::string& ClientSession::client_jid() const {
388 void ClientSession::DisconnectSession() {
389 DCHECK(CalledOnValidThread());
390 DCHECK(connection_.get());
392 max_duration_timer_.Stop();
394 // This triggers OnConnectionClosed(), and the session may be destroyed
395 // as the result, so this call must be the last in this method.
396 connection_->Disconnect();
399 void ClientSession::OnLocalMouseMoved(const SkIPoint& position) {
400 DCHECK(CalledOnValidThread());
401 remote_input_filter_.LocalMouseMoved(position);
404 void ClientSession::SetDisableInputs(bool disable_inputs) {
405 DCHECK(CalledOnValidThread());
408 input_tracker_.ReleaseAll();
410 disable_input_filter_.set_enabled(!disable_inputs);
411 disable_clipboard_filter_.set_enabled(!disable_inputs);
414 scoped_ptr<protocol::ClipboardStub> ClientSession::CreateClipboardProxy() {
415 DCHECK(CalledOnValidThread());
417 return scoped_ptr<protocol::ClipboardStub>(
418 new protocol::ClipboardThreadProxy(
419 client_clipboard_factory_.GetWeakPtr(),
420 base::MessageLoopProxy::current()));
423 // TODO(sergeyu): Move this to SessionManager?
425 scoped_ptr<VideoEncoder> ClientSession::CreateVideoEncoder(
426 const protocol::SessionConfig& config) {
427 const protocol::ChannelConfig& video_config = config.video_config();
429 if (video_config.codec == protocol::ChannelConfig::CODEC_VP8) {
430 return remoting::VideoEncoderVpx::CreateForVP8().PassAs<VideoEncoder>();
434 return scoped_ptr<VideoEncoder>();
438 scoped_ptr<AudioEncoder> ClientSession::CreateAudioEncoder(
439 const protocol::SessionConfig& config) {
440 const protocol::ChannelConfig& audio_config = config.audio_config();
442 if (audio_config.codec == protocol::ChannelConfig::CODEC_VERBATIM) {
443 return scoped_ptr<AudioEncoder>(new AudioEncoderVerbatim());
444 } else if (audio_config.codec == protocol::ChannelConfig::CODEC_OPUS) {
445 return scoped_ptr<AudioEncoder>(new AudioEncoderOpus());
449 return scoped_ptr<AudioEncoder>();
452 } // namespace remoting