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