DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
segmenter_test_base.cc
1 // Copyright 2015 Google Inc. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #include "packager/media/formats/webm/segmenter_test_base.h"
8 
9 #include "packager/media/file/memory_file.h"
10 #include "packager/media/formats/webm/webm_constants.h"
11 #include "packager/version/version.h"
12 
13 namespace shaka {
14 namespace media {
15 namespace {
16 
17 // The contents of a frame does not mater.
18 const uint8_t kTestMediaSampleData[] = {0xde, 0xad, 0xbe, 0xef, 0x00};
19 const uint8_t kTestMediaSampleSideData[] = {
20  // First 8 bytes of side_data is the BlockAddID element in big endian.
21  0x12, 0x34, 0x56, 0x78, 0x9a, 0x00, 0x00, 0x00,
22  0x73, 0x69, 0x64, 0x65, 0x00};
23 
24 const int kTrackId = 1;
25 const uint32_t kTimeScale = 1000;
26 const uint64_t kDuration = 8000;
27 const Codec kCodec = kCodecVP8;
28 const std::string kCodecString = "vp8";
29 const std::string kLanguage = "en";
30 const uint16_t kWidth = 100;
31 const uint16_t kHeight = 100;
32 const uint16_t kPixelWidth = 100;
33 const uint16_t kPixelHeight = 100;
34 const int16_t kTrickPlayRate = 1;
35 const uint8_t kNaluLengthSize = 0;
36 
37 } // namespace
38 
39 SegmentTestBase::SegmentTestBase() {}
40 
41 void SegmentTestBase::SetUp() {
42  SetPackagerVersionForTesting("test");
43 
44  output_file_name_ = std::string(kMemoryFilePrefix) + "output-file.webm";
45  cur_time_timescale_ = 0;
46 }
47 
48 void SegmentTestBase::TearDown() {
50 }
51 
52 std::shared_ptr<MediaSample> SegmentTestBase::CreateSample(
53  KeyFrameFlag key_frame_flag,
54  uint64_t duration,
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) {
59  sample = MediaSample::CopyFrom(
60  kTestMediaSampleData, sizeof(kTestMediaSampleData),
61  kTestMediaSampleSideData, sizeof(kTestMediaSampleSideData),
62  is_key_frame);
63  } else {
64  sample = MediaSample::CopyFrom(kTestMediaSampleData,
65  sizeof(kTestMediaSampleData), is_key_frame);
66  }
67  sample->set_dts(cur_time_timescale_);
68  sample->set_pts(cur_time_timescale_);
69  sample->set_duration(duration);
70 
71  cur_time_timescale_ += duration;
72  return sample;
73 }
74 
76  MuxerOptions ret;
77  ret.output_file_name = output_file_name_;
78  // Use memory files for temp storage. Normally this would be a bad idea
79  // since it wouldn't support large files, but for tests the files are small.
80  ret.temp_dir = std::string(kMemoryFilePrefix) + "temp/";
81  return ret;
82 }
83 
85  return new VideoStreamInfo(kTrackId, kTimeScale, kDuration, kCodec,
86  H26xStreamFormat::kUnSpecified, kCodecString, NULL,
87  0, kWidth, kHeight, kPixelWidth, kPixelHeight,
88  kTrickPlayRate, kNaluLengthSize, kLanguage, false);
89 }
90 
91 std::string SegmentTestBase::OutputFileName() const {
92  return output_file_name_;
93 }
94 
95 SegmentTestBase::ClusterParser::ClusterParser() : in_cluster_(false) {}
96 
97 SegmentTestBase::ClusterParser::~ClusterParser() {}
98 
99 void SegmentTestBase::ClusterParser::PopulateFromCluster(
100  const std::string& file_name) {
101  cluster_sizes_.clear();
102  std::string file_contents;
103  ASSERT_TRUE(File::ReadFileToString(file_name.c_str(), &file_contents));
104 
105  const uint8_t* data = reinterpret_cast<const uint8_t*>(file_contents.c_str());
106  const size_t size = file_contents.size();
107  WebMListParser cluster_parser(kWebMIdCluster, this);
108  size_t position = 0;
109  while (position < size) {
110  int read = cluster_parser.Parse(data + position,
111  static_cast<int>(size - position));
112  ASSERT_LT(0, read);
113 
114  cluster_parser.Reset();
115  position += read;
116  }
117 }
118 
119 void SegmentTestBase::ClusterParser::PopulateFromSegment(
120  const std::string& file_name) {
121  cluster_sizes_.clear();
122  std::string file_contents;
123  ASSERT_TRUE(File::ReadFileToString(file_name.c_str(), &file_contents));
124 
125  const uint8_t* data = reinterpret_cast<const uint8_t*>(file_contents.c_str());
126  const size_t size = file_contents.size();
127  WebMListParser header_parser(kWebMIdEBMLHeader, this);
128  int offset = header_parser.Parse(data, static_cast<int>(size));
129  ASSERT_LT(0, offset);
130 
131  WebMListParser segment_parser(kWebMIdSegment, this);
132  ASSERT_LT(
133  0, segment_parser.Parse(data + offset, static_cast<int>(size) - offset));
134 }
135 
136 int SegmentTestBase::ClusterParser::GetFrameCountForCluster(size_t i) const {
137  DCHECK(i < cluster_sizes_.size());
138  return cluster_sizes_[i];
139 }
140 
141 size_t SegmentTestBase::ClusterParser::cluster_count() const {
142  return cluster_sizes_.size();
143 }
144 
145 WebMParserClient* SegmentTestBase::ClusterParser::OnListStart(int id) {
146  if (id == kWebMIdCluster) {
147  if (in_cluster_)
148  return NULL;
149 
150  cluster_sizes_.push_back(0);
151  in_cluster_ = true;
152  }
153 
154  return this;
155 }
156 
157 bool SegmentTestBase::ClusterParser::OnListEnd(int id) {
158  if (id == kWebMIdCluster) {
159  if (!in_cluster_)
160  return false;
161  in_cluster_ = false;
162  }
163 
164  return true;
165 }
166 
167 bool SegmentTestBase::ClusterParser::OnUInt(int id, int64_t val) {
168  return true;
169 }
170 
171 bool SegmentTestBase::ClusterParser::OnFloat(int id, double val) {
172  return true;
173 }
174 
175 bool SegmentTestBase::ClusterParser::OnBinary(int id,
176  const uint8_t* data,
177  int size) {
178  if (in_cluster_ && (id == kWebMIdSimpleBlock || id == kWebMIdBlock)) {
179  cluster_sizes_.back()++;
180  }
181 
182  return true;
183 }
184 
185 bool SegmentTestBase::ClusterParser::OnString(int id, const std::string& str) {
186  return true;
187 }
188 
189 } // namespace media
190 } // namespace shaka
VideoStreamInfo * CreateVideoStreamInfo() const
Creates a video stream info object for testing.
This structure contains the list of configuration options for Muxer.
Definition: muxer_options.h:18
std::string OutputFileName() const
Gets the file name of the current output file.
std::shared_ptr< MediaSample > CreateSample(KeyFrameFlag key_frame_flag, uint64_t duration, SideDataFlag side_data_flag)
Creates a new media sample.
static bool ReadFileToString(const char *file_name, std::string *contents)
Definition: file.cc:185
std::string temp_dir
Specify temporary directory for intermediate files.
Definition: muxer_options.h:48
MuxerOptions CreateMuxerOptions() const
Creates a Muxer options object for testing.
static std::shared_ptr< MediaSample > CopyFrom(const uint8_t *data, size_t size, bool is_key_frame)
Definition: media_sample.cc:45
Holds video stream information.