7 #include "packager/mpd/base/xml/xml_node.h"
9 #include <gflags/gflags.h>
10 #include <libxml/tree.h>
16 #include "packager/base/logging.h"
17 #include "packager/base/macros.h"
18 #include "packager/base/strings/string_number_conversions.h"
19 #include "packager/base/sys_byteorder.h"
20 #include "packager/media/base/rcheck.h"
21 #include "packager/mpd/base/media_info.pb.h"
22 #include "packager/mpd/base/mpd_utils.h"
23 #include "packager/mpd/base/segment_info.h"
24 #include "packager/mpd/base/xml/scoped_xml_ptr.h"
26 DEFINE_bool(segment_template_constant_duration,
28 "Generates SegmentTemplate@duration if all segments except the "
29 "last one has the same duration if this flag is set to true.");
31 DEFINE_bool(dash_add_last_segment_number_when_needed,
33 "Adds a Supplemental Descriptor with @schemeIdUri "
34 "set to http://dashif.org/guidelines/last-segment-number with "
35 "the @value set to the last segment number.");
40 typedef MediaInfo::AudioInfo AudioInfo;
41 typedef MediaInfo::VideoInfo VideoInfo;
44 const char kEC3Codec[] =
"ec-3";
45 const char kAC4Codec[] =
"ac-4";
47 std::string RangeToString(
const Range& range) {
48 return base::Uint64ToString(range.begin()) +
"-" +
49 base::Uint64ToString(range.end());
54 bool IsTimelineConstantDuration(
const std::list<SegmentInfo>& segment_infos,
55 uint32_t start_number) {
56 if (!FLAGS_segment_template_constant_duration)
59 DCHECK(!segment_infos.empty());
60 if (segment_infos.size() > 2)
63 const SegmentInfo& first_segment = segment_infos.front();
64 if (first_segment.start_time != first_segment.duration * (start_number - 1))
67 if (segment_infos.size() == 1)
70 const SegmentInfo& last_segment = segment_infos.back();
71 if (last_segment.repeat != 0)
74 const int64_t expected_last_segment_start_time =
75 first_segment.start_time +
76 first_segment.duration * (first_segment.repeat + 1);
77 return expected_last_segment_start_time == last_segment.start_time;
80 bool PopulateSegmentTimeline(
const std::list<SegmentInfo>& segment_infos,
81 XmlNode* segment_timeline) {
82 for (
const SegmentInfo& segment_info : segment_infos) {
83 XmlNode s_element(
"S");
84 RCHECK(s_element.SetIntegerAttribute(
"t", segment_info.start_time));
85 RCHECK(s_element.SetIntegerAttribute(
"d", segment_info.duration));
86 if (segment_info.repeat > 0)
87 RCHECK(s_element.SetIntegerAttribute(
"r", segment_info.repeat));
89 RCHECK(segment_timeline->AddChild(std::move(s_element)));
95 void CollectNamespaceFromName(
const std::string& name,
96 std::set<std::string>* namespaces) {
97 const size_t pos = name.find(
':');
98 if (pos != std::string::npos)
99 namespaces->insert(name.substr(0, pos));
102 void TraverseAttrsAndCollectNamespaces(
const xmlAttr* attr,
103 std::set<std::string>* namespaces) {
104 for (
const xmlAttr* cur_attr = attr; cur_attr; cur_attr = cur_attr->next) {
105 CollectNamespaceFromName(
reinterpret_cast<const char*
>(cur_attr->name),
110 void TraverseNodesAndCollectNamespaces(
const xmlNode* node,
111 std::set<std::string>* namespaces) {
112 for (
const xmlNode* cur_node = node; cur_node; cur_node = cur_node->next) {
113 CollectNamespaceFromName(
reinterpret_cast<const char*
>(cur_node->name),
116 TraverseNodesAndCollectNamespaces(cur_node->children, namespaces);
117 TraverseAttrsAndCollectNamespaces(cur_node->properties, namespaces);
125 class XmlNode::Impl {
127 scoped_xml_ptr<xmlNode> node;
131 impl_->node.reset(xmlNewNode(NULL, BAD_CAST name.c_str()));
137 XmlNode::~XmlNode() {}
143 DCHECK(child.impl_->node);
144 RCHECK(xmlAddChild(impl_->node.get(), child.impl_->node.get()));
148 ignore_result(child.impl_->node.release());
153 for (
size_t element_index = 0; element_index < elements.size();
155 const Element& child_element = elements[element_index];
156 XmlNode child_node(child_element.name);
157 for (std::map<std::string, std::string>::const_iterator attribute_it =
158 child_element.attributes.begin();
159 attribute_it != child_element.attributes.end(); ++attribute_it) {
161 attribute_it->second));
169 RCHECK(child_node.
AddElements(child_element.subelements));
171 if (!xmlAddChild(impl_->node.get(), child_node.impl_->node.get())) {
172 LOG(ERROR) <<
"Failed to set child " << child_element.name
173 <<
" to parent element "
174 <<
reinterpret_cast<const char*
>(impl_->node->name);
179 child_node.impl_->node.release();
185 const std::string& attribute) {
187 return xmlSetProp(impl_->node.get(), BAD_CAST attribute_name.c_str(),
188 BAD_CAST attribute.c_str()) !=
nullptr;
194 return xmlSetProp(impl_->node.get(), BAD_CAST attribute_name.c_str(),
195 BAD_CAST(base::Uint64ToString(number).c_str())) !=
nullptr;
201 return xmlSetProp(impl_->node.get(), BAD_CAST attribute_name.c_str(),
202 BAD_CAST(base::DoubleToString(number).c_str())) !=
nullptr;
211 xmlNodeAddContent(impl_->node.get(), BAD_CAST content.c_str());
216 xmlNodeSetContent(impl_->node.get(), BAD_CAST content.c_str());
220 std::set<std::string> namespaces;
221 TraverseNodesAndCollectNamespaces(impl_->node.get(), &namespaces);
228 xml::scoped_xml_ptr<xmlDoc> doc(xmlNewDoc(BAD_CAST
"1.0"));
229 if (comment.empty()) {
230 xmlDocSetRootElement(doc.get(), xmlCopyNode(impl_->node.get(),
true));
232 xml::scoped_xml_ptr<xmlNode> comment_xml(
233 xmlNewDocComment(doc.get(), BAD_CAST comment.c_str()));
234 xmlDocSetRootElement(doc.get(), comment_xml.get());
235 xmlAddSibling(comment_xml.release(), xmlCopyNode(impl_->node.get(),
true));
239 static const int kNiceFormat = 1;
240 int doc_str_size = 0;
241 xmlChar* doc_str =
nullptr;
242 xmlDocDumpFormatMemoryEnc(doc.get(), &doc_str, &doc_str_size,
"UTF-8",
244 std::string output(doc_str, doc_str + doc_str_size);
250 xml::scoped_xml_ptr<xmlChar> str(
251 xmlGetProp(impl_->node.get(), BAD_CAST name.c_str()));
254 *value =
reinterpret_cast<const char*
>(str.get());
258 xmlNode* XmlNode::GetRawPtr()
const {
259 return impl_->node.get();
262 RepresentationBaseXmlNode::RepresentationBaseXmlNode(
const std::string& name)
264 RepresentationBaseXmlNode::~RepresentationBaseXmlNode() {}
266 bool RepresentationBaseXmlNode::AddContentProtectionElements(
267 const std::list<ContentProtectionElement>& content_protection_elements) {
268 for (
const auto& elem : content_protection_elements) {
269 RCHECK(AddContentProtectionElement(elem));
276 const std::string& scheme_id_uri,
277 const std::string& value) {
278 return AddDescriptor(
"SupplementalProperty", scheme_id_uri, value);
282 const std::string& scheme_id_uri,
283 const std::string& value) {
284 return AddDescriptor(
"EssentialProperty", scheme_id_uri, value);
288 const std::string& descriptor_name,
289 const std::string& scheme_id_uri,
290 const std::string& value) {
291 XmlNode descriptor(descriptor_name);
295 return AddChild(std::move(descriptor));
298 bool RepresentationBaseXmlNode::AddContentProtectionElement(
300 XmlNode content_protection_node(
"ContentProtection");
303 if (!content_protection_element.value.empty()) {
304 RCHECK(content_protection_node.SetStringAttribute(
305 "value", content_protection_element.value));
307 RCHECK(content_protection_node.SetStringAttribute(
308 "schemeIdUri", content_protection_element.scheme_id_uri));
310 for (
const auto& pair : content_protection_element.additional_attributes) {
311 RCHECK(content_protection_node.SetStringAttribute(pair.first, pair.second));
314 RCHECK(content_protection_node.AddElements(
315 content_protection_element.subelements));
316 return AddChild(std::move(content_protection_node));
319 AdaptationSetXmlNode::AdaptationSetXmlNode()
320 : RepresentationBaseXmlNode(
"AdaptationSet") {}
321 AdaptationSetXmlNode::~AdaptationSetXmlNode() {}
324 const std::string& scheme_id_uri,
325 const std::string& value) {
330 const std::string& value) {
334 RepresentationXmlNode::RepresentationXmlNode()
336 RepresentationXmlNode::~RepresentationXmlNode() {}
341 bool set_frame_rate) {
342 if (!video_info.has_width() || !video_info.has_height()) {
343 LOG(ERROR) <<
"Missing width or height for adding a video info.";
347 if (video_info.has_pixel_width() && video_info.has_pixel_height()) {
349 "sar", base::IntToString(video_info.pixel_width()) +
":" +
350 base::IntToString(video_info.pixel_height())));
357 if (set_frame_rate) {
359 "frameRate", base::IntToString(video_info.time_scale()) +
"/" +
360 base::IntToString(video_info.frame_duration())));
363 if (video_info.has_playback_rate()) {
365 base::IntToString(video_info.playback_rate())));
376 return AddAudioChannelInfo(audio_info) &&
377 AddAudioSamplingRateInfo(audio_info);
381 bool use_segment_list,
382 double target_segment_duration) {
383 const bool use_single_segment_url_with_media =
384 media_info.has_text_info() && media_info.has_presentation_time_offset();
386 if (media_info.has_media_file_url() && !use_single_segment_url_with_media) {
388 base_url.
SetContent(media_info.media_file_url());
390 RCHECK(
AddChild(std::move(base_url)));
393 const bool need_segment_base_or_list =
394 use_segment_list || media_info.has_index_range() ||
395 media_info.has_init_range() ||
396 (media_info.has_reference_time_scale() && !media_info.has_text_info()) ||
397 use_single_segment_url_with_media;
399 if (!need_segment_base_or_list) {
403 XmlNode child(use_segment_list || use_single_segment_url_with_media
409 if (media_info.has_index_range() && !use_segment_list) {
411 RangeToString(media_info.index_range())));
414 if (media_info.has_reference_time_scale()) {
416 media_info.reference_time_scale()));
418 if (use_segment_list && !use_single_segment_url_with_media) {
419 const uint64_t duration_seconds =
static_cast<uint64_t
>(
420 floor(target_segment_duration * media_info.reference_time_scale()));
425 if (media_info.has_presentation_time_offset()) {
427 media_info.presentation_time_offset()));
430 if (media_info.has_init_range()) {
431 XmlNode initialization(
"Initialization");
433 "range", RangeToString(media_info.init_range())));
435 RCHECK(child.
AddChild(std::move(initialization)));
438 if (use_single_segment_url_with_media) {
439 XmlNode media_url(
"SegmentURL");
441 RCHECK(child.
AddChild(std::move(media_url)));
446 if (use_segment_list) {
447 for (
const Range& subsegment_range : media_info.subsegment_ranges()) {
448 XmlNode subsegment(
"SegmentURL");
450 RangeToString(subsegment_range)));
452 RCHECK(child.
AddChild(std::move(subsegment)));
461 const MediaInfo& media_info,
462 const std::list<SegmentInfo>& segment_infos,
463 uint32_t start_number) {
464 XmlNode segment_template(
"SegmentTemplate");
465 if (media_info.has_reference_time_scale()) {
467 "timescale", media_info.reference_time_scale()));
470 if (media_info.has_presentation_time_offset()) {
472 "presentationTimeOffset", media_info.presentation_time_offset()));
475 if (media_info.has_init_segment_url()) {
477 media_info.init_segment_url()));
480 if (media_info.has_segment_template_url()) {
482 "media", media_info.segment_template_url()));
486 if (!segment_infos.empty()) {
489 if (IsTimelineConstantDuration(segment_infos, start_number)) {
491 "duration", segment_infos.front().duration));
492 if (FLAGS_dash_add_last_segment_number_when_needed) {
493 uint32_t last_segment_number = start_number - 1;
494 for (
const auto& segment_info_element : segment_infos)
495 last_segment_number += segment_info_element.repeat + 1;
498 "http://dashif.org/guidelines/last-segment-number",
499 std::to_string(last_segment_number)));
502 XmlNode segment_timeline(
"SegmentTimeline");
503 RCHECK(PopulateSegmentTimeline(segment_infos, &segment_timeline));
504 RCHECK(segment_template.
AddChild(std::move(segment_timeline)));
507 return AddChild(std::move(segment_template));
510 bool RepresentationXmlNode::AddAudioChannelInfo(
const AudioInfo& audio_info) {
511 std::string audio_channel_config_scheme;
512 std::string audio_channel_config_value;
514 if (audio_info.codec() == kEC3Codec) {
515 const auto& codec_data = audio_info.codec_specific_data();
519 const uint32_t ec3_channel_mpeg_value = codec_data.channel_mpeg_value();
520 const uint32_t NO_MAPPING = 0xFFFFFFFF;
521 if (ec3_channel_mpeg_value == NO_MAPPING) {
524 const uint16_t ec3_channel_map =
525 base::HostToNet16(codec_data.channel_mask());
526 audio_channel_config_value =
527 base::HexEncode(&ec3_channel_map,
sizeof(ec3_channel_map));
528 audio_channel_config_scheme =
529 "tag:dolby.com,2014:dash:audio_channel_configuration:2011";
534 audio_channel_config_value = base::UintToString(ec3_channel_mpeg_value);
535 audio_channel_config_scheme =
"urn:mpeg:mpegB:cicp:ChannelConfiguration";
538 audio_channel_config_scheme,
539 audio_channel_config_value);
543 if (codec_data.ec3_joc_complexity() != 0) {
544 std::string ec3_joc_complexity =
545 base::UintToString(codec_data.ec3_joc_complexity());
547 "tag:dolby.com,2018:dash:EC3_ExtensionType:2018",
550 "tag:dolby.com,2018:dash:"
551 "EC3_ExtensionComplexityIndex:2018",
555 }
else if (audio_info.codec().substr(0, 4) == kAC4Codec) {
556 const auto& codec_data = audio_info.codec_specific_data();
557 const bool ac4_ims_flag = codec_data.ac4_ims_flag();
561 const uint32_t ac4_channel_mpeg_value = codec_data.channel_mpeg_value();
562 const uint32_t NO_MAPPING = 0xFFFFFFFF;
563 if (ac4_channel_mpeg_value == NO_MAPPING) {
567 const uint32_t ac4_channel_mask =
568 base::HostToNet32(codec_data.channel_mask() << 8);
569 audio_channel_config_value =
570 base::HexEncode(&ac4_channel_mask,
sizeof(ac4_channel_mask) - 1);
573 audio_channel_config_scheme =
574 "tag:dolby.com,2015:dash:audio_channel_configuration:2015";
579 audio_channel_config_value = base::UintToString(ac4_channel_mpeg_value);
580 audio_channel_config_scheme =
"urn:mpeg:mpegB:cicp:ChannelConfiguration";
583 audio_channel_config_scheme,
584 audio_channel_config_value);
587 "tag:dolby.com,2016:dash:virtualized_content:2016",
592 audio_channel_config_value = base::UintToString(audio_info.num_channels());
593 audio_channel_config_scheme =
594 "urn:mpeg:dash:23003:3:audio_channel_configuration:2011";
597 return AddDescriptor(
"AudioChannelConfiguration", audio_channel_config_scheme,
598 audio_channel_config_value);
603 bool RepresentationXmlNode::AddAudioSamplingRateInfo(
604 const AudioInfo& audio_info) {
605 return !audio_info.has_sampling_frequency() ||
607 audio_info.sampling_frequency());