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.Pass();
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.segment_duration = 30; // seconds
67  ret.fragment_duration = 30; // seconds
68  ret.segment_sap_aligned = false;
69  ret.fragment_sap_aligned = false;
70  // Use memory files for temp storage. Normally this would be a bad idea
71  // since it wouldn't support large files, but for tests the files are small.
72  ret.temp_dir = "memory://temp/";
73  return ret;
74 }
75 
77  return new VideoStreamInfo(kTrackId, kTimeScale, kDuration, kVideoCodec,
78  kCodecString, kLanguage, kWidth, kHeight,
79  kPixelWidth, kPixelHeight, kTrickPlayRate,
80  kNaluLengthSize, NULL, 0, false);
81 }
82 
83 std::string SegmentTestBase::OutputFileName() const {
84  return output_file_name_;
85 }
86 
87 std::string SegmentTestBase::TemplateFileName(int number) const {
88  return GetSegmentName(segment_template_, 0, number, 0);
89 }
90 
91 SegmentTestBase::ClusterParser::ClusterParser() : in_cluster_(false) {}
92 
93 SegmentTestBase::ClusterParser::~ClusterParser() {}
94 
95 void SegmentTestBase::ClusterParser::PopulateFromCluster(
96  const std::string& file_name) {
97  cluster_sizes_.clear();
98  std::string file_contents;
99  ASSERT_TRUE(File::ReadFileToString(file_name.c_str(), &file_contents));
100 
101  const uint8_t* data = reinterpret_cast<const uint8_t*>(file_contents.c_str());
102  const size_t size = file_contents.size();
103  WebMListParser cluster_parser(kWebMIdCluster, this);
104  size_t position = 0;
105  while (position < size) {
106  int read = cluster_parser.Parse(data + position, size - position);
107  ASSERT_LT(0, read);
108 
109  cluster_parser.Reset();
110  position += read;
111  }
112 }
113 
114 void SegmentTestBase::ClusterParser::PopulateFromSegment(
115  const std::string& file_name) {
116  cluster_sizes_.clear();
117  std::string file_contents;
118  ASSERT_TRUE(File::ReadFileToString(file_name.c_str(), &file_contents));
119 
120  const uint8_t* data = reinterpret_cast<const uint8_t*>(file_contents.c_str());
121  const size_t size = file_contents.size();
122  WebMListParser header_parser(kWebMIdEBMLHeader, this);
123  int offset = header_parser.Parse(data, size);
124  ASSERT_LT(0, offset);
125 
126  WebMListParser segment_parser(kWebMIdSegment, this);
127  ASSERT_LT(0, segment_parser.Parse(data + offset, size - offset));
128 }
129 
130 int SegmentTestBase::ClusterParser::GetFrameCountForCluster(size_t i) const {
131  DCHECK(i < cluster_sizes_.size());
132  return cluster_sizes_[i];
133 }
134 
135 int SegmentTestBase::ClusterParser::cluster_count() const {
136  return cluster_sizes_.size();
137 }
138 
139 WebMParserClient* SegmentTestBase::ClusterParser::OnListStart(int id) {
140  if (id == kWebMIdCluster) {
141  if (in_cluster_)
142  return NULL;
143 
144  cluster_sizes_.push_back(0);
145  in_cluster_ = true;
146  }
147 
148  return this;
149 }
150 
151 bool SegmentTestBase::ClusterParser::OnListEnd(int id) {
152  if (id == kWebMIdCluster) {
153  if (!in_cluster_)
154  return false;
155  in_cluster_ = false;
156  }
157 
158  return true;
159 }
160 
161 bool SegmentTestBase::ClusterParser::OnUInt(int id, int64_t val) {
162  return true;
163 }
164 
165 bool SegmentTestBase::ClusterParser::OnFloat(int id, double val) {
166  return true;
167 }
168 
169 bool SegmentTestBase::ClusterParser::OnBinary(int id,
170  const uint8_t* data,
171  int size) {
172  if (in_cluster_ && id == kWebMIdSimpleBlock) {
173  cluster_sizes_[cluster_sizes_.size() - 1]++;
174  }
175 
176  return true;
177 }
178 
179 bool SegmentTestBase::ClusterParser::OnString(int id, const std::string& str) {
180  return true;
181 }
182 
183 } // namespace media
184 } // namespace edash_packager
185 
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.
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