mp4_stream_parser_unittest.cc revision a02191e04bc25c4935f804f2c080ae28663d096d
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.
4
5#include <algorithm>
6#include <string>
7
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"
22
23using base::TimeDelta;
24
25namespace media {
26namespace mp4 {
27
28// TODO(xhwang): Figure out the init data type appropriately once it's spec'ed.
29static const char kMp4InitDataType[] = "video/mp4";
30
31class MP4StreamParserTest : public testing::Test {
32 public:
33  MP4StreamParserTest()
34      : configs_received_(false) {
35    std::set<int> audio_object_types;
36    audio_object_types.insert(kISO_14496_3);
37    parser_.reset(new MP4StreamParser(audio_object_types, false));
38  }
39
40 protected:
41  scoped_ptr<MP4StreamParser> parser_;
42  bool configs_received_;
43
44  bool AppendData(const uint8* data, size_t length) {
45    return parser_->Parse(data, length);
46  }
47
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;
57    }
58    return true;
59  }
60
61  void InitF(bool init_ok,
62             base::TimeDelta duration,
63             bool auto_update_timestamp_offset) {
64    DVLOG(1) << "InitF: ok=" << init_ok << ", dur=" << duration.InMilliseconds()
65             << ", autoTimestampOffset=" << auto_update_timestamp_offset;
66  }
67
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;
75  }
76
77
78  void DumpBuffers(const std::string& label,
79                   const StreamParser::BufferQueue& buffers) {
80    DVLOG(2) << "DumpBuffers: " << label << " size " << buffers.size();
81    for (StreamParser::BufferQueue::const_iterator buf = buffers.begin();
82         buf != buffers.end(); buf++) {
83      DVLOG(3) << "  n=" << buf - buffers.begin()
84               << ", size=" << (*buf)->data_size()
85               << ", dur=" << (*buf)->duration().InMilliseconds();
86    }
87  }
88
89  bool NewBuffersF(const StreamParser::BufferQueue& audio_buffers,
90                   const StreamParser::BufferQueue& video_buffers,
91                   const StreamParser::TextBufferQueueMap& text_map) {
92    DumpBuffers("audio_buffers", audio_buffers);
93    DumpBuffers("video_buffers", video_buffers);
94
95    // TODO(wolenetz/acolwell): Add text track support to more MSE parsers. See
96    // http://crbug.com/336926.
97    if (!text_map.empty())
98      return false;
99
100    return true;
101  }
102
103  void KeyNeededF(const std::string& type,
104                  const std::vector<uint8>& init_data) {
105    DVLOG(1) << "KeyNeededF: " << init_data.size();
106    EXPECT_EQ(kMp4InitDataType, type);
107    EXPECT_FALSE(init_data.empty());
108  }
109
110  void NewSegmentF() {
111    DVLOG(1) << "NewSegmentF";
112  }
113
114  void EndOfSegmentF() {
115    DVLOG(1) << "EndOfSegmentF()";
116  }
117
118  void InitializeParser() {
119    parser_->Init(
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        true,
124        base::Bind(&MP4StreamParserTest::KeyNeededF, base::Unretained(this)),
125        base::Bind(&MP4StreamParserTest::NewSegmentF, base::Unretained(this)),
126        base::Bind(&MP4StreamParserTest::EndOfSegmentF,
127                   base::Unretained(this)),
128        LogCB());
129  }
130
131  bool ParseMP4File(const std::string& filename, int append_bytes) {
132    InitializeParser();
133
134    scoped_refptr<DecoderBuffer> buffer = ReadTestDataFile(filename);
135    EXPECT_TRUE(AppendDataInPieces(buffer->data(),
136                                   buffer->data_size(),
137                                   append_bytes));
138    return true;
139  }
140};
141
142TEST_F(MP4StreamParserTest, UnalignedAppend) {
143  // Test small, non-segment-aligned appends (small enough to exercise
144  // incremental append system)
145  ParseMP4File("bear-1280x720-av_frag.mp4", 512);
146}
147
148TEST_F(MP4StreamParserTest, BytewiseAppend) {
149  // Ensure no incremental errors occur when parsing
150  ParseMP4File("bear-1280x720-av_frag.mp4", 1);
151}
152
153TEST_F(MP4StreamParserTest, MultiFragmentAppend) {
154  // Large size ensures multiple fragments are appended in one call (size is
155  // larger than this particular test file)
156  ParseMP4File("bear-1280x720-av_frag.mp4", 768432);
157}
158
159TEST_F(MP4StreamParserTest, Flush) {
160  // Flush while reading sample data, then start a new stream.
161  InitializeParser();
162
163  scoped_refptr<DecoderBuffer> buffer =
164      ReadTestDataFile("bear-1280x720-av_frag.mp4");
165  EXPECT_TRUE(AppendDataInPieces(buffer->data(), 65536, 512));
166  parser_->Flush();
167  EXPECT_TRUE(AppendDataInPieces(buffer->data(),
168                                 buffer->data_size(),
169                                 512));
170}
171
172TEST_F(MP4StreamParserTest, Reinitialization) {
173  InitializeParser();
174
175  scoped_refptr<DecoderBuffer> buffer =
176      ReadTestDataFile("bear-1280x720-av_frag.mp4");
177  EXPECT_TRUE(AppendDataInPieces(buffer->data(),
178                                 buffer->data_size(),
179                                 512));
180  EXPECT_TRUE(AppendDataInPieces(buffer->data(),
181                                 buffer->data_size(),
182                                 512));
183}
184
185TEST_F(MP4StreamParserTest, MPEG2_AAC_LC) {
186  std::set<int> audio_object_types;
187  audio_object_types.insert(kISO_13818_7_AAC_LC);
188  parser_.reset(new MP4StreamParser(audio_object_types, false));
189  ParseMP4File("bear-mpeg2-aac-only_frag.mp4", 512);
190}
191
192// Test that a moov box is not always required after Flush() is called.
193TEST_F(MP4StreamParserTest, NoMoovAfterFlush) {
194  InitializeParser();
195
196  scoped_refptr<DecoderBuffer> buffer =
197      ReadTestDataFile("bear-1280x720-av_frag.mp4");
198  EXPECT_TRUE(AppendDataInPieces(buffer->data(),
199                                 buffer->data_size(),
200                                 512));
201  parser_->Flush();
202
203  const int kFirstMoofOffset = 1307;
204  EXPECT_TRUE(AppendDataInPieces(buffer->data() + kFirstMoofOffset,
205                                 buffer->data_size() - kFirstMoofOffset,
206                                 512));
207}
208
209// Test an invalid file where there are encrypted samples, but
210// SampleAuxiliaryInformation{Sizes|Offsets}Box (saiz|saio) are missing.
211// The parser should fail instead of crash. See http://crbug.com/361347
212TEST_F(MP4StreamParserTest, MissingSampleAuxInfo) {
213  ParseMP4File("bear-1280x720-a_frag-cenc_missing-saiz-saio.mp4", 512);
214}
215
216// TODO(strobe): Create and test media which uses CENC auxiliary info stored
217// inside a private box
218
219}  // namespace mp4
220}  // namespace media
221