Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / media / formats / mp4 / mp4_stream_parser_unittest.cc
blob0e631f47b763811057e2747fcd5718a21708846d
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 <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/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;
25 namespace media {
26 namespace mp4 {
28 class MP4StreamParserTest : public testing::Test {
29 public:
30 MP4StreamParserTest()
31 : configs_received_(false),
32 lower_bound_(
33 DecodeTimestamp::FromPresentationTime(base::TimeDelta::Max())) {
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_;
42 AudioDecoderConfig audio_decoder_config_;
43 VideoDecoderConfig video_decoder_config_;
44 DecodeTimestamp lower_bound_;
46 bool AppendData(const uint8* data, size_t length) {
47 return parser_->Parse(data, length);
50 bool AppendDataInPieces(const uint8* data, size_t length, size_t piece_size) {
51 const uint8* start = data;
52 const uint8* end = data + length;
53 while (start < end) {
54 size_t append_size = std::min(piece_size,
55 static_cast<size_t>(end - start));
56 if (!AppendData(start, append_size))
57 return false;
58 start += append_size;
60 return true;
63 void InitF(DemuxerStream::Liveness expected_liveness,
64 const StreamParser::InitParameters& params) {
65 DVLOG(1) << "InitF: dur=" << params.duration.InMilliseconds()
66 << ", autoTimestampOffset=" << params.auto_update_timestamp_offset;
67 EXPECT_EQ(expected_liveness, params.liveness);
70 bool NewConfigF(const AudioDecoderConfig& ac,
71 const VideoDecoderConfig& vc,
72 const StreamParser::TextTrackConfigMap& tc) {
73 DVLOG(1) << "NewConfigF: audio=" << ac.IsValidConfig()
74 << ", video=" << vc.IsValidConfig();
75 configs_received_ = true;
76 audio_decoder_config_ = ac;
77 video_decoder_config_ = vc;
78 return true;
81 void DumpBuffers(const std::string& label,
82 const StreamParser::BufferQueue& buffers) {
83 DVLOG(2) << "DumpBuffers: " << label << " size " << buffers.size();
84 for (StreamParser::BufferQueue::const_iterator buf = buffers.begin();
85 buf != buffers.end(); buf++) {
86 DVLOG(3) << " n=" << buf - buffers.begin()
87 << ", size=" << (*buf)->data_size()
88 << ", dur=" << (*buf)->duration().InMilliseconds();
92 bool NewBuffersF(const StreamParser::BufferQueue& audio_buffers,
93 const StreamParser::BufferQueue& video_buffers,
94 const StreamParser::TextBufferQueueMap& text_map) {
95 DumpBuffers("audio_buffers", audio_buffers);
96 DumpBuffers("video_buffers", video_buffers);
98 // TODO(wolenetz/acolwell): Add text track support to more MSE parsers. See
99 // http://crbug.com/336926.
100 if (!text_map.empty())
101 return false;
103 // Find the second highest timestamp so that we know what the
104 // timestamps on the next set of buffers must be >= than.
105 DecodeTimestamp audio = !audio_buffers.empty() ?
106 audio_buffers.back()->GetDecodeTimestamp() : kNoDecodeTimestamp();
107 DecodeTimestamp video = !video_buffers.empty() ?
108 video_buffers.back()->GetDecodeTimestamp() : kNoDecodeTimestamp();
109 DecodeTimestamp second_highest_timestamp =
110 (audio == kNoDecodeTimestamp() ||
111 (video != kNoDecodeTimestamp() && audio > video)) ? video : audio;
113 DCHECK(second_highest_timestamp != kNoDecodeTimestamp());
115 if (lower_bound_ != kNoDecodeTimestamp() &&
116 second_highest_timestamp < lower_bound_) {
117 return false;
120 lower_bound_ = second_highest_timestamp;
121 return true;
124 void KeyNeededF(EmeInitDataType type, const std::vector<uint8>& init_data) {
125 DVLOG(1) << "KeyNeededF: " << init_data.size();
126 EXPECT_EQ(EmeInitDataType::CENC, type);
127 EXPECT_FALSE(init_data.empty());
130 void NewSegmentF() {
131 DVLOG(1) << "NewSegmentF";
132 lower_bound_ = kNoDecodeTimestamp();
135 void EndOfSegmentF() {
136 DVLOG(1) << "EndOfSegmentF()";
137 lower_bound_ =
138 DecodeTimestamp::FromPresentationTime(base::TimeDelta::Max());
141 void InitializeParserAndExpectLiveness(
142 DemuxerStream::Liveness expected_liveness) {
143 parser_->Init(
144 base::Bind(&MP4StreamParserTest::InitF, base::Unretained(this),
145 expected_liveness),
146 base::Bind(&MP4StreamParserTest::NewConfigF, base::Unretained(this)),
147 base::Bind(&MP4StreamParserTest::NewBuffersF, base::Unretained(this)),
148 true,
149 base::Bind(&MP4StreamParserTest::KeyNeededF, base::Unretained(this)),
150 base::Bind(&MP4StreamParserTest::NewSegmentF, base::Unretained(this)),
151 base::Bind(&MP4StreamParserTest::EndOfSegmentF, base::Unretained(this)),
152 new MediaLog());
155 void InitializeParser() {
156 // Most unencrypted test mp4 files have zero duration and are treated as
157 // live streams.
158 InitializeParserAndExpectLiveness(DemuxerStream::LIVENESS_LIVE);
161 bool ParseMP4File(const std::string& filename, int append_bytes) {
162 InitializeParser();
164 scoped_refptr<DecoderBuffer> buffer = ReadTestDataFile(filename);
165 EXPECT_TRUE(AppendDataInPieces(buffer->data(),
166 buffer->data_size(),
167 append_bytes));
168 return true;
172 TEST_F(MP4StreamParserTest, UnalignedAppend) {
173 // Test small, non-segment-aligned appends (small enough to exercise
174 // incremental append system)
175 ParseMP4File("bear-1280x720-av_frag.mp4", 512);
178 TEST_F(MP4StreamParserTest, BytewiseAppend) {
179 // Ensure no incremental errors occur when parsing
180 ParseMP4File("bear-1280x720-av_frag.mp4", 1);
183 TEST_F(MP4StreamParserTest, MultiFragmentAppend) {
184 // Large size ensures multiple fragments are appended in one call (size is
185 // larger than this particular test file)
186 ParseMP4File("bear-1280x720-av_frag.mp4", 768432);
189 TEST_F(MP4StreamParserTest, Flush) {
190 // Flush while reading sample data, then start a new stream.
191 InitializeParser();
193 scoped_refptr<DecoderBuffer> buffer =
194 ReadTestDataFile("bear-1280x720-av_frag.mp4");
195 EXPECT_TRUE(AppendDataInPieces(buffer->data(), 65536, 512));
196 parser_->Flush();
197 EXPECT_TRUE(AppendDataInPieces(buffer->data(),
198 buffer->data_size(),
199 512));
202 TEST_F(MP4StreamParserTest, Reinitialization) {
203 InitializeParser();
205 scoped_refptr<DecoderBuffer> buffer =
206 ReadTestDataFile("bear-1280x720-av_frag.mp4");
207 EXPECT_TRUE(AppendDataInPieces(buffer->data(),
208 buffer->data_size(),
209 512));
210 EXPECT_TRUE(AppendDataInPieces(buffer->data(),
211 buffer->data_size(),
212 512));
215 TEST_F(MP4StreamParserTest, MPEG2_AAC_LC) {
216 std::set<int> audio_object_types;
217 audio_object_types.insert(kISO_13818_7_AAC_LC);
218 parser_.reset(new MP4StreamParser(audio_object_types, false));
219 ParseMP4File("bear-mpeg2-aac-only_frag.mp4", 512);
222 // Test that a moov box is not always required after Flush() is called.
223 TEST_F(MP4StreamParserTest, NoMoovAfterFlush) {
224 InitializeParser();
226 scoped_refptr<DecoderBuffer> buffer =
227 ReadTestDataFile("bear-1280x720-av_frag.mp4");
228 EXPECT_TRUE(AppendDataInPieces(buffer->data(),
229 buffer->data_size(),
230 512));
231 parser_->Flush();
233 const int kFirstMoofOffset = 1307;
234 EXPECT_TRUE(AppendDataInPieces(buffer->data() + kFirstMoofOffset,
235 buffer->data_size() - kFirstMoofOffset,
236 512));
239 // Test an invalid file where there are encrypted samples, but
240 // SampleAuxiliaryInformation{Sizes|Offsets}Box (saiz|saio) are missing.
241 // The parser should fail instead of crash. See http://crbug.com/361347
242 TEST_F(MP4StreamParserTest, MissingSampleAuxInfo) {
243 // Encrypted test mp4 files have non-zero duration and are treated as
244 // recorded streams.
245 InitializeParserAndExpectLiveness(DemuxerStream::LIVENESS_RECORDED);
247 scoped_refptr<DecoderBuffer> buffer =
248 ReadTestDataFile("bear-1280x720-a_frag-cenc_missing-saiz-saio.mp4");
249 EXPECT_FALSE(AppendDataInPieces(buffer->data(), buffer->data_size(), 512));
252 // Test a file where all video samples start with an Access Unit
253 // Delimiter (AUD) NALU.
254 TEST_F(MP4StreamParserTest, VideoSamplesStartWithAUDs) {
255 ParseMP4File("bear-1280x720-av_with-aud-nalus_frag.mp4", 512);
258 TEST_F(MP4StreamParserTest, CENC) {
259 // Encrypted test mp4 files have non-zero duration and are treated as
260 // recorded streams.
261 InitializeParserAndExpectLiveness(DemuxerStream::LIVENESS_RECORDED);
263 scoped_refptr<DecoderBuffer> buffer =
264 ReadTestDataFile("bear-1280x720-v_frag-cenc.mp4");
265 EXPECT_TRUE(AppendDataInPieces(buffer->data(), buffer->data_size(), 512));
268 TEST_F(MP4StreamParserTest, NaturalSizeWithoutPASP) {
269 InitializeParserAndExpectLiveness(DemuxerStream::LIVENESS_RECORDED);
271 scoped_refptr<DecoderBuffer> buffer =
272 ReadTestDataFile("bear-640x360-non_square_pixel-without_pasp.mp4");
274 EXPECT_TRUE(AppendDataInPieces(buffer->data(), buffer->data_size(), 512));
275 EXPECT_EQ(gfx::Size(638, 360), video_decoder_config_.natural_size());
278 TEST_F(MP4StreamParserTest, NaturalSizeWithPASP) {
279 InitializeParserAndExpectLiveness(DemuxerStream::LIVENESS_RECORDED);
281 scoped_refptr<DecoderBuffer> buffer =
282 ReadTestDataFile("bear-640x360-non_square_pixel-with_pasp.mp4");
284 EXPECT_TRUE(AppendDataInPieces(buffer->data(), buffer->data_size(), 512));
285 EXPECT_EQ(gfx::Size(638, 360), video_decoder_config_.natural_size());
288 } // namespace mp4
289 } // namespace media