cc: Added inline to Tile::IsReadyToDraw
[chromium-blink-merge.git] / media / mp4 / mp4_stream_parser_unittest.cc
blob816a2106e397646d9e817a2460daedba0b5d1304
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 <algorithm>
6 #include <string>
8 #include "base/bind.h"
9 #include "base/bind_helpers.h"
10 #include "base/logging.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/time/time.h"
13 #include "media/base/audio_decoder_config.h"
14 #include "media/base/decoder_buffer.h"
15 #include "media/base/stream_parser_buffer.h"
16 #include "media/base/test_data_util.h"
17 #include "media/base/video_decoder_config.h"
18 #include "media/mp4/es_descriptor.h"
19 #include "media/mp4/mp4_stream_parser.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 using base::TimeDelta;
24 namespace media {
25 namespace mp4 {
27 // TODO(xhwang): Figure out the init data type appropriately once it's spec'ed.
28 static const char kMp4InitDataType[] = "video/mp4";
30 class MP4StreamParserTest : public testing::Test {
31 public:
32 MP4StreamParserTest()
33 : configs_received_(false) {
34 std::set<int> audio_object_types;
35 audio_object_types.insert(kISO_14496_3);
36 parser_.reset(new MP4StreamParser(audio_object_types, false));
39 protected:
40 scoped_ptr<MP4StreamParser> parser_;
41 bool configs_received_;
43 bool AppendData(const uint8* data, size_t length) {
44 return parser_->Parse(data, length);
47 bool AppendDataInPieces(const uint8* data, size_t length, size_t piece_size) {
48 const uint8* start = data;
49 const uint8* end = data + length;
50 while (start < end) {
51 size_t append_size = std::min(piece_size,
52 static_cast<size_t>(end - start));
53 if (!AppendData(start, append_size))
54 return false;
55 start += append_size;
57 return true;
60 void InitF(bool init_ok, base::TimeDelta duration) {
61 DVLOG(1) << "InitF: ok=" << init_ok
62 << ", dur=" << duration.InMilliseconds();
65 bool NewConfigF(const AudioDecoderConfig& ac, const VideoDecoderConfig& vc) {
66 DVLOG(1) << "NewConfigF: audio=" << ac.IsValidConfig()
67 << ", video=" << vc.IsValidConfig();
68 configs_received_ = true;
69 return true;
73 void DumpBuffers(const std::string& label,
74 const StreamParser::BufferQueue& buffers) {
75 DVLOG(2) << "DumpBuffers: " << label << " size " << buffers.size();
76 for (StreamParser::BufferQueue::const_iterator buf = buffers.begin();
77 buf != buffers.end(); buf++) {
78 DVLOG(3) << " n=" << buf - buffers.begin()
79 << ", size=" << (*buf)->data_size()
80 << ", dur=" << (*buf)->duration().InMilliseconds();
84 bool NewBuffersF(const StreamParser::BufferQueue& audio_buffers,
85 const StreamParser::BufferQueue& video_buffers) {
86 DumpBuffers("audio_buffers", audio_buffers);
87 DumpBuffers("video_buffers", video_buffers);
88 return true;
91 bool NewTextBuffersF(TextTrack* text_track,
92 const StreamParser::BufferQueue& buffers) {
93 return true;
96 void KeyNeededF(const std::string& type,
97 const std::vector<uint8>& init_data) {
98 DVLOG(1) << "KeyNeededF: " << init_data.size();
99 EXPECT_EQ(kMp4InitDataType, type);
100 EXPECT_FALSE(init_data.empty());
103 scoped_ptr<TextTrack> AddTextTrackF(
104 TextKind kind,
105 const std::string& label,
106 const std::string& language) {
107 return scoped_ptr<TextTrack>();
110 void NewSegmentF() {
111 DVLOG(1) << "NewSegmentF";
114 void EndOfSegmentF() {
115 DVLOG(1) << "EndOfSegmentF()";
118 void InitializeParser() {
119 parser_->Init(
120 base::Bind(&MP4StreamParserTest::InitF, base::Unretained(this)),
121 base::Bind(&MP4StreamParserTest::NewConfigF, base::Unretained(this)),
122 base::Bind(&MP4StreamParserTest::NewBuffersF, base::Unretained(this)),
123 base::Bind(&MP4StreamParserTest::NewTextBuffersF,
124 base::Unretained(this)),
125 base::Bind(&MP4StreamParserTest::KeyNeededF, base::Unretained(this)),
126 base::Bind(&MP4StreamParserTest::AddTextTrackF, base::Unretained(this)),
127 base::Bind(&MP4StreamParserTest::NewSegmentF, base::Unretained(this)),
128 base::Bind(&MP4StreamParserTest::EndOfSegmentF,
129 base::Unretained(this)),
130 LogCB());
133 bool ParseMP4File(const std::string& filename, int append_bytes) {
134 InitializeParser();
136 scoped_refptr<DecoderBuffer> buffer = ReadTestDataFile(filename);
137 EXPECT_TRUE(AppendDataInPieces(buffer->data(),
138 buffer->data_size(),
139 append_bytes));
140 return true;
144 TEST_F(MP4StreamParserTest, UnalignedAppend) {
145 // Test small, non-segment-aligned appends (small enough to exercise
146 // incremental append system)
147 ParseMP4File("bear-1280x720-av_frag.mp4", 512);
150 TEST_F(MP4StreamParserTest, BytewiseAppend) {
151 // Ensure no incremental errors occur when parsing
152 ParseMP4File("bear-1280x720-av_frag.mp4", 1);
155 TEST_F(MP4StreamParserTest, MultiFragmentAppend) {
156 // Large size ensures multiple fragments are appended in one call (size is
157 // larger than this particular test file)
158 ParseMP4File("bear-1280x720-av_frag.mp4", 768432);
161 TEST_F(MP4StreamParserTest, Flush) {
162 // Flush while reading sample data, then start a new stream.
163 InitializeParser();
165 scoped_refptr<DecoderBuffer> buffer =
166 ReadTestDataFile("bear-1280x720-av_frag.mp4");
167 EXPECT_TRUE(AppendDataInPieces(buffer->data(), 65536, 512));
168 parser_->Flush();
169 EXPECT_TRUE(AppendDataInPieces(buffer->data(),
170 buffer->data_size(),
171 512));
174 TEST_F(MP4StreamParserTest, Reinitialization) {
175 InitializeParser();
177 scoped_refptr<DecoderBuffer> buffer =
178 ReadTestDataFile("bear-1280x720-av_frag.mp4");
179 EXPECT_TRUE(AppendDataInPieces(buffer->data(),
180 buffer->data_size(),
181 512));
182 EXPECT_TRUE(AppendDataInPieces(buffer->data(),
183 buffer->data_size(),
184 512));
187 TEST_F(MP4StreamParserTest, MPEG2_AAC_LC) {
188 std::set<int> audio_object_types;
189 audio_object_types.insert(kISO_13818_7_AAC_LC);
190 parser_.reset(new MP4StreamParser(audio_object_types, false));
191 ParseMP4File("bear-mpeg2-aac-only_frag.mp4", 512);
194 // Test that a moov box is not always required after Flush() is called.
195 TEST_F(MP4StreamParserTest, NoMoovAfterFlush) {
196 InitializeParser();
198 scoped_refptr<DecoderBuffer> buffer =
199 ReadTestDataFile("bear-1280x720-av_frag.mp4");
200 EXPECT_TRUE(AppendDataInPieces(buffer->data(),
201 buffer->data_size(),
202 512));
203 parser_->Flush();
205 const int kFirstMoofOffset = 1307;
206 EXPECT_TRUE(AppendDataInPieces(buffer->data() + kFirstMoofOffset,
207 buffer->data_size() - kFirstMoofOffset,
208 512));
211 // TODO(strobe): Create and test media which uses CENC auxiliary info stored
212 // inside a private box
214 } // namespace mp4
215 } // namespace media