OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/host/client_session.h" | 5 #include "remoting/host/client_session.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/message_loop_proxy.h" | 9 #include "base/message_loop_proxy.h" |
| 10 #include "remoting/base/encoder.h" |
| 11 #include "remoting/base/encoder_row_based.h" |
| 12 #include "remoting/base/encoder_vp8.h" |
| 13 #include "remoting/codec/audio_encoder.h" |
| 14 #include "remoting/codec/audio_encoder_verbatim.h" |
| 15 #include "remoting/host/audio_capturer.h" |
| 16 #include "remoting/host/audio_scheduler.h" |
| 17 #include "remoting/host/desktop_environment.h" |
| 18 #include "remoting/host/event_executor.h" |
| 19 #include "remoting/host/screen_recorder.h" |
10 #include "remoting/host/video_frame_capturer.h" | 20 #include "remoting/host/video_frame_capturer.h" |
11 #include "remoting/proto/control.pb.h" | 21 #include "remoting/proto/control.pb.h" |
12 #include "remoting/proto/event.pb.h" | 22 #include "remoting/proto/event.pb.h" |
13 #include "remoting/protocol/client_stub.h" | 23 #include "remoting/protocol/client_stub.h" |
14 #include "remoting/protocol/clipboard_thread_proxy.h" | 24 #include "remoting/protocol/clipboard_thread_proxy.h" |
15 | 25 |
16 namespace remoting { | 26 namespace remoting { |
17 | 27 |
18 ClientSession::ClientSession( | 28 ClientSession::ClientSession( |
19 EventHandler* event_handler, | 29 EventHandler* event_handler, |
| 30 scoped_refptr<base::SingleThreadTaskRunner> capture_task_runner, |
| 31 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner, |
| 32 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner, |
20 scoped_ptr<protocol::ConnectionToClient> connection, | 33 scoped_ptr<protocol::ConnectionToClient> connection, |
21 protocol::ClipboardStub* host_clipboard_stub, | 34 scoped_ptr<DesktopEnvironment> desktop_environment, |
22 protocol::InputStub* host_input_stub, | |
23 VideoFrameCapturer* capturer, | |
24 const base::TimeDelta& max_duration) | 35 const base::TimeDelta& max_duration) |
25 : event_handler_(event_handler), | 36 : event_handler_(event_handler), |
26 connection_(connection.Pass()), | 37 connection_(connection.Pass()), |
| 38 desktop_environment_(desktop_environment.Pass()), |
27 client_jid_(connection_->session()->jid()), | 39 client_jid_(connection_->session()->jid()), |
28 is_authenticated_(false), | 40 is_authenticated_(false), |
29 host_clipboard_stub_(host_clipboard_stub), | 41 host_clipboard_stub_(desktop_environment_->event_executor()), |
30 host_input_stub_(host_input_stub), | 42 host_input_stub_(desktop_environment_->event_executor()), |
31 input_tracker_(host_input_stub_), | 43 input_tracker_(host_input_stub_), |
32 remote_input_filter_(&input_tracker_), | 44 remote_input_filter_(&input_tracker_), |
33 mouse_input_filter_(&remote_input_filter_), | 45 mouse_input_filter_(&remote_input_filter_), |
34 client_clipboard_factory_(clipboard_echo_filter_.client_filter()), | 46 client_clipboard_factory_(clipboard_echo_filter_.client_filter()), |
35 capturer_(capturer), | 47 capturer_(desktop_environment_->video_capturer()), |
36 max_duration_(max_duration) { | 48 max_duration_(max_duration), |
| 49 capture_task_runner_(capture_task_runner), |
| 50 encode_task_runner_(encode_task_runner), |
| 51 network_task_runner_(network_task_runner), |
| 52 active_recorders_(0) { |
37 connection_->SetEventHandler(this); | 53 connection_->SetEventHandler(this); |
38 | 54 |
39 // TODO(sergeyu): Currently ConnectionToClient expects stubs to be | 55 // TODO(sergeyu): Currently ConnectionToClient expects stubs to be |
40 // set before channels are connected. Make it possible to set stubs | 56 // set before channels are connected. Make it possible to set stubs |
41 // later and set them only when connection is authenticated. | 57 // later and set them only when connection is authenticated. |
42 connection_->set_clipboard_stub(&auth_clipboard_filter_); | 58 connection_->set_clipboard_stub(&auth_clipboard_filter_); |
43 connection_->set_host_stub(this); | 59 connection_->set_host_stub(this); |
44 connection_->set_input_stub(this); | 60 connection_->set_input_stub(this); |
45 clipboard_echo_filter_.set_host_stub(host_clipboard_stub_); | 61 clipboard_echo_filter_.set_host_stub(host_clipboard_stub_); |
46 } | 62 } |
47 | 63 |
48 ClientSession::~ClientSession() { | 64 ClientSession::~ClientSession() { |
| 65 DCHECK(desktop_environment_.get() == NULL); |
49 } | 66 } |
50 | 67 |
51 void ClientSession::InjectKeyEvent(const protocol::KeyEvent& event) { | 68 void ClientSession::InjectKeyEvent(const protocol::KeyEvent& event) { |
52 DCHECK(CalledOnValidThread()); | 69 DCHECK(CalledOnValidThread()); |
53 auth_input_filter_.InjectKeyEvent(event); | 70 auth_input_filter_.InjectKeyEvent(event); |
54 } | 71 } |
55 | 72 |
56 void ClientSession::InjectMouseEvent(const protocol::MouseEvent& event) { | 73 void ClientSession::InjectMouseEvent(const protocol::MouseEvent& event) { |
57 DCHECK(CalledOnValidThread()); | 74 DCHECK(CalledOnValidThread()); |
58 | 75 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 } | 117 } |
101 | 118 |
102 event_handler_->OnSessionAuthenticated(this); | 119 event_handler_->OnSessionAuthenticated(this); |
103 } | 120 } |
104 | 121 |
105 void ClientSession::OnConnectionChannelsConnected( | 122 void ClientSession::OnConnectionChannelsConnected( |
106 protocol::ConnectionToClient* connection) { | 123 protocol::ConnectionToClient* connection) { |
107 DCHECK(CalledOnValidThread()); | 124 DCHECK(CalledOnValidThread()); |
108 DCHECK_EQ(connection_.get(), connection); | 125 DCHECK_EQ(connection_.get(), connection); |
109 SetDisableInputs(false); | 126 SetDisableInputs(false); |
| 127 |
| 128 // Create a ScreenRecorder passing the message loops that it should run on. |
| 129 Encoder* encoder = CreateEncoder(connection_->session()->config()); |
| 130 video_recorder_ = new ScreenRecorder( |
| 131 capture_task_runner_, |
| 132 encode_task_runner_, |
| 133 network_task_runner_, |
| 134 desktop_environment_->video_capturer(), |
| 135 encoder); |
| 136 ++active_recorders_; |
| 137 |
| 138 if (connection_->session()->config().is_audio_enabled()) { |
| 139 scoped_ptr<AudioEncoder> audio_encoder = |
| 140 CreateAudioEncoder(connection_->session()->config()); |
| 141 audio_scheduler_ = new AudioScheduler( |
| 142 capture_task_runner_, |
| 143 network_task_runner_, |
| 144 desktop_environment_->audio_capturer(), |
| 145 audio_encoder.Pass(), |
| 146 connection_->audio_stub()); |
| 147 ++active_recorders_; |
| 148 } |
| 149 |
| 150 // Start the session. |
| 151 video_recorder_->AddConnection(connection_.get()); |
| 152 video_recorder_->Start(); |
| 153 desktop_environment_->Start(CreateClipboardProxy()); |
| 154 |
110 event_handler_->OnSessionChannelsConnected(this); | 155 event_handler_->OnSessionChannelsConnected(this); |
111 } | 156 } |
112 | 157 |
113 void ClientSession::OnConnectionClosed( | 158 void ClientSession::OnConnectionClosed( |
114 protocol::ConnectionToClient* connection, | 159 protocol::ConnectionToClient* connection, |
115 protocol::ErrorCode error) { | 160 protocol::ErrorCode error) { |
116 DCHECK(CalledOnValidThread()); | 161 DCHECK(CalledOnValidThread()); |
117 DCHECK_EQ(connection_.get(), connection); | 162 DCHECK_EQ(connection_.get(), connection); |
118 if (!is_authenticated_) | 163 if (!is_authenticated_) |
119 event_handler_->OnSessionAuthenticationFailed(this); | 164 event_handler_->OnSessionAuthenticationFailed(this); |
120 auth_input_filter_.set_input_stub(NULL); | 165 auth_input_filter_.set_input_stub(NULL); |
121 auth_clipboard_filter_.set_clipboard_stub(NULL); | 166 auth_clipboard_filter_.set_clipboard_stub(NULL); |
122 | 167 |
123 // Ensure that any pressed keys or buttons are released. | 168 // Ensure that any pressed keys or buttons are released. |
124 input_tracker_.ReleaseAll(); | 169 input_tracker_.ReleaseAll(); |
125 | 170 |
126 // TODO(sergeyu): Log failure reason? | 171 // TODO(sergeyu): Log failure reason? |
127 event_handler_->OnSessionClosed(this); | 172 event_handler_->OnSessionClosed(this); |
128 } | 173 } |
129 | 174 |
130 void ClientSession::OnSequenceNumberUpdated( | 175 void ClientSession::OnSequenceNumberUpdated( |
131 protocol::ConnectionToClient* connection, int64 sequence_number) { | 176 protocol::ConnectionToClient* connection, int64 sequence_number) { |
132 DCHECK(CalledOnValidThread()); | 177 DCHECK(CalledOnValidThread()); |
133 DCHECK_EQ(connection_.get(), connection); | 178 DCHECK_EQ(connection_.get(), connection); |
| 179 |
| 180 if (video_recorder_.get()) |
| 181 video_recorder_->UpdateSequenceNumber(sequence_number); |
| 182 |
134 event_handler_->OnSessionSequenceNumber(this, sequence_number); | 183 event_handler_->OnSessionSequenceNumber(this, sequence_number); |
135 } | 184 } |
136 | 185 |
137 void ClientSession::OnRouteChange( | 186 void ClientSession::OnRouteChange( |
138 protocol::ConnectionToClient* connection, | 187 protocol::ConnectionToClient* connection, |
139 const std::string& channel_name, | 188 const std::string& channel_name, |
140 const protocol::TransportRoute& route) { | 189 const protocol::TransportRoute& route) { |
141 DCHECK(CalledOnValidThread()); | 190 DCHECK(CalledOnValidThread()); |
142 DCHECK_EQ(connection_.get(), connection); | 191 DCHECK_EQ(connection_.get(), connection); |
143 event_handler_->OnSessionRouteChange(this, channel_name, route); | 192 event_handler_->OnSessionRouteChange(this, channel_name, route); |
144 } | 193 } |
145 | 194 |
146 void ClientSession::Disconnect() { | 195 void ClientSession::Disconnect() { |
147 DCHECK(CalledOnValidThread()); | 196 DCHECK(CalledOnValidThread()); |
148 DCHECK(connection_.get()); | 197 DCHECK(connection_.get()); |
149 | 198 |
150 max_duration_timer_.Stop(); | 199 max_duration_timer_.Stop(); |
151 // This triggers OnConnectionClosed(), and the session may be destroyed | 200 // This triggers OnConnectionClosed(), and the session may be destroyed |
152 // as the result, so this call must be the last in this method. | 201 // as the result, so this call must be the last in this method. |
153 connection_->Disconnect(); | 202 connection_->Disconnect(); |
154 } | 203 } |
155 | 204 |
| 205 void ClientSession::StopAndDelete() { |
| 206 DCHECK(CalledOnValidThread()); |
| 207 |
| 208 if (audio_scheduler_.get()) { |
| 209 audio_scheduler_->OnClientDisconnected(); |
| 210 StopAudioScheduler(); |
| 211 } |
| 212 |
| 213 if (video_recorder_.get()) { |
| 214 video_recorder_->RemoveConnection(connection_.get()); |
| 215 StopScreenRecorder(); |
| 216 } |
| 217 |
| 218 if (!active_recorders_) { |
| 219 delete this; |
| 220 } |
| 221 } |
| 222 |
156 void ClientSession::LocalMouseMoved(const SkIPoint& mouse_pos) { | 223 void ClientSession::LocalMouseMoved(const SkIPoint& mouse_pos) { |
157 DCHECK(CalledOnValidThread()); | 224 DCHECK(CalledOnValidThread()); |
158 remote_input_filter_.LocalMouseMoved(mouse_pos); | 225 remote_input_filter_.LocalMouseMoved(mouse_pos); |
159 } | 226 } |
160 | 227 |
161 void ClientSession::SetDisableInputs(bool disable_inputs) { | 228 void ClientSession::SetDisableInputs(bool disable_inputs) { |
162 DCHECK(CalledOnValidThread()); | 229 DCHECK(CalledOnValidThread()); |
163 | 230 |
164 if (disable_inputs) { | 231 if (disable_inputs) { |
165 disable_input_filter_.set_input_stub(NULL); | 232 disable_input_filter_.set_input_stub(NULL); |
166 disable_clipboard_filter_.set_clipboard_stub(NULL); | 233 disable_clipboard_filter_.set_clipboard_stub(NULL); |
167 input_tracker_.ReleaseAll(); | 234 input_tracker_.ReleaseAll(); |
168 } else { | 235 } else { |
169 disable_input_filter_.set_input_stub(&mouse_input_filter_); | 236 disable_input_filter_.set_input_stub(&mouse_input_filter_); |
170 disable_clipboard_filter_.set_clipboard_stub( | 237 disable_clipboard_filter_.set_clipboard_stub( |
171 clipboard_echo_filter_.host_filter()); | 238 clipboard_echo_filter_.host_filter()); |
172 } | 239 } |
173 } | 240 } |
174 | 241 |
175 scoped_ptr<protocol::ClipboardStub> ClientSession::CreateClipboardProxy() { | 242 scoped_ptr<protocol::ClipboardStub> ClientSession::CreateClipboardProxy() { |
176 DCHECK(CalledOnValidThread()); | 243 DCHECK(CalledOnValidThread()); |
177 | 244 |
178 return scoped_ptr<protocol::ClipboardStub>( | 245 return scoped_ptr<protocol::ClipboardStub>( |
179 new protocol::ClipboardThreadProxy( | 246 new protocol::ClipboardThreadProxy( |
180 client_clipboard_factory_.GetWeakPtr(), | 247 client_clipboard_factory_.GetWeakPtr(), |
181 base::MessageLoopProxy::current())); | 248 base::MessageLoopProxy::current())); |
182 } | 249 } |
183 | 250 |
| 251 // TODO(sergeyu): Move this to SessionManager? |
| 252 // static |
| 253 Encoder* ClientSession::CreateEncoder(const protocol::SessionConfig& config) { |
| 254 const protocol::ChannelConfig& video_config = config.video_config(); |
| 255 |
| 256 if (video_config.codec == protocol::ChannelConfig::CODEC_VERBATIM) { |
| 257 return EncoderRowBased::CreateVerbatimEncoder(); |
| 258 } else if (video_config.codec == protocol::ChannelConfig::CODEC_ZIP) { |
| 259 return EncoderRowBased::CreateZlibEncoder(); |
| 260 } else if (video_config.codec == protocol::ChannelConfig::CODEC_VP8) { |
| 261 return new remoting::EncoderVp8(); |
| 262 } |
| 263 |
| 264 return NULL; |
| 265 } |
| 266 |
| 267 // static |
| 268 scoped_ptr<AudioEncoder> ClientSession::CreateAudioEncoder( |
| 269 const protocol::SessionConfig& config) { |
| 270 const protocol::ChannelConfig& audio_config = config.audio_config(); |
| 271 |
| 272 if (audio_config.codec == protocol::ChannelConfig::CODEC_VERBATIM) { |
| 273 return scoped_ptr<AudioEncoder>(new AudioEncoderVerbatim()); |
| 274 } |
| 275 |
| 276 NOTIMPLEMENTED(); |
| 277 return scoped_ptr<AudioEncoder>(NULL); |
| 278 } |
| 279 |
| 280 void ClientSession::OnRecorderStopped() { |
| 281 if (!network_task_runner_->BelongsToCurrentThread()) { |
| 282 network_task_runner_->PostTask( |
| 283 FROM_HERE, base::Bind(&ClientSession::OnRecorderStopped, |
| 284 base::Unretained(this))); |
| 285 return; |
| 286 } |
| 287 |
| 288 --active_recorders_; |
| 289 DCHECK_GE(active_recorders_, 0); |
| 290 |
| 291 StopAndDelete(); |
| 292 } |
| 293 |
| 294 void ClientSession::StopAudioScheduler() { |
| 295 DCHECK(CalledOnValidThread()); |
| 296 DCHECK(audio_scheduler_.get()); |
| 297 |
| 298 scoped_refptr<AudioScheduler> audio_scheduler = audio_scheduler_; |
| 299 audio_scheduler_ = NULL; |
| 300 audio_scheduler->Stop(base::Bind(&ClientSession::OnRecorderStopped, |
| 301 base::Unretained(this))); |
| 302 } |
| 303 |
| 304 void ClientSession::StopScreenRecorder() { |
| 305 DCHECK(CalledOnValidThread()); |
| 306 DCHECK(video_recorder_.get()); |
| 307 |
| 308 scoped_refptr<ScreenRecorder> video_recorder = video_recorder_; |
| 309 video_recorder_ = NULL; |
| 310 video_recorder->Stop(base::Bind(&ClientSession::OnRecorderStopped, |
| 311 base::Unretained(this))); |
| 312 } |
| 313 |
184 } // namespace remoting | 314 } // namespace remoting |
OLD | NEW |