Remove INJECT_EVENTS permissions from test APKs.
[chromium-blink-merge.git] / media / formats / mp4 / mp4_stream_parser_unittest.cc
blob536b2782242696727ab3a8c6b407833b6d185b63
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 class MP4StreamParserTest : public testing::Test {
29 public:
30 MP4StreamParserTest()
31 : configs_received_(false),
32 lower_bound_(
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));
39 protected:
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;
51 while (start < end) {
52 size_t append_size = std::min(piece_size,
53 static_cast<size_t>(end - start));
54 if (!AppendData(start, append_size))
55 return false;
56 start += append_size;
58 return true;
61 void InitF(DemuxerStream::Liveness expected_liveness,
62 const StreamParser::InitParameters& params) {
63 DVLOG(1) << "InitF: dur=" << params.duration.InMilliseconds()
64 << ", autoTimestampOffset=" << params.auto_update_timestamp_offset;
65 EXPECT_EQ(expected_liveness, params.liveness);
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(EmeInitDataType type, const std::vector<uint8>& init_data) {
121 DVLOG(1) << "KeyNeededF: " << init_data.size();
122 EXPECT_EQ(EmeInitDataType::CENC, type);
123 EXPECT_FALSE(init_data.empty());
126 void NewSegmentF() {
127 DVLOG(1) << "NewSegmentF";
128 lower_bound_ = kNoDecodeTimestamp();
131 void EndOfSegmentF() {
132 DVLOG(1) << "EndOfSegmentF()";
133 lower_bound_ =
134 DecodeTimestamp::FromPresentationTime(base::TimeDelta::Max());
137 void InitializeParserAndExpectLiveness(
138 DemuxerStream::Liveness expected_liveness) {
139 parser_->Init(
140 base::Bind(&MP4StreamParserTest::InitF, base::Unretained(this),
141 expected_liveness),
142 base::Bind(&MP4StreamParserTest::NewConfigF, base::Unretained(this)),
143 base::Bind(&MP4StreamParserTest::NewBuffersF, base::Unretained(this)),
144 true,
145 base::Bind(&MP4StreamParserTest::KeyNeededF, base::Unretained(this)),
146 base::Bind(&MP4StreamParserTest::NewSegmentF, base::Unretained(this)),
147 base::Bind(&MP4StreamParserTest::EndOfSegmentF, base::Unretained(this)),
148 LogCB());
151 void InitializeParser() {
152 // Most unencrypted test mp4 files have zero duration and are treated as
153 // live streams.
154 InitializeParserAndExpectLiveness(DemuxerStream::LIVENESS_LIVE);
157 bool ParseMP4File(const std::string& filename, int append_bytes) {
158 InitializeParser();
160 scoped_refptr<DecoderBuffer> buffer = ReadTestDataFile(filename);
161 EXPECT_TRUE(AppendDataInPieces(buffer->data(),
162 buffer->data_size(),
163 append_bytes));
164 return true;
168 TEST_F(MP4StreamParserTest, UnalignedAppend) {
169 // Test small, non-segment-aligned appends (small enough to exercise
170 // incremental append system)
171 ParseMP4File("bear-1280x720-av_frag.mp4", 512);
174 TEST_F(MP4StreamParserTest, BytewiseAppend) {
175 // Ensure no incremental errors occur when parsing
176 ParseMP4File("bear-1280x720-av_frag.mp4", 1);
179 TEST_F(MP4StreamParserTest, MultiFragmentAppend) {
180 // Large size ensures multiple fragments are appended in one call (size is
181 // larger than this particular test file)
182 ParseMP4File("bear-1280x720-av_frag.mp4", 768432);
185 TEST_F(MP4StreamParserTest, Flush) {
186 // Flush while reading sample data, then start a new stream.
187 InitializeParser();
189 scoped_refptr<DecoderBuffer> buffer =
190 ReadTestDataFile("bear-1280x720-av_frag.mp4");
191 EXPECT_TRUE(AppendDataInPieces(buffer->data(), 65536, 512));
192 parser_->Flush();
193 EXPECT_TRUE(AppendDataInPieces(buffer->data(),
194 buffer->data_size(),
195 512));
198 TEST_F(MP4StreamParserTest, Reinitialization) {
199 InitializeParser();
201 scoped_refptr<DecoderBuffer> buffer =
202 ReadTestDataFile("bear-1280x720-av_frag.mp4");
203 EXPECT_TRUE(AppendDataInPieces(buffer->data(),
204 buffer->data_size(),
205 512));
206 EXPECT_TRUE(AppendDataInPieces(buffer->data(),
207 buffer->data_size(),
208 512));
211 TEST_F(MP4StreamParserTest, MPEG2_AAC_LC) {
212 std::set<int> audio_object_types;
213 audio_object_types.insert(kISO_13818_7_AAC_LC);
214 parser_.reset(new MP4StreamParser(audio_object_types, false));
215 ParseMP4File("bear-mpeg2-aac-only_frag.mp4", 512);
218 // Test that a moov box is not always required after Flush() is called.
219 TEST_F(MP4StreamParserTest, NoMoovAfterFlush) {
220 InitializeParser();
222 scoped_refptr<DecoderBuffer> buffer =
223 ReadTestDataFile("bear-1280x720-av_frag.mp4");
224 EXPECT_TRUE(AppendDataInPieces(buffer->data(),
225 buffer->data_size(),
226 512));
227 parser_->Flush();
229 const int kFirstMoofOffset = 1307;
230 EXPECT_TRUE(AppendDataInPieces(buffer->data() + kFirstMoofOffset,
231 buffer->data_size() - kFirstMoofOffset,
232 512));
235 // Test an invalid file where there are encrypted samples, but
236 // SampleAuxiliaryInformation{Sizes|Offsets}Box (saiz|saio) are missing.
237 // The parser should fail instead of crash. See http://crbug.com/361347
238 TEST_F(MP4StreamParserTest, MissingSampleAuxInfo) {
239 // Encrypted test mp4 files have non-zero duration and are treated as
240 // recorded streams.
241 InitializeParserAndExpectLiveness(DemuxerStream::LIVENESS_RECORDED);
243 scoped_refptr<DecoderBuffer> buffer =
244 ReadTestDataFile("bear-1280x720-a_frag-cenc_missing-saiz-saio.mp4");
245 EXPECT_FALSE(AppendDataInPieces(buffer->data(), buffer->data_size(), 512));
248 // Test a file where all video samples start with an Access Unit
249 // Delimiter (AUD) NALU.
250 TEST_F(MP4StreamParserTest, VideoSamplesStartWithAUDs) {
251 ParseMP4File("bear-1280x720-av_with-aud-nalus_frag.mp4", 512);
254 TEST_F(MP4StreamParserTest, CENC) {
255 // Encrypted test mp4 files have non-zero duration and are treated as
256 // recorded streams.
257 InitializeParserAndExpectLiveness(DemuxerStream::LIVENESS_RECORDED);
259 scoped_refptr<DecoderBuffer> buffer =
260 ReadTestDataFile("bear-1280x720-v_frag-cenc.mp4");
261 EXPECT_TRUE(AppendDataInPieces(buffer->data(), buffer->data_size(), 512));
264 } // namespace mp4
265 } // namespace media