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.h" | 8 #include "base/message_loop.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "media/video/capture/screen/screen_capture_data.h" |
| 11 #include "media/video/capture/screen/screen_capturer_mock_objects.h" |
10 #include "remoting/base/auto_thread_task_runner.h" | 12 #include "remoting/base/auto_thread_task_runner.h" |
11 #include "remoting/capturer/capture_data.h" | |
12 #include "remoting/capturer/video_capturer_mock_objects.h" | |
13 #include "remoting/codec/video_encoder.h" | 13 #include "remoting/codec/video_encoder.h" |
14 #include "remoting/proto/video.pb.h" | 14 #include "remoting/proto/video.pb.h" |
15 #include "remoting/protocol/protocol_mock_objects.h" | 15 #include "remoting/protocol/protocol_mock_objects.h" |
16 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 | 18 |
19 using ::remoting::protocol::MockClientStub; | 19 using ::remoting::protocol::MockClientStub; |
20 using ::remoting::protocol::MockVideoStub; | 20 using ::remoting::protocol::MockVideoStub; |
21 | 21 |
22 using ::testing::_; | 22 using ::testing::_; |
(...skipping 26 matching lines...) Expand all Loading... |
49 | 49 |
50 static const int kWidth = 640; | 50 static const int kWidth = 640; |
51 static const int kHeight = 480; | 51 static const int kHeight = 480; |
52 | 52 |
53 class MockVideoEncoder : public VideoEncoder { | 53 class MockVideoEncoder : public VideoEncoder { |
54 public: | 54 public: |
55 MockVideoEncoder(); | 55 MockVideoEncoder(); |
56 virtual ~MockVideoEncoder(); | 56 virtual ~MockVideoEncoder(); |
57 | 57 |
58 MOCK_METHOD3(Encode, void( | 58 MOCK_METHOD3(Encode, void( |
59 scoped_refptr<CaptureData> capture_data, | 59 scoped_refptr<media::ScreenCaptureData> capture_data, |
60 bool key_frame, | 60 bool key_frame, |
61 const DataAvailableCallback& data_available_callback)); | 61 const DataAvailableCallback& data_available_callback)); |
62 | 62 |
63 private: | 63 private: |
64 DISALLOW_COPY_AND_ASSIGN(MockVideoEncoder); | 64 DISALLOW_COPY_AND_ASSIGN(MockVideoEncoder); |
65 }; | 65 }; |
66 | 66 |
67 MockVideoEncoder::MockVideoEncoder() {} | 67 MockVideoEncoder::MockVideoEncoder() {} |
68 | 68 |
69 MockVideoEncoder::~MockVideoEncoder() {} | 69 MockVideoEncoder::~MockVideoEncoder() {} |
70 | 70 |
71 class VideoSchedulerTest : public testing::Test { | 71 class VideoSchedulerTest : public testing::Test { |
72 public: | 72 public: |
73 VideoSchedulerTest(); | 73 VideoSchedulerTest(); |
74 | 74 |
75 virtual void SetUp() OVERRIDE; | 75 virtual void SetUp() OVERRIDE; |
76 | 76 |
77 void StartVideoScheduler(scoped_ptr<VideoFrameCapturer> capturer); | 77 void StartVideoScheduler(scoped_ptr<media::ScreenCapturer> capturer); |
78 void StopVideoScheduler(); | 78 void StopVideoScheduler(); |
79 | 79 |
80 // VideoFrameCapturer mocks. | 80 // media::ScreenCapturer mocks. |
81 void OnCapturerStart(VideoFrameCapturer::Delegate* delegate); | 81 void OnCapturerStart(media::ScreenCapturer::Delegate* delegate); |
82 void OnCapturerStop(); | 82 void OnCapturerStop(); |
83 void OnCaptureFrame(); | 83 void OnCaptureFrame(); |
84 | 84 |
85 protected: | 85 protected: |
86 MessageLoop message_loop_; | 86 MessageLoop message_loop_; |
87 base::RunLoop run_loop_; | 87 base::RunLoop run_loop_; |
88 scoped_refptr<AutoThreadTaskRunner> task_runner_; | 88 scoped_refptr<AutoThreadTaskRunner> task_runner_; |
89 scoped_refptr<VideoScheduler> scheduler_; | 89 scoped_refptr<VideoScheduler> scheduler_; |
90 | 90 |
91 MockClientStub client_stub_; | 91 MockClientStub client_stub_; |
92 MockVideoStub video_stub_; | 92 MockVideoStub video_stub_; |
93 | 93 |
94 // The following mock objects are owned by VideoScheduler. | 94 // The following mock objects are owned by VideoScheduler. |
95 MockVideoEncoder* encoder_; | 95 MockVideoEncoder* encoder_; |
96 | 96 |
97 scoped_refptr<CaptureData> data_; | 97 scoped_refptr<media::ScreenCaptureData> data_; |
98 | 98 |
99 // Points to the delegate passed to VideoFrameCapturer::Start(). | 99 // Points to the delegate passed to media::ScreenCapturer::Start(). |
100 VideoFrameCapturer::Delegate* capturer_delegate_; | 100 media::ScreenCapturer::Delegate* capturer_delegate_; |
101 | 101 |
102 private: | 102 private: |
103 DISALLOW_COPY_AND_ASSIGN(VideoSchedulerTest); | 103 DISALLOW_COPY_AND_ASSIGN(VideoSchedulerTest); |
104 }; | 104 }; |
105 | 105 |
106 VideoSchedulerTest::VideoSchedulerTest() | 106 VideoSchedulerTest::VideoSchedulerTest() |
107 : encoder_(NULL), | 107 : encoder_(NULL), |
108 capturer_delegate_(NULL) { | 108 capturer_delegate_(NULL) { |
109 } | 109 } |
110 | 110 |
111 void VideoSchedulerTest::SetUp() { | 111 void VideoSchedulerTest::SetUp() { |
112 task_runner_ = new AutoThreadTaskRunner( | 112 task_runner_ = new AutoThreadTaskRunner( |
113 message_loop_.message_loop_proxy(), run_loop_.QuitClosure()); | 113 message_loop_.message_loop_proxy(), run_loop_.QuitClosure()); |
114 | 114 |
115 encoder_ = new MockVideoEncoder(); | 115 encoder_ = new MockVideoEncoder(); |
116 } | 116 } |
117 | 117 |
118 void VideoSchedulerTest::StartVideoScheduler( | 118 void VideoSchedulerTest::StartVideoScheduler( |
119 scoped_ptr<VideoFrameCapturer> capturer) { | 119 scoped_ptr<media::ScreenCapturer> capturer) { |
120 scheduler_ = VideoScheduler::Create( | 120 scheduler_ = VideoScheduler::Create( |
121 task_runner_, // Capture | 121 task_runner_, // Capture |
122 task_runner_, // Encode | 122 task_runner_, // Encode |
123 task_runner_, // Network | 123 task_runner_, // Network |
124 capturer.Pass(), | 124 capturer.Pass(), |
125 scoped_ptr<VideoEncoder>(encoder_), | 125 scoped_ptr<VideoEncoder>(encoder_), |
126 &client_stub_, | 126 &client_stub_, |
127 &video_stub_); | 127 &video_stub_); |
128 } | 128 } |
129 | 129 |
130 void VideoSchedulerTest::StopVideoScheduler() { | 130 void VideoSchedulerTest::StopVideoScheduler() { |
131 scheduler_->Stop(); | 131 scheduler_->Stop(); |
132 scheduler_ = NULL; | 132 scheduler_ = NULL; |
133 } | 133 } |
134 | 134 |
135 void VideoSchedulerTest::OnCapturerStart( | 135 void VideoSchedulerTest::OnCapturerStart( |
136 VideoFrameCapturer::Delegate* delegate) { | 136 media::ScreenCapturer::Delegate* delegate) { |
137 EXPECT_FALSE(capturer_delegate_); | 137 EXPECT_FALSE(capturer_delegate_); |
138 EXPECT_TRUE(delegate); | 138 EXPECT_TRUE(delegate); |
139 | 139 |
140 capturer_delegate_ = delegate; | 140 capturer_delegate_ = delegate; |
141 } | 141 } |
142 | 142 |
143 void VideoSchedulerTest::OnCapturerStop() { | 143 void VideoSchedulerTest::OnCapturerStop() { |
144 capturer_delegate_ = NULL; | 144 capturer_delegate_ = NULL; |
145 } | 145 } |
146 | 146 |
147 void VideoSchedulerTest::OnCaptureFrame() { | 147 void VideoSchedulerTest::OnCaptureFrame() { |
148 SkRegion update_region(SkIRect::MakeXYWH(0, 0, 10, 10)); | 148 SkRegion update_region(SkIRect::MakeXYWH(0, 0, 10, 10)); |
149 data_->mutable_dirty_region().op(update_region, SkRegion::kUnion_Op); | 149 data_->mutable_dirty_region().op(update_region, SkRegion::kUnion_Op); |
150 | 150 |
151 capturer_delegate_->OnCaptureCompleted(data_); | 151 capturer_delegate_->OnCaptureCompleted(data_); |
152 } | 152 } |
153 | 153 |
154 // This test mocks capturer, encoder and network layer to simulate one capture | 154 // This test mocks capturer, encoder and network layer to simulate one capture |
155 // cycle. When the first encoded packet is submitted to the network | 155 // cycle. When the first encoded packet is submitted to the network |
156 // VideoScheduler is instructed to come to a complete stop. We expect the stop | 156 // VideoScheduler is instructed to come to a complete stop. We expect the stop |
157 // sequence to be executed successfully. | 157 // sequence to be executed successfully. |
158 TEST_F(VideoSchedulerTest, StartAndStop) { | 158 TEST_F(VideoSchedulerTest, StartAndStop) { |
159 scoped_ptr<MockVideoFrameCapturer> capturer_(new MockVideoFrameCapturer()); | 159 scoped_ptr<media::MockScreenCapturer> capturer( |
| 160 new media::MockScreenCapturer()); |
160 Expectation capturer_start = | 161 Expectation capturer_start = |
161 EXPECT_CALL(*capturer_, Start(_)) | 162 EXPECT_CALL(*capturer, Start(_)) |
162 .WillOnce(Invoke(this, &VideoSchedulerTest::OnCapturerStart)); | 163 .WillOnce(Invoke(this, &VideoSchedulerTest::OnCapturerStart)); |
163 | 164 |
164 data_ = new CaptureData(NULL, kWidth * CaptureData::kBytesPerPixel, | 165 data_ = new media::ScreenCaptureData( |
165 SkISize::Make(kWidth, kHeight)); | 166 NULL, kWidth * media::ScreenCaptureData::kBytesPerPixel, |
| 167 SkISize::Make(kWidth, kHeight)); |
166 | 168 |
167 // First the capturer is called. | 169 // First the capturer is called. |
168 Expectation capturer_capture = EXPECT_CALL(*capturer_, CaptureFrame()) | 170 Expectation capturer_capture = EXPECT_CALL(*capturer, CaptureFrame()) |
169 .After(capturer_start) | 171 .After(capturer_start) |
170 .WillRepeatedly(Invoke(this, &VideoSchedulerTest::OnCaptureFrame)); | 172 .WillRepeatedly(Invoke(this, &VideoSchedulerTest::OnCaptureFrame)); |
171 | 173 |
172 // Expect the encoder be called. | 174 // Expect the encoder be called. |
173 EXPECT_CALL(*encoder_, Encode(data_, false, _)) | 175 EXPECT_CALL(*encoder_, Encode(data_, false, _)) |
174 .WillRepeatedly(FinishEncode()); | 176 .WillRepeatedly(FinishEncode()); |
175 | 177 |
176 // By default delete the arguments when ProcessVideoPacket is received. | 178 // By default delete the arguments when ProcessVideoPacket is received. |
177 EXPECT_CALL(video_stub_, ProcessVideoPacketPtr(_, _)) | 179 EXPECT_CALL(video_stub_, ProcessVideoPacketPtr(_, _)) |
178 .WillRepeatedly(FinishSend()); | 180 .WillRepeatedly(FinishSend()); |
179 | 181 |
180 // For the first time when ProcessVideoPacket is received we stop the | 182 // For the first time when ProcessVideoPacket is received we stop the |
181 // VideoScheduler. | 183 // VideoScheduler. |
182 EXPECT_CALL(video_stub_, ProcessVideoPacketPtr(_, _)) | 184 EXPECT_CALL(video_stub_, ProcessVideoPacketPtr(_, _)) |
183 .WillOnce(DoAll( | 185 .WillOnce(DoAll( |
184 FinishSend(), | 186 FinishSend(), |
185 InvokeWithoutArgs(this, &VideoSchedulerTest::StopVideoScheduler))) | 187 InvokeWithoutArgs(this, &VideoSchedulerTest::StopVideoScheduler))) |
186 .RetiresOnSaturation(); | 188 .RetiresOnSaturation(); |
187 | 189 |
188 EXPECT_CALL(*capturer_, Stop()) | 190 EXPECT_CALL(*capturer, Stop()) |
189 .After(capturer_capture) | 191 .After(capturer_capture) |
190 .WillOnce(Invoke(this, &VideoSchedulerTest::OnCapturerStop)); | 192 .WillOnce(Invoke(this, &VideoSchedulerTest::OnCapturerStop)); |
191 | 193 |
192 // Start video frame capture. | 194 // Start video frame capture. |
193 StartVideoScheduler(capturer_.PassAs<VideoFrameCapturer>()); | 195 StartVideoScheduler(capturer.PassAs<media::ScreenCapturer>()); |
194 | 196 |
195 task_runner_ = NULL; | 197 task_runner_ = NULL; |
196 run_loop_.Run(); | 198 run_loop_.Run(); |
197 } | 199 } |
198 | 200 |
199 } // namespace remoting | 201 } // namespace remoting |
OLD | NEW |