1 // Copyright (c) 2012 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/video_decoder_config.h"
18 #include "media/mp4/es_descriptor.h"
19 #include "media/mp4/mp4_stream_parser.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 using base::TimeDelta
;
27 // TODO(xhwang): Figure out the init data type appropriately once it's spec'ed.
28 static const char kMp4InitDataType
[] = "video/mp4";
30 class MP4StreamParserTest
: public testing::Test
{
33 : configs_received_(false) {
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_
;
43 bool AppendData(const uint8
* data
, size_t length
) {
44 return parser_
->Parse(data
, length
);
47 bool AppendDataInPieces(const uint8
* data
, size_t length
, size_t piece_size
) {
48 const uint8
* start
= data
;
49 const uint8
* end
= data
+ length
;
51 size_t append_size
= std::min(piece_size
,
52 static_cast<size_t>(end
- start
));
53 if (!AppendData(start
, append_size
))
60 void InitF(bool init_ok
, base::TimeDelta duration
) {
61 DVLOG(1) << "InitF: ok=" << init_ok
62 << ", dur=" << duration
.InMilliseconds();
65 bool NewConfigF(const AudioDecoderConfig
& ac
, const VideoDecoderConfig
& vc
) {
66 DVLOG(1) << "NewConfigF: audio=" << ac
.IsValidConfig()
67 << ", video=" << vc
.IsValidConfig();
68 configs_received_
= true;
73 void DumpBuffers(const std::string
& label
,
74 const StreamParser::BufferQueue
& buffers
) {
75 DVLOG(2) << "DumpBuffers: " << label
<< " size " << buffers
.size();
76 for (StreamParser::BufferQueue::const_iterator buf
= buffers
.begin();
77 buf
!= buffers
.end(); buf
++) {
78 DVLOG(3) << " n=" << buf
- buffers
.begin()
79 << ", size=" << (*buf
)->data_size()
80 << ", dur=" << (*buf
)->duration().InMilliseconds();
84 bool NewBuffersF(const StreamParser::BufferQueue
& audio_buffers
,
85 const StreamParser::BufferQueue
& video_buffers
) {
86 DumpBuffers("audio_buffers", audio_buffers
);
87 DumpBuffers("video_buffers", video_buffers
);
91 bool NewTextBuffersF(TextTrack
* text_track
,
92 const StreamParser::BufferQueue
& buffers
) {
96 void KeyNeededF(const std::string
& type
,
97 const std::vector
<uint8
>& init_data
) {
98 DVLOG(1) << "KeyNeededF: " << init_data
.size();
99 EXPECT_EQ(kMp4InitDataType
, type
);
100 EXPECT_FALSE(init_data
.empty());
103 scoped_ptr
<TextTrack
> AddTextTrackF(
105 const std::string
& label
,
106 const std::string
& language
) {
107 return scoped_ptr
<TextTrack
>();
111 DVLOG(1) << "NewSegmentF";
114 void EndOfSegmentF() {
115 DVLOG(1) << "EndOfSegmentF()";
118 void InitializeParser() {
120 base::Bind(&MP4StreamParserTest::InitF
, base::Unretained(this)),
121 base::Bind(&MP4StreamParserTest::NewConfigF
, base::Unretained(this)),
122 base::Bind(&MP4StreamParserTest::NewBuffersF
, base::Unretained(this)),
123 base::Bind(&MP4StreamParserTest::NewTextBuffersF
,
124 base::Unretained(this)),
125 base::Bind(&MP4StreamParserTest::KeyNeededF
, base::Unretained(this)),
126 base::Bind(&MP4StreamParserTest::AddTextTrackF
, base::Unretained(this)),
127 base::Bind(&MP4StreamParserTest::NewSegmentF
, base::Unretained(this)),
128 base::Bind(&MP4StreamParserTest::EndOfSegmentF
,
129 base::Unretained(this)),
133 bool ParseMP4File(const std::string
& filename
, int append_bytes
) {
136 scoped_refptr
<DecoderBuffer
> buffer
= ReadTestDataFile(filename
);
137 EXPECT_TRUE(AppendDataInPieces(buffer
->data(),
144 TEST_F(MP4StreamParserTest
, UnalignedAppend
) {
145 // Test small, non-segment-aligned appends (small enough to exercise
146 // incremental append system)
147 ParseMP4File("bear-1280x720-av_frag.mp4", 512);
150 TEST_F(MP4StreamParserTest
, BytewiseAppend
) {
151 // Ensure no incremental errors occur when parsing
152 ParseMP4File("bear-1280x720-av_frag.mp4", 1);
155 TEST_F(MP4StreamParserTest
, MultiFragmentAppend
) {
156 // Large size ensures multiple fragments are appended in one call (size is
157 // larger than this particular test file)
158 ParseMP4File("bear-1280x720-av_frag.mp4", 768432);
161 TEST_F(MP4StreamParserTest
, Flush
) {
162 // Flush while reading sample data, then start a new stream.
165 scoped_refptr
<DecoderBuffer
> buffer
=
166 ReadTestDataFile("bear-1280x720-av_frag.mp4");
167 EXPECT_TRUE(AppendDataInPieces(buffer
->data(), 65536, 512));
169 EXPECT_TRUE(AppendDataInPieces(buffer
->data(),
174 TEST_F(MP4StreamParserTest
, Reinitialization
) {
177 scoped_refptr
<DecoderBuffer
> buffer
=
178 ReadTestDataFile("bear-1280x720-av_frag.mp4");
179 EXPECT_TRUE(AppendDataInPieces(buffer
->data(),
182 EXPECT_TRUE(AppendDataInPieces(buffer
->data(),
187 TEST_F(MP4StreamParserTest
, MPEG2_AAC_LC
) {
188 std::set
<int> audio_object_types
;
189 audio_object_types
.insert(kISO_13818_7_AAC_LC
);
190 parser_
.reset(new MP4StreamParser(audio_object_types
, false));
191 ParseMP4File("bear-mpeg2-aac-only_frag.mp4", 512);
194 // Test that a moov box is not always required after Flush() is called.
195 TEST_F(MP4StreamParserTest
, NoMoovAfterFlush
) {
198 scoped_refptr
<DecoderBuffer
> buffer
=
199 ReadTestDataFile("bear-1280x720-av_frag.mp4");
200 EXPECT_TRUE(AppendDataInPieces(buffer
->data(),
205 const int kFirstMoofOffset
= 1307;
206 EXPECT_TRUE(AppendDataInPieces(buffer
->data() + kFirstMoofOffset
,
207 buffer
->data_size() - kFirstMoofOffset
,
211 // TODO(strobe): Create and test media which uses CENC auxiliary info stored
212 // inside a private box