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 |
71 DISALLOW_COPY_AND_ASSIGN(VideoFrameCompositorTest); | 108 DISALLOW_COPY_AND_ASSIGN(VideoFrameCompositorTest); |
72 }; | 109 }; |
73 | 110 |
74 TEST_F(VideoFrameCompositorTest, InitialValues) { | 111 TEST_F(VideoFrameCompositorTest, InitialValues) { |
(...skipping 13 matching lines...) Expand all Loading... |
88 | 125 |
89 TEST_F(VideoFrameCompositorTest, NaturalSizeChanged) { | 126 TEST_F(VideoFrameCompositorTest, NaturalSizeChanged) { |
90 gfx::Size initial_size(8, 8); | 127 gfx::Size initial_size(8, 8); |
91 scoped_refptr<VideoFrame> initial_frame = | 128 scoped_refptr<VideoFrame> initial_frame = |
92 VideoFrame::CreateBlackFrame(initial_size); | 129 VideoFrame::CreateBlackFrame(initial_size); |
93 | 130 |
94 gfx::Size larger_size(16, 16); | 131 gfx::Size larger_size(16, 16); |
95 scoped_refptr<VideoFrame> larger_frame = | 132 scoped_refptr<VideoFrame> larger_frame = |
96 VideoFrame::CreateBlackFrame(larger_size); | 133 VideoFrame::CreateBlackFrame(larger_size); |
97 | 134 |
| 135 gfx::Size empty_size(0, 0); |
| 136 |
98 // Initial expectations. | 137 // Initial expectations. |
99 EXPECT_EQ(0, natural_size().width()); | 138 EXPECT_EQ(empty_size, natural_size()); |
100 EXPECT_EQ(0, natural_size().height()); | |
101 EXPECT_EQ(0, natural_size_changed_count()); | 139 EXPECT_EQ(0, natural_size_changed_count()); |
102 | 140 |
103 // Callback isn't fired for the first frame. | 141 // Callback isn't fired for the first frame. |
104 compositor()->PaintFrameUsingOldRenderingPath(initial_frame); | 142 compositor()->PaintFrameUsingOldRenderingPath(initial_frame); |
105 EXPECT_EQ(0, natural_size().width()); | 143 EXPECT_EQ(empty_size, natural_size()); |
106 EXPECT_EQ(0, natural_size().height()); | |
107 EXPECT_EQ(0, natural_size_changed_count()); | 144 EXPECT_EQ(0, natural_size_changed_count()); |
108 | 145 |
109 // Callback should be fired once. | 146 // Callback should be fired once. |
110 compositor()->PaintFrameUsingOldRenderingPath(larger_frame); | 147 compositor()->PaintFrameUsingOldRenderingPath(larger_frame); |
111 EXPECT_EQ(larger_size.width(), natural_size().width()); | 148 EXPECT_EQ(larger_size, natural_size()); |
112 EXPECT_EQ(larger_size.height(), natural_size().height()); | |
113 EXPECT_EQ(1, natural_size_changed_count()); | 149 EXPECT_EQ(1, natural_size_changed_count()); |
114 | 150 |
115 compositor()->PaintFrameUsingOldRenderingPath(larger_frame); | 151 compositor()->PaintFrameUsingOldRenderingPath(larger_frame); |
116 EXPECT_EQ(larger_size.width(), natural_size().width()); | 152 EXPECT_EQ(larger_size, natural_size()); |
117 EXPECT_EQ(larger_size.height(), natural_size().height()); | |
118 EXPECT_EQ(1, natural_size_changed_count()); | 153 EXPECT_EQ(1, natural_size_changed_count()); |
119 | 154 |
120 // Callback is fired once more when switching back to initial size. | 155 // Callback is fired once more when switching back to initial size. |
121 compositor()->PaintFrameUsingOldRenderingPath(initial_frame); | 156 compositor()->PaintFrameUsingOldRenderingPath(initial_frame); |
122 EXPECT_EQ(initial_size.width(), natural_size().width()); | 157 EXPECT_EQ(initial_size, natural_size()); |
123 EXPECT_EQ(initial_size.height(), natural_size().height()); | |
124 EXPECT_EQ(2, natural_size_changed_count()); | 158 EXPECT_EQ(2, natural_size_changed_count()); |
125 | 159 |
126 compositor()->PaintFrameUsingOldRenderingPath(initial_frame); | 160 compositor()->PaintFrameUsingOldRenderingPath(initial_frame); |
127 EXPECT_EQ(initial_size.width(), natural_size().width()); | |
128 EXPECT_EQ(initial_size, natural_size()); | 161 EXPECT_EQ(initial_size, natural_size()); |
129 EXPECT_EQ(2, natural_size_changed_count()); | 162 EXPECT_EQ(2, natural_size_changed_count()); |
| 163 |
| 164 natural_size_changed_count_ = 0; |
| 165 natural_size_ = empty_size; |
| 166 compositor()->clear_current_frame_for_testing(); |
| 167 |
| 168 StartVideoRendererSink(); |
| 169 EXPECT_CALL(*this, Render(_, _)) |
| 170 .WillOnce(Return(initial_frame)) |
| 171 .WillOnce(Return(larger_frame)) |
| 172 .WillOnce(Return(initial_frame)) |
| 173 .WillOnce(Return(initial_frame)); |
| 174 // Callback isn't fired for the first frame. |
| 175 EXPECT_EQ(0, natural_size_changed_count()); |
| 176 EXPECT_TRUE( |
| 177 compositor()->UpdateCurrentFrame(base::TimeTicks(), base::TimeTicks())); |
| 178 RenderFrame(); |
| 179 EXPECT_EQ(empty_size, natural_size()); |
| 180 EXPECT_EQ(0, natural_size_changed_count()); |
| 181 |
| 182 EXPECT_TRUE( |
| 183 compositor()->UpdateCurrentFrame(base::TimeTicks(), base::TimeTicks())); |
| 184 RenderFrame(); |
| 185 EXPECT_EQ(larger_size, natural_size()); |
| 186 EXPECT_EQ(1, natural_size_changed_count()); |
| 187 |
| 188 EXPECT_TRUE( |
| 189 compositor()->UpdateCurrentFrame(base::TimeTicks(), base::TimeTicks())); |
| 190 RenderFrame(); |
| 191 EXPECT_EQ(initial_size, natural_size()); |
| 192 EXPECT_EQ(2, natural_size_changed_count()); |
| 193 |
| 194 EXPECT_FALSE( |
| 195 compositor()->UpdateCurrentFrame(base::TimeTicks(), base::TimeTicks())); |
| 196 EXPECT_EQ(initial_size, natural_size()); |
| 197 EXPECT_EQ(2, natural_size_changed_count()); |
| 198 RenderFrame(); |
| 199 |
| 200 StopVideoRendererSink(); |
130 } | 201 } |
131 | 202 |
132 TEST_F(VideoFrameCompositorTest, OpacityChanged) { | 203 TEST_F(VideoFrameCompositorTest, OpacityChanged) { |
133 gfx::Size size(8, 8); | 204 gfx::Size size(8, 8); |
134 gfx::Rect rect(gfx::Point(0, 0), size); | |
135 scoped_refptr<VideoFrame> opaque_frame = VideoFrame::CreateFrame( | 205 scoped_refptr<VideoFrame> opaque_frame = VideoFrame::CreateFrame( |
136 VideoFrame::YV12, size, rect, size, base::TimeDelta()); | 206 VideoFrame::YV12, size, gfx::Rect(size), size, base::TimeDelta()); |
137 scoped_refptr<VideoFrame> not_opaque_frame = VideoFrame::CreateFrame( | 207 scoped_refptr<VideoFrame> not_opaque_frame = VideoFrame::CreateFrame( |
138 VideoFrame::YV12A, size, rect, size, base::TimeDelta()); | 208 VideoFrame::YV12A, size, gfx::Rect(size), size, base::TimeDelta()); |
139 | 209 |
140 // Initial expectations. | 210 // Initial expectations. |
141 EXPECT_FALSE(opaque()); | 211 EXPECT_FALSE(opaque()); |
142 EXPECT_EQ(0, opacity_changed_count()); | 212 EXPECT_EQ(0, opacity_changed_count()); |
143 | 213 |
144 // Callback is fired for the first frame. | 214 // Callback is fired for the first frame. |
145 compositor()->PaintFrameUsingOldRenderingPath(not_opaque_frame); | 215 compositor()->PaintFrameUsingOldRenderingPath(not_opaque_frame); |
146 EXPECT_FALSE(opaque()); | 216 EXPECT_FALSE(opaque()); |
147 EXPECT_EQ(1, opacity_changed_count()); | 217 EXPECT_EQ(1, opacity_changed_count()); |
148 | 218 |
149 // Callback shouldn't be first subsequent times with same opaqueness. | 219 // Callback shouldn't be first subsequent times with same opaqueness. |
150 compositor()->PaintFrameUsingOldRenderingPath(not_opaque_frame); | 220 compositor()->PaintFrameUsingOldRenderingPath(not_opaque_frame); |
151 EXPECT_FALSE(opaque()); | 221 EXPECT_FALSE(opaque()); |
152 EXPECT_EQ(1, opacity_changed_count()); | 222 EXPECT_EQ(1, opacity_changed_count()); |
153 | 223 |
154 // Callback is fired when using opacity changes. | 224 // Callback is fired when using opacity changes. |
155 compositor()->PaintFrameUsingOldRenderingPath(opaque_frame); | 225 compositor()->PaintFrameUsingOldRenderingPath(opaque_frame); |
156 EXPECT_TRUE(opaque()); | 226 EXPECT_TRUE(opaque()); |
157 EXPECT_EQ(2, opacity_changed_count()); | 227 EXPECT_EQ(2, opacity_changed_count()); |
158 | 228 |
159 // Callback shouldn't be first subsequent times with same opaqueness. | 229 // Callback shouldn't be first subsequent times with same opaqueness. |
160 compositor()->PaintFrameUsingOldRenderingPath(opaque_frame); | 230 compositor()->PaintFrameUsingOldRenderingPath(opaque_frame); |
161 EXPECT_TRUE(opaque()); | 231 EXPECT_TRUE(opaque()); |
162 EXPECT_EQ(2, opacity_changed_count()); | 232 EXPECT_EQ(2, opacity_changed_count()); |
| 233 |
| 234 opacity_changed_count_ = 0; |
| 235 compositor()->clear_current_frame_for_testing(); |
| 236 |
| 237 StartVideoRendererSink(); |
| 238 EXPECT_CALL(*this, Render(_, _)) |
| 239 .WillOnce(Return(not_opaque_frame)) |
| 240 .WillOnce(Return(not_opaque_frame)) |
| 241 .WillOnce(Return(opaque_frame)) |
| 242 .WillOnce(Return(opaque_frame)); |
| 243 EXPECT_EQ(0, opacity_changed_count()); |
| 244 EXPECT_TRUE( |
| 245 compositor()->UpdateCurrentFrame(base::TimeTicks(), base::TimeTicks())); |
| 246 RenderFrame(); |
| 247 EXPECT_FALSE(opaque()); |
| 248 EXPECT_EQ(1, opacity_changed_count()); |
| 249 |
| 250 EXPECT_FALSE( |
| 251 compositor()->UpdateCurrentFrame(base::TimeTicks(), base::TimeTicks())); |
| 252 RenderFrame(); |
| 253 EXPECT_FALSE(opaque()); |
| 254 EXPECT_EQ(1, opacity_changed_count()); |
| 255 |
| 256 EXPECT_TRUE( |
| 257 compositor()->UpdateCurrentFrame(base::TimeTicks(), base::TimeTicks())); |
| 258 RenderFrame(); |
| 259 EXPECT_TRUE(opaque()); |
| 260 EXPECT_EQ(2, opacity_changed_count()); |
| 261 |
| 262 EXPECT_FALSE( |
| 263 compositor()->UpdateCurrentFrame(base::TimeTicks(), base::TimeTicks())); |
| 264 EXPECT_TRUE(opaque()); |
| 265 EXPECT_EQ(2, opacity_changed_count()); |
| 266 RenderFrame(); |
| 267 |
| 268 StopVideoRendererSink(); |
| 269 } |
| 270 |
| 271 TEST_F(VideoFrameCompositorTest, VideoRendererSinkFrameDropped) { |
| 272 gfx::Size size(8, 8); |
| 273 scoped_refptr<VideoFrame> opaque_frame = VideoFrame::CreateFrame( |
| 274 VideoFrame::YV12, size, gfx::Rect(size), size, base::TimeDelta()); |
| 275 |
| 276 StartVideoRendererSink(); |
| 277 EXPECT_CALL(*this, Render(_, _)).WillRepeatedly(Return(opaque_frame)); |
| 278 EXPECT_TRUE( |
| 279 compositor()->UpdateCurrentFrame(base::TimeTicks(), base::TimeTicks())); |
| 280 |
| 281 // If we don't call RenderFrame() the frame should be reported as dropped. |
| 282 EXPECT_CALL(*this, OnFrameDropped()); |
| 283 EXPECT_FALSE( |
| 284 compositor()->UpdateCurrentFrame(base::TimeTicks(), base::TimeTicks())); |
| 285 |
| 286 // Ensure it always happens until the frame is rendered. |
| 287 EXPECT_CALL(*this, OnFrameDropped()); |
| 288 EXPECT_FALSE( |
| 289 compositor()->UpdateCurrentFrame(base::TimeTicks(), base::TimeTicks())); |
| 290 |
| 291 // Call GetCurrentFrame() but not PutCurrentFrame() |
| 292 compositor()->GetCurrentFrame(); |
| 293 |
| 294 // The frame should still register as dropped until PutCurrentFrame is called. |
| 295 EXPECT_CALL(*this, OnFrameDropped()); |
| 296 EXPECT_FALSE( |
| 297 compositor()->UpdateCurrentFrame(base::TimeTicks(), base::TimeTicks())); |
| 298 |
| 299 RenderFrame(); |
| 300 EXPECT_FALSE( |
| 301 compositor()->UpdateCurrentFrame(base::TimeTicks(), base::TimeTicks())); |
| 302 |
| 303 StopVideoRendererSink(); |
| 304 } |
| 305 |
| 306 TEST_F(VideoFrameCompositorTest, GetCurrentFrameAndUpdateIfStale) { |
| 307 gfx::Size size(8, 8); |
| 308 scoped_refptr<VideoFrame> opaque_frame = VideoFrame::CreateFrame( |
| 309 VideoFrame::YV12, size, gfx::Rect(size), size, base::TimeDelta()); |
| 310 scoped_refptr<VideoFrame> opaque_frame_2 = VideoFrame::CreateFrame( |
| 311 VideoFrame::YV12, size, gfx::Rect(size), size, base::TimeDelta()); |
| 312 |
| 313 StartVideoRendererSink(); |
| 314 EXPECT_CALL(*this, Render(_, _)) |
| 315 .WillOnce(Return(opaque_frame)) |
| 316 .WillOnce(Return(opaque_frame_2)); |
| 317 EXPECT_TRUE( |
| 318 compositor()->UpdateCurrentFrame(base::TimeTicks(), base::TimeTicks())); |
| 319 |
| 320 base::TimeDelta stale_frame_threshold = |
| 321 compositor()->get_stale_frame_threshold_for_testing(); |
| 322 |
| 323 // Advancing time a little bit shouldn't cause the frame to be stale. |
| 324 tick_clock_->Advance(stale_frame_threshold / 2); |
| 325 EXPECT_EQ(opaque_frame, compositor()->GetCurrentFrameAndUpdateIfStale()); |
| 326 |
| 327 // Since rendering of frames is likely not happening, this will trigger a |
| 328 // dropped frame call. |
| 329 EXPECT_CALL(*this, OnFrameDropped()); |
| 330 |
| 331 // Advancing the clock over the threshold should cause a new frame request. |
| 332 tick_clock_->Advance(stale_frame_threshold / 2 + |
| 333 base::TimeDelta::FromMicroseconds(1)); |
| 334 EXPECT_EQ(opaque_frame_2, compositor()->GetCurrentFrameAndUpdateIfStale()); |
| 335 |
| 336 StopVideoRendererSink(); |
| 337 } |
| 338 |
| 339 TEST_F(VideoFrameCompositorTest, StopUpdatesCurrentFrameIfStale) { |
| 340 gfx::Size size(8, 8); |
| 341 scoped_refptr<VideoFrame> opaque_frame = VideoFrame::CreateFrame( |
| 342 VideoFrame::YV12, size, gfx::Rect(size), size, base::TimeDelta()); |
| 343 |
| 344 const base::TimeDelta interval = base::TimeDelta::FromSecondsD(1.0 / 60); |
| 345 |
| 346 StartVideoRendererSink(); |
| 347 |
| 348 // Expect two calls to Render(), one from UpdateCurrentFrame() and one from |
| 349 // Stop() because the frame is too old. |
| 350 EXPECT_CALL(*this, Render(_, _)) |
| 351 .WillOnce(Return(opaque_frame)) |
| 352 .WillOnce(Return(opaque_frame)); |
| 353 EXPECT_TRUE(compositor()->UpdateCurrentFrame(base::TimeTicks(), |
| 354 base::TimeTicks() + interval)); |
| 355 tick_clock_->Advance(interval * 2); |
| 356 StopVideoRendererSink(); |
163 } | 357 } |
164 | 358 |
165 } // namespace media | 359 } // namespace media |
OLD | NEW |