Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(116)

Side by Side Diff: remoting/host/client_session.cc

Issue 11761019: Tiny little refactoring of DesktopEnvironment. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « remoting/host/client_session.h ('k') | remoting/host/client_session_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/capturer/video_frame_capturer.h" 10 #include "remoting/capturer/video_frame_capturer.h"
11 #include "remoting/codec/audio_encoder.h" 11 #include "remoting/codec/audio_encoder.h"
12 #include "remoting/codec/audio_encoder_opus.h" 12 #include "remoting/codec/audio_encoder_opus.h"
13 #include "remoting/codec/audio_encoder_speex.h" 13 #include "remoting/codec/audio_encoder_speex.h"
14 #include "remoting/codec/audio_encoder_verbatim.h" 14 #include "remoting/codec/audio_encoder_verbatim.h"
15 #include "remoting/codec/video_encoder.h" 15 #include "remoting/codec/video_encoder.h"
16 #include "remoting/codec/video_encoder_verbatim.h" 16 #include "remoting/codec/video_encoder_verbatim.h"
17 #include "remoting/codec/video_encoder_vp8.h" 17 #include "remoting/codec/video_encoder_vp8.h"
18 #include "remoting/host/audio_scheduler.h"
19 #include "remoting/host/desktop_environment.h" 18 #include "remoting/host/desktop_environment.h"
20 #include "remoting/host/desktop_environment_factory.h" 19 #include "remoting/host/desktop_environment_factory.h"
21 #include "remoting/host/event_executor.h" 20 #include "remoting/host/event_executor.h"
22 #include "remoting/host/video_scheduler.h"
23 #include "remoting/proto/control.pb.h" 21 #include "remoting/proto/control.pb.h"
24 #include "remoting/proto/event.pb.h" 22 #include "remoting/proto/event.pb.h"
25 #include "remoting/protocol/client_stub.h" 23 #include "remoting/protocol/client_stub.h"
26 #include "remoting/protocol/clipboard_thread_proxy.h"
27 24
28 namespace remoting { 25 namespace remoting {
29 26
30 ClientSession::ClientSession( 27 ClientSession::ClientSession(
31 EventHandler* event_handler, 28 EventHandler* event_handler,
32 scoped_refptr<base::SingleThreadTaskRunner> audio_task_runner, 29 scoped_refptr<base::SingleThreadTaskRunner> audio_task_runner,
30 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner,
33 scoped_refptr<base::SingleThreadTaskRunner> video_capture_task_runner, 31 scoped_refptr<base::SingleThreadTaskRunner> video_capture_task_runner,
34 scoped_refptr<base::SingleThreadTaskRunner> video_encode_task_runner, 32 scoped_refptr<base::SingleThreadTaskRunner> video_encode_task_runner,
35 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner, 33 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner,
34 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
36 scoped_ptr<protocol::ConnectionToClient> connection, 35 scoped_ptr<protocol::ConnectionToClient> connection,
37 DesktopEnvironmentFactory* desktop_environment_factory, 36 DesktopEnvironmentFactory* desktop_environment_factory,
38 const base::TimeDelta& max_duration) 37 const base::TimeDelta& max_duration)
39 : event_handler_(event_handler), 38 : event_handler_(event_handler),
40 connection_(connection.Pass()), 39 connection_(connection.Pass()),
41 connection_factory_(connection_.get()), 40 connection_factory_(connection_.get()),
42 desktop_environment_(desktop_environment_factory->Create()),
43 client_jid_(connection_->session()->jid()), 41 client_jid_(connection_->session()->jid()),
44 host_clipboard_stub_(desktop_environment_->event_executor()), 42 desktop_environment_(desktop_environment_factory->Create(
45 host_input_stub_(desktop_environment_->event_executor()), 43 client_jid_,
44 base::Bind(&protocol::ConnectionToClient::Disconnect,
45 connection_factory_.GetWeakPtr()))),
46 host_clipboard_stub_(desktop_environment_.get()),
47 host_input_stub_(desktop_environment_.get()),
46 input_tracker_(host_input_stub_), 48 input_tracker_(host_input_stub_),
47 remote_input_filter_(&input_tracker_), 49 remote_input_filter_(&input_tracker_),
48 mouse_clamping_filter_(desktop_environment_->video_capturer(), 50 mouse_clamping_filter_(desktop_environment_.get(), &remote_input_filter_),
49 &remote_input_filter_),
50 disable_input_filter_(&mouse_clamping_filter_), 51 disable_input_filter_(&mouse_clamping_filter_),
51 disable_clipboard_filter_(clipboard_echo_filter_.host_filter()), 52 disable_clipboard_filter_(clipboard_echo_filter_.host_filter()),
52 auth_input_filter_(&disable_input_filter_), 53 auth_input_filter_(&disable_input_filter_),
53 auth_clipboard_filter_(&disable_clipboard_filter_), 54 auth_clipboard_filter_(&disable_clipboard_filter_),
54 client_clipboard_factory_(clipboard_echo_filter_.client_filter()),
55 max_duration_(max_duration), 55 max_duration_(max_duration),
56 audio_task_runner_(audio_task_runner), 56 audio_task_runner_(audio_task_runner),
57 input_task_runner_(input_task_runner),
57 video_capture_task_runner_(video_capture_task_runner), 58 video_capture_task_runner_(video_capture_task_runner),
58 video_encode_task_runner_(video_encode_task_runner), 59 video_encode_task_runner_(video_encode_task_runner),
59 network_task_runner_(network_task_runner), 60 network_task_runner_(network_task_runner),
60 active_recorders_(0) { 61 ui_task_runner_(ui_task_runner) {
61 connection_->SetEventHandler(this); 62 connection_->SetEventHandler(this);
62 63
63 // TODO(sergeyu): Currently ConnectionToClient expects stubs to be 64 // TODO(sergeyu): Currently ConnectionToClient expects stubs to be
64 // set before channels are connected. Make it possible to set stubs 65 // set before channels are connected. Make it possible to set stubs
65 // later and set them only when connection is authenticated. 66 // later and set them only when connection is authenticated.
66 connection_->set_clipboard_stub(&auth_clipboard_filter_); 67 connection_->set_clipboard_stub(&auth_clipboard_filter_);
67 connection_->set_host_stub(this); 68 connection_->set_host_stub(this);
68 connection_->set_input_stub(&auth_input_filter_); 69 connection_->set_input_stub(&auth_input_filter_);
69 clipboard_echo_filter_.set_host_stub(host_clipboard_stub_); 70 clipboard_echo_filter_.set_host_stub(host_clipboard_stub_);
70 71
71 // |auth_*_filter_|'s states reflect whether the session is authenticated. 72 // |auth_*_filter_|'s states reflect whether the session is authenticated.
72 auth_input_filter_.set_enabled(false); 73 auth_input_filter_.set_enabled(false);
73 auth_clipboard_filter_.set_enabled(false); 74 auth_clipboard_filter_.set_enabled(false);
74 } 75 }
75 76
76 void ClientSession::NotifyClientDimensions( 77 void ClientSession::NotifyClientDimensions(
77 const protocol::ClientDimensions& dimensions) { 78 const protocol::ClientDimensions& dimensions) {
78 if (dimensions.has_width() && dimensions.has_height()) { 79 if (dimensions.has_width() && dimensions.has_height()) {
79 VLOG(1) << "Received ClientDimensions (width=" 80 VLOG(1) << "Received ClientDimensions (width="
80 << dimensions.width() << ", height=" << dimensions.height() << ")"; 81 << dimensions.width() << ", height=" << dimensions.height() << ")";
81 event_handler_->OnClientDimensionsChanged( 82 event_handler_->OnClientDimensionsChanged(
82 this, SkISize::Make(dimensions.width(), dimensions.height())); 83 this, SkISize::Make(dimensions.width(), dimensions.height()));
83 } 84 }
84 } 85 }
85 86
86 void ClientSession::ControlVideo(const protocol::VideoControl& video_control) { 87 void ClientSession::ControlVideo(const protocol::VideoControl& video_control) {
87 if (video_control.has_enable()) { 88 if (video_control.has_enable()) {
88 VLOG(1) << "Received VideoControl (enable=" 89 VLOG(1) << "Received VideoControl (enable="
89 << video_control.enable() << ")"; 90 << video_control.enable() << ")";
90 if (video_scheduler_.get()) { 91 desktop_environment()->PauseVideo(!video_control.enable());
91 video_scheduler_->Pause(!video_control.enable());
92 }
93 } 92 }
94 } 93 }
95 94
96 void ClientSession::ControlAudio(const protocol::AudioControl& audio_control) { 95 void ClientSession::ControlAudio(const protocol::AudioControl& audio_control) {
97 if (audio_control.has_enable()) { 96 if (audio_control.has_enable()) {
98 VLOG(1) << "Received AudioControl (enable=" 97 VLOG(1) << "Received AudioControl (enable="
99 << audio_control.enable() << ")"; 98 << audio_control.enable() << ")";
100 if (audio_scheduler_.get()) { 99 desktop_environment()->PauseAudio(!audio_control.enable());
101 audio_scheduler_->SetEnabled(audio_control.enable());
102 }
103 } 100 }
104 } 101 }
105 102
106 void ClientSession::OnConnectionAuthenticated( 103 void ClientSession::OnConnectionAuthenticated(
107 protocol::ConnectionToClient* connection) { 104 protocol::ConnectionToClient* connection) {
108 DCHECK(CalledOnValidThread()); 105 DCHECK(CalledOnValidThread());
109 DCHECK_EQ(connection_.get(), connection); 106 DCHECK_EQ(connection_.get(), connection);
110 107
111 auth_input_filter_.set_enabled(true); 108 auth_input_filter_.set_enabled(true);
112 auth_clipboard_filter_.set_enabled(true); 109 auth_clipboard_filter_.set_enabled(true);
(...skipping 10 matching lines...) Expand all
123 event_handler_->OnSessionAuthenticated(this); 120 event_handler_->OnSessionAuthenticated(this);
124 } 121 }
125 122
126 void ClientSession::OnConnectionChannelsConnected( 123 void ClientSession::OnConnectionChannelsConnected(
127 protocol::ConnectionToClient* connection) { 124 protocol::ConnectionToClient* connection) {
128 DCHECK(CalledOnValidThread()); 125 DCHECK(CalledOnValidThread());
129 DCHECK_EQ(connection_.get(), connection); 126 DCHECK_EQ(connection_.get(), connection);
130 SetDisableInputs(false); 127 SetDisableInputs(false);
131 128
132 // Let the desktop environment notify us of local clipboard changes. 129 // Let the desktop environment notify us of local clipboard changes.
133 desktop_environment_->Start( 130 desktop_environment()->StartInput(input_task_runner_, ui_task_runner_,
134 CreateClipboardProxy(), 131 clipboard_echo_filter_.client_filter());
135 client_jid(),
136 base::Bind(&protocol::ConnectionToClient::Disconnect,
137 connection_factory_.GetWeakPtr()));
138 132
139 // Create a VideoEncoder based on the session's video channel configuration. 133 // Create a VideoEncoder based on the session's video channel configuration.
140 scoped_ptr<VideoEncoder> video_encoder = 134 scoped_ptr<VideoEncoder> video_encoder =
141 CreateVideoEncoder(connection_->session()->config()); 135 CreateVideoEncoder(connection_->session()->config());
142 136
143 // Create a VideoScheduler to pump frames from the capturer to the client. 137 // Create a VideoScheduler to pump frames from the capturer to the client.
144 video_scheduler_ = VideoScheduler::Create( 138 desktop_environment()->StartVideo(video_capture_task_runner_,
145 video_capture_task_runner_, 139 video_encode_task_runner_,
146 video_encode_task_runner_, 140 video_encoder.Pass(),
147 network_task_runner_, 141 connection_->client_stub(),
148 desktop_environment_->video_capturer(), 142 connection_->video_stub());
149 video_encoder.Pass(),
150 connection_->client_stub(),
151 connection_->video_stub());
152 ++active_recorders_;
153 143
154 // Create an AudioScheduler if audio is enabled, to pump audio samples. 144 // Create an AudioScheduler if audio is enabled, to pump audio samples.
155 if (connection_->session()->config().is_audio_enabled()) { 145 if (connection_->session()->config().is_audio_enabled()) {
156 scoped_ptr<AudioEncoder> audio_encoder = 146 scoped_ptr<AudioEncoder> audio_encoder =
157 CreateAudioEncoder(connection_->session()->config()); 147 CreateAudioEncoder(connection_->session()->config());
158 audio_scheduler_ = AudioScheduler::Create( 148 desktop_environment()->StartAudio(audio_task_runner_,
159 audio_task_runner_, 149 audio_encoder.Pass(),
160 network_task_runner_, 150 connection_->audio_stub());
161 desktop_environment_->audio_capturer(),
162 audio_encoder.Pass(),
163 connection_->audio_stub());
164 ++active_recorders_;
165 } 151 }
166 152
167 // Notify the event handler that all our channels are now connected. 153 // Notify the event handler that all our channels are now connected.
168 event_handler_->OnSessionChannelsConnected(this); 154 event_handler_->OnSessionChannelsConnected(this);
169 } 155 }
170 156
171 void ClientSession::OnConnectionClosed( 157 void ClientSession::OnConnectionClosed(
172 protocol::ConnectionToClient* connection, 158 protocol::ConnectionToClient* connection,
173 protocol::ErrorCode error) { 159 protocol::ErrorCode error) {
174 DCHECK(CalledOnValidThread()); 160 DCHECK(CalledOnValidThread());
(...skipping 10 matching lines...) Expand all
185 // TODO(wez): Fix ChromotingHost::OnSessionClosed not to check our 171 // TODO(wez): Fix ChromotingHost::OnSessionClosed not to check our
186 // is_authenticated(), so that we can disable |auth_*_filter_| here. 172 // is_authenticated(), so that we can disable |auth_*_filter_| here.
187 disable_input_filter_.set_enabled(false); 173 disable_input_filter_.set_enabled(false);
188 disable_clipboard_filter_.set_enabled(false); 174 disable_clipboard_filter_.set_enabled(false);
189 175
190 // Ensure that any pressed keys or buttons are released. 176 // Ensure that any pressed keys or buttons are released.
191 input_tracker_.ReleaseAll(); 177 input_tracker_.ReleaseAll();
192 178
193 // Stop components access the client, audio or video stubs, which are no 179 // Stop components access the client, audio or video stubs, which are no
194 // longer valid once ConnectionToClient calls OnConnectionClosed(). 180 // longer valid once ConnectionToClient calls OnConnectionClosed().
195 if (audio_scheduler_.get()) { 181 desktop_environment_.reset();
196 audio_scheduler_->Stop(base::Bind(&ClientSession::OnRecorderStopped, this));
197 audio_scheduler_ = NULL;
198 }
199 if (video_scheduler_.get()) {
200 video_scheduler_->Stop(base::Bind(&ClientSession::OnRecorderStopped, this));
201 video_scheduler_ = NULL;
202 }
203 client_clipboard_factory_.InvalidateWeakPtrs();
204 182
205 // Notify the ChromotingHost that this client is disconnected. 183 // Notify the ChromotingHost that this client is disconnected.
206 // TODO(sergeyu): Log failure reason? 184 // TODO(sergeyu): Log failure reason?
207 event_handler_->OnSessionClosed(this); 185 event_handler_->OnSessionClosed(this);
208 } 186 }
209 187
210 void ClientSession::OnSequenceNumberUpdated( 188 void ClientSession::OnSequenceNumberUpdated(
211 protocol::ConnectionToClient* connection, int64 sequence_number) { 189 protocol::ConnectionToClient* connection, int64 sequence_number) {
212 DCHECK(CalledOnValidThread()); 190 DCHECK(CalledOnValidThread());
213 DCHECK_EQ(connection_.get(), connection); 191 DCHECK_EQ(connection_.get(), connection);
214 192
215 if (video_scheduler_.get()) 193 desktop_environment()->UpdateSequenceNumber(sequence_number);
216 video_scheduler_->UpdateSequenceNumber(sequence_number);
217 194
218 event_handler_->OnSessionSequenceNumber(this, sequence_number); 195 event_handler_->OnSessionSequenceNumber(this, sequence_number);
219 } 196 }
220 197
221 void ClientSession::OnRouteChange( 198 void ClientSession::OnRouteChange(
222 protocol::ConnectionToClient* connection, 199 protocol::ConnectionToClient* connection,
223 const std::string& channel_name, 200 const std::string& channel_name,
224 const protocol::TransportRoute& route) { 201 const protocol::TransportRoute& route) {
225 DCHECK(CalledOnValidThread()); 202 DCHECK(CalledOnValidThread());
226 DCHECK_EQ(connection_.get(), connection); 203 DCHECK_EQ(connection_.get(), connection);
227 event_handler_->OnSessionRouteChange(this, channel_name, route); 204 event_handler_->OnSessionRouteChange(this, channel_name, route);
228 } 205 }
229 206
230 void ClientSession::Disconnect() { 207 void ClientSession::Disconnect() {
231 DCHECK(CalledOnValidThread()); 208 DCHECK(CalledOnValidThread());
232 DCHECK(connection_.get()); 209 DCHECK(connection_.get());
233 210
234 max_duration_timer_.Stop(); 211 max_duration_timer_.Stop();
235 212
236 // This triggers OnConnectionClosed(), and the session may be destroyed 213 // This triggers OnConnectionClosed(), and the session may be destroyed
237 // as the result, so this call must be the last in this method. 214 // as the result, so this call must be the last in this method.
238 connection_->Disconnect(); 215 connection_->Disconnect();
239 } 216 }
240 217
241 void ClientSession::Stop(const base::Closure& stopped_task) { 218 void ClientSession::Stop() {
242 DCHECK(CalledOnValidThread()); 219 DCHECK(CalledOnValidThread());
243 DCHECK(stopped_task_.is_null()); 220 DCHECK(!desktop_environment_);
244 DCHECK(!stopped_task.is_null());
245 DCHECK(audio_scheduler_.get() == NULL);
246 DCHECK(video_scheduler_.get() == NULL);
247 221
248 stopped_task_ = stopped_task; 222 connection_.reset();
249
250 if (active_recorders_ == 0) {
251 // |stopped_task_| may tear down the signalling layer, so tear-down
252 // |connection_| before invoking it.
253 connection_.reset();
254 stopped_task_.Run();
255 }
256 } 223 }
257 224
258 void ClientSession::LocalMouseMoved(const SkIPoint& mouse_pos) { 225 void ClientSession::LocalMouseMoved(const SkIPoint& mouse_pos) {
259 DCHECK(CalledOnValidThread()); 226 DCHECK(CalledOnValidThread());
260 remote_input_filter_.LocalMouseMoved(mouse_pos); 227 remote_input_filter_.LocalMouseMoved(mouse_pos);
261 } 228 }
262 229
263 void ClientSession::SetDisableInputs(bool disable_inputs) { 230 void ClientSession::SetDisableInputs(bool disable_inputs) {
264 DCHECK(CalledOnValidThread()); 231 DCHECK(CalledOnValidThread());
265 232
266 if (disable_inputs) 233 if (disable_inputs)
267 input_tracker_.ReleaseAll(); 234 input_tracker_.ReleaseAll();
268 235
269 disable_input_filter_.set_enabled(!disable_inputs); 236 disable_input_filter_.set_enabled(!disable_inputs);
270 disable_clipboard_filter_.set_enabled(!disable_inputs); 237 disable_clipboard_filter_.set_enabled(!disable_inputs);
271 } 238 }
272 239
273 ClientSession::~ClientSession() { 240 ClientSession::~ClientSession() {
274 DCHECK_EQ(active_recorders_, 0); 241 DCHECK(!desktop_environment_);
275 DCHECK(audio_scheduler_.get() == NULL);
276 DCHECK(video_scheduler_.get() == NULL);
277 }
278
279 scoped_ptr<protocol::ClipboardStub> ClientSession::CreateClipboardProxy() {
280 DCHECK(CalledOnValidThread());
281
282 return scoped_ptr<protocol::ClipboardStub>(
283 new protocol::ClipboardThreadProxy(
284 client_clipboard_factory_.GetWeakPtr(),
285 base::MessageLoopProxy::current()));
286 }
287
288 void ClientSession::OnRecorderStopped() {
289 if (!network_task_runner_->BelongsToCurrentThread()) {
290 network_task_runner_->PostTask(
291 FROM_HERE, base::Bind(&ClientSession::OnRecorderStopped, this));
292 return;
293 }
294
295 --active_recorders_;
296 DCHECK_GE(active_recorders_, 0);
297
298 DCHECK(!stopped_task_.is_null());
299 if (active_recorders_ == 0) {
300 // |stopped_task_| may result in the signalling layer being torn down, so
301 // tear down the ConnectionToClient first.
302 connection_.reset();
303 stopped_task_.Run();
304 }
305 } 242 }
306 243
307 // TODO(sergeyu): Move this to SessionManager? 244 // TODO(sergeyu): Move this to SessionManager?
308 // static 245 // static
309 scoped_ptr<VideoEncoder> ClientSession::CreateVideoEncoder( 246 scoped_ptr<VideoEncoder> ClientSession::CreateVideoEncoder(
310 const protocol::SessionConfig& config) { 247 const protocol::SessionConfig& config) {
311 const protocol::ChannelConfig& video_config = config.video_config(); 248 const protocol::ChannelConfig& video_config = config.video_config();
312 249
313 if (video_config.codec == protocol::ChannelConfig::CODEC_VERBATIM) { 250 if (video_config.codec == protocol::ChannelConfig::CODEC_VERBATIM) {
314 return scoped_ptr<VideoEncoder>(new remoting::VideoEncoderVerbatim()); 251 return scoped_ptr<VideoEncoder>(new remoting::VideoEncoderVerbatim());
(...skipping 21 matching lines...) Expand all
336 NOTIMPLEMENTED(); 273 NOTIMPLEMENTED();
337 return scoped_ptr<AudioEncoder>(NULL); 274 return scoped_ptr<AudioEncoder>(NULL);
338 } 275 }
339 276
340 // static 277 // static
341 void ClientSessionTraits::Destruct(const ClientSession* client) { 278 void ClientSessionTraits::Destruct(const ClientSession* client) {
342 client->network_task_runner_->DeleteSoon(FROM_HERE, client); 279 client->network_task_runner_->DeleteSoon(FROM_HERE, client);
343 } 280 }
344 281
345 } // namespace remoting 282 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/host/client_session.h ('k') | remoting/host/client_session_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698