1 // Copyright 2013 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/mp2t/mp2t_stream_parser.h" 20 #include "testing/gtest/include/gtest/gtest.h" 21 22 namespace media { 23 namespace mp2t { 24 25 class Mp2tStreamParserTest : public testing::Test { 26 public: 27 Mp2tStreamParserTest() 28 : audio_frame_count_(0), 29 video_frame_count_(0), 30 video_min_dts_(kNoTimestamp()), 31 video_max_dts_(kNoTimestamp()) { 32 bool has_sbr = false; 33 parser_.reset(new Mp2tStreamParser(has_sbr)); 34 } 35 36 protected: 37 scoped_ptr<Mp2tStreamParser> parser_; 38 int audio_frame_count_; 39 int video_frame_count_; 40 base::TimeDelta video_min_dts_; 41 base::TimeDelta video_max_dts_; 42 43 bool AppendData(const uint8* data, size_t length) { 44 return parser_->Parse(data, length); 45 } 46 47 bool AppendDataInPieces(const uint8* data, size_t length, size_t piece_size) { 48 const uint8* start = data; 49 const uint8* end = data + length; 50 while (start < end) { 51 size_t append_size = std::min(piece_size, 52 static_cast<size_t>(end - start)); 53 if (!AppendData(start, append_size)) 54 return false; 55 start += append_size; 56 } 57 return true; 58 } 59 60 void OnInit(bool init_ok, base::TimeDelta duration) { 61 DVLOG(1) << "OnInit: ok=" << init_ok 62 << ", dur=" << duration.InMilliseconds(); 63 } 64 65 bool OnNewConfig(const AudioDecoderConfig& ac, 66 const VideoDecoderConfig& vc, 67 const StreamParser::TextTrackConfigMap& tc) { 68 DVLOG(1) << "OnNewConfig: audio=" << ac.IsValidConfig() 69 << ", video=" << vc.IsValidConfig(); 70 return true; 71 } 72 73 74 void DumpBuffers(const std::string& label, 75 const StreamParser::BufferQueue& buffers) { 76 DVLOG(2) << "DumpBuffers: " << label << " size " << buffers.size(); 77 for (StreamParser::BufferQueue::const_iterator buf = buffers.begin(); 78 buf != buffers.end(); buf++) { 79 DVLOG(3) << " n=" << buf - buffers.begin() 80 << ", size=" << (*buf)->data_size() 81 << ", dur=" << (*buf)->duration().InMilliseconds(); 82 } 83 } 84 85 bool OnNewBuffers(const StreamParser::BufferQueue& audio_buffers, 86 const StreamParser::BufferQueue& video_buffers) { 87 DumpBuffers("audio_buffers", audio_buffers); 88 DumpBuffers("video_buffers", video_buffers); 89 audio_frame_count_ += audio_buffers.size(); 90 video_frame_count_ += video_buffers.size(); 91 92 if (video_min_dts_ == kNoTimestamp() && !video_buffers.empty()) 93 video_min_dts_ = video_buffers.front()->GetDecodeTimestamp(); 94 if (!video_buffers.empty()) { 95 video_max_dts_ = video_buffers.back()->GetDecodeTimestamp(); 96 // Verify monotonicity. 97 StreamParser::BufferQueue::const_iterator it1 = video_buffers.begin(); 98 StreamParser::BufferQueue::const_iterator it2 = ++it1; 99 for ( ; it2 != video_buffers.end(); ++it1, ++it2) { 100 if ((*it2)->GetDecodeTimestamp() < (*it1)->GetDecodeTimestamp()) 101 return false; 102 } 103 } 104 105 return true; 106 } 107 108 void OnKeyNeeded(const std::string& type, 109 const std::vector<uint8>& init_data) { 110 DVLOG(1) << "OnKeyNeeded: " << init_data.size(); 111 } 112 113 void OnNewSegment() { 114 DVLOG(1) << "OnNewSegment"; 115 } 116 117 void OnEndOfSegment() { 118 DVLOG(1) << "OnEndOfSegment()"; 119 } 120 121 void InitializeParser() { 122 parser_->Init( 123 base::Bind(&Mp2tStreamParserTest::OnInit, 124 base::Unretained(this)), 125 base::Bind(&Mp2tStreamParserTest::OnNewConfig, 126 base::Unretained(this)), 127 base::Bind(&Mp2tStreamParserTest::OnNewBuffers, 128 base::Unretained(this)), 129 StreamParser::NewTextBuffersCB(), 130 base::Bind(&Mp2tStreamParserTest::OnKeyNeeded, 131 base::Unretained(this)), 132 base::Bind(&Mp2tStreamParserTest::OnNewSegment, 133 base::Unretained(this)), 134 base::Bind(&Mp2tStreamParserTest::OnEndOfSegment, 135 base::Unretained(this)), 136 LogCB()); 137 } 138 139 bool ParseMpeg2TsFile(const std::string& filename, int append_bytes) { 140 InitializeParser(); 141 142 scoped_refptr<DecoderBuffer> buffer = ReadTestDataFile(filename); 143 EXPECT_TRUE(AppendDataInPieces(buffer->data(), 144 buffer->data_size(), 145 append_bytes)); 146 return true; 147 } 148 }; 149 150 TEST_F(Mp2tStreamParserTest, UnalignedAppend17) { 151 // Test small, non-segment-aligned appends. 152 ParseMpeg2TsFile("bear-1280x720.ts", 17); 153 EXPECT_EQ(video_frame_count_, 81); 154 parser_->Flush(); 155 EXPECT_EQ(video_frame_count_, 82); 156 } 157 158 TEST_F(Mp2tStreamParserTest, UnalignedAppend512) { 159 // Test small, non-segment-aligned appends. 160 ParseMpeg2TsFile("bear-1280x720.ts", 512); 161 EXPECT_EQ(video_frame_count_, 81); 162 parser_->Flush(); 163 EXPECT_EQ(video_frame_count_, 82); 164 } 165 166 TEST_F(Mp2tStreamParserTest, TimestampWrapAround) { 167 // "bear-1280x720_ptswraparound.ts" has been transcoded 168 // from bear-1280x720.mp4 by applying a time offset of 95442s 169 // (close to 2^33 / 90000) which results in timestamps wrap around 170 // in the Mpeg2 TS stream. 171 ParseMpeg2TsFile("bear-1280x720_ptswraparound.ts", 512); 172 EXPECT_EQ(video_frame_count_, 81); 173 EXPECT_GE(video_min_dts_, base::TimeDelta::FromSeconds(95443 - 10)); 174 EXPECT_LE(video_max_dts_, base::TimeDelta::FromSeconds(95443 + 10)); 175 } 176 177 } // namespace mp2t 178 } // namespace media 179