Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "remoting/host/mouse_shape_pump.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/location.h" | |
| 9 #include "base/single_thread_task_runner.h" | |
| 10 #include "base/timer/timer.h" | |
| 11 #include "remoting/proto/control.pb.h" | |
| 12 #include "remoting/protocol/cursor_shape_stub.h" | |
| 13 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" | |
| 14 #include "third_party/webrtc/modules/desktop_capture/mouse_cursor.h" | |
| 15 #include "third_party/webrtc/modules/desktop_capture/mouse_cursor_monitor.h" | |
| 16 | |
| 17 namespace remoting { | |
| 18 | |
| 19 // Poll mouse shape 10 times a second. | |
| 20 static const int kCursorCaptureIntervalMs = 100; | |
| 21 | |
| 22 class MouseShapePump::Core : public base::NonThreadSafe, | |
|
Wez
2015/02/12 21:59:15
ThreadChecker?
Sergey Ulanov
2015/02/13 08:39:44
Done.
| |
| 23 public webrtc::MouseCursorMonitor::Callback { | |
| 24 public: | |
| 25 Core(base::WeakPtr<MouseShapePump> proxy, | |
| 26 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, | |
| 27 scoped_ptr<webrtc::MouseCursorMonitor> mouse_cursor_monitor); | |
| 28 ~Core(); | |
| 29 | |
| 30 void Start(); | |
| 31 void Capture(); | |
| 32 | |
| 33 private: | |
| 34 // webrtc::MouseCursorMonitor::Callback implementation. | |
| 35 void OnMouseCursor(webrtc::MouseCursor* mouse_cursor) override; | |
| 36 void OnMouseCursorPosition(webrtc::MouseCursorMonitor::CursorState state, | |
| 37 const webrtc::DesktopVector& position) override; | |
| 38 | |
| 39 base::WeakPtr<MouseShapePump> proxy_; | |
| 40 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner_; | |
| 41 scoped_ptr<webrtc::MouseCursorMonitor> mouse_cursor_monitor_; | |
| 42 | |
| 43 base::Timer capture_timer_; | |
| 44 | |
| 45 DISALLOW_COPY_AND_ASSIGN(Core); | |
| 46 }; | |
| 47 | |
| 48 MouseShapePump::Core::Core( | |
| 49 base::WeakPtr<MouseShapePump> proxy, | |
| 50 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, | |
| 51 scoped_ptr<webrtc::MouseCursorMonitor> mouse_cursor_monitor) | |
| 52 : proxy_(proxy), | |
| 53 caller_task_runner_(caller_task_runner), | |
| 54 mouse_cursor_monitor_(mouse_cursor_monitor.Pass()), | |
| 55 capture_timer_(true, true) { | |
| 56 DetachFromThread(); | |
| 57 } | |
| 58 | |
| 59 MouseShapePump::Core::~Core() { | |
| 60 DCHECK(CalledOnValidThread()); | |
| 61 } | |
| 62 | |
| 63 void MouseShapePump::Core::Start() { | |
| 64 DCHECK(CalledOnValidThread()); | |
| 65 | |
| 66 mouse_cursor_monitor_->Init(this, webrtc::MouseCursorMonitor::SHAPE_ONLY); | |
| 67 | |
| 68 capture_timer_.Start( | |
| 69 FROM_HERE, base::TimeDelta::FromMilliseconds(kCursorCaptureIntervalMs), | |
| 70 base::Bind(&MouseShapePump::Core::Capture, base::Unretained(this))); | |
| 71 } | |
| 72 | |
| 73 void MouseShapePump::Core::Capture() { | |
| 74 DCHECK(CalledOnValidThread()); | |
| 75 | |
| 76 mouse_cursor_monitor_->Capture(); | |
| 77 } | |
| 78 | |
| 79 void MouseShapePump::Core::OnMouseCursor(webrtc::MouseCursor* cursor) { | |
| 80 DCHECK(CalledOnValidThread()); | |
| 81 | |
| 82 scoped_ptr<webrtc::MouseCursor> owned_cursor(cursor); | |
| 83 | |
| 84 scoped_ptr<protocol::CursorShapeInfo> cursor_proto( | |
| 85 new protocol::CursorShapeInfo()); | |
| 86 cursor_proto->set_width(cursor->image()->size().width()); | |
| 87 cursor_proto->set_height(cursor->image()->size().height()); | |
| 88 cursor_proto->set_hotspot_x(cursor->hotspot().x()); | |
| 89 cursor_proto->set_hotspot_y(cursor->hotspot().y()); | |
| 90 | |
| 91 cursor_proto->set_data(std::string()); | |
| 92 uint8_t* current_row = cursor->image()->data(); | |
| 93 for (int y = 0; y < cursor->image()->size().height(); ++y) { | |
| 94 cursor_proto->mutable_data()->append( | |
| 95 current_row, | |
| 96 current_row + cursor->image()->size().width() * | |
| 97 webrtc::DesktopFrame::kBytesPerPixel); | |
| 98 current_row += cursor->image()->stride(); | |
| 99 } | |
| 100 | |
| 101 caller_task_runner_->PostTask( | |
| 102 FROM_HERE, base::Bind(&MouseShapePump::OnCursorShape, proxy_, | |
| 103 base::Passed(&cursor_proto))); | |
| 104 } | |
| 105 | |
| 106 void MouseShapePump::Core::OnMouseCursorPosition( | |
| 107 webrtc::MouseCursorMonitor::CursorState state, | |
| 108 const webrtc::DesktopVector& position) { | |
| 109 // We're not subscribing to mouse position changes. | |
| 110 NOTREACHED(); | |
| 111 } | |
| 112 | |
| 113 MouseShapePump::MouseShapePump( | |
| 114 scoped_refptr<base::SingleThreadTaskRunner> capture_task_runner, | |
| 115 scoped_ptr<webrtc::MouseCursorMonitor> mouse_cursor_monitor, | |
| 116 protocol::CursorShapeStub* cursor_shape_stub) | |
| 117 : capture_task_runner_(capture_task_runner), | |
| 118 cursor_shape_stub_(cursor_shape_stub), | |
| 119 weak_factory_(this) { | |
| 120 core_.reset(new Core(weak_factory_.GetWeakPtr(), capture_task_runner, | |
|
Wez
2015/02/12 21:59:15
I think you mean ThreadTaskRunnerHandle::Get() - y
Sergey Ulanov
2015/02/13 08:39:44
Yes.
| |
| 121 mouse_cursor_monitor.Pass())); | |
| 122 capture_task_runner_->PostTask( | |
| 123 FROM_HERE, base::Bind(&Core::Start, base::Unretained(core_.get()))); | |
| 124 } | |
| 125 | |
| 126 MouseShapePump::~MouseShapePump() { | |
| 127 capture_task_runner_->DeleteSoon(FROM_HERE, core_.release()); | |
| 128 } | |
| 129 | |
| 130 void MouseShapePump::OnCursorShape( | |
| 131 scoped_ptr<protocol::CursorShapeInfo> cursor) { | |
| 132 DCHECK(CalledOnValidThread()); | |
| 133 | |
| 134 cursor_shape_stub_->SetCursorShape(*cursor); | |
| 135 } | |
| 136 | |
| 137 } // namespace remoting | |
| OLD | NEW |