[Ozone-Gbm] Explicitly crash if trying software rendering on GBM
[chromium-blink-merge.git] / media / formats / mp4 / mp4_stream_parser_unittest.cc
blobc82d67bf6302c9b9aea824cc13bd79bf745ab801
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 static const char kCencInitDataType[] = "cenc";
30 class MP4StreamParserTest : public testing::Test {
31 public:
32 MP4StreamParserTest()
33 : configs_received_(false),
34 lower_bound_(
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));
41 protected:
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;
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(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;
74 return 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())
97 return false;
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_) {
113 return false;
116 lower_bound_ = second_highest_timestamp;
117 return true;
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());
127 void NewSegmentF() {
128 DVLOG(1) << "NewSegmentF";
129 lower_bound_ = kNoDecodeTimestamp();
132 void EndOfSegmentF() {
133 DVLOG(1) << "EndOfSegmentF()";
134 lower_bound_ =
135 DecodeTimestamp::FromPresentationTime(base::TimeDelta::Max());
138 void InitializeParser() {
139 parser_->Init(
140 base::Bind(&MP4StreamParserTest::InitF, base::Unretained(this)),
141 base::Bind(&MP4StreamParserTest::NewConfigF, base::Unretained(this)),
142 base::Bind(&MP4StreamParserTest::NewBuffersF, base::Unretained(this)),
143 true,
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)),
148 LogCB());
151 bool ParseMP4File(const std::string& filename, int append_bytes) {
152 InitializeParser();
154 scoped_refptr<DecoderBuffer> buffer = ReadTestDataFile(filename);
155 EXPECT_TRUE(AppendDataInPieces(buffer->data(),
156 buffer->data_size(),
157 append_bytes));
158 return true;
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.
181 InitializeParser();
183 scoped_refptr<DecoderBuffer> buffer =
184 ReadTestDataFile("bear-1280x720-av_frag.mp4");
185 EXPECT_TRUE(AppendDataInPieces(buffer->data(), 65536, 512));
186 parser_->Flush();
187 EXPECT_TRUE(AppendDataInPieces(buffer->data(),
188 buffer->data_size(),
189 512));
192 TEST_F(MP4StreamParserTest, Reinitialization) {
193 InitializeParser();
195 scoped_refptr<DecoderBuffer> buffer =
196 ReadTestDataFile("bear-1280x720-av_frag.mp4");
197 EXPECT_TRUE(AppendDataInPieces(buffer->data(),
198 buffer->data_size(),
199 512));
200 EXPECT_TRUE(AppendDataInPieces(buffer->data(),
201 buffer->data_size(),
202 512));
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) {
214 InitializeParser();
216 scoped_refptr<DecoderBuffer> buffer =
217 ReadTestDataFile("bear-1280x720-av_frag.mp4");
218 EXPECT_TRUE(AppendDataInPieces(buffer->data(),
219 buffer->data_size(),
220 512));
221 parser_->Flush();
223 const int kFirstMoofOffset = 1307;
224 EXPECT_TRUE(AppendDataInPieces(buffer->data() + kFirstMoofOffset,
225 buffer->data_size() - kFirstMoofOffset,
226 512));
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) {
233 InitializeParser();
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
249 } // namespace mp4
250 } // namespace media