| 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 SerializedMouseCursor serialized_cursor; |
| 329 *cursor_shape)); | 331 serialized_cursor.size = cursor->image().size(); |
| 332 serialized_cursor.hotspot = cursor->hotspot(); |
| 333 |
| 334 uint8_t* current_row = cursor->image().data(); |
| 335 for (int y = 0; y < cursor->image().size().height(); ++y) { |
| 336 serialized_cursor.data.append( |
| 337 current_row, |
| 338 current_row + cursor->image().size().width() * |
| 339 webrtc::DesktopFrame::kBytesPerPixel); |
| 340 current_row += cursor->image().stride(); |
| 341 } |
| 342 |
| 343 SendToNetwork( |
| 344 new ChromotingDesktopNetworkMsg_MouseCursor(serialized_cursor)); |
| 345 } |
| 346 |
| 347 void DesktopSessionAgent::OnMouseCursorPosition( |
| 348 webrtc::MouseCursorMonitor::CursorState state, |
| 349 const webrtc::DesktopVector& position) { |
| 350 // We're not subscribing to mouse position changes. |
| 351 NOTREACHED(); |
| 330 } | 352 } |
| 331 | 353 |
| 332 void DesktopSessionAgent::InjectClipboardEvent( | 354 void DesktopSessionAgent::InjectClipboardEvent( |
| 333 const protocol::ClipboardEvent& event) { | 355 const protocol::ClipboardEvent& event) { |
| 334 DCHECK(caller_task_runner_->BelongsToCurrentThread()); | 356 DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
| 335 | 357 |
| 336 std::string serialized_event; | 358 std::string serialized_event; |
| 337 if (!event.SerializeToString(&serialized_event)) { | 359 if (!event.SerializeToString(&serialized_event)) { |
| 338 LOG(ERROR) << "Failed to serialize protocol::ClipboardEvent."; | 360 LOG(ERROR) << "Failed to serialize protocol::ClipboardEvent."; |
| 339 return; | 361 return; |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 395 desktop_environment_.reset(); | 417 desktop_environment_.reset(); |
| 396 input_injector_.reset(); | 418 input_injector_.reset(); |
| 397 screen_controls_.reset(); | 419 screen_controls_.reset(); |
| 398 | 420 |
| 399 // Stop the audio capturer. | 421 // Stop the audio capturer. |
| 400 audio_capture_task_runner_->PostTask( | 422 audio_capture_task_runner_->PostTask( |
| 401 FROM_HERE, base::Bind(&DesktopSessionAgent::StopAudioCapturer, this)); | 423 FROM_HERE, base::Bind(&DesktopSessionAgent::StopAudioCapturer, this)); |
| 402 | 424 |
| 403 // Stop the video capturer. | 425 // Stop the video capturer. |
| 404 video_capture_task_runner_->PostTask( | 426 video_capture_task_runner_->PostTask( |
| 405 FROM_HERE, base::Bind(&DesktopSessionAgent::StopVideoCapturer, this)); | 427 FROM_HERE, base::Bind( |
| 428 &DesktopSessionAgent::StopVideoCapturerAndMouseMonitor, this)); |
| 406 } | 429 } |
| 407 } | 430 } |
| 408 | 431 |
| 409 void DesktopSessionAgent::OnCaptureFrame() { | 432 void DesktopSessionAgent::OnCaptureFrame() { |
| 410 if (!video_capture_task_runner_->BelongsToCurrentThread()) { | 433 if (!video_capture_task_runner_->BelongsToCurrentThread()) { |
| 411 video_capture_task_runner_->PostTask( | 434 video_capture_task_runner_->PostTask( |
| 412 FROM_HERE, | 435 FROM_HERE, |
| 413 base::Bind(&DesktopSessionAgent::OnCaptureFrame, this)); | 436 base::Bind(&DesktopSessionAgent::OnCaptureFrame, this)); |
| 414 return; | 437 return; |
| 415 } | 438 } |
| 416 | 439 |
| 440 mouse_cursor_monitor_->Capture(); |
| 441 |
| 417 // webrtc::ScreenCapturer supports a very few (currently 2) outstanding | 442 // webrtc::ScreenCapturer supports a very few (currently 2) outstanding |
| 418 // capture requests. The requests are serialized on | 443 // capture requests. The requests are serialized on |
| 419 // |video_capture_task_runner()| task runner. If the client issues more | 444 // |video_capture_task_runner()| task runner. If the client issues more |
| 420 // requests, pixel data in captured frames will likely be corrupted but | 445 // requests, pixel data in captured frames will likely be corrupted but |
| 421 // stability of webrtc::ScreenCapturer will not be affected. | 446 // stability of webrtc::ScreenCapturer will not be affected. |
| 422 video_capturer_->Capture(webrtc::DesktopRegion()); | 447 video_capturer_->Capture(webrtc::DesktopRegion()); |
| 423 } | 448 } |
| 424 | 449 |
| 425 void DesktopSessionAgent::OnInjectClipboardEvent( | 450 void DesktopSessionAgent::OnInjectClipboardEvent( |
| 426 const std::string& serialized_event) { | 451 const std::string& serialized_event) { |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 503 this)); | 528 this)); |
| 504 } | 529 } |
| 505 } | 530 } |
| 506 | 531 |
| 507 void DesktopSessionAgent::StopAudioCapturer() { | 532 void DesktopSessionAgent::StopAudioCapturer() { |
| 508 DCHECK(audio_capture_task_runner_->BelongsToCurrentThread()); | 533 DCHECK(audio_capture_task_runner_->BelongsToCurrentThread()); |
| 509 | 534 |
| 510 audio_capturer_.reset(); | 535 audio_capturer_.reset(); |
| 511 } | 536 } |
| 512 | 537 |
| 513 void DesktopSessionAgent::StartVideoCapturer() { | 538 void DesktopSessionAgent::StartVideoCapturerAndMouseMonitor() { |
| 514 DCHECK(video_capture_task_runner_->BelongsToCurrentThread()); | 539 DCHECK(video_capture_task_runner_->BelongsToCurrentThread()); |
| 515 | 540 |
| 516 if (video_capturer_) { | 541 if (video_capturer_) { |
| 517 video_capturer_->SetMouseShapeObserver(this); | |
| 518 video_capturer_->Start(this); | 542 video_capturer_->Start(this); |
| 519 } | 543 } |
| 544 |
| 545 if (mouse_cursor_monitor_) { |
| 546 mouse_cursor_monitor_->Init(this, webrtc::MouseCursorMonitor::SHAPE_ONLY); |
| 547 } |
| 520 } | 548 } |
| 521 | 549 |
| 522 void DesktopSessionAgent::StopVideoCapturer() { | 550 void DesktopSessionAgent::StopVideoCapturerAndMouseMonitor() { |
| 523 DCHECK(video_capture_task_runner_->BelongsToCurrentThread()); | 551 DCHECK(video_capture_task_runner_->BelongsToCurrentThread()); |
| 524 | 552 |
| 525 video_capturer_.reset(); | 553 video_capturer_.reset(); |
| 526 last_frame_.reset(); | 554 last_frame_.reset(); |
| 555 mouse_cursor_monitor_.reset(); |
| 527 | 556 |
| 528 // Video capturer must delete all buffers. | 557 // Video capturer must delete all buffers. |
| 529 DCHECK_EQ(shared_buffers_, 0); | 558 DCHECK_EQ(shared_buffers_, 0); |
| 530 } | 559 } |
| 531 | 560 |
| 532 void DesktopSessionAgent::OnSharedBufferDeleted(int id) { | 561 void DesktopSessionAgent::OnSharedBufferDeleted(int id) { |
| 533 DCHECK(video_capture_task_runner_->BelongsToCurrentThread()); | 562 DCHECK(video_capture_task_runner_->BelongsToCurrentThread()); |
| 534 DCHECK(id != 0); | 563 DCHECK(id != 0); |
| 535 | 564 |
| 536 shared_buffers_--; | 565 shared_buffers_--; |
| 537 DCHECK_GE(shared_buffers_, 0); | 566 DCHECK_GE(shared_buffers_, 0); |
| 538 SendToNetwork(new ChromotingDesktopNetworkMsg_ReleaseSharedBuffer(id)); | 567 SendToNetwork(new ChromotingDesktopNetworkMsg_ReleaseSharedBuffer(id)); |
| 539 } | 568 } |
| 540 | 569 |
| 541 void DesktopSessionAgent::CloseDesktopPipeHandle() { | 570 void DesktopSessionAgent::CloseDesktopPipeHandle() { |
| 542 if (!(desktop_pipe_ == IPC::InvalidPlatformFileForTransit())) { | 571 if (!(desktop_pipe_ == IPC::InvalidPlatformFileForTransit())) { |
| 543 #if defined(OS_WIN) | 572 #if defined(OS_WIN) |
| 544 base::ClosePlatformFile(desktop_pipe_); | 573 base::ClosePlatformFile(desktop_pipe_); |
| 545 #elif defined(OS_POSIX) | 574 #elif defined(OS_POSIX) |
| 546 base::ClosePlatformFile(desktop_pipe_.fd); | 575 base::ClosePlatformFile(desktop_pipe_.fd); |
| 547 #else // !defined(OS_POSIX) | 576 #else // !defined(OS_POSIX) |
| 548 #error Unsupported platform. | 577 #error Unsupported platform. |
| 549 #endif // !defined(OS_POSIX) | 578 #endif // !defined(OS_POSIX) |
| 550 | 579 |
| 551 desktop_pipe_ = IPC::InvalidPlatformFileForTransit(); | 580 desktop_pipe_ = IPC::InvalidPlatformFileForTransit(); |
| 552 } | 581 } |
| 553 } | 582 } |
| 554 | 583 |
| 555 } // namespace remoting | 584 } // namespace remoting |
| OLD | NEW |