Roll ANGLE e754fb8..6ffeb74
[chromium-blink-merge.git] / media / formats / mp2t / es_adapter_video_unittest.cc
blobb601f7ece1f0dffdc2760d20c11f071e90c6bd01
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
3 // found in the LICENSE file.
5 #include <sstream>
6 #include <string>
7 #include <vector>
9 #include "base/bind.h"
10 #include "base/logging.h"
11 #include "base/strings/string_util.h"
12 #include "base/time/time.h"
13 #include "media/base/stream_parser_buffer.h"
14 #include "media/base/video_decoder_config.h"
15 #include "media/formats/mp2t/es_adapter_video.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 namespace media {
19 namespace mp2t {
21 namespace {
23 VideoDecoderConfig CreateFakeVideoConfig() {
24 gfx::Size coded_size(320, 240);
25 gfx::Rect visible_rect(0, 0, 320, 240);
26 gfx::Size natural_size(320, 240);
27 return VideoDecoderConfig(
28 kCodecH264,
29 H264PROFILE_MAIN,
30 VideoFrame::I420,
31 coded_size,
32 visible_rect,
33 natural_size,
34 NULL, 0, false);
37 StreamParserBuffer::BufferQueue
38 GenerateFakeBuffers(const int* frame_pts_ms,
39 const bool* is_key_frame,
40 size_t frame_count) {
41 uint8 dummy_buffer[] = {0, 0, 0, 0};
43 StreamParserBuffer::BufferQueue buffers(frame_count);
44 for (size_t k = 0; k < frame_count; k++) {
45 buffers[k] = StreamParserBuffer::CopyFrom(
46 dummy_buffer, arraysize(dummy_buffer),
47 is_key_frame[k], DemuxerStream::VIDEO, 0);
48 if (frame_pts_ms[k] < 0) {
49 buffers[k]->set_timestamp(kNoTimestamp());
50 } else {
51 buffers[k]->set_timestamp(
52 base::TimeDelta::FromMilliseconds(frame_pts_ms[k]));
55 return buffers;
60 class EsAdapterVideoTest : public testing::Test {
61 public:
62 EsAdapterVideoTest();
64 protected:
65 // Feed the ES adapter with the buffers from |buffer_queue|.
66 // Return the durations computed by the ES adapter as well as
67 // whether each frame emitted by the adapter is a key frame.
68 std::string RunAdapterTest(const StreamParser::BufferQueue& buffer_queue);
70 private:
71 void OnNewConfig(const VideoDecoderConfig& video_config);
72 void OnNewBuffer(scoped_refptr<StreamParserBuffer> buffer);
74 EsAdapterVideo es_adapter_;
76 std::stringstream buffer_descriptors_;
78 DISALLOW_COPY_AND_ASSIGN(EsAdapterVideoTest);
81 EsAdapterVideoTest::EsAdapterVideoTest()
82 : es_adapter_(base::Bind(&EsAdapterVideoTest::OnNewConfig,
83 base::Unretained(this)),
84 base::Bind(&EsAdapterVideoTest::OnNewBuffer,
85 base::Unretained(this))) {
88 void EsAdapterVideoTest::OnNewConfig(const VideoDecoderConfig& video_config) {
91 void EsAdapterVideoTest::OnNewBuffer(
92 scoped_refptr<StreamParserBuffer> buffer) {
93 buffer_descriptors_ << "(" << buffer->duration().InMilliseconds() << ","
94 << (buffer->is_key_frame() ? "Y" : "N") << ") ";
97 std::string EsAdapterVideoTest::RunAdapterTest(
98 const StreamParserBuffer::BufferQueue& buffer_queue) {
99 buffer_descriptors_.clear();
101 es_adapter_.OnConfigChanged(CreateFakeVideoConfig());
102 for (StreamParserBuffer::BufferQueue::const_iterator it =
103 buffer_queue.begin(); it != buffer_queue.end(); ++it) {
104 es_adapter_.OnNewBuffer(*it);
106 es_adapter_.Flush();
108 std::string s = buffer_descriptors_.str();
109 base::TrimWhitespaceASCII(s, base::TRIM_ALL, &s);
110 return s;
113 TEST_F(EsAdapterVideoTest, FrameDurationSimpleGop) {
114 // PTS for a GOP without B frames - strictly increasing.
115 int pts_ms[] = {30, 31, 33, 36, 40, 45, 51, 58};
116 bool is_key_frame[] = {
117 true, false, false, false,
118 false, false, false, false };
119 StreamParserBuffer::BufferQueue buffer_queue =
120 GenerateFakeBuffers(pts_ms, is_key_frame, arraysize(pts_ms));
122 EXPECT_EQ("(1,Y) (2,N) (3,N) (4,N) (5,N) (6,N) (7,N) (7,N)",
123 RunAdapterTest(buffer_queue));
126 TEST_F(EsAdapterVideoTest, FrameDurationComplexGop) {
127 // PTS for a GOP with B frames.
128 int pts_ms[] = {30, 120, 60, 90, 210, 150, 180, 300, 240, 270};
129 bool is_key_frame[] = {
130 true, false, false, false, false,
131 false, false, false, false, false };
132 StreamParserBuffer::BufferQueue buffer_queue =
133 GenerateFakeBuffers(pts_ms, is_key_frame, arraysize(pts_ms));
135 EXPECT_EQ("(30,Y) (30,N) (30,N) (30,N) (30,N) "
136 "(30,N) (30,N) (30,N) (30,N) (30,N)",
137 RunAdapterTest(buffer_queue));
140 TEST_F(EsAdapterVideoTest, LeadingNonKeyFrames) {
141 int pts_ms[] = {30, 40, 50, 120, 150, 180};
142 bool is_key_frame[] = {false, false, false, true, false, false};
143 StreamParserBuffer::BufferQueue buffer_queue =
144 GenerateFakeBuffers(pts_ms, is_key_frame, arraysize(pts_ms));
146 EXPECT_EQ("(30,Y) (30,Y) (30,Y) (30,Y) (30,N) (30,N)",
147 RunAdapterTest(buffer_queue));
150 TEST_F(EsAdapterVideoTest, LeadingKeyFrameWithNoTimestamp) {
151 int pts_ms[] = {-1, 40, 50, 120, 150, 180};
152 bool is_key_frame[] = {true, false, false, true, false, false};
153 StreamParserBuffer::BufferQueue buffer_queue =
154 GenerateFakeBuffers(pts_ms, is_key_frame, arraysize(pts_ms));
156 EXPECT_EQ("(40,Y) (40,Y) (30,Y) (30,N) (30,N)",
157 RunAdapterTest(buffer_queue));
160 TEST_F(EsAdapterVideoTest, LeadingFramesWithNoTimestamp) {
161 int pts_ms[] = {-1, -1, 50, 120, 150, 180};
162 bool is_key_frame[] = {false, false, false, true, false, false};
163 StreamParserBuffer::BufferQueue buffer_queue =
164 GenerateFakeBuffers(pts_ms, is_key_frame, arraysize(pts_ms));
166 EXPECT_EQ("(70,Y) (30,Y) (30,N) (30,N)",
167 RunAdapterTest(buffer_queue));
170 } // namespace mp2t
171 } // namespace media