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 "media/formats/common/stream_parser_test_base.h" 6 7 #include "base/bind.h" 8 #include "media/base/test_data_util.h" 9 #include "testing/gtest/include/gtest/gtest.h" 10 11 namespace media { 12 13 static std::string BufferQueueToString( 14 const StreamParser::BufferQueue& buffers) { 15 std::stringstream ss; 16 17 ss << "{"; 18 for (StreamParser::BufferQueue::const_iterator itr = buffers.begin(); 19 itr != buffers.end(); 20 ++itr) { 21 ss << " " << (*itr)->timestamp().InMilliseconds(); 22 if ((*itr)->IsKeyframe()) 23 ss << "K"; 24 } 25 ss << " }"; 26 27 return ss.str(); 28 } 29 30 StreamParserTestBase::StreamParserTestBase( 31 scoped_ptr<StreamParser> stream_parser) 32 : parser_(stream_parser.Pass()) { 33 parser_->Init( 34 base::Bind(&StreamParserTestBase::OnInitDone, base::Unretained(this)), 35 base::Bind(&StreamParserTestBase::OnNewConfig, base::Unretained(this)), 36 base::Bind(&StreamParserTestBase::OnNewBuffers, base::Unretained(this)), 37 true, 38 base::Bind(&StreamParserTestBase::OnKeyNeeded, base::Unretained(this)), 39 base::Bind(&StreamParserTestBase::OnNewSegment, base::Unretained(this)), 40 base::Bind(&StreamParserTestBase::OnEndOfSegment, base::Unretained(this)), 41 LogCB()); 42 } 43 44 StreamParserTestBase::~StreamParserTestBase() {} 45 46 std::string StreamParserTestBase::ParseFile(const std::string& filename, 47 int append_bytes) { 48 results_stream_.clear(); 49 scoped_refptr<DecoderBuffer> buffer = ReadTestDataFile(filename); 50 EXPECT_TRUE( 51 AppendDataInPieces(buffer->data(), buffer->data_size(), append_bytes)); 52 return results_stream_.str(); 53 } 54 55 std::string StreamParserTestBase::ParseData(const uint8* data, size_t length) { 56 results_stream_.clear(); 57 EXPECT_TRUE(AppendDataInPieces(data, length, length)); 58 return results_stream_.str(); 59 } 60 61 bool StreamParserTestBase::AppendDataInPieces(const uint8* data, 62 size_t length, 63 size_t piece_size) { 64 const uint8* start = data; 65 const uint8* end = data + length; 66 while (start < end) { 67 size_t append_size = std::min(piece_size, static_cast<size_t>(end - start)); 68 if (!parser_->Parse(start, append_size)) 69 return false; 70 start += append_size; 71 } 72 return true; 73 } 74 75 void StreamParserTestBase::OnInitDone( 76 bool success, 77 const StreamParser::InitParameters& params) { 78 EXPECT_TRUE(params.auto_update_timestamp_offset); 79 DVLOG(1) << __FUNCTION__ << "(" << success << ", " 80 << params.duration.InMilliseconds() << ", " 81 << params.auto_update_timestamp_offset << ")"; 82 } 83 84 bool StreamParserTestBase::OnNewConfig( 85 const AudioDecoderConfig& audio_config, 86 const VideoDecoderConfig& video_config, 87 const StreamParser::TextTrackConfigMap& text_config) { 88 DVLOG(1) << __FUNCTION__ << "(" << audio_config.IsValidConfig() << ", " 89 << video_config.IsValidConfig() << ")"; 90 EXPECT_TRUE(audio_config.IsValidConfig()); 91 EXPECT_FALSE(video_config.IsValidConfig()); 92 last_audio_config_ = audio_config; 93 return true; 94 } 95 96 bool StreamParserTestBase::OnNewBuffers( 97 const StreamParser::BufferQueue& audio_buffers, 98 const StreamParser::BufferQueue& video_buffers, 99 const StreamParser::TextBufferQueueMap& text_map) { 100 EXPECT_FALSE(audio_buffers.empty()); 101 EXPECT_TRUE(video_buffers.empty()); 102 103 // TODO(wolenetz/acolwell): Add text track support to more MSE parsers. See 104 // http://crbug.com/336926. 105 EXPECT_TRUE(text_map.empty()); 106 107 const std::string buffers_str = BufferQueueToString(audio_buffers); 108 DVLOG(1) << __FUNCTION__ << " : " << buffers_str; 109 results_stream_ << buffers_str; 110 return true; 111 } 112 113 void StreamParserTestBase::OnKeyNeeded(const std::string& type, 114 const std::vector<uint8>& init_data) { 115 DVLOG(1) << __FUNCTION__ << "(" << type << ", " << init_data.size() << ")"; 116 } 117 118 void StreamParserTestBase::OnNewSegment() { 119 DVLOG(1) << __FUNCTION__; 120 results_stream_ << "NewSegment"; 121 } 122 123 void StreamParserTestBase::OnEndOfSegment() { 124 DVLOG(1) << __FUNCTION__; 125 results_stream_ << "EndOfSegment"; 126 } 127 128 } // namespace media 129