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

Side by Side Diff: remoting/host/desktop_session_proxy.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/desktop_session_proxy.h ('k') | remoting/host/host_mock_objects.h » ('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/desktop_session_proxy.h" 5 #include "remoting/host/desktop_session_proxy.h"
6 6
7 #include "base/compiler_specific.h" 7 #include "base/compiler_specific.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/platform_file.h" 9 #include "base/platform_file.h"
10 #include "base/single_thread_task_runner.h" 10 #include "base/single_thread_task_runner.h"
11 #include "ipc/ipc_channel_proxy.h" 11 #include "ipc/ipc_channel_proxy.h"
12 #include "ipc/ipc_message_macros.h" 12 #include "ipc/ipc_message_macros.h"
13 #include "remoting/capturer/capture_data.h" 13 #include "remoting/capturer/capture_data.h"
14 #include "remoting/host/audio_capturer.h" 14 #include "remoting/codec/audio_encoder.h"
15 #include "remoting/codec/video_encoder.h"
16 #include "remoting/host/audio_scheduler.h"
15 #include "remoting/host/chromoting_messages.h" 17 #include "remoting/host/chromoting_messages.h"
16 #include "remoting/host/client_session.h" 18 #include "remoting/host/client_session.h"
17 #include "remoting/host/ipc_audio_capturer.h" 19 #include "remoting/host/ipc_audio_capturer.h"
18 #include "remoting/host/ipc_video_frame_capturer.h" 20 #include "remoting/host/ipc_video_frame_capturer.h"
21 #include "remoting/host/video_scheduler.h"
19 #include "remoting/proto/audio.pb.h" 22 #include "remoting/proto/audio.pb.h"
20 #include "remoting/proto/control.pb.h" 23 #include "remoting/proto/control.pb.h"
21 #include "remoting/proto/event.pb.h" 24 #include "remoting/proto/event.pb.h"
22 25
23 #if defined(OS_WIN) 26 #if defined(OS_WIN)
24 #include "base/win/scoped_handle.h" 27 #include "base/win/scoped_handle.h"
25 #endif // defined(OS_WIN) 28 #endif // defined(OS_WIN)
26 29
27 namespace remoting { 30 namespace remoting {
28 31
29 DesktopSessionProxy::DesktopSessionProxy( 32 DesktopSessionProxy::DesktopSessionProxy(
30 scoped_refptr<base::SingleThreadTaskRunner> audio_capture_task_runner,
31 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, 33 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner,
32 scoped_refptr<base::SingleThreadTaskRunner> video_capture_task_runner) 34 const std::string& client_jid,
33 : audio_capture_task_runner_(audio_capture_task_runner), 35 const base::Closure& disconnect_callback)
34 caller_task_runner_(caller_task_runner), 36 : caller_task_runner_(caller_task_runner),
35 video_capture_task_runner_(video_capture_task_runner), 37 clipboard_stub_(NULL),
36 audio_capturer_(NULL), 38 client_jid_(client_jid),
39 disconnect_callback_(disconnect_callback),
37 pending_capture_frame_requests_(0), 40 pending_capture_frame_requests_(0),
38 video_capturer_(NULL) { 41 video_capturer_(NULL) {
39 DCHECK(caller_task_runner_->BelongsToCurrentThread()); 42 DCHECK(caller_task_runner_->BelongsToCurrentThread());
43 DCHECK(!client_jid_.empty());
44 DCHECK(!disconnect_callback_.is_null());
45 }
46
47 void DesktopSessionProxy::InjectClipboardEvent(
48 const protocol::ClipboardEvent& event) {
49 DCHECK(caller_task_runner_->BelongsToCurrentThread());
50
51 std::string serialized_event;
52 if (!event.SerializeToString(&serialized_event)) {
53 LOG(ERROR) << "Failed to serialize protocol::ClipboardEvent.";
54 return;
55 }
56
57 SendToDesktop(
58 new ChromotingNetworkDesktopMsg_InjectClipboardEvent(serialized_event));
59 }
60
61 void DesktopSessionProxy::InjectKeyEvent(const protocol::KeyEvent& event) {
62 DCHECK(caller_task_runner_->BelongsToCurrentThread());
63
64 std::string serialized_event;
65 if (!event.SerializeToString(&serialized_event)) {
66 LOG(ERROR) << "Failed to serialize protocol::KeyEvent.";
67 return;
68 }
69
70 SendToDesktop(
71 new ChromotingNetworkDesktopMsg_InjectKeyEvent(serialized_event));
72 }
73
74 void DesktopSessionProxy::InjectMouseEvent(
75 const protocol::MouseEvent& event) {
76 DCHECK(caller_task_runner_->BelongsToCurrentThread());
77
78 std::string serialized_event;
79 if (!event.SerializeToString(&serialized_event)) {
80 LOG(ERROR) << "Failed to serialize protocol::MouseEvent.";
81 return;
82 }
83
84 SendToDesktop(
85 new ChromotingNetworkDesktopMsg_InjectMouseEvent(serialized_event));
86 }
87
88 void DesktopSessionProxy::StartAudio(
89 scoped_refptr<base::SingleThreadTaskRunner> audio_task_runner,
90 scoped_ptr<AudioEncoder> audio_encoder,
91 protocol::AudioStub* audio_stub) {
92 DCHECK(caller_task_runner_->BelongsToCurrentThread());
93 DCHECK(!audio_scheduler_.get());
94 DCHECK(!audio_capture_task_runner_.get());
95
96 audio_capture_task_runner_ = audio_task_runner;
97
98 // Create and start the audio scheduler. The |audio_stub| reference is cleared
99 // by Stop().
100 scoped_ptr<AudioCapturer> audio_capturer(new IpcAudioCapturer(this));
101 audio_scheduler_ = AudioScheduler::Create(audio_task_runner,
102 caller_task_runner_,
103 audio_capturer.Pass(),
104 audio_encoder.Pass(),
105 audio_stub);
106 }
107
108 void DesktopSessionProxy::PauseAudio(bool pause) {
109 DCHECK(caller_task_runner_->BelongsToCurrentThread());
110
111 if (audio_scheduler_.get())
112 audio_scheduler_->Pause(pause);
113 }
114
115 void DesktopSessionProxy::StartInput(
116 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner,
117 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
118 protocol::ClipboardStub* clipboard_stub) {
119 DCHECK(caller_task_runner_->BelongsToCurrentThread());
120 DCHECK(!clipboard_stub_);
121 DCHECK(clipboard_stub);
122
123 // |clipboard_stub_| reference is cleared by Stop().
124 clipboard_stub_ = clipboard_stub;
125 }
126
127 void DesktopSessionProxy::StartVideo(
128 scoped_refptr<base::SingleThreadTaskRunner> capture_task_runner,
129 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner,
130 scoped_ptr<VideoEncoder> video_encoder,
131 protocol::CursorShapeStub* cursor_shape_stub,
132 protocol::VideoStub* video_stub) {
133 DCHECK(caller_task_runner_->BelongsToCurrentThread());
134 DCHECK(!video_scheduler_.get());
135 DCHECK(!video_capture_task_runner_.get());
136
137 video_capture_task_runner_ = capture_task_runner;
138
139 // Create and start video scheduler. The |cursor_shape_stub| and |video_stub|
140 // references are cleared by Stop().
141 scoped_ptr<VideoFrameCapturer> video_capturer(
142 new IpcVideoFrameCapturer(this));
143 video_scheduler_ = VideoScheduler::Create(capture_task_runner,
144 encode_task_runner,
145 caller_task_runner_,
146 video_capturer.Pass(),
147 video_encoder.Pass(),
148 cursor_shape_stub,
149 video_stub);
150 }
151
152 SkISize DesktopSessionProxy::GetScreenSize() const {
153 if (video_scheduler_.get())
154 return video_scheduler_->size_most_recent();
155 else
156 return SkISize::Make(0, 0);
157 }
158
159 void DesktopSessionProxy::PauseVideo(bool pause) {
160 DCHECK(caller_task_runner_->BelongsToCurrentThread());
161
162 if (video_scheduler_.get())
163 video_scheduler_->Pause(pause);
164 }
165
166 void DesktopSessionProxy::UpdateSequenceNumber(int64 sequence_number) {
167 DCHECK(caller_task_runner_->BelongsToCurrentThread());
168
169 if (video_scheduler_.get())
170 video_scheduler_->UpdateSequenceNumber(sequence_number);
40 } 171 }
41 172
42 bool DesktopSessionProxy::OnMessageReceived(const IPC::Message& message) { 173 bool DesktopSessionProxy::OnMessageReceived(const IPC::Message& message) {
43 DCHECK(caller_task_runner_->BelongsToCurrentThread()); 174 DCHECK(caller_task_runner_->BelongsToCurrentThread());
44 175
45 bool handled = true; 176 bool handled = true;
46 IPC_BEGIN_MESSAGE_MAP(DesktopSessionProxy, message) 177 IPC_BEGIN_MESSAGE_MAP(DesktopSessionProxy, message)
47 IPC_MESSAGE_HANDLER(ChromotingDesktopNetworkMsg_AudioPacket, 178 IPC_MESSAGE_HANDLER(ChromotingDesktopNetworkMsg_AudioPacket,
48 OnAudioPacket) 179 OnAudioPacket)
49 IPC_MESSAGE_HANDLER(ChromotingDesktopNetworkMsg_CaptureCompleted, 180 IPC_MESSAGE_HANDLER(ChromotingDesktopNetworkMsg_CaptureCompleted,
(...skipping 18 matching lines...) Expand all
68 199
69 VLOG(1) << "IPC: network <- desktop (" << peer_pid << ")"; 200 VLOG(1) << "IPC: network <- desktop (" << peer_pid << ")";
70 } 201 }
71 202
72 void DesktopSessionProxy::OnChannelError() { 203 void DesktopSessionProxy::OnChannelError() {
73 DCHECK(caller_task_runner_->BelongsToCurrentThread()); 204 DCHECK(caller_task_runner_->BelongsToCurrentThread());
74 205
75 DetachFromDesktop(); 206 DetachFromDesktop();
76 } 207 }
77 208
78 void DesktopSessionProxy::Initialize(const std::string& client_jid, 209 void DesktopSessionProxy::Stop() {
79 const base::Closure& disconnect_callback) {
80 DCHECK(caller_task_runner_->BelongsToCurrentThread()); 210 DCHECK(caller_task_runner_->BelongsToCurrentThread());
81 DCHECK(client_jid_.empty());
82 DCHECK(!client_jid.empty());
83 DCHECK(disconnect_callback_.is_null());
84 DCHECK(!disconnect_callback.is_null());
85 211
86 client_jid_ = client_jid; 212 clipboard_stub_ = NULL;
87 disconnect_callback_ = disconnect_callback; 213
214 // Stop the video scheduler and clear references to the client's stubs.
215 if (video_scheduler_.get()) {
216 video_scheduler_->Stop();
217 video_scheduler_ = NULL;
218 }
219
220 // Stop the audio scheduler and clear references to the client's stubs.
221 if (audio_scheduler_.get()) {
222 audio_scheduler_->Stop();
223 audio_scheduler_ = NULL;
224 }
88 } 225 }
89 226
90 bool DesktopSessionProxy::AttachToDesktop( 227 bool DesktopSessionProxy::AttachToDesktop(
91 IPC::PlatformFileForTransit desktop_process, 228 IPC::PlatformFileForTransit desktop_process,
92 IPC::PlatformFileForTransit desktop_pipe) { 229 IPC::PlatformFileForTransit desktop_pipe) {
93 DCHECK(caller_task_runner_->BelongsToCurrentThread()); 230 DCHECK(caller_task_runner_->BelongsToCurrentThread());
94 DCHECK(!client_jid_.empty()); 231 DCHECK(!client_jid_.empty());
95 DCHECK(!desktop_channel_); 232 DCHECK(!desktop_channel_);
96 DCHECK(!disconnect_callback_.is_null()); 233 DCHECK(!disconnect_callback_.is_null());
97 234
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 } 292 }
156 } 293 }
157 294
158 void DesktopSessionProxy::DisconnectSession() { 295 void DesktopSessionProxy::DisconnectSession() {
159 DCHECK(caller_task_runner_->BelongsToCurrentThread()); 296 DCHECK(caller_task_runner_->BelongsToCurrentThread());
160 297
161 // Disconnect the client session if it hasn't been disconnected yet. 298 // Disconnect the client session if it hasn't been disconnected yet.
162 disconnect_callback_.Run(); 299 disconnect_callback_.Run();
163 } 300 }
164 301
165 void DesktopSessionProxy::InjectClipboardEvent(
166 const protocol::ClipboardEvent& event) {
167 DCHECK(caller_task_runner_->BelongsToCurrentThread());
168
169 std::string serialized_event;
170 if (!event.SerializeToString(&serialized_event)) {
171 LOG(ERROR) << "Failed to serialize protocol::ClipboardEvent.";
172 return;
173 }
174
175 SendToDesktop(
176 new ChromotingNetworkDesktopMsg_InjectClipboardEvent(serialized_event));
177 }
178
179 void DesktopSessionProxy::InjectKeyEvent(const protocol::KeyEvent& event) {
180 DCHECK(caller_task_runner_->BelongsToCurrentThread());
181
182 std::string serialized_event;
183 if (!event.SerializeToString(&serialized_event)) {
184 LOG(ERROR) << "Failed to serialize protocol::KeyEvent.";
185 return;
186 }
187
188 SendToDesktop(
189 new ChromotingNetworkDesktopMsg_InjectKeyEvent(serialized_event));
190 }
191
192 void DesktopSessionProxy::InjectMouseEvent(const protocol::MouseEvent& event) {
193 DCHECK(caller_task_runner_->BelongsToCurrentThread());
194
195 std::string serialized_event;
196 if (!event.SerializeToString(&serialized_event)) {
197 LOG(ERROR) << "Failed to serialize protocol::MouseEvent.";
198 return;
199 }
200
201 SendToDesktop(
202 new ChromotingNetworkDesktopMsg_InjectMouseEvent(serialized_event));
203 }
204
205 void DesktopSessionProxy::StartEventExecutor(
206 scoped_ptr<protocol::ClipboardStub> client_clipboard) {
207 DCHECK(caller_task_runner_->BelongsToCurrentThread());
208
209 client_clipboard_ = client_clipboard.Pass();
210 }
211
212 void DesktopSessionProxy::InvalidateRegion(const SkRegion& invalid_region) { 302 void DesktopSessionProxy::InvalidateRegion(const SkRegion& invalid_region) {
213 if (!caller_task_runner_->BelongsToCurrentThread()) { 303 if (!caller_task_runner_->BelongsToCurrentThread()) {
214 caller_task_runner_->PostTask( 304 caller_task_runner_->PostTask(
215 FROM_HERE, base::Bind(&DesktopSessionProxy::InvalidateRegion, this, 305 FROM_HERE, base::Bind(&DesktopSessionProxy::InvalidateRegion, this,
216 invalid_region)); 306 invalid_region));
217 return; 307 return;
218 } 308 }
219 309
220 std::vector<SkIRect> invalid_rects; 310 std::vector<SkIRect> invalid_rects;
221 for (SkRegion::Iterator i(invalid_region); !i.done(); i.next()) 311 for (SkRegion::Iterator i(invalid_region); !i.done(); i.next())
222 invalid_rects.push_back(i.rect()); 312 invalid_rects.push_back(i.rect());
223 313
224 SendToDesktop( 314 SendToDesktop(
225 new ChromotingNetworkDesktopMsg_InvalidateRegion(invalid_rects)); 315 new ChromotingNetworkDesktopMsg_InvalidateRegion(invalid_rects));
226 } 316 }
227 317
228 void DesktopSessionProxy::CaptureFrame() { 318 void DesktopSessionProxy::CaptureFrame() {
229 if (!caller_task_runner_->BelongsToCurrentThread()) { 319 if (!caller_task_runner_->BelongsToCurrentThread()) {
230 caller_task_runner_->PostTask( 320 caller_task_runner_->PostTask(
231 FROM_HERE, base::Bind(&DesktopSessionProxy::CaptureFrame, this)); 321 FROM_HERE, base::Bind(&DesktopSessionProxy::CaptureFrame, this));
232 return; 322 return;
233 } 323 }
234 324
235 ++pending_capture_frame_requests_; 325 ++pending_capture_frame_requests_;
236 SendToDesktop(new ChromotingNetworkDesktopMsg_CaptureFrame()); 326 SendToDesktop(new ChromotingNetworkDesktopMsg_CaptureFrame());
237 } 327 }
238 328
239 void DesktopSessionProxy::StartAudioCapturer(IpcAudioCapturer* audio_capturer) { 329 void DesktopSessionProxy::StartAudioCapturer(
330 const AudioCapturer::PacketCapturedCallback& audio_packet_callback) {
240 DCHECK(audio_capture_task_runner_->BelongsToCurrentThread()); 331 DCHECK(audio_capture_task_runner_->BelongsToCurrentThread());
241 DCHECK(audio_capturer_ == NULL); 332 DCHECK(audio_packet_callback_.is_null());
333 DCHECK(!audio_packet_callback.is_null());
242 334
243 audio_capturer_ = audio_capturer; 335 audio_packet_callback_ = audio_packet_callback;
244 } 336 }
245 337
246 void DesktopSessionProxy::StopAudioCapturer() { 338 void DesktopSessionProxy::StopAudioCapturer() {
247 DCHECK(audio_capture_task_runner_->BelongsToCurrentThread()); 339 DCHECK(audio_capture_task_runner_->BelongsToCurrentThread());
248 340
249 audio_capturer_ = NULL; 341 audio_packet_callback_.Reset();
250 } 342 }
251 343
252 void DesktopSessionProxy::StartVideoCapturer( 344 void DesktopSessionProxy::StartVideoCapturer(
253 IpcVideoFrameCapturer* video_capturer) { 345 IpcVideoFrameCapturer* video_capturer) {
254 DCHECK(video_capture_task_runner_->BelongsToCurrentThread()); 346 DCHECK(video_capture_task_runner_->BelongsToCurrentThread());
255 DCHECK(video_capturer_ == NULL); 347 DCHECK(video_capturer_ == NULL);
256 348
257 video_capturer_ = video_capturer; 349 video_capturer_ = video_capturer;
258 } 350 }
259 351
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
373 const MouseCursorShape& cursor_shape) { 465 const MouseCursorShape& cursor_shape) {
374 DCHECK(caller_task_runner_->BelongsToCurrentThread()); 466 DCHECK(caller_task_runner_->BelongsToCurrentThread());
375 PostCursorShape( 467 PostCursorShape(
376 scoped_ptr<MouseCursorShape>(new MouseCursorShape(cursor_shape))); 468 scoped_ptr<MouseCursorShape>(new MouseCursorShape(cursor_shape)));
377 } 469 }
378 470
379 void DesktopSessionProxy::OnInjectClipboardEvent( 471 void DesktopSessionProxy::OnInjectClipboardEvent(
380 const std::string& serialized_event) { 472 const std::string& serialized_event) {
381 DCHECK(caller_task_runner_->BelongsToCurrentThread()); 473 DCHECK(caller_task_runner_->BelongsToCurrentThread());
382 474
383 protocol::ClipboardEvent event; 475 if (clipboard_stub_) {
384 if (!event.ParseFromString(serialized_event)) { 476 protocol::ClipboardEvent event;
385 LOG(ERROR) << "Failed to parse protocol::ClipboardEvent."; 477 if (!event.ParseFromString(serialized_event)) {
386 return; 478 LOG(ERROR) << "Failed to parse protocol::ClipboardEvent.";
479 return;
480 }
481
482 clipboard_stub_->InjectClipboardEvent(event);
387 } 483 }
388
389 client_clipboard_->InjectClipboardEvent(event);
390 } 484 }
391 485
392 void DesktopSessionProxy::PostAudioPacket(scoped_ptr<AudioPacket> packet) { 486 void DesktopSessionProxy::PostAudioPacket(scoped_ptr<AudioPacket> packet) {
393 if (!audio_capture_task_runner_->BelongsToCurrentThread()) { 487 if (!audio_capture_task_runner_->BelongsToCurrentThread()) {
394 audio_capture_task_runner_->PostTask( 488 audio_capture_task_runner_->PostTask(
395 FROM_HERE, base::Bind(&DesktopSessionProxy::PostAudioPacket, 489 FROM_HERE, base::Bind(&DesktopSessionProxy::PostAudioPacket,
396 this, base::Passed(&packet))); 490 this, base::Passed(&packet)));
397 return; 491 return;
398 } 492 }
399 493
400 if (audio_capturer_) 494 if (!audio_packet_callback_.is_null())
401 audio_capturer_->OnAudioPacket(packet.Pass()); 495 audio_packet_callback_.Run(packet.Pass());
402 } 496 }
403 497
404 void DesktopSessionProxy::PostCaptureCompleted( 498 void DesktopSessionProxy::PostCaptureCompleted(
405 scoped_refptr<CaptureData> capture_data) { 499 scoped_refptr<CaptureData> capture_data) {
406 if (!video_capture_task_runner_->BelongsToCurrentThread()) { 500 if (!video_capture_task_runner_->BelongsToCurrentThread()) {
407 video_capture_task_runner_->PostTask( 501 video_capture_task_runner_->PostTask(
408 FROM_HERE, base::Bind(&DesktopSessionProxy::PostCaptureCompleted, 502 FROM_HERE, base::Bind(&DesktopSessionProxy::PostCaptureCompleted,
409 this, capture_data)); 503 this, capture_data));
410 return; 504 return;
411 } 505 }
(...skipping 19 matching lines...) Expand all
431 DCHECK(caller_task_runner_->BelongsToCurrentThread()); 525 DCHECK(caller_task_runner_->BelongsToCurrentThread());
432 526
433 if (desktop_channel_) { 527 if (desktop_channel_) {
434 desktop_channel_->Send(message); 528 desktop_channel_->Send(message);
435 } else { 529 } else {
436 delete message; 530 delete message;
437 } 531 }
438 } 532 }
439 533
440 } // namespace remoting 534 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/host/desktop_session_proxy.h ('k') | remoting/host/host_mock_objects.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698