Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / media / mp4 / mp4_stream_parser_unittest.cc
blob41055996c5b5dde8f4fe2d5bd6685ff01ae92550
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.
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.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;
24 namespace media {
25 namespace mp4 {
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 {
31 public:
32 MP4StreamParserTest()
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));
39 protected:
40 scoped_ptr<MP4StreamParser> parser_;
41 base::TimeDelta segment_start_;
42 bool configs_received_;
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(bool init_ok, base::TimeDelta duration) {
62 DVLOG(1) << "InitF: ok=" << init_ok
63 << ", dur=" << duration.InMilliseconds();
66 bool NewConfigF(const AudioDecoderConfig& ac, const VideoDecoderConfig& vc) {
67 DVLOG(1) << "NewConfigF: audio=" << ac.IsValidConfig()
68 << ", video=" << vc.IsValidConfig();
69 configs_received_ = true;
70 return true;
73 bool NewBuffersF(const StreamParser::BufferQueue& bufs) {
74 DVLOG(2) << "NewBuffersF: " << bufs.size() << " buffers";
75 for (StreamParser::BufferQueue::const_iterator buf = bufs.begin();
76 buf != bufs.end(); buf++) {
77 DVLOG(3) << " n=" << buf - bufs.begin()
78 << ", size=" << (*buf)->GetDataSize()
79 << ", dur=" << (*buf)->GetDuration().InMilliseconds();
80 EXPECT_GE((*buf)->GetTimestamp(), segment_start_);
82 return true;
85 bool KeyNeededF(const std::string& type,
86 scoped_ptr<uint8[]> init_data, int init_data_size) {
87 DVLOG(1) << "KeyNeededF: " << init_data_size;
88 EXPECT_EQ(kMp4InitDataType, type);
89 EXPECT_TRUE(init_data.get());
90 EXPECT_GT(init_data_size, 0);
91 return true;
94 void NewSegmentF(TimeDelta start_dts) {
95 DVLOG(1) << "NewSegmentF: " << start_dts.InMilliseconds();
96 segment_start_ = start_dts;
99 void EndOfSegmentF() {
100 DVLOG(1) << "EndOfSegmentF()";
103 void InitializeParser() {
104 parser_->Init(
105 base::Bind(&MP4StreamParserTest::InitF, base::Unretained(this)),
106 base::Bind(&MP4StreamParserTest::NewConfigF, base::Unretained(this)),
107 base::Bind(&MP4StreamParserTest::NewBuffersF, base::Unretained(this)),
108 base::Bind(&MP4StreamParserTest::NewBuffersF, base::Unretained(this)),
109 base::Bind(&MP4StreamParserTest::KeyNeededF, base::Unretained(this)),
110 base::Bind(&MP4StreamParserTest::NewSegmentF, base::Unretained(this)),
111 base::Bind(&MP4StreamParserTest::EndOfSegmentF,
112 base::Unretained(this)),
113 LogCB());
116 bool ParseMP4File(const std::string& filename, int append_bytes) {
117 InitializeParser();
119 scoped_refptr<DecoderBuffer> buffer = ReadTestDataFile(filename);
120 EXPECT_TRUE(AppendDataInPieces(buffer->GetData(),
121 buffer->GetDataSize(),
122 append_bytes));
123 return true;
127 TEST_F(MP4StreamParserTest, TestUnalignedAppend) {
128 // Test small, non-segment-aligned appends (small enough to exercise
129 // incremental append system)
130 ParseMP4File("bear-1280x720-av_frag.mp4", 512);
133 TEST_F(MP4StreamParserTest, TestBytewiseAppend) {
134 // Ensure no incremental errors occur when parsing
135 ParseMP4File("bear-1280x720-av_frag.mp4", 1);
138 TEST_F(MP4StreamParserTest, TestMultiFragmentAppend) {
139 // Large size ensures multiple fragments are appended in one call (size is
140 // larger than this particular test file)
141 ParseMP4File("bear-1280x720-av_frag.mp4", 768432);
144 TEST_F(MP4StreamParserTest, TestFlush) {
145 // Flush while reading sample data, then start a new stream.
146 InitializeParser();
148 scoped_refptr<DecoderBuffer> buffer =
149 ReadTestDataFile("bear-1280x720-av_frag.mp4");
150 EXPECT_TRUE(AppendDataInPieces(buffer->GetData(), 65536, 512));
151 parser_->Flush();
152 EXPECT_TRUE(AppendDataInPieces(buffer->GetData(),
153 buffer->GetDataSize(),
154 512));
157 TEST_F(MP4StreamParserTest, TestReinitialization) {
158 InitializeParser();
160 scoped_refptr<DecoderBuffer> buffer =
161 ReadTestDataFile("bear-1280x720-av_frag.mp4");
162 EXPECT_TRUE(AppendDataInPieces(buffer->GetData(),
163 buffer->GetDataSize(),
164 512));
165 EXPECT_TRUE(AppendDataInPieces(buffer->GetData(),
166 buffer->GetDataSize(),
167 512));
170 TEST_F(MP4StreamParserTest, TestMPEG2_AAC_LC) {
171 std::set<int> audio_object_types;
172 audio_object_types.insert(kISO_13818_7_AAC_LC);
173 parser_.reset(new MP4StreamParser(audio_object_types, false));
174 ParseMP4File("bear-mpeg2-aac-only_frag.mp4", 512);
177 // TODO(strobe): Create and test media which uses CENC auxiliary info stored
178 // inside a private box
180 } // namespace mp4
181 } // namespace media