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