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 class MP4StreamParserTest
: public testing::Test
{
31 : configs_received_(false),
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));
40 scoped_ptr
<MP4StreamParser
> parser_
;
41 bool configs_received_
;
42 DecodeTimestamp lower_bound_
;
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(const StreamParser::InitParameters
& params
) {
62 DVLOG(1) << "InitF: dur=" << params
.duration
.InMilliseconds()
63 << ", autoTimestampOffset=" << params
.auto_update_timestamp_offset
;
66 bool NewConfigF(const AudioDecoderConfig
& ac
,
67 const VideoDecoderConfig
& vc
,
68 const StreamParser::TextTrackConfigMap
& tc
) {
69 DVLOG(1) << "NewConfigF: audio=" << ac
.IsValidConfig()
70 << ", video=" << vc
.IsValidConfig();
71 configs_received_
= true;
75 void DumpBuffers(const std::string
& label
,
76 const StreamParser::BufferQueue
& buffers
) {
77 DVLOG(2) << "DumpBuffers: " << label
<< " size " << buffers
.size();
78 for (StreamParser::BufferQueue::const_iterator buf
= buffers
.begin();
79 buf
!= buffers
.end(); buf
++) {
80 DVLOG(3) << " n=" << buf
- buffers
.begin()
81 << ", size=" << (*buf
)->data_size()
82 << ", dur=" << (*buf
)->duration().InMilliseconds();
86 bool NewBuffersF(const StreamParser::BufferQueue
& audio_buffers
,
87 const StreamParser::BufferQueue
& video_buffers
,
88 const StreamParser::TextBufferQueueMap
& text_map
) {
89 DumpBuffers("audio_buffers", audio_buffers
);
90 DumpBuffers("video_buffers", video_buffers
);
92 // TODO(wolenetz/acolwell): Add text track support to more MSE parsers. See
93 // http://crbug.com/336926.
94 if (!text_map
.empty())
97 // Find the second highest timestamp so that we know what the
98 // timestamps on the next set of buffers must be >= than.
99 DecodeTimestamp audio
= !audio_buffers
.empty() ?
100 audio_buffers
.back()->GetDecodeTimestamp() : kNoDecodeTimestamp();
101 DecodeTimestamp video
= !video_buffers
.empty() ?
102 video_buffers
.back()->GetDecodeTimestamp() : kNoDecodeTimestamp();
103 DecodeTimestamp second_highest_timestamp
=
104 (audio
== kNoDecodeTimestamp() ||
105 (video
!= kNoDecodeTimestamp() && audio
> video
)) ? video
: audio
;
107 DCHECK(second_highest_timestamp
!= kNoDecodeTimestamp());
109 if (lower_bound_
!= kNoDecodeTimestamp() &&
110 second_highest_timestamp
< lower_bound_
) {
114 lower_bound_
= second_highest_timestamp
;
118 void KeyNeededF(EmeInitDataType type
, const std::vector
<uint8
>& init_data
) {
119 DVLOG(1) << "KeyNeededF: " << init_data
.size();
120 EXPECT_EQ(EmeInitDataType::CENC
, type
);
121 EXPECT_FALSE(init_data
.empty());
125 DVLOG(1) << "NewSegmentF";
126 lower_bound_
= kNoDecodeTimestamp();
129 void EndOfSegmentF() {
130 DVLOG(1) << "EndOfSegmentF()";
132 DecodeTimestamp::FromPresentationTime(base::TimeDelta::Max());
135 void InitializeParser() {
137 base::Bind(&MP4StreamParserTest::InitF
, base::Unretained(this)),
138 base::Bind(&MP4StreamParserTest::NewConfigF
, base::Unretained(this)),
139 base::Bind(&MP4StreamParserTest::NewBuffersF
, base::Unretained(this)),
141 base::Bind(&MP4StreamParserTest::KeyNeededF
, base::Unretained(this)),
142 base::Bind(&MP4StreamParserTest::NewSegmentF
, base::Unretained(this)),
143 base::Bind(&MP4StreamParserTest::EndOfSegmentF
,
144 base::Unretained(this)),
148 bool ParseMP4File(const std::string
& filename
, int append_bytes
) {
151 scoped_refptr
<DecoderBuffer
> buffer
= ReadTestDataFile(filename
);
152 EXPECT_TRUE(AppendDataInPieces(buffer
->data(),
159 TEST_F(MP4StreamParserTest
, UnalignedAppend
) {
160 // Test small, non-segment-aligned appends (small enough to exercise
161 // incremental append system)
162 ParseMP4File("bear-1280x720-av_frag.mp4", 512);
165 TEST_F(MP4StreamParserTest
, BytewiseAppend
) {
166 // Ensure no incremental errors occur when parsing
167 ParseMP4File("bear-1280x720-av_frag.mp4", 1);
170 TEST_F(MP4StreamParserTest
, MultiFragmentAppend
) {
171 // Large size ensures multiple fragments are appended in one call (size is
172 // larger than this particular test file)
173 ParseMP4File("bear-1280x720-av_frag.mp4", 768432);
176 TEST_F(MP4StreamParserTest
, Flush
) {
177 // Flush while reading sample data, then start a new stream.
180 scoped_refptr
<DecoderBuffer
> buffer
=
181 ReadTestDataFile("bear-1280x720-av_frag.mp4");
182 EXPECT_TRUE(AppendDataInPieces(buffer
->data(), 65536, 512));
184 EXPECT_TRUE(AppendDataInPieces(buffer
->data(),
189 TEST_F(MP4StreamParserTest
, Reinitialization
) {
192 scoped_refptr
<DecoderBuffer
> buffer
=
193 ReadTestDataFile("bear-1280x720-av_frag.mp4");
194 EXPECT_TRUE(AppendDataInPieces(buffer
->data(),
197 EXPECT_TRUE(AppendDataInPieces(buffer
->data(),
202 TEST_F(MP4StreamParserTest
, MPEG2_AAC_LC
) {
203 std::set
<int> audio_object_types
;
204 audio_object_types
.insert(kISO_13818_7_AAC_LC
);
205 parser_
.reset(new MP4StreamParser(audio_object_types
, false));
206 ParseMP4File("bear-mpeg2-aac-only_frag.mp4", 512);
209 // Test that a moov box is not always required after Flush() is called.
210 TEST_F(MP4StreamParserTest
, NoMoovAfterFlush
) {
213 scoped_refptr
<DecoderBuffer
> buffer
=
214 ReadTestDataFile("bear-1280x720-av_frag.mp4");
215 EXPECT_TRUE(AppendDataInPieces(buffer
->data(),
220 const int kFirstMoofOffset
= 1307;
221 EXPECT_TRUE(AppendDataInPieces(buffer
->data() + kFirstMoofOffset
,
222 buffer
->data_size() - kFirstMoofOffset
,
226 // Test an invalid file where there are encrypted samples, but
227 // SampleAuxiliaryInformation{Sizes|Offsets}Box (saiz|saio) are missing.
228 // The parser should fail instead of crash. See http://crbug.com/361347
229 TEST_F(MP4StreamParserTest
, MissingSampleAuxInfo
) {
232 scoped_refptr
<DecoderBuffer
> buffer
=
233 ReadTestDataFile("bear-1280x720-a_frag-cenc_missing-saiz-saio.mp4");
234 EXPECT_FALSE(AppendDataInPieces(buffer
->data(), buffer
->data_size(), 512));
237 // Test a file where all video samples start with an Access Unit
238 // Delimiter (AUD) NALU.
239 TEST_F(MP4StreamParserTest
, VideoSamplesStartWithAUDs
) {
240 ParseMP4File("bear-1280x720-av_with-aud-nalus_frag.mp4", 512);
243 // TODO(strobe): Create and test media which uses CENC auxiliary info stored
244 // inside a private box