| 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/video_scheduler.h" | 5 #include "remoting/host/video_scheduler.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "remoting/base/auto_thread_task_runner.h" | 10 #include "remoting/base/auto_thread_task_runner.h" |
| 11 #include "remoting/codec/video_encoder.h" | 11 #include "remoting/codec/video_encoder.h" |
| 12 #include "remoting/host/host_mock_objects.h" |
| 13 #include "remoting/proto/control.pb.h" |
| 12 #include "remoting/proto/video.pb.h" | 14 #include "remoting/proto/video.pb.h" |
| 13 #include "remoting/protocol/protocol_mock_objects.h" | 15 #include "remoting/protocol/protocol_mock_objects.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" | 18 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" |
| 19 #include "third_party/webrtc/modules/desktop_capture/mouse_cursor.h" |
| 17 #include "third_party/webrtc/modules/desktop_capture/screen_capturer_mock_object
s.h" | 20 #include "third_party/webrtc/modules/desktop_capture/screen_capturer_mock_object
s.h" |
| 18 | 21 |
| 19 using ::remoting::protocol::MockClientStub; | 22 using ::remoting::protocol::MockClientStub; |
| 20 using ::remoting::protocol::MockVideoStub; | 23 using ::remoting::protocol::MockVideoStub; |
| 21 | 24 |
| 22 using ::testing::_; | 25 using ::testing::_; |
| 23 using ::testing::AtLeast; | 26 using ::testing::AtLeast; |
| 24 using ::testing::AnyNumber; | 27 using ::testing::AnyNumber; |
| 25 using ::testing::DeleteArg; | 28 using ::testing::DeleteArg; |
| 26 using ::testing::DoAll; | 29 using ::testing::DoAll; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 41 } | 44 } |
| 42 | 45 |
| 43 ACTION(FinishSend) { | 46 ACTION(FinishSend) { |
| 44 arg1.Run(); | 47 arg1.Run(); |
| 45 } | 48 } |
| 46 | 49 |
| 47 } // namespace | 50 } // namespace |
| 48 | 51 |
| 49 static const int kWidth = 640; | 52 static const int kWidth = 640; |
| 50 static const int kHeight = 480; | 53 static const int kHeight = 480; |
| 54 static const int kCursorWidth = 64; |
| 55 static const int kCursorHeight = 32; |
| 56 static const int kHotspotX = 11; |
| 57 static const int kHotspotY = 12; |
| 51 | 58 |
| 52 class MockVideoEncoder : public VideoEncoder { | 59 class MockVideoEncoder : public VideoEncoder { |
| 53 public: | 60 public: |
| 54 MockVideoEncoder(); | 61 MockVideoEncoder(); |
| 55 virtual ~MockVideoEncoder(); | 62 virtual ~MockVideoEncoder(); |
| 56 | 63 |
| 57 scoped_ptr<VideoPacket> Encode( | 64 scoped_ptr<VideoPacket> Encode( |
| 58 const webrtc::DesktopFrame& frame) { | 65 const webrtc::DesktopFrame& frame) { |
| 59 return scoped_ptr<VideoPacket>(EncodePtr(frame)); | 66 return scoped_ptr<VideoPacket>(EncodePtr(frame)); |
| 60 } | 67 } |
| 61 MOCK_METHOD1(EncodePtr, VideoPacket*(const webrtc::DesktopFrame& frame)); | 68 MOCK_METHOD1(EncodePtr, VideoPacket*(const webrtc::DesktopFrame& frame)); |
| 62 | 69 |
| 63 private: | 70 private: |
| 64 DISALLOW_COPY_AND_ASSIGN(MockVideoEncoder); | 71 DISALLOW_COPY_AND_ASSIGN(MockVideoEncoder); |
| 65 }; | 72 }; |
| 66 | 73 |
| 67 MockVideoEncoder::MockVideoEncoder() {} | 74 MockVideoEncoder::MockVideoEncoder() {} |
| 68 | 75 |
| 69 MockVideoEncoder::~MockVideoEncoder() {} | 76 MockVideoEncoder::~MockVideoEncoder() {} |
| 70 | 77 |
| 71 class VideoSchedulerTest : public testing::Test { | 78 class VideoSchedulerTest : public testing::Test { |
| 72 public: | 79 public: |
| 73 VideoSchedulerTest(); | 80 VideoSchedulerTest(); |
| 74 | 81 |
| 75 virtual void SetUp() OVERRIDE; | 82 virtual void SetUp() OVERRIDE; |
| 76 | 83 |
| 77 void StartVideoScheduler(scoped_ptr<webrtc::ScreenCapturer> capturer); | 84 void StartVideoScheduler( |
| 85 scoped_ptr<webrtc::ScreenCapturer> capturer, |
| 86 scoped_ptr<webrtc::MouseCursorMonitor> mouse_monitor); |
| 78 void StopVideoScheduler(); | 87 void StopVideoScheduler(); |
| 79 | 88 |
| 80 // webrtc::ScreenCapturer mocks. | 89 // webrtc::ScreenCapturer mocks. |
| 81 void OnCapturerStart(webrtc::ScreenCapturer::Callback* callback); | 90 void OnCapturerStart(webrtc::ScreenCapturer::Callback* callback); |
| 82 void OnCaptureFrame(const webrtc::DesktopRegion& region); | 91 void OnCaptureFrame(const webrtc::DesktopRegion& region); |
| 92 void OnMouseCursorMonitorInit( |
| 93 webrtc::MouseCursorMonitor::Callback* callback, |
| 94 webrtc::MouseCursorMonitor::Mode mode); |
| 95 void OnCaptureMouse(); |
| 96 void SetCursorShape(const protocol::CursorShapeInfo& cursor_shape); |
| 83 | 97 |
| 84 protected: | 98 protected: |
| 85 base::MessageLoop message_loop_; | 99 base::MessageLoop message_loop_; |
| 86 base::RunLoop run_loop_; | 100 base::RunLoop run_loop_; |
| 87 scoped_refptr<AutoThreadTaskRunner> task_runner_; | 101 scoped_refptr<AutoThreadTaskRunner> task_runner_; |
| 88 scoped_refptr<VideoScheduler> scheduler_; | 102 scoped_refptr<VideoScheduler> scheduler_; |
| 89 | 103 |
| 90 MockClientStub client_stub_; | 104 MockClientStub client_stub_; |
| 91 MockVideoStub video_stub_; | 105 MockVideoStub video_stub_; |
| 92 | 106 |
| 93 // The following mock objects are owned by VideoScheduler. | 107 // The following mock objects are owned by VideoScheduler. |
| 94 MockVideoEncoder* encoder_; | 108 MockVideoEncoder* encoder_; |
| 95 | 109 |
| 96 scoped_ptr<webrtc::DesktopFrame> frame_; | 110 scoped_ptr<webrtc::DesktopFrame> frame_; |
| 111 scoped_ptr<webrtc::MouseCursor> mouse_cursor_; |
| 97 | 112 |
| 98 // Points to the callback passed to webrtc::ScreenCapturer::Start(). | 113 // Points to the callback passed to webrtc::ScreenCapturer::Start(). |
| 99 webrtc::ScreenCapturer::Callback* capturer_callback_; | 114 webrtc::ScreenCapturer::Callback* capturer_callback_; |
| 100 | 115 |
| 116 // Points to the callback passed to webrtc::MouseCursor::Init(). |
| 117 webrtc::MouseCursorMonitor::Callback* mouse_monitor_callback_; |
| 118 |
| 101 private: | 119 private: |
| 102 DISALLOW_COPY_AND_ASSIGN(VideoSchedulerTest); | 120 DISALLOW_COPY_AND_ASSIGN(VideoSchedulerTest); |
| 103 }; | 121 }; |
| 104 | 122 |
| 105 VideoSchedulerTest::VideoSchedulerTest() | 123 VideoSchedulerTest::VideoSchedulerTest() |
| 106 : encoder_(NULL), | 124 : encoder_(NULL), |
| 107 capturer_callback_(NULL) { | 125 capturer_callback_(NULL), |
| 126 mouse_monitor_callback_(NULL) { |
| 108 } | 127 } |
| 109 | 128 |
| 110 void VideoSchedulerTest::SetUp() { | 129 void VideoSchedulerTest::SetUp() { |
| 111 task_runner_ = new AutoThreadTaskRunner( | 130 task_runner_ = new AutoThreadTaskRunner( |
| 112 message_loop_.message_loop_proxy(), run_loop_.QuitClosure()); | 131 message_loop_.message_loop_proxy(), run_loop_.QuitClosure()); |
| 113 | 132 |
| 114 encoder_ = new MockVideoEncoder(); | 133 encoder_ = new MockVideoEncoder(); |
| 115 } | 134 } |
| 116 | 135 |
| 117 void VideoSchedulerTest::StartVideoScheduler( | 136 void VideoSchedulerTest::StartVideoScheduler( |
| 118 scoped_ptr<webrtc::ScreenCapturer> capturer) { | 137 scoped_ptr<webrtc::ScreenCapturer> capturer, |
| 138 scoped_ptr<webrtc::MouseCursorMonitor> mouse_monitor) { |
| 119 scheduler_ = new VideoScheduler( | 139 scheduler_ = new VideoScheduler( |
| 120 task_runner_, // Capture | 140 task_runner_, // Capture |
| 121 task_runner_, // Encode | 141 task_runner_, // Encode |
| 122 task_runner_, // Network | 142 task_runner_, // Network |
| 123 capturer.Pass(), | 143 capturer.Pass(), |
| 144 mouse_monitor.Pass(), |
| 124 scoped_ptr<VideoEncoder>(encoder_), | 145 scoped_ptr<VideoEncoder>(encoder_), |
| 125 &client_stub_, | 146 &client_stub_, |
| 126 &video_stub_); | 147 &video_stub_); |
| 127 scheduler_->Start(); | 148 scheduler_->Start(); |
| 128 } | 149 } |
| 129 | 150 |
| 130 void VideoSchedulerTest::StopVideoScheduler() { | 151 void VideoSchedulerTest::StopVideoScheduler() { |
| 131 scheduler_->Stop(); | 152 scheduler_->Stop(); |
| 132 scheduler_ = NULL; | 153 scheduler_ = NULL; |
| 133 } | 154 } |
| 134 | 155 |
| 135 void VideoSchedulerTest::OnCapturerStart( | 156 void VideoSchedulerTest::OnCapturerStart( |
| 136 webrtc::ScreenCapturer::Callback* callback) { | 157 webrtc::ScreenCapturer::Callback* callback) { |
| 137 EXPECT_FALSE(capturer_callback_); | 158 EXPECT_FALSE(capturer_callback_); |
| 138 EXPECT_TRUE(callback); | 159 EXPECT_TRUE(callback); |
| 139 | 160 |
| 140 capturer_callback_ = callback; | 161 capturer_callback_ = callback; |
| 141 } | 162 } |
| 142 | 163 |
| 143 void VideoSchedulerTest::OnCaptureFrame(const webrtc::DesktopRegion& region) { | 164 void VideoSchedulerTest::OnCaptureFrame(const webrtc::DesktopRegion& region) { |
| 144 frame_->mutable_updated_region()->SetRect( | 165 frame_->mutable_updated_region()->SetRect( |
| 145 webrtc::DesktopRect::MakeXYWH(0, 0, 10, 10)); | 166 webrtc::DesktopRect::MakeXYWH(0, 0, 10, 10)); |
| 146 capturer_callback_->OnCaptureCompleted(frame_.release()); | 167 capturer_callback_->OnCaptureCompleted(frame_.release()); |
| 147 } | 168 } |
| 148 | 169 |
| 170 void VideoSchedulerTest::OnCaptureMouse() { |
| 171 EXPECT_TRUE(mouse_monitor_callback_); |
| 172 mouse_monitor_callback_->OnMouseCursor(mouse_cursor_.release()); |
| 173 } |
| 174 |
| 175 void VideoSchedulerTest::OnMouseCursorMonitorInit( |
| 176 webrtc::MouseCursorMonitor::Callback* callback, |
| 177 webrtc::MouseCursorMonitor::Mode mode) { |
| 178 EXPECT_FALSE(mouse_monitor_callback_); |
| 179 EXPECT_TRUE(callback); |
| 180 |
| 181 mouse_monitor_callback_ = callback; |
| 182 } |
| 183 |
| 184 void VideoSchedulerTest::SetCursorShape( |
| 185 const protocol::CursorShapeInfo& cursor_shape) { |
| 186 EXPECT_TRUE(cursor_shape.has_width()); |
| 187 EXPECT_EQ(kCursorWidth, cursor_shape.width()); |
| 188 EXPECT_TRUE(cursor_shape.has_height()); |
| 189 EXPECT_EQ(kCursorHeight, cursor_shape.height()); |
| 190 EXPECT_TRUE(cursor_shape.has_hotspot_x()); |
| 191 EXPECT_EQ(kHotspotX, cursor_shape.hotspot_x()); |
| 192 EXPECT_TRUE(cursor_shape.has_hotspot_y()); |
| 193 EXPECT_EQ(kHotspotY, cursor_shape.hotspot_y()); |
| 194 EXPECT_TRUE(cursor_shape.has_data()); |
| 195 EXPECT_EQ(kCursorWidth * kCursorHeight * webrtc::DesktopFrame::kBytesPerPixel, |
| 196 cursor_shape.data().size()); |
| 197 } |
| 198 |
| 149 // This test mocks capturer, encoder and network layer to simulate one capture | 199 // This test mocks capturer, encoder and network layer to simulate one capture |
| 150 // cycle. When the first encoded packet is submitted to the network | 200 // cycle. When the first encoded packet is submitted to the network |
| 151 // VideoScheduler is instructed to come to a complete stop. We expect the stop | 201 // VideoScheduler is instructed to come to a complete stop. We expect the stop |
| 152 // sequence to be executed successfully. | 202 // sequence to be executed successfully. |
| 153 TEST_F(VideoSchedulerTest, StartAndStop) { | 203 TEST_F(VideoSchedulerTest, StartAndStop) { |
| 154 scoped_ptr<webrtc::MockScreenCapturer> capturer( | 204 scoped_ptr<webrtc::MockScreenCapturer> capturer( |
| 155 new webrtc::MockScreenCapturer()); | 205 new webrtc::MockScreenCapturer()); |
| 206 scoped_ptr<MockMouseCursorMonitor> cursor_monitor( |
| 207 new MockMouseCursorMonitor()); |
| 208 |
| 209 { |
| 210 InSequence s; |
| 211 |
| 212 EXPECT_CALL(*cursor_monitor, Init(_, _)) |
| 213 .WillOnce( |
| 214 Invoke(this, &VideoSchedulerTest::OnMouseCursorMonitorInit)); |
| 215 |
| 216 EXPECT_CALL(*cursor_monitor, Capture()) |
| 217 .WillRepeatedly(Invoke(this, &VideoSchedulerTest::OnCaptureMouse)); |
| 218 } |
| 219 |
| 156 Expectation capturer_start = | 220 Expectation capturer_start = |
| 157 EXPECT_CALL(*capturer, Start(_)) | 221 EXPECT_CALL(*capturer, Start(_)) |
| 158 .WillOnce(Invoke(this, &VideoSchedulerTest::OnCapturerStart)); | 222 .WillOnce(Invoke(this, &VideoSchedulerTest::OnCapturerStart)); |
| 159 | 223 |
| 160 frame_.reset(new webrtc::BasicDesktopFrame( | 224 frame_.reset(new webrtc::BasicDesktopFrame( |
| 161 webrtc::DesktopSize(kWidth, kHeight))); | 225 webrtc::DesktopSize(kWidth, kHeight))); |
| 162 | 226 |
| 227 mouse_cursor_.reset(new webrtc::MouseCursor( |
| 228 new webrtc::BasicDesktopFrame(webrtc::DesktopSize(kCursorWidth, |
| 229 kCursorHeight)), |
| 230 webrtc::DesktopVector(kHotspotX, kHotspotY))); |
| 231 |
| 163 // First the capturer is called. | 232 // First the capturer is called. |
| 164 Expectation capturer_capture = EXPECT_CALL(*capturer, Capture(_)) | 233 Expectation capturer_capture = EXPECT_CALL(*capturer, Capture(_)) |
| 165 .After(capturer_start) | 234 .After(capturer_start) |
| 166 .WillRepeatedly(Invoke(this, &VideoSchedulerTest::OnCaptureFrame)); | 235 .WillRepeatedly(Invoke(this, &VideoSchedulerTest::OnCaptureFrame)); |
| 167 | 236 |
| 168 // Expect the encoder be called. | 237 // Expect the encoder be called. |
| 169 EXPECT_CALL(*encoder_, EncodePtr(_)) | 238 EXPECT_CALL(*encoder_, EncodePtr(_)) |
| 170 .WillRepeatedly(FinishEncode()); | 239 .WillRepeatedly(FinishEncode()); |
| 171 | 240 |
| 172 // By default delete the arguments when ProcessVideoPacket is received. | 241 // By default delete the arguments when ProcessVideoPacket is received. |
| 173 EXPECT_CALL(video_stub_, ProcessVideoPacketPtr(_, _)) | 242 EXPECT_CALL(video_stub_, ProcessVideoPacketPtr(_, _)) |
| 174 .WillRepeatedly(FinishSend()); | 243 .WillRepeatedly(FinishSend()); |
| 175 | 244 |
| 245 EXPECT_CALL(client_stub_, SetCursorShape(_)) |
| 246 .WillOnce(Invoke(this, &VideoSchedulerTest::SetCursorShape)); |
| 247 |
| 176 // For the first time when ProcessVideoPacket is received we stop the | 248 // For the first time when ProcessVideoPacket is received we stop the |
| 177 // VideoScheduler. | 249 // VideoScheduler. |
| 178 EXPECT_CALL(video_stub_, ProcessVideoPacketPtr(_, _)) | 250 EXPECT_CALL(video_stub_, ProcessVideoPacketPtr(_, _)) |
| 179 .WillOnce(DoAll( | 251 .WillOnce(DoAll( |
| 180 FinishSend(), | 252 FinishSend(), |
| 181 InvokeWithoutArgs(this, &VideoSchedulerTest::StopVideoScheduler))) | 253 InvokeWithoutArgs(this, &VideoSchedulerTest::StopVideoScheduler))) |
| 182 .RetiresOnSaturation(); | 254 .RetiresOnSaturation(); |
| 183 | 255 |
| 184 // Start video frame capture. | 256 // Start video frame capture. |
| 185 StartVideoScheduler(capturer.PassAs<webrtc::ScreenCapturer>()); | 257 StartVideoScheduler(capturer.PassAs<webrtc::ScreenCapturer>(), |
| 258 cursor_monitor.PassAs<webrtc::MouseCursorMonitor>()); |
| 186 | 259 |
| 187 task_runner_ = NULL; | 260 task_runner_ = NULL; |
| 188 run_loop_.Run(); | 261 run_loop_.Run(); |
| 189 } | 262 } |
| 190 | 263 |
| 191 } // namespace remoting | 264 } // namespace remoting |
| OLD | NEW |