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