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