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/base/muxer_util.h"
10 #include "packager/media/file/memory_file.h"
11 #include "packager/media/formats/webm/webm_constants.h"
12 #include "packager/version/version.h"
13 
14 namespace shaka {
15 namespace media {
16 namespace {
17 
18 // The contents of a frame does not mater.
19 const uint8_t kTestMediaSampleData[] = {0xde, 0xad, 0xbe, 0xef, 0x00};
20 const uint8_t kTestMediaSampleSideData[] = {
21  // First 8 bytes of side_data is the BlockAddID element in big endian.
22  0x12, 0x34, 0x56, 0x78, 0x9a, 0x00, 0x00, 0x00,
23  0x73, 0x69, 0x64, 0x65, 0x00};
24 
25 const int kTrackId = 1;
26 const uint32_t kTimeScale = 1000;
27 const uint64_t kDuration = 8000;
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 int16_t kTrickPlayRate = 1;
36 const uint8_t kNaluLengthSize = 0;
37 
38 } // namespace
39 
40 SegmentTestBase::SegmentTestBase() {}
41 
42 void SegmentTestBase::SetUp() {
43  SetPackagerVersionForTesting("test");
44 
45  output_file_name_ = std::string(kMemoryFilePrefix) + "output-file.webm";
46  segment_template_ =
47  std::string(kMemoryFilePrefix) + "output-template-$Number$.webm";
48  cur_time_timescale_ = 0;
49  single_segment_ = true;
50 }
51 
52 void SegmentTestBase::TearDown() {
54 }
55 
56 scoped_refptr<MediaSample> SegmentTestBase::CreateSample(
57  KeyFrameFlag key_frame_flag,
58  uint64_t duration,
59  SideDataFlag side_data_flag) {
60  scoped_refptr<MediaSample> sample;
61  const bool is_key_frame = key_frame_flag == kKeyFrame;
62  if (side_data_flag == kGenerateSideData) {
63  sample = MediaSample::CopyFrom(
64  kTestMediaSampleData, sizeof(kTestMediaSampleData),
65  kTestMediaSampleSideData, sizeof(kTestMediaSampleSideData),
66  is_key_frame);
67  } else {
68  sample = MediaSample::CopyFrom(kTestMediaSampleData,
69  sizeof(kTestMediaSampleData), is_key_frame);
70  }
71  sample->set_dts(cur_time_timescale_);
72  sample->set_pts(cur_time_timescale_);
73  sample->set_duration(duration);
74 
75  cur_time_timescale_ += duration;
76  return sample;
77 }
78 
80  MuxerOptions ret;
81  ret.single_segment = single_segment_;
82  ret.output_file_name = output_file_name_;
83  ret.segment_template = segment_template_;
84  ret.segment_duration = 30; // seconds
85  ret.fragment_duration = 30; // seconds
86  ret.segment_sap_aligned = false;
87  ret.fragment_sap_aligned = false;
88  // Use memory files for temp storage. Normally this would be a bad idea
89  // since it wouldn't support large files, but for tests the files are small.
90  ret.temp_dir = std::string(kMemoryFilePrefix) + "temp/";
91  return ret;
92 }
93 
95  return new VideoStreamInfo(kTrackId, kTimeScale, kDuration, kCodec,
96  kCodecString, NULL, 0, kWidth, kHeight,
97  kPixelWidth, kPixelHeight, kTrickPlayRate,
98  kNaluLengthSize, kLanguage, false);
99 }
100 
101 std::string SegmentTestBase::OutputFileName() const {
102  return output_file_name_;
103 }
104 
105 std::string SegmentTestBase::TemplateFileName(int number) const {
106  return GetSegmentName(segment_template_, 0, number, 0);
107 }
108 
109 SegmentTestBase::ClusterParser::ClusterParser() : in_cluster_(false) {}
110 
111 SegmentTestBase::ClusterParser::~ClusterParser() {}
112 
113 void SegmentTestBase::ClusterParser::PopulateFromCluster(
114  const std::string& file_name) {
115  cluster_sizes_.clear();
116  std::string file_contents;
117  ASSERT_TRUE(File::ReadFileToString(file_name.c_str(), &file_contents));
118 
119  const uint8_t* data = reinterpret_cast<const uint8_t*>(file_contents.c_str());
120  const size_t size = file_contents.size();
121  WebMListParser cluster_parser(kWebMIdCluster, this);
122  size_t position = 0;
123  while (position < size) {
124  int read = cluster_parser.Parse(data + position,
125  static_cast<int>(size - position));
126  ASSERT_LT(0, read);
127 
128  cluster_parser.Reset();
129  position += read;
130  }
131 }
132 
133 void SegmentTestBase::ClusterParser::PopulateFromSegment(
134  const std::string& file_name) {
135  cluster_sizes_.clear();
136  std::string file_contents;
137  ASSERT_TRUE(File::ReadFileToString(file_name.c_str(), &file_contents));
138 
139  const uint8_t* data = reinterpret_cast<const uint8_t*>(file_contents.c_str());
140  const size_t size = file_contents.size();
141  WebMListParser header_parser(kWebMIdEBMLHeader, this);
142  int offset = header_parser.Parse(data, static_cast<int>(size));
143  ASSERT_LT(0, offset);
144 
145  WebMListParser segment_parser(kWebMIdSegment, this);
146  ASSERT_LT(
147  0, segment_parser.Parse(data + offset, static_cast<int>(size) - offset));
148 }
149 
150 int SegmentTestBase::ClusterParser::GetFrameCountForCluster(size_t i) const {
151  DCHECK(i < cluster_sizes_.size());
152  return cluster_sizes_[i];
153 }
154 
155 size_t SegmentTestBase::ClusterParser::cluster_count() const {
156  return cluster_sizes_.size();
157 }
158 
159 WebMParserClient* SegmentTestBase::ClusterParser::OnListStart(int id) {
160  if (id == kWebMIdCluster) {
161  if (in_cluster_)
162  return NULL;
163 
164  cluster_sizes_.push_back(0);
165  in_cluster_ = true;
166  }
167 
168  return this;
169 }
170 
171 bool SegmentTestBase::ClusterParser::OnListEnd(int id) {
172  if (id == kWebMIdCluster) {
173  if (!in_cluster_)
174  return false;
175  in_cluster_ = false;
176  }
177 
178  return true;
179 }
180 
181 bool SegmentTestBase::ClusterParser::OnUInt(int id, int64_t val) {
182  return true;
183 }
184 
185 bool SegmentTestBase::ClusterParser::OnFloat(int id, double val) {
186  return true;
187 }
188 
189 bool SegmentTestBase::ClusterParser::OnBinary(int id,
190  const uint8_t* data,
191  int size) {
192  if (in_cluster_ && (id == kWebMIdSimpleBlock || id == kWebMIdBlock)) {
193  cluster_sizes_.back()++;
194  }
195 
196  return true;
197 }
198 
199 bool SegmentTestBase::ClusterParser::OnString(int id, const std::string& str) {
200  return true;
201 }
202 
203 } // namespace media
204 } // 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.
scoped_refptr< 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:71
MuxerOptions CreateMuxerOptions() const
Creates a Muxer options object for testing.
static scoped_refptr< MediaSample > CopyFrom(const uint8_t *data, size_t size, bool is_key_frame)
Definition: media_sample.cc:45
Holds video stream information.
std::string TemplateFileName(int number) const
Gets the file name of the given template file.