QUIC - use size_t for kMaxInitialRoundTripTimeUs.
[chromium-blink-merge.git] / media / base / video_frame_unittest.cc
blob20210e56a37c552eb05509e2b1c1adf53363eaab
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
3 // found in the LICENSE file.
5 #include "media/base/video_frame.h"
7 #include "base/bind.h"
8 #include "base/callback_helpers.h"
9 #include "base/format_macros.h"
10 #include "base/memory/aligned_memory.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/strings/stringprintf.h"
13 #include "media/base/buffers.h"
14 #include "media/base/yuv_convert.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 namespace media {
19 using base::MD5DigestToBase16;
21 // Helper function that initializes a YV12 frame with white and black scan
22 // lines based on the |white_to_black| parameter. If 0, then the entire
23 // frame will be black, if 1 then the entire frame will be white.
24 void InitializeYV12Frame(VideoFrame* frame, double white_to_black) {
25 EXPECT_EQ(VideoFrame::YV12, frame->format());
26 int first_black_row = static_cast<int>(frame->coded_size().height() *
27 white_to_black);
28 uint8* y_plane = frame->data(VideoFrame::kYPlane);
29 for (int row = 0; row < frame->coded_size().height(); ++row) {
30 int color = (row < first_black_row) ? 0xFF : 0x00;
31 memset(y_plane, color, frame->stride(VideoFrame::kYPlane));
32 y_plane += frame->stride(VideoFrame::kYPlane);
34 uint8* u_plane = frame->data(VideoFrame::kUPlane);
35 uint8* v_plane = frame->data(VideoFrame::kVPlane);
36 for (int row = 0; row < frame->coded_size().height(); row += 2) {
37 memset(u_plane, 0x80, frame->stride(VideoFrame::kUPlane));
38 memset(v_plane, 0x80, frame->stride(VideoFrame::kVPlane));
39 u_plane += frame->stride(VideoFrame::kUPlane);
40 v_plane += frame->stride(VideoFrame::kVPlane);
44 // Given a |yv12_frame| this method converts the YV12 frame to RGBA and
45 // makes sure that all the pixels of the RBG frame equal |expect_rgb_color|.
46 void ExpectFrameColor(media::VideoFrame* yv12_frame, uint32 expect_rgb_color) {
47 ASSERT_EQ(VideoFrame::YV12, yv12_frame->format());
48 ASSERT_EQ(yv12_frame->stride(VideoFrame::kUPlane),
49 yv12_frame->stride(VideoFrame::kVPlane));
50 ASSERT_EQ(
51 yv12_frame->coded_size().width() & (VideoFrame::kFrameSizeAlignment - 1),
52 0);
53 ASSERT_EQ(
54 yv12_frame->coded_size().height() & (VideoFrame::kFrameSizeAlignment - 1),
55 0);
57 size_t bytes_per_row = yv12_frame->coded_size().width() * 4u;
58 uint8* rgb_data = reinterpret_cast<uint8*>(
59 base::AlignedAlloc(bytes_per_row * yv12_frame->coded_size().height() +
60 VideoFrame::kFrameSizePadding,
61 VideoFrame::kFrameAddressAlignment));
63 media::ConvertYUVToRGB32(yv12_frame->data(VideoFrame::kYPlane),
64 yv12_frame->data(VideoFrame::kUPlane),
65 yv12_frame->data(VideoFrame::kVPlane),
66 rgb_data,
67 yv12_frame->coded_size().width(),
68 yv12_frame->coded_size().height(),
69 yv12_frame->stride(VideoFrame::kYPlane),
70 yv12_frame->stride(VideoFrame::kUPlane),
71 bytes_per_row,
72 media::YV12);
74 for (int row = 0; row < yv12_frame->coded_size().height(); ++row) {
75 uint32* rgb_row_data = reinterpret_cast<uint32*>(
76 rgb_data + (bytes_per_row * row));
77 for (int col = 0; col < yv12_frame->coded_size().width(); ++col) {
78 SCOPED_TRACE(
79 base::StringPrintf("Checking (%d, %d)", row, col));
80 EXPECT_EQ(expect_rgb_color, rgb_row_data[col]);
84 base::AlignedFree(rgb_data);
87 // Fill each plane to its reported extents and verify accessors report non
88 // zero values. Additionally, for the first plane verify the rows and
89 // row_bytes values are correct.
90 void ExpectFrameExtents(VideoFrame::Format format, int planes,
91 int bytes_per_pixel, const char* expected_hash) {
92 const unsigned char kFillByte = 0x80;
93 const int kWidth = 61;
94 const int kHeight = 31;
95 const base::TimeDelta kTimestamp = base::TimeDelta::FromMicroseconds(1337);
97 gfx::Size size(kWidth, kHeight);
98 scoped_refptr<VideoFrame> frame = VideoFrame::CreateFrame(
99 format, size, gfx::Rect(size), size, kTimestamp);
100 ASSERT_TRUE(frame.get());
102 for(int plane = 0; plane < planes; plane++) {
103 SCOPED_TRACE(base::StringPrintf("Checking plane %d", plane));
104 EXPECT_TRUE(frame->data(plane));
105 EXPECT_TRUE(frame->stride(plane));
106 EXPECT_TRUE(frame->rows(plane));
107 EXPECT_TRUE(frame->row_bytes(plane));
109 if (plane == 0) {
110 EXPECT_EQ(frame->rows(plane), kHeight);
111 EXPECT_EQ(frame->row_bytes(plane), kWidth * bytes_per_pixel);
114 memset(frame->data(plane), kFillByte,
115 frame->stride(plane) * frame->rows(plane));
118 base::MD5Context context;
119 base::MD5Init(&context);
120 frame->HashFrameForTesting(&context);
121 base::MD5Digest digest;
122 base::MD5Final(&digest, &context);
123 EXPECT_EQ(MD5DigestToBase16(digest), expected_hash);
126 TEST(VideoFrame, CreateFrame) {
127 const int kWidth = 64;
128 const int kHeight = 48;
129 const base::TimeDelta kTimestamp = base::TimeDelta::FromMicroseconds(1337);
131 // Create a YV12 Video Frame.
132 gfx::Size size(kWidth, kHeight);
133 scoped_refptr<media::VideoFrame> frame =
134 VideoFrame::CreateFrame(media::VideoFrame::YV12, size, gfx::Rect(size),
135 size, kTimestamp);
136 ASSERT_TRUE(frame.get());
138 // Test VideoFrame implementation.
139 EXPECT_EQ(media::VideoFrame::YV12, frame->format());
141 SCOPED_TRACE("");
142 InitializeYV12Frame(frame.get(), 0.0f);
143 ExpectFrameColor(frame.get(), 0xFF000000);
145 base::MD5Digest digest;
146 base::MD5Context context;
147 base::MD5Init(&context);
148 frame->HashFrameForTesting(&context);
149 base::MD5Final(&digest, &context);
150 EXPECT_EQ(MD5DigestToBase16(digest), "9065c841d9fca49186ef8b4ef547e79b");
152 SCOPED_TRACE("");
153 InitializeYV12Frame(frame.get(), 1.0f);
154 ExpectFrameColor(frame.get(), 0xFFFFFFFF);
156 base::MD5Init(&context);
157 frame->HashFrameForTesting(&context);
158 base::MD5Final(&digest, &context);
159 EXPECT_EQ(MD5DigestToBase16(digest), "911991d51438ad2e1a40ed5f6fc7c796");
161 // Test an empty frame.
162 frame = VideoFrame::CreateEOSFrame();
163 EXPECT_TRUE(frame->end_of_stream());
166 TEST(VideoFrame, CreateBlackFrame) {
167 const int kWidth = 2;
168 const int kHeight = 2;
169 const uint8 kExpectedYRow[] = { 0, 0 };
170 const uint8 kExpectedUVRow[] = { 128 };
172 scoped_refptr<media::VideoFrame> frame =
173 VideoFrame::CreateBlackFrame(gfx::Size(kWidth, kHeight));
174 ASSERT_TRUE(frame.get());
176 // Test basic properties.
177 EXPECT_EQ(0, frame->GetTimestamp().InMicroseconds());
178 EXPECT_FALSE(frame->end_of_stream());
180 // Test |frame| properties.
181 EXPECT_EQ(VideoFrame::YV12, frame->format());
182 EXPECT_EQ(kWidth, frame->coded_size().width());
183 EXPECT_EQ(kHeight, frame->coded_size().height());
185 // Test frames themselves.
186 uint8* y_plane = frame->data(VideoFrame::kYPlane);
187 for (int y = 0; y < frame->coded_size().height(); ++y) {
188 EXPECT_EQ(0, memcmp(kExpectedYRow, y_plane, arraysize(kExpectedYRow)));
189 y_plane += frame->stride(VideoFrame::kYPlane);
192 uint8* u_plane = frame->data(VideoFrame::kUPlane);
193 uint8* v_plane = frame->data(VideoFrame::kVPlane);
194 for (int y = 0; y < frame->coded_size().height() / 2; ++y) {
195 EXPECT_EQ(0, memcmp(kExpectedUVRow, u_plane, arraysize(kExpectedUVRow)));
196 EXPECT_EQ(0, memcmp(kExpectedUVRow, v_plane, arraysize(kExpectedUVRow)));
197 u_plane += frame->stride(VideoFrame::kUPlane);
198 v_plane += frame->stride(VideoFrame::kVPlane);
202 // Ensure each frame is properly sized and allocated. Will trigger OOB reads
203 // and writes as well as incorrect frame hashes otherwise.
204 TEST(VideoFrame, CheckFrameExtents) {
205 // Each call consists of a VideoFrame::Format, # of planes, bytes per pixel,
206 // and the expected hash of all planes if filled with kFillByte (defined in
207 // ExpectFrameExtents).
208 ExpectFrameExtents(
209 VideoFrame::YV12, 3, 1, "71113bdfd4c0de6cf62f48fb74f7a0b1");
210 ExpectFrameExtents(
211 VideoFrame::YV16, 3, 1, "9bb99ac3ff350644ebff4d28dc01b461");
214 static void TextureCallback(uint32* called_sync_point, uint32 sync_point) {
215 *called_sync_point = sync_point;
218 // Verify the TextureNoLongerNeededCallback is called when VideoFrame is
219 // destroyed with the original sync point.
220 TEST(VideoFrame, TextureNoLongerNeededCallbackIsCalled) {
221 uint32 sync_point = 7;
222 uint32 called_sync_point = 0;
225 scoped_refptr<VideoFrame> frame = VideoFrame::WrapNativeTexture(
226 new VideoFrame::MailboxHolder(
227 gpu::Mailbox(),
228 sync_point,
229 base::Bind(&TextureCallback, &called_sync_point)),
230 5, // texture_target
231 gfx::Size(10, 10), // coded_size
232 gfx::Rect(10, 10), // visible_rect
233 gfx::Size(10, 10), // natural_size
234 base::TimeDelta(), // timestamp
235 base::Callback<void(const SkBitmap&)>(), // read_pixels_cb
236 base::Closure()); // no_longer_needed_cb
238 EXPECT_EQ(0u, called_sync_point);
240 EXPECT_EQ(sync_point, called_sync_point);
243 // Verify the TextureNoLongerNeededCallback is called when VideoFrame is
244 // destroyed with the new sync point, when the mailbox is taken by a caller.
245 TEST(VideoFrame, TextureNoLongerNeededCallbackAfterTakingAndReleasingMailbox) {
246 uint32 called_sync_point = 0;
248 gpu::Mailbox mailbox;
249 mailbox.name[0] = 50;
250 uint32 sync_point = 7;
251 uint32 target = 9;
254 scoped_refptr<VideoFrame> frame = VideoFrame::WrapNativeTexture(
255 new VideoFrame::MailboxHolder(
256 mailbox,
257 sync_point,
258 base::Bind(&TextureCallback, &called_sync_point)),
259 target,
260 gfx::Size(10, 10), // coded_size
261 gfx::Rect(10, 10), // visible_rect
262 gfx::Size(10, 10), // natural_size
263 base::TimeDelta(), // timestamp
264 base::Callback<void(const SkBitmap&)>(), // read_pixels_cb
265 base::Closure()); // no_longer_needed_cb
268 scoped_refptr<VideoFrame::MailboxHolder> mailbox_holder =
269 frame->texture_mailbox();
271 EXPECT_EQ(mailbox.name[0], mailbox_holder->mailbox().name[0]);
272 EXPECT_EQ(sync_point, mailbox_holder->sync_point());
273 EXPECT_EQ(target, frame->texture_target());
275 // Misuse the callback.
276 sync_point = 12;
277 mailbox_holder->Return(sync_point);
278 EXPECT_EQ(0u, called_sync_point);
280 // Finish using the mailbox_holder and drop our reference.
281 sync_point = 10;
282 mailbox_holder->Return(sync_point);
284 EXPECT_EQ(0u, called_sync_point);
286 EXPECT_EQ(sync_point, called_sync_point);
289 // If a caller has taken ownership of the texture mailbox, it should
290 // not be released when the VideoFrame is destroyed, but should when
291 // the TextureNoLongerNeededCallback is called.
292 TEST(VideoFrame,
293 TextureNoLongerNeededCallbackAfterTakingMailboxWithDestroyedFrame) {
294 uint32 called_sync_point = 0;
296 gpu::Mailbox mailbox;
297 mailbox.name[0] = 50;
298 uint32 sync_point = 7;
299 uint32 target = 9;
302 scoped_refptr<VideoFrame::MailboxHolder> mailbox_holder;
305 scoped_refptr<VideoFrame> frame = VideoFrame::WrapNativeTexture(
306 new VideoFrame::MailboxHolder(
307 mailbox,
308 sync_point,
309 base::Bind(&TextureCallback, &called_sync_point)),
310 target,
311 gfx::Size(10, 10), // coded_size
312 gfx::Rect(10, 10), // visible_rect
313 gfx::Size(10, 10), // natural_size
314 base::TimeDelta(), // timestamp
315 base::Callback<void(const SkBitmap&)>(), // read_pixels_cb
316 base::Closure()); // no_longer_needed_cb
318 mailbox_holder = frame->texture_mailbox();
320 EXPECT_EQ(mailbox.name[0], mailbox_holder->mailbox().name[0]);
321 EXPECT_EQ(sync_point, mailbox_holder->sync_point());
322 EXPECT_EQ(target, frame->texture_target());
324 // Keep a ref on the mailbox_holder after the VideoFrame is dropped.
326 EXPECT_EQ(0u, called_sync_point);
328 // Misuse the callback.
329 sync_point = 12;
330 mailbox_holder->Return(sync_point);
331 EXPECT_EQ(0u, called_sync_point);
333 // Finish using the mailbox_holder and drop our ref.
334 sync_point = 10;
335 mailbox_holder->Return(sync_point);
337 EXPECT_EQ(sync_point, called_sync_point);
340 // If a caller has taken ownership of the texture mailbox, but does
341 // not call the callback, it should still happen with the original
342 // sync point.
343 TEST(VideoFrame,
344 TextureNoLongerNeededCallbackWhenNotCallingAndFrameDestroyed) {
345 uint32 called_sync_point = 0;
347 gpu::Mailbox mailbox;
348 mailbox.name[0] = 50;
349 uint32 sync_point = 7;
350 uint32 target = 9;
353 scoped_refptr<VideoFrame::MailboxHolder> mailbox_holder;
356 scoped_refptr<VideoFrame> frame = VideoFrame::WrapNativeTexture(
357 new VideoFrame::MailboxHolder(
358 mailbox,
359 sync_point,
360 base::Bind(&TextureCallback, &called_sync_point)),
361 target,
362 gfx::Size(10, 10), // coded_size
363 gfx::Rect(10, 10), // visible_rect
364 gfx::Size(10, 10), // natural_size
365 base::TimeDelta(), // timestamp
366 base::Callback<void(const SkBitmap&)>(), // read_pixels_cb
367 base::Closure()); // no_longer_needed_cb
369 mailbox_holder = frame->texture_mailbox();
371 EXPECT_EQ(mailbox.name[0], mailbox_holder->mailbox().name[0]);
372 EXPECT_EQ(sync_point, mailbox_holder->sync_point());
373 EXPECT_EQ(target, frame->texture_target());
375 // Destroy the video frame.
377 EXPECT_EQ(0u, called_sync_point);
379 // Drop the reference on the mailbox without using it at all.
381 EXPECT_EQ(sync_point, called_sync_point);
384 // If a caller has taken ownership of the texture mailbox, but does
385 // not call the callback, it should still happen with the original
386 // sync point.
387 TEST(VideoFrame,
388 TextureNoLongerNeededCallbackAfterTakingMailboxAndNotCalling) {
389 uint32 called_sync_point = 0;
391 gpu::Mailbox mailbox;
392 mailbox.name[0] = 50;
393 uint32 sync_point = 7;
394 uint32 target = 9;
397 scoped_refptr<VideoFrame> frame = VideoFrame::WrapNativeTexture(
398 new VideoFrame::MailboxHolder(
399 mailbox,
400 sync_point,
401 base::Bind(&TextureCallback, &called_sync_point)),
402 target,
403 gfx::Size(10, 10), // coded_size
404 gfx::Rect(10, 10), // visible_rect
405 gfx::Size(10, 10), // natural_size
406 base::TimeDelta(), // timestamp
407 base::Callback<void(const SkBitmap&)>(), // read_pixels_cb
408 base::Closure()); // no_longer_needed_cb
410 scoped_refptr<VideoFrame::MailboxHolder> mailbox_holder =
411 frame->texture_mailbox();
413 EXPECT_EQ(mailbox.name[0], mailbox_holder->mailbox().name[0]);
414 EXPECT_EQ(sync_point, mailbox_holder->sync_point());
415 EXPECT_EQ(target, frame->texture_target());
417 EXPECT_EQ(0u, called_sync_point);
419 // Don't use the mailbox at all and drop our ref on it.
421 // The VideoFrame is destroyed, it should call the callback.
422 EXPECT_EQ(sync_point, called_sync_point);
425 } // namespace media