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 size_t kTestMediaSampleDataSize = sizeof(kTestMediaSampleData);
20 
21 const int kTrackId = 1;
22 const uint32_t kTimeScale = 1000;
23 const uint64_t kDuration = 8000;
24 const VideoCodec kVideoCodec = kCodecVP8;
25 const std::string kCodecString = "vp8";
26 const std::string kLanguage = "en";
27 const uint16_t kWidth = 100;
28 const uint16_t kHeight = 100;
29 const uint16_t kPixelWidth = 100;
30 const uint16_t kPixelHeight = 100;
31 const int16_t kTrickPlayRate = 1;
32 const uint8_t kNaluLengthSize = 0;
33 
34 } // namespace
35 
36 SegmentTestBase::SegmentTestBase() {}
37 
38 void SegmentTestBase::SetUp() {
39  output_file_name_ = "memory://output-file.webm";
40  segment_template_ = "memory://output-template-$Number$.webm";
41  cur_time_timescale_ = 0;
42  single_segment_ = true;
43 }
44 
45 void SegmentTestBase::TearDown() {
47 }
48 
49 scoped_refptr<MediaSample> SegmentTestBase::CreateSample(bool is_key_frame,
50  uint64_t duration) {
51  scoped_refptr<MediaSample> sample = MediaSample::CopyFrom(
52  kTestMediaSampleData, kTestMediaSampleDataSize, is_key_frame);
53  sample->set_dts(cur_time_timescale_);
54  sample->set_pts(cur_time_timescale_);
55  sample->set_duration(duration);
56 
57  cur_time_timescale_ += duration;
58  return sample;
59 }
60 
62  MuxerOptions ret;
63  ret.single_segment = single_segment_;
64  ret.output_file_name = output_file_name_;
65  ret.segment_template = segment_template_;
66  ret.packager_version_string = "test";
67  ret.segment_duration = 30; // seconds
68  ret.fragment_duration = 30; // seconds
69  ret.segment_sap_aligned = false;
70  ret.fragment_sap_aligned = false;
71  // Use memory files for temp storage. Normally this would be a bad idea
72  // since it wouldn't support large files, but for tests the files are small.
73  ret.temp_dir = "memory://temp/";
74  return ret;
75 }
76 
78  return new VideoStreamInfo(kTrackId, kTimeScale, kDuration, kVideoCodec,
79  kCodecString, kLanguage, kWidth, kHeight,
80  kPixelWidth, kPixelHeight, kTrickPlayRate,
81  kNaluLengthSize, NULL, 0, false);
82 }
83 
84 std::string SegmentTestBase::OutputFileName() const {
85  return output_file_name_;
86 }
87 
88 std::string SegmentTestBase::TemplateFileName(int number) const {
89  return GetSegmentName(segment_template_, 0, number, 0);
90 }
91 
92 SegmentTestBase::ClusterParser::ClusterParser() : in_cluster_(false) {}
93 
94 SegmentTestBase::ClusterParser::~ClusterParser() {}
95 
96 void SegmentTestBase::ClusterParser::PopulateFromCluster(
97  const std::string& file_name) {
98  cluster_sizes_.clear();
99  std::string file_contents;
100  ASSERT_TRUE(File::ReadFileToString(file_name.c_str(), &file_contents));
101 
102  const uint8_t* data = reinterpret_cast<const uint8_t*>(file_contents.c_str());
103  const size_t size = file_contents.size();
104  WebMListParser cluster_parser(kWebMIdCluster, this);
105  size_t position = 0;
106  while (position < size) {
107  int read = cluster_parser.Parse(data + position, size - position);
108  ASSERT_LT(0, read);
109 
110  cluster_parser.Reset();
111  position += read;
112  }
113 }
114 
115 void SegmentTestBase::ClusterParser::PopulateFromSegment(
116  const std::string& file_name) {
117  cluster_sizes_.clear();
118  std::string file_contents;
119  ASSERT_TRUE(File::ReadFileToString(file_name.c_str(), &file_contents));
120 
121  const uint8_t* data = reinterpret_cast<const uint8_t*>(file_contents.c_str());
122  const size_t size = file_contents.size();
123  WebMListParser header_parser(kWebMIdEBMLHeader, this);
124  int offset = header_parser.Parse(data, size);
125  ASSERT_LT(0, offset);
126 
127  WebMListParser segment_parser(kWebMIdSegment, this);
128  ASSERT_LT(0, segment_parser.Parse(data + offset, size - offset));
129 }
130 
131 int SegmentTestBase::ClusterParser::GetFrameCountForCluster(size_t i) const {
132  DCHECK(i < cluster_sizes_.size());
133  return cluster_sizes_[i];
134 }
135 
136 int SegmentTestBase::ClusterParser::cluster_count() const {
137  return cluster_sizes_.size();
138 }
139 
140 WebMParserClient* SegmentTestBase::ClusterParser::OnListStart(int id) {
141  if (id == kWebMIdCluster) {
142  if (in_cluster_)
143  return NULL;
144 
145  cluster_sizes_.push_back(0);
146  in_cluster_ = true;
147  }
148 
149  return this;
150 }
151 
152 bool SegmentTestBase::ClusterParser::OnListEnd(int id) {
153  if (id == kWebMIdCluster) {
154  if (!in_cluster_)
155  return false;
156  in_cluster_ = false;
157  }
158 
159  return true;
160 }
161 
162 bool SegmentTestBase::ClusterParser::OnUInt(int id, int64_t val) {
163  return true;
164 }
165 
166 bool SegmentTestBase::ClusterParser::OnFloat(int id, double val) {
167  return true;
168 }
169 
170 bool SegmentTestBase::ClusterParser::OnBinary(int id,
171  const uint8_t* data,
172  int size) {
173  if (in_cluster_ && id == kWebMIdSimpleBlock) {
174  cluster_sizes_[cluster_sizes_.size() - 1]++;
175  }
176 
177  return true;
178 }
179 
180 bool SegmentTestBase::ClusterParser::OnString(int id, const std::string& str) {
181  return true;
182 }
183 
184 } // namespace media
185 } // namespace edash_packager
186 
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
scoped_refptr< MediaSample > CreateSample(bool is_key_frame, uint64_t duration)
Creates a new media sample.
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