DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
xml_node.cc
1 // Copyright 2014 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/mpd/base/xml/xml_node.h"
8 
9 #include <limits>
10 #include <set>
11 
12 #include "packager/base/logging.h"
13 #include "packager/base/macros.h"
14 #include "packager/base/strings/string_number_conversions.h"
15 #include "packager/base/sys_byteorder.h"
16 #include "packager/mpd/base/media_info.pb.h"
17 #include "packager/mpd/base/segment_info.h"
18 
19 namespace shaka {
20 
21 using xml::XmlNode;
22 typedef MediaInfo::AudioInfo AudioInfo;
23 typedef MediaInfo::VideoInfo VideoInfo;
24 
25 namespace {
26 const char kEC3Codec[] = "ec-3";
27 
28 std::string RangeToString(const Range& range) {
29  return base::Uint64ToString(range.begin()) + "-" +
30  base::Uint64ToString(range.end());
31 }
32 
33 bool PopulateSegmentTimeline(const std::list<SegmentInfo>& segment_infos,
34  XmlNode* segment_timeline) {
35  for (std::list<SegmentInfo>::const_iterator it = segment_infos.begin();
36  it != segment_infos.end();
37  ++it) {
38  XmlNode s_element("S");
39  s_element.SetIntegerAttribute("t", it->start_time);
40  s_element.SetIntegerAttribute("d", it->duration);
41  if (it->repeat > 0)
42  s_element.SetIntegerAttribute("r", it->repeat);
43 
44  CHECK(segment_timeline->AddChild(s_element.PassScopedPtr()));
45  }
46 
47  return true;
48 }
49 
50 } // namespace
51 
52 namespace xml {
53 
54 XmlNode::XmlNode(const char* name) : node_(xmlNewNode(NULL, BAD_CAST name)) {
55  DCHECK(name);
56  DCHECK(node_);
57 }
58 
59 XmlNode::~XmlNode() {}
60 
61 bool XmlNode::AddChild(scoped_xml_ptr<xmlNode> child) {
62  DCHECK(node_);
63  DCHECK(child);
64  if (!xmlAddChild(node_.get(), child.get()))
65  return false;
66 
67  // Reaching here means the ownership of |child| transfered to |node_|.
68  // Release the pointer so that it doesn't get destructed in this scope.
69  ignore_result(child.release());
70  return true;
71 }
72 
73 bool XmlNode::AddElements(const std::vector<Element>& elements) {
74  for (size_t element_index = 0; element_index < elements.size();
75  ++element_index) {
76  const Element& child_element = elements[element_index];
77  XmlNode child_node(child_element.name.c_str());
78  for (std::map<std::string, std::string>::const_iterator attribute_it =
79  child_element.attributes.begin();
80  attribute_it != child_element.attributes.end(); ++attribute_it) {
81  child_node.SetStringAttribute(attribute_it->first.c_str(),
82  attribute_it->second);
83  }
84  // Recursively set children for the child.
85  if (!child_node.AddElements(child_element.subelements))
86  return false;
87 
88  child_node.SetContent(child_element.content);
89 
90  if (!xmlAddChild(node_.get(), child_node.GetRawPtr())) {
91  LOG(ERROR) << "Failed to set child " << child_element.name
92  << " to parent element "
93  << reinterpret_cast<const char*>(node_->name);
94  return false;
95  }
96  // Reaching here means the ownership of |child_node| transfered to |node_|.
97  // Release the pointer so that it doesn't get destructed in this scope.
98  ignore_result(child_node.Release());
99  }
100  return true;
101 }
102 
103 void XmlNode::SetStringAttribute(const char* attribute_name,
104  const std::string& attribute) {
105  DCHECK(node_);
106  DCHECK(attribute_name);
107  xmlSetProp(node_.get(), BAD_CAST attribute_name, BAD_CAST attribute.c_str());
108 }
109 
110 void XmlNode::SetIntegerAttribute(const char* attribute_name, uint64_t number) {
111  DCHECK(node_);
112  DCHECK(attribute_name);
113  xmlSetProp(node_.get(),
114  BAD_CAST attribute_name,
115  BAD_CAST (base::Uint64ToString(number).c_str()));
116 }
117 
118 void XmlNode::SetFloatingPointAttribute(const char* attribute_name,
119  double number) {
120  DCHECK(node_);
121  DCHECK(attribute_name);
122  xmlSetProp(node_.get(),
123  BAD_CAST attribute_name,
124  BAD_CAST (base::DoubleToString(number).c_str()));
125 }
126 
127 void XmlNode::SetId(uint32_t id) {
128  SetIntegerAttribute("id", id);
129 }
130 
131 void XmlNode::SetContent(const std::string& content) {
132  DCHECK(node_);
133  xmlNodeSetContent(node_.get(), BAD_CAST content.c_str());
134 }
135 
136 scoped_xml_ptr<xmlNode> XmlNode::PassScopedPtr() {
137  DVLOG(2) << "Passing node_.";
138  DCHECK(node_);
139  return std::move(node_);
140 }
141 
142 xmlNodePtr XmlNode::Release() {
143  DVLOG(2) << "Releasing node_.";
144  DCHECK(node_);
145  return node_.release();
146 }
147 
148 xmlNodePtr XmlNode::GetRawPtr() {
149  return node_.get();
150 }
151 
152 RepresentationBaseXmlNode::RepresentationBaseXmlNode(const char* name)
153  : XmlNode(name) {}
154 RepresentationBaseXmlNode::~RepresentationBaseXmlNode() {}
155 
156 bool RepresentationBaseXmlNode::AddContentProtectionElements(
157  const std::list<ContentProtectionElement>& content_protection_elements) {
158  std::list<ContentProtectionElement>::const_iterator content_protection_it =
159  content_protection_elements.begin();
160  for (; content_protection_it != content_protection_elements.end();
161  ++content_protection_it) {
162  if (!AddContentProtectionElement(*content_protection_it))
163  return false;
164  }
165 
166  return true;
167 }
168 
169 bool RepresentationBaseXmlNode::AddContentProtectionElement(
170  const ContentProtectionElement& content_protection_element) {
171  XmlNode content_protection_node("ContentProtection");
172 
173  // @value is an optional attribute.
174  if (!content_protection_element.value.empty()) {
175  content_protection_node.SetStringAttribute(
176  "value", content_protection_element.value);
177  }
178  content_protection_node.SetStringAttribute(
179  "schemeIdUri", content_protection_element.scheme_id_uri);
180 
181  typedef std::map<std::string, std::string> AttributesMapType;
182  const AttributesMapType& additional_attributes =
183  content_protection_element.additional_attributes;
184 
185  AttributesMapType::const_iterator attributes_it =
186  additional_attributes.begin();
187  for (; attributes_it != additional_attributes.end(); ++attributes_it) {
188  content_protection_node.SetStringAttribute(attributes_it->first.c_str(),
189  attributes_it->second);
190  }
191 
192  if (!content_protection_node.AddElements(
193  content_protection_element.subelements)) {
194  return false;
195  }
196  return AddChild(content_protection_node.PassScopedPtr());
197 }
198 
199 AdaptationSetXmlNode::AdaptationSetXmlNode()
200  : RepresentationBaseXmlNode("AdaptationSet") {}
201 AdaptationSetXmlNode::~AdaptationSetXmlNode() {}
202 
203 void AdaptationSetXmlNode::AddRoleElement(const std::string& scheme_id_uri,
204  const std::string& value) {
205  XmlNode role("Role");
206  role.SetStringAttribute("schemeIdUri", scheme_id_uri);
207  role.SetStringAttribute("value", value);
208  AddChild(role.PassScopedPtr());
209 }
210 
211 RepresentationXmlNode::RepresentationXmlNode()
212  : RepresentationBaseXmlNode("Representation") {}
213 RepresentationXmlNode::~RepresentationXmlNode() {}
214 
215 bool RepresentationXmlNode::AddVideoInfo(const VideoInfo& video_info,
216  bool set_width,
217  bool set_height,
218  bool set_frame_rate) {
219  if (!video_info.has_width() || !video_info.has_height()) {
220  LOG(ERROR) << "Missing width or height for adding a video info.";
221  return false;
222  }
223 
224  if (video_info.has_pixel_width() && video_info.has_pixel_height()) {
225  SetStringAttribute("sar", base::IntToString(video_info.pixel_width()) +
226  ":" +
227  base::IntToString(video_info.pixel_height()));
228  }
229 
230  if (set_width)
231  SetIntegerAttribute("width", video_info.width());
232  if (set_height)
233  SetIntegerAttribute("height", video_info.height());
234  if (set_frame_rate) {
235  SetStringAttribute("frameRate",
236  base::IntToString(video_info.time_scale()) + "/" +
237  base::IntToString(video_info.frame_duration()));
238  }
239  return true;
240 }
241 
242 bool RepresentationXmlNode::AddAudioInfo(const AudioInfo& audio_info) {
243  if (!AddAudioChannelInfo(audio_info))
244  return false;
245 
246  AddAudioSamplingRateInfo(audio_info);
247  return true;
248 }
249 
250 bool RepresentationXmlNode::AddVODOnlyInfo(const MediaInfo& media_info) {
251  if (media_info.has_media_file_name()) {
252  XmlNode base_url("BaseURL");
253  base_url.SetContent(media_info.media_file_name());
254 
255  if (!AddChild(base_url.PassScopedPtr()))
256  return false;
257  }
258 
259  const bool need_segment_base = media_info.has_index_range() ||
260  media_info.has_init_range() ||
261  media_info.has_reference_time_scale();
262 
263  if (need_segment_base) {
264  XmlNode segment_base("SegmentBase");
265  if (media_info.has_index_range()) {
266  segment_base.SetStringAttribute("indexRange",
267  RangeToString(media_info.index_range()));
268  }
269 
270  if (media_info.has_reference_time_scale()) {
271  segment_base.SetIntegerAttribute("timescale",
272  media_info.reference_time_scale());
273  }
274 
275  if (media_info.has_init_range()) {
276  XmlNode initialization("Initialization");
277  initialization.SetStringAttribute("range",
278  RangeToString(media_info.init_range()));
279 
280  if (!segment_base.AddChild(initialization.PassScopedPtr()))
281  return false;
282  }
283 
284  if (!AddChild(segment_base.PassScopedPtr()))
285  return false;
286  }
287 
288  if (media_info.has_media_duration_seconds()) {
289  // Adding 'duration' attribute, so that this information can be used when
290  // generating one MPD file. This should be removed from the final MPD.
291  SetFloatingPointAttribute("duration", media_info.media_duration_seconds());
292  }
293 
294  return true;
295 }
296 
298  const MediaInfo& media_info,
299  const std::list<SegmentInfo>& segment_infos,
300  uint32_t start_number) {
301  XmlNode segment_template("SegmentTemplate");
302  if (media_info.has_reference_time_scale()) {
303  segment_template.SetIntegerAttribute("timescale",
304  media_info.reference_time_scale());
305  }
306 
307  if (media_info.has_init_segment_name()) {
308  // The spec does not allow '$Number$' and '$Time$' in initialization
309  // attribute.
310  // TODO(rkuroiwa, kqyang): Swap this check out with a better check. These
311  // templates allow formatting as well.
312  const std::string& init_segment_name = media_info.init_segment_name();
313  if (init_segment_name.find("$Number$") != std::string::npos ||
314  init_segment_name.find("$Time$") != std::string::npos) {
315  LOG(ERROR) << "$Number$ and $Time$ cannot be used for "
316  "SegmentTemplate@initialization";
317  return false;
318  }
319  segment_template.SetStringAttribute("initialization",
320  media_info.init_segment_name());
321  }
322 
323  if (media_info.has_segment_template()) {
324  segment_template.SetStringAttribute("media", media_info.segment_template());
325 
326  // TODO(rkuroiwa): Need a better check. $$Number is legitimate but not a
327  // template.
328  if (media_info.segment_template().find("$Number") != std::string::npos) {
329  DCHECK_GE(start_number, 1u);
330  segment_template.SetIntegerAttribute("startNumber", start_number);
331  }
332  }
333 
334  // TODO(rkuroiwa): Find out when a live MPD doesn't require SegmentTimeline.
335  XmlNode segment_timeline("SegmentTimeline");
336 
337  return PopulateSegmentTimeline(segment_infos, &segment_timeline) &&
338  segment_template.AddChild(segment_timeline.PassScopedPtr()) &&
339  AddChild(segment_template.PassScopedPtr());
340 }
341 
342 bool RepresentationXmlNode::AddAudioChannelInfo(const AudioInfo& audio_info) {
343  std::string audio_channel_config_scheme;
344  std::string audio_channel_config_value;
345 
346  if (audio_info.codec() == kEC3Codec) {
347  // Convert EC3 channel map into string of hexadecimal digits. Spec: DASH-IF
348  // Interoperability Points v3.0 9.2.1.2.
349  const uint16_t ec3_channel_map =
350  base::HostToNet16(audio_info.codec_specific_data().ec3_channel_map());
351  audio_channel_config_value =
352  base::HexEncode(&ec3_channel_map, sizeof(ec3_channel_map));
353  audio_channel_config_scheme =
354  "tag:dolby.com,2014:dash:audio_channel_configuration:2011";
355  } else {
356  audio_channel_config_value = base::UintToString(audio_info.num_channels());
357  audio_channel_config_scheme =
358  "urn:mpeg:dash:23003:3:audio_channel_configuration:2011";
359  }
360 
361  XmlNode audio_channel_config("AudioChannelConfiguration");
362  audio_channel_config.SetStringAttribute("schemeIdUri",
363  audio_channel_config_scheme);
364  audio_channel_config.SetStringAttribute("value", audio_channel_config_value);
365 
366  return AddChild(audio_channel_config.PassScopedPtr());
367 }
368 
369 // MPD expects one number for sampling frequency, or if it is a range it should
370 // be space separated.
371 void RepresentationXmlNode::AddAudioSamplingRateInfo(
372  const AudioInfo& audio_info) {
373  if (audio_info.has_sampling_frequency())
374  SetIntegerAttribute("audioSamplingRate", audio_info.sampling_frequency());
375 }
376 
377 } // namespace xml
378 } // namespace shaka
bool AddVideoInfo(const MediaInfo::VideoInfo &video_info, bool set_width, bool set_height, bool set_frame_rate)
Definition: xml_node.cc:215
void SetFloatingPointAttribute(const char *attribute_name, double number)
Definition: xml_node.cc:118
scoped_xml_ptr< xmlNode > PassScopedPtr()
Definition: xml_node.cc:136
XmlNode(const char *name)
Definition: xml_node.cc:54
bool AddVODOnlyInfo(const MediaInfo &media_info)
Definition: xml_node.cc:250
void SetStringAttribute(const char *attribute_name, const std::string &attribute)
Definition: xml_node.cc:103
bool AddChild(scoped_xml_ptr< xmlNode > child)
Definition: xml_node.cc:61
xmlNodePtr Release()
Definition: xml_node.cc:142
bool AddLiveOnlyInfo(const MediaInfo &media_info, const std::list< SegmentInfo > &segment_infos, uint32_t start_number)
Definition: xml_node.cc:297
void SetId(uint32_t id)
Definition: xml_node.cc:127
bool AddElements(const std::vector< Element > &elements)
Adds Elements to this node using the Element struct.
Definition: xml_node.cc:73
void SetIntegerAttribute(const char *attribute_name, uint64_t number)
Definition: xml_node.cc:110
void AddRoleElement(const std::string &scheme_id_uri, const std::string &value)
Definition: xml_node.cc:203
void SetContent(const std::string &content)
Definition: xml_node.cc:131
bool AddAudioInfo(const MediaInfo::AudioInfo &audio_info)
Definition: xml_node.cc:242
xmlNodePtr GetRawPtr()
Definition: xml_node.cc:148