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 VideoCodec kVideoCodec = 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, kVideoCodec,
96  kCodecString, kLanguage, kWidth, kHeight,
97  kPixelWidth, kPixelHeight, kTrickPlayRate,
98  kNaluLengthSize, NULL, 0, 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, size - position);
125  ASSERT_LT(0, read);
126 
127  cluster_parser.Reset();
128  position += read;
129  }
130 }
131 
132 void SegmentTestBase::ClusterParser::PopulateFromSegment(
133  const std::string& file_name) {
134  cluster_sizes_.clear();
135  std::string file_contents;
136  ASSERT_TRUE(File::ReadFileToString(file_name.c_str(), &file_contents));
137 
138  const uint8_t* data = reinterpret_cast<const uint8_t*>(file_contents.c_str());
139  const size_t size = file_contents.size();
140  WebMListParser header_parser(kWebMIdEBMLHeader, this);
141  int offset = header_parser.Parse(data, size);
142  ASSERT_LT(0, offset);
143 
144  WebMListParser segment_parser(kWebMIdSegment, this);
145  ASSERT_LT(0, segment_parser.Parse(data + offset, size - offset));
146 }
147 
148 int SegmentTestBase::ClusterParser::GetFrameCountForCluster(size_t i) const {
149  DCHECK(i < cluster_sizes_.size());
150  return cluster_sizes_[i];
151 }
152 
153 int SegmentTestBase::ClusterParser::cluster_count() const {
154  return cluster_sizes_.size();
155 }
156 
157 WebMParserClient* SegmentTestBase::ClusterParser::OnListStart(int id) {
158  if (id == kWebMIdCluster) {
159  if (in_cluster_)
160  return NULL;
161 
162  cluster_sizes_.push_back(0);
163  in_cluster_ = true;
164  }
165 
166  return this;
167 }
168 
169 bool SegmentTestBase::ClusterParser::OnListEnd(int id) {
170  if (id == kWebMIdCluster) {
171  if (!in_cluster_)
172  return false;
173  in_cluster_ = false;
174  }
175 
176  return true;
177 }
178 
179 bool SegmentTestBase::ClusterParser::OnUInt(int id, int64_t val) {
180  return true;
181 }
182 
183 bool SegmentTestBase::ClusterParser::OnFloat(int id, double val) {
184  return true;
185 }
186 
187 bool SegmentTestBase::ClusterParser::OnBinary(int id,
188  const uint8_t* data,
189  int size) {
190  if (in_cluster_ && (id == kWebMIdSimpleBlock || id == kWebMIdBlock)) {
191  cluster_sizes_.back()++;
192  }
193 
194  return true;
195 }
196 
197 bool SegmentTestBase::ClusterParser::OnString(int id, const std::string& str) {
198  return true;
199 }
200 
201 } // namespace media
202 } // namespace shaka
203 
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: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.