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