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/desktop_session_agent.h" | 5 #include "remoting/host/desktop_session_agent.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/shared_memory.h" | 9 #include "base/memory/shared_memory.h" |
10 #include "ipc/ipc_channel_proxy.h" | 10 #include "ipc/ipc_channel_proxy.h" |
11 #include "ipc/ipc_message.h" | 11 #include "ipc/ipc_message.h" |
12 #include "ipc/ipc_message_macros.h" | 12 #include "ipc/ipc_message_macros.h" |
13 #include "remoting/base/auto_thread_task_runner.h" | 13 #include "remoting/base/auto_thread_task_runner.h" |
14 #include "remoting/base/constants.h" | 14 #include "remoting/base/constants.h" |
15 #include "remoting/host/audio_capturer.h" | 15 #include "remoting/host/audio_capturer.h" |
16 #include "remoting/host/chromoting_messages.h" | 16 #include "remoting/host/chromoting_messages.h" |
17 #include "remoting/host/desktop_environment.h" | 17 #include "remoting/host/desktop_environment.h" |
18 #include "remoting/host/input_injector.h" | 18 #include "remoting/host/input_injector.h" |
19 #include "remoting/host/ipc_util.h" | 19 #include "remoting/host/ipc_util.h" |
20 #include "remoting/host/remote_input_filter.h" | 20 #include "remoting/host/remote_input_filter.h" |
21 #include "remoting/host/screen_controls.h" | 21 #include "remoting/host/screen_controls.h" |
22 #include "remoting/host/screen_resolution.h" | 22 #include "remoting/host/screen_resolution.h" |
23 #include "remoting/proto/audio.pb.h" | 23 #include "remoting/proto/audio.pb.h" |
24 #include "remoting/proto/control.pb.h" | 24 #include "remoting/proto/control.pb.h" |
25 #include "remoting/proto/event.pb.h" | 25 #include "remoting/proto/event.pb.h" |
26 #include "remoting/protocol/clipboard_stub.h" | 26 #include "remoting/protocol/clipboard_stub.h" |
27 #include "remoting/protocol/input_event_tracker.h" | 27 #include "remoting/protocol/input_event_tracker.h" |
28 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" | 28 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" |
| 29 #include "third_party/webrtc/modules/desktop_capture/mouse_cursor.h" |
29 #include "third_party/webrtc/modules/desktop_capture/shared_memory.h" | 30 #include "third_party/webrtc/modules/desktop_capture/shared_memory.h" |
30 | 31 |
31 namespace remoting { | 32 namespace remoting { |
32 | 33 |
33 namespace { | 34 namespace { |
34 | 35 |
35 // Routes local clipboard events though the IPC channel to the network process. | 36 // Routes local clipboard events though the IPC channel to the network process. |
36 class DesktopSesssionClipboardStub : public protocol::ClipboardStub { | 37 class DesktopSesssionClipboardStub : public protocol::ClipboardStub { |
37 public: | 38 public: |
38 explicit DesktopSesssionClipboardStub( | 39 explicit DesktopSesssionClipboardStub( |
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 new DesktopSesssionClipboardStub(this)); | 284 new DesktopSesssionClipboardStub(this)); |
284 input_injector_->Start(clipboard_stub.Pass()); | 285 input_injector_->Start(clipboard_stub.Pass()); |
285 | 286 |
286 // Start the audio capturer. | 287 // Start the audio capturer. |
287 if (delegate_->desktop_environment_factory().SupportsAudioCapture()) { | 288 if (delegate_->desktop_environment_factory().SupportsAudioCapture()) { |
288 audio_capturer_ = desktop_environment_->CreateAudioCapturer(); | 289 audio_capturer_ = desktop_environment_->CreateAudioCapturer(); |
289 audio_capture_task_runner_->PostTask( | 290 audio_capture_task_runner_->PostTask( |
290 FROM_HERE, base::Bind(&DesktopSessionAgent::StartAudioCapturer, this)); | 291 FROM_HERE, base::Bind(&DesktopSessionAgent::StartAudioCapturer, this)); |
291 } | 292 } |
292 | 293 |
293 // Start the video capturer. | 294 // Start the video capturer and mouse cursor monitor. |
294 video_capturer_ = desktop_environment_->CreateVideoCapturer(); | 295 video_capturer_ = desktop_environment_->CreateVideoCapturer(); |
| 296 mouse_cursor_monitor_ = desktop_environment_->CreateMouseCursorMonitor(); |
295 video_capture_task_runner_->PostTask( | 297 video_capture_task_runner_->PostTask( |
296 FROM_HERE, base::Bind(&DesktopSessionAgent::StartVideoCapturer, this)); | 298 FROM_HERE, base::Bind( |
| 299 &DesktopSessionAgent::StartVideoCapturerAndMouseMonitor, this)); |
297 } | 300 } |
298 | 301 |
299 void DesktopSessionAgent::OnCaptureCompleted(webrtc::DesktopFrame* frame) { | 302 void DesktopSessionAgent::OnCaptureCompleted(webrtc::DesktopFrame* frame) { |
300 DCHECK(video_capture_task_runner_->BelongsToCurrentThread()); | 303 DCHECK(video_capture_task_runner_->BelongsToCurrentThread()); |
301 | 304 |
302 last_frame_.reset(frame); | 305 last_frame_.reset(frame); |
303 | 306 |
304 current_size_ = frame->size(); | 307 current_size_ = frame->size(); |
305 | 308 |
306 // Serialize webrtc::DesktopFrame. | 309 // Serialize webrtc::DesktopFrame. |
307 SerializedDesktopFrame serialized_frame; | 310 SerializedDesktopFrame serialized_frame; |
308 serialized_frame.shared_buffer_id = frame->shared_memory()->id(); | 311 serialized_frame.shared_buffer_id = frame->shared_memory()->id(); |
309 serialized_frame.bytes_per_row = frame->stride(); | 312 serialized_frame.bytes_per_row = frame->stride(); |
310 serialized_frame.dimensions = frame->size(); | 313 serialized_frame.dimensions = frame->size(); |
311 serialized_frame.capture_time_ms = frame->capture_time_ms(); | 314 serialized_frame.capture_time_ms = frame->capture_time_ms(); |
312 serialized_frame.dpi = frame->dpi(); | 315 serialized_frame.dpi = frame->dpi(); |
313 for (webrtc::DesktopRegion::Iterator i(frame->updated_region()); | 316 for (webrtc::DesktopRegion::Iterator i(frame->updated_region()); |
314 !i.IsAtEnd(); i.Advance()) { | 317 !i.IsAtEnd(); i.Advance()) { |
315 serialized_frame.dirty_region.push_back(i.rect()); | 318 serialized_frame.dirty_region.push_back(i.rect()); |
316 } | 319 } |
317 | 320 |
318 SendToNetwork( | 321 SendToNetwork( |
319 new ChromotingDesktopNetworkMsg_CaptureCompleted(serialized_frame)); | 322 new ChromotingDesktopNetworkMsg_CaptureCompleted(serialized_frame)); |
320 } | 323 } |
321 | 324 |
322 void DesktopSessionAgent::OnCursorShapeChanged( | 325 void DesktopSessionAgent::OnMouseCursor(webrtc::MouseCursor* cursor) { |
323 webrtc::MouseCursorShape* cursor_shape) { | |
324 DCHECK(video_capture_task_runner_->BelongsToCurrentThread()); | 326 DCHECK(video_capture_task_runner_->BelongsToCurrentThread()); |
325 | 327 |
326 scoped_ptr<webrtc::MouseCursorShape> owned_cursor(cursor_shape); | 328 scoped_ptr<webrtc::MouseCursor> owned_cursor(cursor); |
327 | 329 |
328 SendToNetwork(new ChromotingDesktopNetworkMsg_CursorShapeChanged( | 330 SendToNetwork( |
329 *cursor_shape)); | 331 new ChromotingDesktopNetworkMsg_MouseCursor(owned_cursor.Pass())); |
| 332 } |
| 333 |
| 334 void DesktopSessionAgent::OnMouseCursorPosition( |
| 335 webrtc::MouseCursorMonitor::CursorState state, |
| 336 const webrtc::DesktopVector& position) { |
| 337 // We're not subscribing to mouse position changes. |
| 338 NOTREACHED(); |
330 } | 339 } |
331 | 340 |
332 void DesktopSessionAgent::InjectClipboardEvent( | 341 void DesktopSessionAgent::InjectClipboardEvent( |
333 const protocol::ClipboardEvent& event) { | 342 const protocol::ClipboardEvent& event) { |
334 DCHECK(caller_task_runner_->BelongsToCurrentThread()); | 343 DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
335 | 344 |
336 std::string serialized_event; | 345 std::string serialized_event; |
337 if (!event.SerializeToString(&serialized_event)) { | 346 if (!event.SerializeToString(&serialized_event)) { |
338 LOG(ERROR) << "Failed to serialize protocol::ClipboardEvent."; | 347 LOG(ERROR) << "Failed to serialize protocol::ClipboardEvent."; |
339 return; | 348 return; |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
395 desktop_environment_.reset(); | 404 desktop_environment_.reset(); |
396 input_injector_.reset(); | 405 input_injector_.reset(); |
397 screen_controls_.reset(); | 406 screen_controls_.reset(); |
398 | 407 |
399 // Stop the audio capturer. | 408 // Stop the audio capturer. |
400 audio_capture_task_runner_->PostTask( | 409 audio_capture_task_runner_->PostTask( |
401 FROM_HERE, base::Bind(&DesktopSessionAgent::StopAudioCapturer, this)); | 410 FROM_HERE, base::Bind(&DesktopSessionAgent::StopAudioCapturer, this)); |
402 | 411 |
403 // Stop the video capturer. | 412 // Stop the video capturer. |
404 video_capture_task_runner_->PostTask( | 413 video_capture_task_runner_->PostTask( |
405 FROM_HERE, base::Bind(&DesktopSessionAgent::StopVideoCapturer, this)); | 414 FROM_HERE, base::Bind( |
| 415 &DesktopSessionAgent::StopVideoCapturerAndMouseMonitor, this)); |
406 } | 416 } |
407 } | 417 } |
408 | 418 |
409 void DesktopSessionAgent::OnCaptureFrame() { | 419 void DesktopSessionAgent::OnCaptureFrame() { |
410 if (!video_capture_task_runner_->BelongsToCurrentThread()) { | 420 if (!video_capture_task_runner_->BelongsToCurrentThread()) { |
411 video_capture_task_runner_->PostTask( | 421 video_capture_task_runner_->PostTask( |
412 FROM_HERE, | 422 FROM_HERE, |
413 base::Bind(&DesktopSessionAgent::OnCaptureFrame, this)); | 423 base::Bind(&DesktopSessionAgent::OnCaptureFrame, this)); |
414 return; | 424 return; |
415 } | 425 } |
416 | 426 |
| 427 mouse_cursor_monitor_->Capture(); |
| 428 |
417 // webrtc::ScreenCapturer supports a very few (currently 2) outstanding | 429 // webrtc::ScreenCapturer supports a very few (currently 2) outstanding |
418 // capture requests. The requests are serialized on | 430 // capture requests. The requests are serialized on |
419 // |video_capture_task_runner()| task runner. If the client issues more | 431 // |video_capture_task_runner()| task runner. If the client issues more |
420 // requests, pixel data in captured frames will likely be corrupted but | 432 // requests, pixel data in captured frames will likely be corrupted but |
421 // stability of webrtc::ScreenCapturer will not be affected. | 433 // stability of webrtc::ScreenCapturer will not be affected. |
422 video_capturer_->Capture(webrtc::DesktopRegion()); | 434 video_capturer_->Capture(webrtc::DesktopRegion()); |
423 } | 435 } |
424 | 436 |
425 void DesktopSessionAgent::OnInjectClipboardEvent( | 437 void DesktopSessionAgent::OnInjectClipboardEvent( |
426 const std::string& serialized_event) { | 438 const std::string& serialized_event) { |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
503 this)); | 515 this)); |
504 } | 516 } |
505 } | 517 } |
506 | 518 |
507 void DesktopSessionAgent::StopAudioCapturer() { | 519 void DesktopSessionAgent::StopAudioCapturer() { |
508 DCHECK(audio_capture_task_runner_->BelongsToCurrentThread()); | 520 DCHECK(audio_capture_task_runner_->BelongsToCurrentThread()); |
509 | 521 |
510 audio_capturer_.reset(); | 522 audio_capturer_.reset(); |
511 } | 523 } |
512 | 524 |
513 void DesktopSessionAgent::StartVideoCapturer() { | 525 void DesktopSessionAgent::StartVideoCapturerAndMouseMonitor() { |
514 DCHECK(video_capture_task_runner_->BelongsToCurrentThread()); | 526 DCHECK(video_capture_task_runner_->BelongsToCurrentThread()); |
515 | 527 |
516 if (video_capturer_) { | 528 if (video_capturer_) { |
517 video_capturer_->SetMouseShapeObserver(this); | |
518 video_capturer_->Start(this); | 529 video_capturer_->Start(this); |
519 } | 530 } |
| 531 |
| 532 if (mouse_cursor_monitor_) { |
| 533 mouse_cursor_monitor_->Init(this, webrtc::MouseCursorMonitor::SHAPE_ONLY); |
| 534 } |
520 } | 535 } |
521 | 536 |
522 void DesktopSessionAgent::StopVideoCapturer() { | 537 void DesktopSessionAgent::StopVideoCapturerAndMouseMonitor() { |
523 DCHECK(video_capture_task_runner_->BelongsToCurrentThread()); | 538 DCHECK(video_capture_task_runner_->BelongsToCurrentThread()); |
524 | 539 |
525 video_capturer_.reset(); | 540 video_capturer_.reset(); |
526 last_frame_.reset(); | 541 last_frame_.reset(); |
| 542 mouse_cursor_monitor_.reset(); |
527 | 543 |
528 // Video capturer must delete all buffers. | 544 // Video capturer must delete all buffers. |
529 DCHECK_EQ(shared_buffers_, 0); | 545 DCHECK_EQ(shared_buffers_, 0); |
530 } | 546 } |
531 | 547 |
532 void DesktopSessionAgent::OnSharedBufferDeleted(int id) { | 548 void DesktopSessionAgent::OnSharedBufferDeleted(int id) { |
533 DCHECK(video_capture_task_runner_->BelongsToCurrentThread()); | 549 DCHECK(video_capture_task_runner_->BelongsToCurrentThread()); |
534 DCHECK(id != 0); | 550 DCHECK(id != 0); |
535 | 551 |
536 shared_buffers_--; | 552 shared_buffers_--; |
537 DCHECK_GE(shared_buffers_, 0); | 553 DCHECK_GE(shared_buffers_, 0); |
538 SendToNetwork(new ChromotingDesktopNetworkMsg_ReleaseSharedBuffer(id)); | 554 SendToNetwork(new ChromotingDesktopNetworkMsg_ReleaseSharedBuffer(id)); |
539 } | 555 } |
540 | 556 |
541 void DesktopSessionAgent::CloseDesktopPipeHandle() { | 557 void DesktopSessionAgent::CloseDesktopPipeHandle() { |
542 if (!(desktop_pipe_ == IPC::InvalidPlatformFileForTransit())) { | 558 if (!(desktop_pipe_ == IPC::InvalidPlatformFileForTransit())) { |
543 #if defined(OS_WIN) | 559 #if defined(OS_WIN) |
544 base::ClosePlatformFile(desktop_pipe_); | 560 base::ClosePlatformFile(desktop_pipe_); |
545 #elif defined(OS_POSIX) | 561 #elif defined(OS_POSIX) |
546 base::ClosePlatformFile(desktop_pipe_.fd); | 562 base::ClosePlatformFile(desktop_pipe_.fd); |
547 #else // !defined(OS_POSIX) | 563 #else // !defined(OS_POSIX) |
548 #error Unsupported platform. | 564 #error Unsupported platform. |
549 #endif // !defined(OS_POSIX) | 565 #endif // !defined(OS_POSIX) |
550 | 566 |
551 desktop_pipe_ = IPC::InvalidPlatformFileForTransit(); | 567 desktop_pipe_ = IPC::InvalidPlatformFileForTransit(); |
552 } | 568 } |
553 } | 569 } |
554 | 570 |
555 } // namespace remoting | 571 } // namespace remoting |
OLD | NEW |