7 #include "packager/media/formats/webm/segmenter_test_base.h"
9 #include "packager/file/memory_file.h"
10 #include "packager/media/formats/webm/webm_constants.h"
11 #include "packager/version/version.h"
18 const uint8_t kTestMediaSampleData[] = {0xde, 0xad, 0xbe, 0xef, 0x00};
19 const uint8_t kTestMediaSampleSideData[] = {
21 0x12, 0x34, 0x56, 0x78, 0x9a, 0x00, 0x00, 0x00,
22 0x73, 0x69, 0x64, 0x65, 0x00};
24 const int kTrackId = 1;
25 const uint64_t kDurationInSeconds = 8;
26 const Codec kCodec = kCodecVP8;
27 const std::string kCodecString =
"vp8";
28 const std::string kLanguage =
"en";
29 const uint16_t kWidth = 100;
30 const uint16_t kHeight = 100;
31 const uint16_t kPixelWidth = 100;
32 const uint16_t kPixelHeight = 100;
33 const uint8_t kTransferCharacteristics = 0;
34 const int16_t kTrickPlayFactor = 1;
35 const uint8_t kNaluLengthSize = 0;
39 SegmentTestBase::SegmentTestBase() {}
41 void SegmentTestBase::SetUp() {
42 SetPackagerVersionForTesting(
"test");
44 output_file_name_ = std::string(kMemoryFilePrefix) +
"output-file.webm";
48 void SegmentTestBase::TearDown() {
53 KeyFrameFlag key_frame_flag,
55 SideDataFlag side_data_flag) {
56 std::shared_ptr<MediaSample> sample;
57 const bool is_key_frame = key_frame_flag == kKeyFrame;
58 if (side_data_flag == kGenerateSideData) {
60 kTestMediaSampleData,
sizeof(kTestMediaSampleData),
61 kTestMediaSampleSideData,
sizeof(kTestMediaSampleSideData),
65 sizeof(kTestMediaSampleData), is_key_frame);
67 sample->set_dts(cur_timestamp_);
68 sample->set_pts(cur_timestamp_);
69 sample->set_duration(duration);
71 cur_timestamp_ += duration;
80 ret.
temp_dir = std::string(kMemoryFilePrefix) +
"temp/";
85 uint32_t time_scale)
const {
87 kTrackId, time_scale, kDurationInSeconds * time_scale, kCodec,
88 H26xStreamFormat::kUnSpecified, kCodecString, NULL, 0, kWidth, kHeight,
89 kPixelWidth, kPixelHeight, kTransferCharacteristics, kTrickPlayFactor,
90 kNaluLengthSize, kLanguage,
false);
94 return output_file_name_;
97 SegmentTestBase::ClusterParser::ClusterParser() {}
99 SegmentTestBase::ClusterParser::~ClusterParser() {}
101 void SegmentTestBase::ClusterParser::PopulateFromCluster(
102 const std::string& file_name) {
103 frame_timecodes_.clear();
104 std::string file_contents;
107 const uint8_t* data =
reinterpret_cast<const uint8_t*
>(file_contents.c_str());
108 const size_t size = file_contents.size();
109 WebMListParser cluster_parser(kWebMIdCluster,
this);
111 while (position < size) {
112 int read = cluster_parser.Parse(data + position,
113 static_cast<int>(size - position));
116 cluster_parser.Reset();
121 void SegmentTestBase::ClusterParser::PopulateFromSegment(
122 const std::string& file_name) {
123 frame_timecodes_.clear();
124 std::string file_contents;
127 const uint8_t* data =
reinterpret_cast<const uint8_t*
>(file_contents.c_str());
128 const size_t size = file_contents.size();
129 WebMListParser header_parser(kWebMIdEBMLHeader,
this);
130 int offset = header_parser.Parse(data,
static_cast<int>(size));
131 ASSERT_LT(0, offset);
133 WebMListParser segment_parser(kWebMIdSegment,
this);
135 0, segment_parser.Parse(data + offset,
static_cast<int>(size) - offset));
138 size_t SegmentTestBase::ClusterParser::GetFrameCountForCluster(
139 size_t cluster_index)
const {
140 DCHECK_LT(cluster_index, frame_timecodes_.size());
141 return frame_timecodes_[cluster_index].size();
144 int64_t SegmentTestBase::ClusterParser::GetFrameTimecode(
145 size_t cluster_index,
146 size_t frame_index)
const {
147 DCHECK_LT(cluster_index, frame_timecodes_.size());
148 DCHECK_LT(frame_index, frame_timecodes_[cluster_index].size());
149 return frame_timecodes_[cluster_index][frame_index];
152 size_t SegmentTestBase::ClusterParser::cluster_count()
const {
153 return frame_timecodes_.size();
156 WebMParserClient* SegmentTestBase::ClusterParser::OnListStart(
int id) {
157 if (
id == kWebMIdCluster) {
161 frame_timecodes_.emplace_back();
162 cluster_timecode_ = -1;
169 bool SegmentTestBase::ClusterParser::OnListEnd(
int id) {
170 if (
id == kWebMIdCluster) {
179 bool SegmentTestBase::ClusterParser::OnUInt(
int id, int64_t val) {
180 if (
id == kWebMIdTimecode)
181 cluster_timecode_ = val;
185 bool SegmentTestBase::ClusterParser::OnFloat(
int id,
double val) {
189 bool SegmentTestBase::ClusterParser::OnBinary(
int id,
192 if (in_cluster_ && (
id == kWebMIdSimpleBlock ||
id == kWebMIdBlock)) {
193 if (cluster_timecode_ == -1) {
194 LOG(WARNING) <<
"Cluster timecode not yet available";
197 int timecode = data[1] << 8 | data[2];
198 frame_timecodes_.back().push_back(cluster_timecode_ + timecode);
204 bool SegmentTestBase::ClusterParser::OnString(
int id,
const std::string& str) {