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 static const char kCencInitDataType
[] = "cenc";
30 class MP4StreamParserTest
: public testing::Test
{
33 : configs_received_(false),
35 DecodeTimestamp::FromPresentationTime(base::TimeDelta::Max())) {
36 std::set
<int> audio_object_types
;
37 audio_object_types
.insert(kISO_14496_3
);
38 parser_
.reset(new MP4StreamParser(audio_object_types
, false));
42 scoped_ptr
<MP4StreamParser
> parser_
;
43 bool configs_received_
;
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(const StreamParser::InitParameters
& params
) {
64 DVLOG(1) << "InitF: dur=" << params
.duration
.InMilliseconds()
65 << ", autoTimestampOffset=" << params
.auto_update_timestamp_offset
;
68 bool NewConfigF(const AudioDecoderConfig
& ac
,
69 const VideoDecoderConfig
& vc
,
70 const StreamParser::TextTrackConfigMap
& tc
) {
71 DVLOG(1) << "NewConfigF: audio=" << ac
.IsValidConfig()
72 << ", video=" << vc
.IsValidConfig();
73 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())
99 // Find the second highest timestamp so that we know what the
100 // timestamps on the next set of buffers must be >= than.
101 DecodeTimestamp audio
= !audio_buffers
.empty() ?
102 audio_buffers
.back()->GetDecodeTimestamp() : kNoDecodeTimestamp();
103 DecodeTimestamp video
= !video_buffers
.empty() ?
104 video_buffers
.back()->GetDecodeTimestamp() : kNoDecodeTimestamp();
105 DecodeTimestamp second_highest_timestamp
=
106 (audio
== kNoDecodeTimestamp() ||
107 (video
!= kNoDecodeTimestamp() && audio
> video
)) ? video
: audio
;
109 DCHECK(second_highest_timestamp
!= kNoDecodeTimestamp());
111 if (lower_bound_
!= kNoDecodeTimestamp() &&
112 second_highest_timestamp
< lower_bound_
) {
116 lower_bound_
= second_highest_timestamp
;
120 void KeyNeededF(const std::string
& type
,
121 const std::vector
<uint8
>& init_data
) {
122 DVLOG(1) << "KeyNeededF: " << init_data
.size();
123 EXPECT_EQ(kCencInitDataType
, type
);
124 EXPECT_FALSE(init_data
.empty());
128 DVLOG(1) << "NewSegmentF";
129 lower_bound_
= kNoDecodeTimestamp();
132 void EndOfSegmentF() {
133 DVLOG(1) << "EndOfSegmentF()";
135 DecodeTimestamp::FromPresentationTime(base::TimeDelta::Max());
138 void InitializeParser() {
140 base::Bind(&MP4StreamParserTest::InitF
, base::Unretained(this)),
141 base::Bind(&MP4StreamParserTest::NewConfigF
, base::Unretained(this)),
142 base::Bind(&MP4StreamParserTest::NewBuffersF
, base::Unretained(this)),
144 base::Bind(&MP4StreamParserTest::KeyNeededF
, base::Unretained(this)),
145 base::Bind(&MP4StreamParserTest::NewSegmentF
, base::Unretained(this)),
146 base::Bind(&MP4StreamParserTest::EndOfSegmentF
,
147 base::Unretained(this)),
151 bool ParseMP4File(const std::string
& filename
, int append_bytes
) {
154 scoped_refptr
<DecoderBuffer
> buffer
= ReadTestDataFile(filename
);
155 EXPECT_TRUE(AppendDataInPieces(buffer
->data(),
162 TEST_F(MP4StreamParserTest
, UnalignedAppend
) {
163 // Test small, non-segment-aligned appends (small enough to exercise
164 // incremental append system)
165 ParseMP4File("bear-1280x720-av_frag.mp4", 512);
168 TEST_F(MP4StreamParserTest
, BytewiseAppend
) {
169 // Ensure no incremental errors occur when parsing
170 ParseMP4File("bear-1280x720-av_frag.mp4", 1);
173 TEST_F(MP4StreamParserTest
, MultiFragmentAppend
) {
174 // Large size ensures multiple fragments are appended in one call (size is
175 // larger than this particular test file)
176 ParseMP4File("bear-1280x720-av_frag.mp4", 768432);
179 TEST_F(MP4StreamParserTest
, Flush
) {
180 // Flush while reading sample data, then start a new stream.
183 scoped_refptr
<DecoderBuffer
> buffer
=
184 ReadTestDataFile("bear-1280x720-av_frag.mp4");
185 EXPECT_TRUE(AppendDataInPieces(buffer
->data(), 65536, 512));
187 EXPECT_TRUE(AppendDataInPieces(buffer
->data(),
192 TEST_F(MP4StreamParserTest
, Reinitialization
) {
195 scoped_refptr
<DecoderBuffer
> buffer
=
196 ReadTestDataFile("bear-1280x720-av_frag.mp4");
197 EXPECT_TRUE(AppendDataInPieces(buffer
->data(),
200 EXPECT_TRUE(AppendDataInPieces(buffer
->data(),
205 TEST_F(MP4StreamParserTest
, MPEG2_AAC_LC
) {
206 std::set
<int> audio_object_types
;
207 audio_object_types
.insert(kISO_13818_7_AAC_LC
);
208 parser_
.reset(new MP4StreamParser(audio_object_types
, false));
209 ParseMP4File("bear-mpeg2-aac-only_frag.mp4", 512);
212 // Test that a moov box is not always required after Flush() is called.
213 TEST_F(MP4StreamParserTest
, NoMoovAfterFlush
) {
216 scoped_refptr
<DecoderBuffer
> buffer
=
217 ReadTestDataFile("bear-1280x720-av_frag.mp4");
218 EXPECT_TRUE(AppendDataInPieces(buffer
->data(),
223 const int kFirstMoofOffset
= 1307;
224 EXPECT_TRUE(AppendDataInPieces(buffer
->data() + kFirstMoofOffset
,
225 buffer
->data_size() - kFirstMoofOffset
,
229 // Test an invalid file where there are encrypted samples, but
230 // SampleAuxiliaryInformation{Sizes|Offsets}Box (saiz|saio) are missing.
231 // The parser should fail instead of crash. See http://crbug.com/361347
232 TEST_F(MP4StreamParserTest
, MissingSampleAuxInfo
) {
235 scoped_refptr
<DecoderBuffer
> buffer
=
236 ReadTestDataFile("bear-1280x720-a_frag-cenc_missing-saiz-saio.mp4");
237 EXPECT_FALSE(AppendDataInPieces(buffer
->data(), buffer
->data_size(), 512));
240 // Test a file where all video samples start with an Access Unit
241 // Delimiter (AUD) NALU.
242 TEST_F(MP4StreamParserTest
, VideoSamplesStartWithAUDs
) {
243 ParseMP4File("bear-1280x720-av_with-aud-nalus_frag.mp4", 512);
246 // TODO(strobe): Create and test media which uses CENC auxiliary info stored
247 // inside a private box