7 #include "packager/mpd/base/xml/xml_node.h"
12 #include "packager/base/logging.h"
13 #include "packager/base/macros.h"
14 #include "packager/base/stl_util.h"
15 #include "packager/base/sys_byteorder.h"
16 #include "packager/base/strings/string_number_conversions.h"
17 #include "packager/mpd/base/media_info.pb.h"
18 #include "packager/mpd/base/segment_info.h"
22 using edash_packager::MediaInfo;
23 typedef edash_packager::MediaInfo::AudioInfo AudioInfo;
24 typedef edash_packager::MediaInfo::VideoInfo VideoInfo;
25 typedef MediaInfo::ContentProtectionXml ContentProtectionXml;
26 typedef ContentProtectionXml::AttributeNameValuePair AttributeNameValuePair;
28 namespace edash_packager {
31 const char kEC3Codec[] =
"ec-3";
33 std::string RangeToString(
const Range& range) {
34 return base::Uint64ToString(range.begin()) +
"-" +
35 base::Uint64ToString(range.end());
38 bool PopulateSegmentTimeline(
const std::list<SegmentInfo>& segment_infos,
40 for (std::list<SegmentInfo>::const_iterator it = segment_infos.begin();
41 it != segment_infos.end();
44 s_element.SetIntegerAttribute(
"t", it->start_time);
45 s_element.SetIntegerAttribute(
"d", it->duration);
47 s_element.SetIntegerAttribute(
"r", it->repeat);
49 CHECK(segment_timeline->
AddChild(s_element.PassScopedPtr()));
64 XmlNode::~XmlNode() {}
69 if (!xmlAddChild(node_.get(), child.get()))
74 ignore_result(child.release());
79 for (
size_t element_index = 0; element_index < elements.size();
81 const Element& child_element = elements[element_index];
82 XmlNode child_node(child_element.name.c_str());
83 for (std::map<std::string, std::string>::const_iterator attribute_it =
84 child_element.attributes.begin();
85 attribute_it != child_element.attributes.end(); ++attribute_it) {
87 attribute_it->second);
90 if (!child_node.AddElements(child_element.subelements))
93 child_node.SetContent(child_element.content);
95 if (!xmlAddChild(node_.get(), child_node.GetRawPtr())) {
96 LOG(ERROR) <<
"Failed to set child " << child_element.name
97 <<
" to parent element "
98 <<
reinterpret_cast<const char*
>(node_->name);
103 ignore_result(child_node.Release());
109 const std::string& attribute) {
111 DCHECK(attribute_name);
112 xmlSetProp(node_.get(), BAD_CAST attribute_name, BAD_CAST attribute.c_str());
117 DCHECK(attribute_name);
118 xmlSetProp(node_.get(),
119 BAD_CAST attribute_name,
120 BAD_CAST (base::Uint64ToString(number).c_str()));
126 DCHECK(attribute_name);
127 xmlSetProp(node_.get(),
128 BAD_CAST attribute_name,
129 BAD_CAST (base::DoubleToString(number).c_str()));
138 xmlNodeSetContent(node_.get(), BAD_CAST content.c_str());
142 DVLOG(2) <<
"Passing node_.";
148 DVLOG(2) <<
"Releasing node_.";
150 return node_.release();
157 RepresentationBaseXmlNode::RepresentationBaseXmlNode(
const char* name)
159 RepresentationBaseXmlNode::~RepresentationBaseXmlNode() {}
161 bool RepresentationBaseXmlNode::AddContentProtectionElements(
162 const std::list<ContentProtectionElement>& content_protection_elements) {
163 std::list<ContentProtectionElement>::const_iterator content_protection_it =
164 content_protection_elements.begin();
165 for (; content_protection_it != content_protection_elements.end();
166 ++content_protection_it) {
167 if (!AddContentProtectionElement(*content_protection_it))
174 bool RepresentationBaseXmlNode::AddContentProtectionElement(
175 const ContentProtectionElement& content_protection_element) {
176 XmlNode content_protection_node(
"ContentProtection");
179 if (!content_protection_element.value.empty()) {
180 content_protection_node.SetStringAttribute(
181 "value", content_protection_element.value);
183 content_protection_node.SetStringAttribute(
184 "schemeIdUri", content_protection_element.scheme_id_uri);
186 typedef std::map<std::string, std::string> AttributesMapType;
187 const AttributesMapType& additional_attributes =
188 content_protection_element.additional_attributes;
190 AttributesMapType::const_iterator attributes_it =
191 additional_attributes.begin();
192 for (; attributes_it != additional_attributes.end(); ++attributes_it) {
193 content_protection_node.SetStringAttribute(attributes_it->first.c_str(),
194 attributes_it->second);
197 if (!content_protection_node.AddElements(
198 content_protection_element.subelements)) {
201 return AddChild(content_protection_node.PassScopedPtr());
204 AdaptationSetXmlNode::AdaptationSetXmlNode()
205 : RepresentationBaseXmlNode(
"AdaptationSet") {}
206 AdaptationSetXmlNode::~AdaptationSetXmlNode() {}
209 const std::string& value) {
216 RepresentationXmlNode::RepresentationXmlNode()
218 RepresentationXmlNode::~RepresentationXmlNode() {}
223 bool set_frame_rate) {
224 if (!video_info.has_width() || !video_info.has_height()) {
225 LOG(ERROR) <<
"Missing width or height for adding a video info.";
229 if (video_info.has_pixel_width() && video_info.has_pixel_height()) {
232 base::IntToString(video_info.pixel_height()));
239 if (set_frame_rate) {
241 base::IntToString(video_info.time_scale()) +
"/" +
242 base::IntToString(video_info.frame_duration()));
248 if (!AddAudioChannelInfo(audio_info))
251 AddAudioSamplingRateInfo(audio_info);
256 if (media_info.has_media_file_name()) {
258 base_url.
SetContent(media_info.media_file_name());
264 const bool need_segment_base = media_info.has_index_range() ||
265 media_info.has_init_range() ||
266 media_info.has_reference_time_scale();
268 if (need_segment_base) {
269 XmlNode segment_base(
"SegmentBase");
270 if (media_info.has_index_range()) {
272 RangeToString(media_info.index_range()));
275 if (media_info.has_reference_time_scale()) {
277 media_info.reference_time_scale());
280 if (media_info.has_init_range()) {
281 XmlNode initialization(
"Initialization");
283 RangeToString(media_info.init_range()));
293 if (media_info.has_media_duration_seconds()) {
303 const MediaInfo& media_info,
304 const std::list<SegmentInfo>& segment_infos,
305 uint32_t start_number) {
306 XmlNode segment_template(
"SegmentTemplate");
307 if (media_info.has_reference_time_scale()) {
309 media_info.reference_time_scale());
312 if (media_info.has_init_segment_name()) {
317 const std::string& init_segment_name = media_info.init_segment_name();
318 if (init_segment_name.find(
"$Number$") != std::string::npos ||
319 init_segment_name.find(
"$Time$") != std::string::npos) {
320 LOG(ERROR) <<
"$Number$ and $Time$ cannot be used for "
321 "SegmentTemplate@initialization";
325 media_info.init_segment_name());
328 if (media_info.has_segment_template()) {
333 if (media_info.segment_template().find(
"$Number") != std::string::npos) {
334 DCHECK_GE(start_number, 1u);
340 XmlNode segment_timeline(
"SegmentTimeline");
342 return PopulateSegmentTimeline(segment_infos, &segment_timeline) &&
347 bool RepresentationXmlNode::AddAudioChannelInfo(
const AudioInfo& audio_info) {
348 std::string audio_channel_config_scheme;
349 std::string audio_channel_config_value;
351 if (audio_info.codec() == kEC3Codec) {
354 const uint16_t ec3_channel_map =
355 base::HostToNet16(audio_info.codec_specific_data().ec3_channel_map());
356 audio_channel_config_value =
357 base::HexEncode(&ec3_channel_map,
sizeof(ec3_channel_map));
358 audio_channel_config_scheme =
359 "tag:dolby.com,2014:dash:audio_channel_configuration:2011";
361 audio_channel_config_value = base::UintToString(audio_info.num_channels());
362 audio_channel_config_scheme =
363 "urn:mpeg:dash:23003:3:audio_channel_configuration:2011";
366 XmlNode audio_channel_config(
"AudioChannelConfiguration");
367 audio_channel_config.SetStringAttribute(
"schemeIdUri",
368 audio_channel_config_scheme);
369 audio_channel_config.SetStringAttribute(
"value", audio_channel_config_value);
371 return AddChild(audio_channel_config.PassScopedPtr());
376 void RepresentationXmlNode::AddAudioSamplingRateInfo(
377 const AudioInfo& audio_info) {
378 if (audio_info.has_sampling_frequency())
bool AddVODOnlyInfo(const MediaInfo &media_info)
bool AddLiveOnlyInfo(const MediaInfo &media_info, const std::list< SegmentInfo > &segment_infos, uint32_t start_number)
void SetStringAttribute(const char *attribute_name, const std::string &attribute)
void SetFloatingPointAttribute(const char *attribute_name, double number)
bool AddAudioInfo(const MediaInfo::AudioInfo &audio_info)
bool AddChild(scoped_xml_ptr< xmlNode > child)
XmlNode(const char *name)
void SetIntegerAttribute(const char *attribute_name, uint64_t number)
bool AddElements(const std::vector< Element > &elements)
Adds Elements to this node using the Element struct.
scoped_xml_ptr< xmlNode > PassScopedPtr()
void SetContent(const std::string &content)
bool AddVideoInfo(const MediaInfo::VideoInfo &video_info, bool set_width, bool set_height, bool set_frame_rate)
void AddRoleElement(const std::string &scheme_id_uri, const std::string &value)