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