Extension syncing: Introduce a NeedsSync pref
[chromium-blink-merge.git] / media / base / mac / video_frame_mac_unittests.cc
blob77a19178ff16a07085ef259ad3438cdea1e3e5f4
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/mac/video_frame_mac.h"
7 #include <utility>
9 #include "base/bind.h"
10 #include "base/callback_helpers.h"
11 #include "media/base/mac/corevideo_glue.h"
12 #include "media/base/video_frame.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace media {
18 namespace {
20 const int kWidth = 64;
21 const int kHeight = 48;
22 const base::TimeDelta kTimestamp = base::TimeDelta::FromMicroseconds(1337);
24 struct FormatPair {
25 VideoPixelFormat chrome;
26 OSType corevideo;
29 void Increment(int* i) {
30 ++(*i);
33 } // namespace
35 TEST(VideoFrameMac, CheckBasicAttributes) {
36 gfx::Size size(kWidth, kHeight);
37 auto frame = VideoFrame::CreateFrame(PIXEL_FORMAT_I420, size, gfx::Rect(size),
38 size, kTimestamp);
39 ASSERT_TRUE(frame.get());
41 auto pb = WrapVideoFrameInCVPixelBuffer(*frame);
42 ASSERT_TRUE(pb.get());
44 gfx::Size coded_size = frame->coded_size();
45 VideoPixelFormat format = frame->format();
47 EXPECT_EQ(coded_size.width(), static_cast<int>(CVPixelBufferGetWidth(pb)));
48 EXPECT_EQ(coded_size.height(), static_cast<int>(CVPixelBufferGetHeight(pb)));
49 EXPECT_EQ(VideoFrame::NumPlanes(format), CVPixelBufferGetPlaneCount(pb));
51 CVPixelBufferLockBaseAddress(pb, 0);
52 for (size_t i = 0; i < VideoFrame::NumPlanes(format); ++i) {
53 gfx::Size plane_size = VideoFrame::PlaneSize(format, i, coded_size);
54 EXPECT_EQ(plane_size.width(),
55 static_cast<int>(CVPixelBufferGetWidthOfPlane(pb, i)));
56 EXPECT_EQ(plane_size.height(),
57 static_cast<int>(CVPixelBufferGetHeightOfPlane(pb, i)));
58 EXPECT_EQ(frame->data(i), CVPixelBufferGetBaseAddressOfPlane(pb, i));
60 CVPixelBufferUnlockBaseAddress(pb, 0);
63 TEST(VideoFrameMac, CheckFormats) {
64 // CreateFrame() does not support non planar YUV, e.g. NV12.
65 const FormatPair format_pairs[] = {
66 {PIXEL_FORMAT_I420, kCVPixelFormatType_420YpCbCr8Planar},
67 {PIXEL_FORMAT_YV12, 0},
68 {PIXEL_FORMAT_YV16, 0},
69 {PIXEL_FORMAT_YV12A, 0},
70 {PIXEL_FORMAT_YV24, 0},
73 gfx::Size size(kWidth, kHeight);
74 for (const auto& format_pair : format_pairs) {
75 auto frame = VideoFrame::CreateFrame(format_pair.chrome, size,
76 gfx::Rect(size), size, kTimestamp);
77 ASSERT_TRUE(frame.get());
78 auto pb = WrapVideoFrameInCVPixelBuffer(*frame);
79 if (format_pair.corevideo) {
80 EXPECT_EQ(format_pair.corevideo, CVPixelBufferGetPixelFormatType(pb));
81 } else {
82 EXPECT_EQ(nullptr, pb.get());
87 TEST(VideoFrameMac, CheckLifetime) {
88 gfx::Size size(kWidth, kHeight);
89 auto frame = VideoFrame::CreateFrame(PIXEL_FORMAT_I420, size, gfx::Rect(size),
90 size, kTimestamp);
91 ASSERT_TRUE(frame.get());
93 int instances_destroyed = 0;
94 auto wrapper_frame = VideoFrame::WrapVideoFrame(
95 frame, frame->visible_rect(), frame->natural_size());
96 wrapper_frame->AddDestructionObserver(
97 base::Bind(&Increment, &instances_destroyed));
98 ASSERT_TRUE(wrapper_frame.get());
100 auto pb = WrapVideoFrameInCVPixelBuffer(*wrapper_frame);
101 ASSERT_TRUE(pb.get());
103 wrapper_frame = nullptr;
104 EXPECT_EQ(0, instances_destroyed);
105 pb.reset();
106 EXPECT_EQ(1, instances_destroyed);
109 TEST(VideoFrameMac, CheckWrapperFrame) {
110 const FormatPair format_pairs[] = {
111 {PIXEL_FORMAT_I420, kCVPixelFormatType_420YpCbCr8Planar},
112 {PIXEL_FORMAT_NV12,
113 CoreVideoGlue::kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange},
116 gfx::Size size(kWidth, kHeight);
117 for (const auto& format_pair : format_pairs) {
118 base::ScopedCFTypeRef<CVPixelBufferRef> pb;
119 CVPixelBufferCreate(nullptr, kWidth, kHeight, format_pair.corevideo,
120 nullptr, pb.InitializeInto());
121 ASSERT_TRUE(pb.get());
123 auto frame = VideoFrame::WrapCVPixelBuffer(pb.get(), kTimestamp);
124 ASSERT_TRUE(frame.get());
125 EXPECT_EQ(pb.get(), frame->cv_pixel_buffer());
126 EXPECT_EQ(format_pair.chrome, frame->format());
128 frame = nullptr;
129 EXPECT_EQ(1, CFGetRetainCount(pb.get()));
133 } // namespace media