OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/message_loop/message_loop.h" | 6 #include "base/message_loop/message_loop.h" |
7 #include "base/test/simple_test_tick_clock.h" | |
7 #include "cc/layers/video_frame_provider.h" | 8 #include "cc/layers/video_frame_provider.h" |
8 #include "media/base/video_frame.h" | 9 #include "media/base/video_frame.h" |
9 #include "media/blink/video_frame_compositor.h" | 10 #include "media/blink/video_frame_compositor.h" |
11 #include "testing/gmock/include/gmock/gmock.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
11 | 13 |
14 using testing::_; | |
15 using testing::Return; | |
16 | |
12 namespace media { | 17 namespace media { |
13 | 18 |
14 class VideoFrameCompositorTest : public testing::Test, | 19 class VideoFrameCompositorTest : public testing::Test, |
15 public cc::VideoFrameProvider::Client { | 20 public cc::VideoFrameProvider::Client, |
21 public VideoRendererSink::RenderCallback { | |
16 public: | 22 public: |
17 VideoFrameCompositorTest() | 23 VideoFrameCompositorTest() |
18 : compositor_(new VideoFrameCompositor( | 24 : tick_clock_(new base::SimpleTestTickClock()), |
25 compositor_(new VideoFrameCompositor( | |
19 message_loop.task_runner(), | 26 message_loop.task_runner(), |
20 base::Bind(&VideoFrameCompositorTest::NaturalSizeChanged, | 27 base::Bind(&VideoFrameCompositorTest::NaturalSizeChanged, |
21 base::Unretained(this)), | 28 base::Unretained(this)), |
22 base::Bind(&VideoFrameCompositorTest::OpacityChanged, | 29 base::Bind(&VideoFrameCompositorTest::OpacityChanged, |
23 base::Unretained(this)))), | 30 base::Unretained(this)))), |
24 did_receive_frame_count_(0), | 31 did_receive_frame_count_(0), |
25 natural_size_changed_count_(0), | 32 natural_size_changed_count_(0), |
26 opacity_changed_count_(0), | 33 opacity_changed_count_(0), |
27 opaque_(false) { | 34 opaque_(false) { |
28 compositor_->SetVideoFrameProviderClient(this); | 35 compositor_->SetVideoFrameProviderClient(this); |
36 compositor_->set_tick_clock_for_testing( | |
37 scoped_ptr<base::TickClock>(tick_clock_)); | |
29 } | 38 } |
30 | 39 |
31 ~VideoFrameCompositorTest() override { | 40 ~VideoFrameCompositorTest() override { |
32 compositor_->SetVideoFrameProviderClient(NULL); | 41 compositor_->SetVideoFrameProviderClient(NULL); |
33 } | 42 } |
34 | 43 |
35 VideoFrameCompositor* compositor() { return compositor_.get(); } | 44 VideoFrameCompositor* compositor() { return compositor_.get(); } |
36 int did_receive_frame_count() { return did_receive_frame_count_; } | 45 int did_receive_frame_count() { return did_receive_frame_count_; } |
37 int natural_size_changed_count() { return natural_size_changed_count_; } | 46 int natural_size_changed_count() { return natural_size_changed_count_; } |
38 gfx::Size natural_size() { return natural_size_; } | 47 gfx::Size natural_size() { return natural_size_; } |
39 | 48 |
40 int opacity_changed_count() { return opacity_changed_count_; } | 49 int opacity_changed_count() { return opacity_changed_count_; } |
41 bool opaque() { return opaque_; } | 50 bool opaque() { return opaque_; } |
42 | 51 |
43 private: | 52 protected: |
44 // cc::VideoFrameProvider::Client implementation. | 53 // cc::VideoFrameProvider::Client implementation. |
45 void StopUsingProvider() override {} | 54 void StopUsingProvider() override {} |
46 void StartRendering() override {}; | 55 MOCK_METHOD0(StartRendering, void()); |
47 void StopRendering() override {}; | 56 MOCK_METHOD0(StopRendering, void()); |
48 void DidReceiveFrame() override { | 57 void DidReceiveFrame() override { ++did_receive_frame_count_; } |
49 ++did_receive_frame_count_; | |
50 } | |
51 void DidUpdateMatrix(const float* matrix) override {} | 58 void DidUpdateMatrix(const float* matrix) override {} |
52 | 59 |
60 // VideoRendererSink::RenderCallback implementation. | |
61 MOCK_METHOD2(Render, | |
62 scoped_refptr<VideoFrame>(base::TimeTicks, base::TimeTicks)); | |
63 MOCK_METHOD0(OnFrameDropped, void()); | |
64 | |
53 void NaturalSizeChanged(gfx::Size natural_size) { | 65 void NaturalSizeChanged(gfx::Size natural_size) { |
54 ++natural_size_changed_count_; | 66 ++natural_size_changed_count_; |
55 natural_size_ = natural_size; | 67 natural_size_ = natural_size; |
56 } | 68 } |
57 | 69 |
58 void OpacityChanged(bool opaque) { | 70 void OpacityChanged(bool opaque) { |
59 ++opacity_changed_count_; | 71 ++opacity_changed_count_; |
60 opaque_ = opaque; | 72 opaque_ = opaque; |
61 } | 73 } |
62 | 74 |
75 void StartVideoRendererSink() { | |
76 EXPECT_CALL(*this, StartRendering()); | |
77 const bool had_current_frame = !!compositor_->GetCurrentFrame(); | |
78 compositor()->Start(this); | |
79 // If we previously had a frame, we should still have one now. | |
80 EXPECT_EQ(had_current_frame, !!compositor_->GetCurrentFrame()); | |
81 message_loop.RunUntilIdle(); | |
82 } | |
83 | |
84 void StopVideoRendererSink() { | |
85 EXPECT_CALL(*this, StopRendering()); | |
86 compositor()->Stop(); | |
87 message_loop.RunUntilIdle(); | |
88 | |
89 // We should still have a frame after stop is called. | |
90 EXPECT_TRUE(compositor_->GetCurrentFrame()); | |
91 } | |
92 | |
93 void RenderFrame() { | |
94 compositor()->GetCurrentFrame(); | |
95 compositor()->PutCurrentFrame(); | |
96 } | |
97 | |
63 base::MessageLoop message_loop; | 98 base::MessageLoop message_loop; |
99 base::SimpleTestTickClock* tick_clock_; // Owned by |compositor_| | |
64 scoped_ptr<VideoFrameCompositor> compositor_; | 100 scoped_ptr<VideoFrameCompositor> compositor_; |
101 | |
65 int did_receive_frame_count_; | 102 int did_receive_frame_count_; |
66 int natural_size_changed_count_; | 103 int natural_size_changed_count_; |
67 gfx::Size natural_size_; | 104 gfx::Size natural_size_; |
68 int opacity_changed_count_; | 105 int opacity_changed_count_; |
69 bool opaque_; | 106 bool opaque_; |
70 | 107 |
108 | |
xhwang
2015/04/29 22:42:03
nit: no need for extra line?
DaleCurtis
2015/04/30 03:49:37
Done.
| |
71 DISALLOW_COPY_AND_ASSIGN(VideoFrameCompositorTest); | 109 DISALLOW_COPY_AND_ASSIGN(VideoFrameCompositorTest); |
72 }; | 110 }; |
73 | 111 |
74 TEST_F(VideoFrameCompositorTest, InitialValues) { | 112 TEST_F(VideoFrameCompositorTest, InitialValues) { |
75 EXPECT_FALSE(compositor()->GetCurrentFrame().get()); | 113 EXPECT_FALSE(compositor()->GetCurrentFrame().get()); |
76 } | 114 } |
77 | 115 |
78 TEST_F(VideoFrameCompositorTest, PaintFrameUsingOldRenderingPath) { | 116 TEST_F(VideoFrameCompositorTest, PaintFrameUsingOldRenderingPath) { |
79 scoped_refptr<VideoFrame> expected = VideoFrame::CreateEOSFrame(); | 117 scoped_refptr<VideoFrame> expected = VideoFrame::CreateEOSFrame(); |
80 | 118 |
81 // Should notify compositor synchronously. | 119 // Should notify compositor synchronously. |
82 EXPECT_EQ(0, did_receive_frame_count()); | 120 EXPECT_EQ(0, did_receive_frame_count()); |
83 compositor()->PaintFrameUsingOldRenderingPath(expected); | 121 compositor()->PaintFrameUsingOldRenderingPath(expected); |
84 scoped_refptr<VideoFrame> actual = compositor()->GetCurrentFrame(); | 122 scoped_refptr<VideoFrame> actual = compositor()->GetCurrentFrame(); |
85 EXPECT_EQ(expected, actual); | 123 EXPECT_EQ(expected, actual); |
86 EXPECT_EQ(1, did_receive_frame_count()); | 124 EXPECT_EQ(1, did_receive_frame_count()); |
87 } | 125 } |
88 | 126 |
89 TEST_F(VideoFrameCompositorTest, NaturalSizeChanged) { | 127 TEST_F(VideoFrameCompositorTest, NaturalSizeChanged) { |
90 gfx::Size initial_size(8, 8); | 128 gfx::Size initial_size(8, 8); |
91 scoped_refptr<VideoFrame> initial_frame = | 129 scoped_refptr<VideoFrame> initial_frame = |
92 VideoFrame::CreateBlackFrame(initial_size); | 130 VideoFrame::CreateBlackFrame(initial_size); |
93 | 131 |
94 gfx::Size larger_size(16, 16); | 132 gfx::Size larger_size(16, 16); |
95 scoped_refptr<VideoFrame> larger_frame = | 133 scoped_refptr<VideoFrame> larger_frame = |
96 VideoFrame::CreateBlackFrame(larger_size); | 134 VideoFrame::CreateBlackFrame(larger_size); |
97 | 135 |
136 gfx::Size empty_size(0, 0); | |
137 | |
98 // Initial expectations. | 138 // Initial expectations. |
99 EXPECT_EQ(0, natural_size().width()); | 139 EXPECT_EQ(empty_size, natural_size()); |
100 EXPECT_EQ(0, natural_size().height()); | |
101 EXPECT_EQ(0, natural_size_changed_count()); | 140 EXPECT_EQ(0, natural_size_changed_count()); |
102 | 141 |
103 // Callback isn't fired for the first frame. | 142 // Callback isn't fired for the first frame. |
104 compositor()->PaintFrameUsingOldRenderingPath(initial_frame); | 143 compositor()->PaintFrameUsingOldRenderingPath(initial_frame); |
105 EXPECT_EQ(0, natural_size().width()); | 144 EXPECT_EQ(empty_size, natural_size()); |
106 EXPECT_EQ(0, natural_size().height()); | |
107 EXPECT_EQ(0, natural_size_changed_count()); | 145 EXPECT_EQ(0, natural_size_changed_count()); |
108 | 146 |
109 // Callback should be fired once. | 147 // Callback should be fired once. |
110 compositor()->PaintFrameUsingOldRenderingPath(larger_frame); | 148 compositor()->PaintFrameUsingOldRenderingPath(larger_frame); |
111 EXPECT_EQ(larger_size.width(), natural_size().width()); | 149 EXPECT_EQ(larger_size, natural_size()); |
112 EXPECT_EQ(larger_size.height(), natural_size().height()); | |
113 EXPECT_EQ(1, natural_size_changed_count()); | 150 EXPECT_EQ(1, natural_size_changed_count()); |
114 | 151 |
115 compositor()->PaintFrameUsingOldRenderingPath(larger_frame); | 152 compositor()->PaintFrameUsingOldRenderingPath(larger_frame); |
116 EXPECT_EQ(larger_size.width(), natural_size().width()); | 153 EXPECT_EQ(larger_size, natural_size()); |
117 EXPECT_EQ(larger_size.height(), natural_size().height()); | |
118 EXPECT_EQ(1, natural_size_changed_count()); | 154 EXPECT_EQ(1, natural_size_changed_count()); |
119 | 155 |
120 // Callback is fired once more when switching back to initial size. | 156 // Callback is fired once more when switching back to initial size. |
121 compositor()->PaintFrameUsingOldRenderingPath(initial_frame); | 157 compositor()->PaintFrameUsingOldRenderingPath(initial_frame); |
122 EXPECT_EQ(initial_size.width(), natural_size().width()); | 158 EXPECT_EQ(initial_size, natural_size()); |
123 EXPECT_EQ(initial_size.height(), natural_size().height()); | |
124 EXPECT_EQ(2, natural_size_changed_count()); | 159 EXPECT_EQ(2, natural_size_changed_count()); |
125 | 160 |
126 compositor()->PaintFrameUsingOldRenderingPath(initial_frame); | 161 compositor()->PaintFrameUsingOldRenderingPath(initial_frame); |
127 EXPECT_EQ(initial_size.width(), natural_size().width()); | |
128 EXPECT_EQ(initial_size, natural_size()); | 162 EXPECT_EQ(initial_size, natural_size()); |
129 EXPECT_EQ(2, natural_size_changed_count()); | 163 EXPECT_EQ(2, natural_size_changed_count()); |
164 | |
165 natural_size_changed_count_ = 0; | |
166 natural_size_ = empty_size; | |
167 compositor()->clear_current_frame_for_testing(); | |
168 | |
169 StartVideoRendererSink(); | |
170 EXPECT_CALL(*this, Render(_, _)) | |
171 .WillOnce(Return(initial_frame)) | |
172 .WillOnce(Return(larger_frame)) | |
173 .WillOnce(Return(initial_frame)) | |
174 .WillOnce(Return(initial_frame)); | |
175 // Callback isn't fired for the first frame. | |
176 EXPECT_EQ(0, natural_size_changed_count()); | |
177 EXPECT_TRUE( | |
178 compositor()->UpdateCurrentFrame(base::TimeTicks(), base::TimeTicks())); | |
179 RenderFrame(); | |
180 EXPECT_EQ(empty_size, natural_size()); | |
181 EXPECT_EQ(0, natural_size_changed_count()); | |
182 | |
183 EXPECT_TRUE( | |
184 compositor()->UpdateCurrentFrame(base::TimeTicks(), base::TimeTicks())); | |
185 RenderFrame(); | |
186 EXPECT_EQ(larger_size, natural_size()); | |
187 EXPECT_EQ(1, natural_size_changed_count()); | |
188 | |
189 EXPECT_TRUE( | |
190 compositor()->UpdateCurrentFrame(base::TimeTicks(), base::TimeTicks())); | |
191 RenderFrame(); | |
192 EXPECT_EQ(initial_size, natural_size()); | |
193 EXPECT_EQ(2, natural_size_changed_count()); | |
194 | |
195 EXPECT_FALSE( | |
196 compositor()->UpdateCurrentFrame(base::TimeTicks(), base::TimeTicks())); | |
197 EXPECT_EQ(initial_size, natural_size()); | |
198 EXPECT_EQ(2, natural_size_changed_count()); | |
199 RenderFrame(); | |
200 | |
201 StopVideoRendererSink(); | |
130 } | 202 } |
131 | 203 |
132 TEST_F(VideoFrameCompositorTest, OpacityChanged) { | 204 TEST_F(VideoFrameCompositorTest, OpacityChanged) { |
133 gfx::Size size(8, 8); | 205 gfx::Size size(8, 8); |
134 gfx::Rect rect(gfx::Point(0, 0), size); | 206 gfx::Rect rect(gfx::Point(0, 0), size); |
135 scoped_refptr<VideoFrame> opaque_frame = VideoFrame::CreateFrame( | 207 scoped_refptr<VideoFrame> opaque_frame = VideoFrame::CreateFrame( |
136 VideoFrame::YV12, size, rect, size, base::TimeDelta()); | 208 VideoFrame::YV12, size, rect, size, base::TimeDelta()); |
137 scoped_refptr<VideoFrame> not_opaque_frame = VideoFrame::CreateFrame( | 209 scoped_refptr<VideoFrame> not_opaque_frame = VideoFrame::CreateFrame( |
138 VideoFrame::YV12A, size, rect, size, base::TimeDelta()); | 210 VideoFrame::YV12A, size, rect, size, base::TimeDelta()); |
139 | 211 |
(...skipping 13 matching lines...) Expand all Loading... | |
153 | 225 |
154 // Callback is fired when using opacity changes. | 226 // Callback is fired when using opacity changes. |
155 compositor()->PaintFrameUsingOldRenderingPath(opaque_frame); | 227 compositor()->PaintFrameUsingOldRenderingPath(opaque_frame); |
156 EXPECT_TRUE(opaque()); | 228 EXPECT_TRUE(opaque()); |
157 EXPECT_EQ(2, opacity_changed_count()); | 229 EXPECT_EQ(2, opacity_changed_count()); |
158 | 230 |
159 // Callback shouldn't be first subsequent times with same opaqueness. | 231 // Callback shouldn't be first subsequent times with same opaqueness. |
160 compositor()->PaintFrameUsingOldRenderingPath(opaque_frame); | 232 compositor()->PaintFrameUsingOldRenderingPath(opaque_frame); |
161 EXPECT_TRUE(opaque()); | 233 EXPECT_TRUE(opaque()); |
162 EXPECT_EQ(2, opacity_changed_count()); | 234 EXPECT_EQ(2, opacity_changed_count()); |
235 | |
236 opacity_changed_count_ = 0; | |
237 compositor()->clear_current_frame_for_testing(); | |
238 | |
239 StartVideoRendererSink(); | |
240 EXPECT_CALL(*this, Render(_, _)) | |
241 .WillOnce(Return(not_opaque_frame)) | |
242 .WillOnce(Return(not_opaque_frame)) | |
243 .WillOnce(Return(opaque_frame)) | |
244 .WillOnce(Return(opaque_frame)); | |
245 EXPECT_EQ(0, opacity_changed_count()); | |
246 EXPECT_TRUE( | |
247 compositor()->UpdateCurrentFrame(base::TimeTicks(), base::TimeTicks())); | |
248 RenderFrame(); | |
249 EXPECT_FALSE(opaque()); | |
250 EXPECT_EQ(1, opacity_changed_count()); | |
251 | |
252 EXPECT_FALSE( | |
253 compositor()->UpdateCurrentFrame(base::TimeTicks(), base::TimeTicks())); | |
254 RenderFrame(); | |
255 EXPECT_FALSE(opaque()); | |
256 EXPECT_EQ(1, opacity_changed_count()); | |
257 | |
258 EXPECT_TRUE( | |
259 compositor()->UpdateCurrentFrame(base::TimeTicks(), base::TimeTicks())); | |
260 RenderFrame(); | |
261 EXPECT_TRUE(opaque()); | |
262 EXPECT_EQ(2, opacity_changed_count()); | |
263 | |
264 EXPECT_FALSE( | |
265 compositor()->UpdateCurrentFrame(base::TimeTicks(), base::TimeTicks())); | |
266 EXPECT_TRUE(opaque()); | |
267 EXPECT_EQ(2, opacity_changed_count()); | |
268 RenderFrame(); | |
269 | |
270 StopVideoRendererSink(); | |
271 } | |
272 | |
273 TEST_F(VideoFrameCompositorTest, VideoRendererSinkFrameDropped) { | |
274 gfx::Size size(8, 8); | |
275 gfx::Rect rect(gfx::Point(0, 0), size); | |
276 scoped_refptr<VideoFrame> opaque_frame = VideoFrame::CreateFrame( | |
277 VideoFrame::YV12, size, rect, size, base::TimeDelta()); | |
278 | |
279 StartVideoRendererSink(); | |
280 EXPECT_CALL(*this, Render(_, _)).WillRepeatedly(Return(opaque_frame)); | |
281 EXPECT_TRUE( | |
282 compositor()->UpdateCurrentFrame(base::TimeTicks(), base::TimeTicks())); | |
283 | |
284 // If we don't call RenderFrame() the frame should be reported as dropped. | |
285 EXPECT_CALL(*this, OnFrameDropped()); | |
286 EXPECT_FALSE( | |
287 compositor()->UpdateCurrentFrame(base::TimeTicks(), base::TimeTicks())); | |
288 | |
289 // Ensure it always happens until the frame is rendered. | |
290 EXPECT_CALL(*this, OnFrameDropped()); | |
291 EXPECT_FALSE( | |
292 compositor()->UpdateCurrentFrame(base::TimeTicks(), base::TimeTicks())); | |
293 | |
294 // Call GetCurrentFrame() but not PutCurrentFrame() | |
295 compositor()->GetCurrentFrame(); | |
296 | |
297 // The frame should still register as dropped until PutCurrentFrame is called. | |
298 EXPECT_CALL(*this, OnFrameDropped()); | |
299 EXPECT_FALSE( | |
300 compositor()->UpdateCurrentFrame(base::TimeTicks(), base::TimeTicks())); | |
301 | |
302 RenderFrame(); | |
303 EXPECT_FALSE( | |
304 compositor()->UpdateCurrentFrame(base::TimeTicks(), base::TimeTicks())); | |
305 | |
306 StopVideoRendererSink(); | |
307 } | |
308 | |
309 TEST_F(VideoFrameCompositorTest, GetCurrentFrameAndUpdateIfStale) { | |
310 gfx::Size size(8, 8); | |
311 gfx::Rect rect(gfx::Point(0, 0), size); | |
312 scoped_refptr<VideoFrame> opaque_frame = VideoFrame::CreateFrame( | |
313 VideoFrame::YV12, size, rect, size, base::TimeDelta()); | |
314 scoped_refptr<VideoFrame> opaque_frame_2 = VideoFrame::CreateFrame( | |
315 VideoFrame::YV12, size, rect, size, base::TimeDelta()); | |
316 | |
317 StartVideoRendererSink(); | |
318 EXPECT_CALL(*this, Render(_, _)) | |
319 .WillOnce(Return(opaque_frame)) | |
320 .WillOnce(Return(opaque_frame_2)); | |
321 EXPECT_TRUE( | |
322 compositor()->UpdateCurrentFrame(base::TimeTicks(), base::TimeTicks())); | |
323 | |
324 base::TimeDelta stale_frame_threshold = | |
325 compositor()->get_stale_frame_threshold_for_testing(); | |
326 | |
327 // Advancing time a little bit shouldn't cause the frame to be stale. | |
328 tick_clock_->Advance(stale_frame_threshold / 2); | |
329 EXPECT_EQ(opaque_frame, compositor()->GetCurrentFrameAndUpdateIfStale()); | |
330 | |
331 // Since rendering of frames is likely not happening, this will trigger a | |
332 // dropped frame call. | |
333 EXPECT_CALL(*this, OnFrameDropped()); | |
334 | |
335 // Advancing the clock over the threshold should cause a new frame request. | |
336 tick_clock_->Advance(stale_frame_threshold / 2 + | |
337 base::TimeDelta::FromMicroseconds(1)); | |
338 EXPECT_EQ(opaque_frame_2, compositor()->GetCurrentFrameAndUpdateIfStale()); | |
339 | |
340 StopVideoRendererSink(); | |
163 } | 341 } |
164 | 342 |
165 } // namespace media | 343 } // namespace media |
OLD | NEW |