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 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
;
54 size_t append_size
= std::min(piece_size
,
55 static_cast<size_t>(end
- start
));
56 if (!AppendData(start
, append_size
))
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
;
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())
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_
) {
120 lower_bound_
= second_highest_timestamp
;
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());
131 DVLOG(1) << "NewSegmentF";
132 lower_bound_
= kNoDecodeTimestamp();
135 void EndOfSegmentF() {
136 DVLOG(1) << "EndOfSegmentF()";
138 DecodeTimestamp::FromPresentationTime(base::TimeDelta::Max());
141 void InitializeParserAndExpectLiveness(
142 DemuxerStream::Liveness expected_liveness
) {
144 base::Bind(&MP4StreamParserTest::InitF
, base::Unretained(this),
146 base::Bind(&MP4StreamParserTest::NewConfigF
, base::Unretained(this)),
147 base::Bind(&MP4StreamParserTest::NewBuffersF
, base::Unretained(this)),
149 base::Bind(&MP4StreamParserTest::KeyNeededF
, base::Unretained(this)),
150 base::Bind(&MP4StreamParserTest::NewSegmentF
, base::Unretained(this)),
151 base::Bind(&MP4StreamParserTest::EndOfSegmentF
, base::Unretained(this)),
155 void InitializeParser() {
156 // Most unencrypted test mp4 files have zero duration and are treated as
158 InitializeParserAndExpectLiveness(DemuxerStream::LIVENESS_LIVE
);
161 bool ParseMP4File(const std::string
& filename
, int append_bytes
) {
164 scoped_refptr
<DecoderBuffer
> buffer
= ReadTestDataFile(filename
);
165 EXPECT_TRUE(AppendDataInPieces(buffer
->data(),
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.
193 scoped_refptr
<DecoderBuffer
> buffer
=
194 ReadTestDataFile("bear-1280x720-av_frag.mp4");
195 EXPECT_TRUE(AppendDataInPieces(buffer
->data(), 65536, 512));
197 EXPECT_TRUE(AppendDataInPieces(buffer
->data(),
202 TEST_F(MP4StreamParserTest
, Reinitialization
) {
205 scoped_refptr
<DecoderBuffer
> buffer
=
206 ReadTestDataFile("bear-1280x720-av_frag.mp4");
207 EXPECT_TRUE(AppendDataInPieces(buffer
->data(),
210 EXPECT_TRUE(AppendDataInPieces(buffer
->data(),
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
) {
226 scoped_refptr
<DecoderBuffer
> buffer
=
227 ReadTestDataFile("bear-1280x720-av_frag.mp4");
228 EXPECT_TRUE(AppendDataInPieces(buffer
->data(),
233 const int kFirstMoofOffset
= 1307;
234 EXPECT_TRUE(AppendDataInPieces(buffer
->data() + kFirstMoofOffset
,
235 buffer
->data_size() - kFirstMoofOffset
,
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
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
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());