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.
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/text_track_config.h"
18 #include "media/base/video_decoder_config.h"
19 #include "media/formats/mp4/es_descriptor.h"
20 #include "media/formats/mp4/mp4_stream_parser.h"
21 #include "testing/gtest/include/gtest/gtest.h"
23 using base::TimeDelta;
28 // TODO(xhwang): Figure out the init data type appropriately once it's spec'ed.
29 static const char kMp4InitDataType[] = "video/mp4";
31 class MP4StreamParserTest : public testing::Test {
34 : configs_received_(false) {
35 std::set<int> audio_object_types;
36 audio_object_types.insert(kISO_14496_3);
37 parser_.reset(new MP4StreamParser(audio_object_types, false));
41 scoped_ptr<MP4StreamParser> parser_;
42 bool configs_received_;
44 bool AppendData(const uint8* data, size_t length) {
45 return parser_->Parse(data, length);
48 bool AppendDataInPieces(const uint8* data, size_t length, size_t piece_size) {
49 const uint8* start = data;
50 const uint8* end = data + length;
52 size_t append_size = std::min(piece_size,
53 static_cast<size_t>(end - start));
54 if (!AppendData(start, append_size))
61 void InitF(bool init_ok, const StreamParser::InitParameters& params) {
62 DVLOG(1) << "InitF: ok=" << init_ok
63 << ", dur=" << params.duration.InMilliseconds()
64 << ", autoTimestampOffset=" << params.auto_update_timestamp_offset;
67 bool NewConfigF(const AudioDecoderConfig& ac,
68 const VideoDecoderConfig& vc,
69 const StreamParser::TextTrackConfigMap& tc) {
70 DVLOG(1) << "NewConfigF: audio=" << ac.IsValidConfig()
71 << ", video=" << vc.IsValidConfig();
72 configs_received_ = true;
77 void DumpBuffers(const std::string& label,
78 const StreamParser::BufferQueue& buffers) {
79 DVLOG(2) << "DumpBuffers: " << label << " size " << buffers.size();
80 for (StreamParser::BufferQueue::const_iterator buf = buffers.begin();
81 buf != buffers.end(); buf++) {
82 DVLOG(3) << " n=" << buf - buffers.begin()
83 << ", size=" << (*buf)->data_size()
84 << ", dur=" << (*buf)->duration().InMilliseconds();
88 bool NewBuffersF(const StreamParser::BufferQueue& audio_buffers,
89 const StreamParser::BufferQueue& video_buffers,
90 const StreamParser::TextBufferQueueMap& text_map) {
91 DumpBuffers("audio_buffers", audio_buffers);
92 DumpBuffers("video_buffers", video_buffers);
94 // TODO(wolenetz/acolwell): Add text track support to more MSE parsers. See
95 // http://crbug.com/336926.
96 if (!text_map.empty())
102 void KeyNeededF(const std::string& type,
103 const std::vector<uint8>& init_data) {
104 DVLOG(1) << "KeyNeededF: " << init_data.size();
105 EXPECT_EQ(kMp4InitDataType, type);
106 EXPECT_FALSE(init_data.empty());
110 DVLOG(1) << "NewSegmentF";
113 void EndOfSegmentF() {
114 DVLOG(1) << "EndOfSegmentF()";
117 void InitializeParser() {
119 base::Bind(&MP4StreamParserTest::InitF, base::Unretained(this)),
120 base::Bind(&MP4StreamParserTest::NewConfigF, base::Unretained(this)),
121 base::Bind(&MP4StreamParserTest::NewBuffersF, base::Unretained(this)),
123 base::Bind(&MP4StreamParserTest::KeyNeededF, base::Unretained(this)),
124 base::Bind(&MP4StreamParserTest::NewSegmentF, base::Unretained(this)),
125 base::Bind(&MP4StreamParserTest::EndOfSegmentF,
126 base::Unretained(this)),
130 bool ParseMP4File(const std::string& filename, int append_bytes) {
133 scoped_refptr<DecoderBuffer> buffer = ReadTestDataFile(filename);
134 EXPECT_TRUE(AppendDataInPieces(buffer->data(),
141 TEST_F(MP4StreamParserTest, UnalignedAppend) {
142 // Test small, non-segment-aligned appends (small enough to exercise
143 // incremental append system)
144 ParseMP4File("bear-1280x720-av_frag.mp4", 512);
147 TEST_F(MP4StreamParserTest, BytewiseAppend) {
148 // Ensure no incremental errors occur when parsing
149 ParseMP4File("bear-1280x720-av_frag.mp4", 1);
152 TEST_F(MP4StreamParserTest, MultiFragmentAppend) {
153 // Large size ensures multiple fragments are appended in one call (size is
154 // larger than this particular test file)
155 ParseMP4File("bear-1280x720-av_frag.mp4", 768432);
158 TEST_F(MP4StreamParserTest, Flush) {
159 // Flush while reading sample data, then start a new stream.
162 scoped_refptr<DecoderBuffer> buffer =
163 ReadTestDataFile("bear-1280x720-av_frag.mp4");
164 EXPECT_TRUE(AppendDataInPieces(buffer->data(), 65536, 512));
166 EXPECT_TRUE(AppendDataInPieces(buffer->data(),
171 TEST_F(MP4StreamParserTest, Reinitialization) {
174 scoped_refptr<DecoderBuffer> buffer =
175 ReadTestDataFile("bear-1280x720-av_frag.mp4");
176 EXPECT_TRUE(AppendDataInPieces(buffer->data(),
179 EXPECT_TRUE(AppendDataInPieces(buffer->data(),
184 TEST_F(MP4StreamParserTest, MPEG2_AAC_LC) {
185 std::set<int> audio_object_types;
186 audio_object_types.insert(kISO_13818_7_AAC_LC);
187 parser_.reset(new MP4StreamParser(audio_object_types, false));
188 ParseMP4File("bear-mpeg2-aac-only_frag.mp4", 512);
191 // Test that a moov box is not always required after Flush() is called.
192 TEST_F(MP4StreamParserTest, NoMoovAfterFlush) {
195 scoped_refptr<DecoderBuffer> buffer =
196 ReadTestDataFile("bear-1280x720-av_frag.mp4");
197 EXPECT_TRUE(AppendDataInPieces(buffer->data(),
202 const int kFirstMoofOffset = 1307;
203 EXPECT_TRUE(AppendDataInPieces(buffer->data() + kFirstMoofOffset,
204 buffer->data_size() - kFirstMoofOffset,
208 // Test an invalid file where there are encrypted samples, but
209 // SampleAuxiliaryInformation{Sizes|Offsets}Box (saiz|saio) are missing.
210 // The parser should fail instead of crash. See http://crbug.com/361347
211 TEST_F(MP4StreamParserTest, MissingSampleAuxInfo) {
214 scoped_refptr<DecoderBuffer> buffer =
215 ReadTestDataFile("bear-1280x720-a_frag-cenc_missing-saiz-saio.mp4");
216 EXPECT_FALSE(AppendDataInPieces(buffer->data(), buffer->data_size(), 512));
219 // Test a file where all video samples start with an Access Unit
220 // Delimiter (AUD) NALU.
221 TEST_F(MP4StreamParserTest, VideoSamplesStartWithAUDs) {
222 ParseMP4File("bear-1280x720-av_with-aud-nalus_frag.mp4", 512);
225 // TODO(strobe): Create and test media which uses CENC auxiliary info stored
226 // inside a private box