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 
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 VideoCodec kVideoCodec = 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  output_file_name_ = std::string(kMemoryFilePrefix) + "output-file.webm";
43  segment_template_ =
44  std::string(kMemoryFilePrefix) + "output-template-$Number$.webm";
45  cur_time_timescale_ = 0;
46  single_segment_ = true;
47 }
48 
49 void SegmentTestBase::TearDown() {
51 }
52 
53 scoped_refptr<MediaSample> SegmentTestBase::CreateSample(
54  KeyFrameFlag key_frame_flag,
55  uint64_t duration,
56  SideDataFlag side_data_flag) {
57  scoped_refptr<MediaSample> sample;
58  const bool is_key_frame = key_frame_flag == kKeyFrame;
59  if (side_data_flag == kGenerateSideData) {
60  sample = MediaSample::CopyFrom(
61  kTestMediaSampleData, sizeof(kTestMediaSampleData),
62  kTestMediaSampleSideData, sizeof(kTestMediaSampleSideData),
63  is_key_frame);
64  } else {
65  sample = MediaSample::CopyFrom(kTestMediaSampleData,
66  sizeof(kTestMediaSampleData), is_key_frame);
67  }
68  sample->set_dts(cur_time_timescale_);
69  sample->set_pts(cur_time_timescale_);
70  sample->set_duration(duration);
71 
72  cur_time_timescale_ += duration;
73  return sample;
74 }
75 
77  MuxerOptions ret;
78  ret.single_segment = single_segment_;
79  ret.output_file_name = output_file_name_;
80  ret.segment_template = segment_template_;
81  ret.packager_version_string = "test";
82  ret.segment_duration = 30; // seconds
83  ret.fragment_duration = 30; // seconds
84  ret.segment_sap_aligned = false;
85  ret.fragment_sap_aligned = false;
86  // Use memory files for temp storage. Normally this would be a bad idea
87  // since it wouldn't support large files, but for tests the files are small.
88  ret.temp_dir = std::string(kMemoryFilePrefix) + "temp/";
89  return ret;
90 }
91 
93  return new VideoStreamInfo(kTrackId, kTimeScale, kDuration, kVideoCodec,
94  kCodecString, kLanguage, kWidth, kHeight,
95  kPixelWidth, kPixelHeight, kTrickPlayRate,
96  kNaluLengthSize, NULL, 0, false);
97 }
98 
99 std::string SegmentTestBase::OutputFileName() const {
100  return output_file_name_;
101 }
102 
103 std::string SegmentTestBase::TemplateFileName(int number) const {
104  return GetSegmentName(segment_template_, 0, number, 0);
105 }
106 
107 SegmentTestBase::ClusterParser::ClusterParser() : in_cluster_(false) {}
108 
109 SegmentTestBase::ClusterParser::~ClusterParser() {}
110 
111 void SegmentTestBase::ClusterParser::PopulateFromCluster(
112  const std::string& file_name) {
113  cluster_sizes_.clear();
114  std::string file_contents;
115  ASSERT_TRUE(File::ReadFileToString(file_name.c_str(), &file_contents));
116 
117  const uint8_t* data = reinterpret_cast<const uint8_t*>(file_contents.c_str());
118  const size_t size = file_contents.size();
119  WebMListParser cluster_parser(kWebMIdCluster, this);
120  size_t position = 0;
121  while (position < size) {
122  int read = cluster_parser.Parse(data + position, size - position);
123  ASSERT_LT(0, read);
124 
125  cluster_parser.Reset();
126  position += read;
127  }
128 }
129 
130 void SegmentTestBase::ClusterParser::PopulateFromSegment(
131  const std::string& file_name) {
132  cluster_sizes_.clear();
133  std::string file_contents;
134  ASSERT_TRUE(File::ReadFileToString(file_name.c_str(), &file_contents));
135 
136  const uint8_t* data = reinterpret_cast<const uint8_t*>(file_contents.c_str());
137  const size_t size = file_contents.size();
138  WebMListParser header_parser(kWebMIdEBMLHeader, this);
139  int offset = header_parser.Parse(data, size);
140  ASSERT_LT(0, offset);
141 
142  WebMListParser segment_parser(kWebMIdSegment, this);
143  ASSERT_LT(0, segment_parser.Parse(data + offset, size - offset));
144 }
145 
146 int SegmentTestBase::ClusterParser::GetFrameCountForCluster(size_t i) const {
147  DCHECK(i < cluster_sizes_.size());
148  return cluster_sizes_[i];
149 }
150 
151 int SegmentTestBase::ClusterParser::cluster_count() const {
152  return cluster_sizes_.size();
153 }
154 
155 WebMParserClient* SegmentTestBase::ClusterParser::OnListStart(int id) {
156  if (id == kWebMIdCluster) {
157  if (in_cluster_)
158  return NULL;
159 
160  cluster_sizes_.push_back(0);
161  in_cluster_ = true;
162  }
163 
164  return this;
165 }
166 
167 bool SegmentTestBase::ClusterParser::OnListEnd(int id) {
168  if (id == kWebMIdCluster) {
169  if (!in_cluster_)
170  return false;
171  in_cluster_ = false;
172  }
173 
174  return true;
175 }
176 
177 bool SegmentTestBase::ClusterParser::OnUInt(int id, int64_t val) {
178  return true;
179 }
180 
181 bool SegmentTestBase::ClusterParser::OnFloat(int id, double val) {
182  return true;
183 }
184 
185 bool SegmentTestBase::ClusterParser::OnBinary(int id,
186  const uint8_t* data,
187  int size) {
188  if (in_cluster_ && (id == kWebMIdSimpleBlock || id == kWebMIdBlock)) {
189  cluster_sizes_.back()++;
190  }
191 
192  return true;
193 }
194 
195 bool SegmentTestBase::ClusterParser::OnString(int id, const std::string& str) {
196  return true;
197 }
198 
199 } // namespace media
200 } // namespace shaka
201 
std::string packager_version_string
Specify the version string to be embedded in the output files.
Definition: muxer_options.h:71
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:184
std::string temp_dir
Specify temporary directory for intermediate files.
Definition: muxer_options.h:64
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.