7 #include <packager/media/formats/webm/segmenter_test_base.h>
9 #include <absl/log/check.h>
11 #include <packager/file/memory_file.h>
12 #include <packager/media/formats/webm/webm_constants.h>
13 #include <packager/version/version.h>
20 const uint8_t kTestMediaSampleData[] = {0xde, 0xad, 0xbe, 0xef, 0x00};
21 const uint8_t kTestMediaSampleSideData[] = {
23 0x12, 0x34, 0x56, 0x78, 0x9a, 0x00, 0x00, 0x00,
24 0x73, 0x69, 0x64, 0x65, 0x00};
26 const int kTrackId = 1;
27 const int64_t kDurationInSeconds = 8;
28 const Codec kCodec = kCodecVP8;
29 const std::string kCodecString =
"vp8";
30 const std::string kLanguage =
"en";
31 const uint16_t kWidth = 100;
32 const uint16_t kHeight = 100;
33 const uint16_t kPixelWidth = 100;
34 const uint16_t kPixelHeight = 100;
35 const uint8_t kColorPrimaries = 0;
36 const uint8_t kMatrixCoefficients = 0;
37 const uint8_t kTransferCharacteristics = 0;
38 const int16_t kTrickPlayFactor = 1;
39 const uint8_t kNaluLengthSize = 0;
43 SegmentTestBase::SegmentTestBase() {}
45 void SegmentTestBase::SetUp() {
46 SetPackagerVersionForTesting(
"test");
48 output_file_name_ = std::string(kMemoryFilePrefix) +
"output-file.webm";
52 void SegmentTestBase::TearDown() {
57 KeyFrameFlag key_frame_flag,
59 SideDataFlag side_data_flag) {
60 std::shared_ptr<MediaSample> sample;
61 const bool is_key_frame = key_frame_flag == kKeyFrame;
62 if (side_data_flag == kGenerateSideData) {
64 kTestMediaSampleData,
sizeof(kTestMediaSampleData),
65 kTestMediaSampleSideData,
sizeof(kTestMediaSampleSideData),
69 sizeof(kTestMediaSampleData), is_key_frame);
71 sample->set_dts(cur_timestamp_);
72 sample->set_pts(cur_timestamp_);
73 sample->set_duration(duration);
75 cur_timestamp_ += duration;
84 ret.
temp_dir = std::string(kMemoryFilePrefix) +
"temp/";
89 int32_t time_scale)
const {
91 kTrackId, time_scale, kDurationInSeconds * time_scale, kCodec,
92 H26xStreamFormat::kUnSpecified, kCodecString, NULL, 0, kWidth, kHeight,
93 kPixelWidth, kPixelHeight, kColorPrimaries, kMatrixCoefficients,
94 kTransferCharacteristics, kTrickPlayFactor, kNaluLengthSize, kLanguage,
99 return output_file_name_;
102 SegmentTestBase::ClusterParser::ClusterParser() {}
104 SegmentTestBase::ClusterParser::~ClusterParser() {}
106 void SegmentTestBase::ClusterParser::PopulateFromCluster(
107 const std::string& file_name) {
108 frame_timecodes_.clear();
109 std::string file_contents;
110 ASSERT_TRUE(File::ReadFileToString(file_name.c_str(), &file_contents));
112 const uint8_t* data =
reinterpret_cast<const uint8_t*
>(file_contents.c_str());
113 const size_t size = file_contents.size();
114 WebMListParser cluster_parser(kWebMIdCluster,
this);
116 while (position < size) {
117 int read = cluster_parser.Parse(data + position,
118 static_cast<int>(size - position));
121 cluster_parser.Reset();
126 void SegmentTestBase::ClusterParser::PopulateFromSegment(
127 const std::string& file_name) {
128 frame_timecodes_.clear();
129 std::string file_contents;
130 ASSERT_TRUE(File::ReadFileToString(file_name.c_str(), &file_contents));
132 const uint8_t* data =
reinterpret_cast<const uint8_t*
>(file_contents.c_str());
133 const size_t size = file_contents.size();
134 WebMListParser header_parser(kWebMIdEBMLHeader,
this);
135 int offset = header_parser.Parse(data,
static_cast<int>(size));
136 ASSERT_LT(0, offset);
138 WebMListParser segment_parser(kWebMIdSegment,
this);
140 0, segment_parser.Parse(data + offset,
static_cast<int>(size) - offset));
143 size_t SegmentTestBase::ClusterParser::GetFrameCountForCluster(
144 size_t cluster_index)
const {
145 DCHECK_LT(cluster_index, frame_timecodes_.size());
146 return frame_timecodes_[cluster_index].size();
149 int64_t SegmentTestBase::ClusterParser::GetFrameTimecode(
150 size_t cluster_index,
151 size_t frame_index)
const {
152 DCHECK_LT(cluster_index, frame_timecodes_.size());
153 DCHECK_LT(frame_index, frame_timecodes_[cluster_index].size());
154 return frame_timecodes_[cluster_index][frame_index];
157 size_t SegmentTestBase::ClusterParser::cluster_count()
const {
158 return frame_timecodes_.size();
161 WebMParserClient* SegmentTestBase::ClusterParser::OnListStart(
int id) {
162 if (
id == kWebMIdCluster) {
166 frame_timecodes_.emplace_back();
167 cluster_timecode_ = -1;
174 bool SegmentTestBase::ClusterParser::OnListEnd(
int id) {
175 if (
id == kWebMIdCluster) {
184 bool SegmentTestBase::ClusterParser::OnUInt(
int id, int64_t val) {
185 if (
id == kWebMIdTimecode)
186 cluster_timecode_ = val;
190 bool SegmentTestBase::ClusterParser::OnFloat(
int ,
double ) {
194 bool SegmentTestBase::ClusterParser::OnBinary(
int id,
197 if (in_cluster_ && (
id == kWebMIdSimpleBlock ||
id == kWebMIdBlock)) {
198 if (cluster_timecode_ == -1) {
199 LOG(WARNING) <<
"Cluster timecode not yet available";
202 int timecode = data[1] << 8 | data[2];
203 frame_timecodes_.back().push_back(cluster_timecode_ + timecode);
209 bool SegmentTestBase::ClusterParser::OnString(
int ,
210 const std::string& ) {
All the methods that are virtual are virtual for mocking.