7#include <packager/mpd/base/xml/xml_node.h>
20#include <absl/flags/flag.h>
21#include <absl/log/check.h>
22#include <absl/log/log.h>
23#include <absl/strings/str_format.h>
25#include <libxml/tree.h>
26#include <libxml/xmlmemory.h>
27#include <libxml/xmlstring.h>
29#include <packager/macros/compiler.h>
30#include <packager/media/base/rcheck.h>
31#include <packager/mpd/base/content_protection_element.h>
32#include <packager/mpd/base/media_info.pb.h>
33#include <packager/mpd/base/mpd_utils.h>
34#include <packager/mpd/base/segment_info.h>
35#include <packager/mpd/base/xml/scoped_xml_ptr.h>
38 segment_template_constant_duration,
40 "Generates SegmentTemplate@duration if all segments except the "
41 "last one has the same duration if this flag is set to true.");
44 dash_add_last_segment_number_when_needed,
46 "Adds a Supplemental Descriptor with @schemeIdUri "
47 "set to http://dashif.org/guidelines/last-segment-number with "
48 "the @value set to the last segment number.");
53typedef MediaInfo::AudioInfo AudioInfo;
54typedef MediaInfo::VideoInfo VideoInfo;
57const char kEC3Codec[] =
"ec-3";
58const char kAC4Codec[] =
"ac-4";
59const char kDTSCCodec[] =
"dtsc";
60const char kDTSECodec[] =
"dtse";
61const char kDTSXCodec[] =
"dtsx";
63std::string urlEncode(
const std::string& input) {
66 char* output = curl_easy_escape(curl, input.c_str(), input.length());
68 std::string encodedUrl(output);
75std::string RangeToString(
const Range& range) {
76 return absl::StrFormat(
"%u-%u", range.begin(), range.end());
81bool IsTimelineConstantDuration(
const std::list<SegmentInfo>& segment_infos,
82 uint32_t start_number) {
83 if (!absl::GetFlag(FLAGS_segment_template_constant_duration))
86 DCHECK(!segment_infos.empty());
87 if (segment_infos.size() > 2)
90 const SegmentInfo& first_segment = segment_infos.front();
91 if (first_segment.start_time != first_segment.duration * (start_number - 1))
94 if (segment_infos.size() == 1)
97 const SegmentInfo& last_segment = segment_infos.back();
98 if (last_segment.repeat != 0)
101 const int64_t expected_last_segment_start_time =
102 first_segment.start_time +
103 first_segment.duration * (first_segment.repeat + 1);
104 return expected_last_segment_start_time == last_segment.start_time;
107bool PopulateSegmentTimeline(
const std::list<SegmentInfo>& segment_infos,
108 XmlNode* segment_timeline) {
109 for (
const SegmentInfo& segment_info : segment_infos) {
110 XmlNode s_element(
"S");
111 RCHECK(s_element.SetIntegerAttribute(
"t", segment_info.start_time));
112 RCHECK(s_element.SetIntegerAttribute(
"d", segment_info.duration));
113 if (segment_info.repeat > 0)
114 RCHECK(s_element.SetIntegerAttribute(
"r", segment_info.repeat));
116 RCHECK(segment_timeline->AddChild(std::move(s_element)));
122void CollectNamespaceFromName(
const std::string& name,
123 std::set<std::string>* namespaces) {
124 const size_t pos = name.find(
':');
125 if (pos != std::string::npos)
126 namespaces->insert(name.substr(0, pos));
129void TraverseAttrsAndCollectNamespaces(
const xmlAttr* attr,
130 std::set<std::string>* namespaces) {
131 for (
const xmlAttr* cur_attr = attr; cur_attr; cur_attr = cur_attr->next) {
132 CollectNamespaceFromName(
reinterpret_cast<const char*
>(cur_attr->name),
137void TraverseNodesAndCollectNamespaces(
const xmlNode* node,
138 std::set<std::string>* namespaces) {
139 for (
const xmlNode* cur_node = node; cur_node; cur_node = cur_node->next) {
140 CollectNamespaceFromName(
reinterpret_cast<const char*
>(cur_node->name),
143 TraverseNodesAndCollectNamespaces(cur_node->children, namespaces);
144 TraverseAttrsAndCollectNamespaces(cur_node->properties, namespaces);
154 scoped_xml_ptr<xmlNode> node;
158 impl_->node.reset(xmlNewNode(NULL, BAD_CAST name.c_str()));
164XmlNode::~XmlNode() {}
170 DCHECK(child.impl_->node);
171 RCHECK(xmlAddChild(impl_->node.get(), child.impl_->node.get()));
175 UNUSED(child.impl_->node.release());
180 for (
size_t element_index = 0; element_index < elements.size();
182 const Element& child_element = elements[element_index];
183 XmlNode child_node(child_element.name);
184 for (std::map<std::string, std::string>::const_iterator attribute_it =
185 child_element.attributes.begin();
186 attribute_it != child_element.attributes.end(); ++attribute_it) {
188 attribute_it->second));
196 RCHECK(child_node.
AddElements(child_element.subelements));
198 if (!xmlAddChild(impl_->node.get(), child_node.impl_->node.get())) {
199 LOG(ERROR) <<
"Failed to set child " << child_element.name
200 <<
" to parent element "
201 <<
reinterpret_cast<const char*
>(impl_->node->name);
206 child_node.impl_->node.release();
212 const std::string& attribute) {
214 return xmlSetProp(impl_->node.get(), BAD_CAST attribute_name.c_str(),
215 BAD_CAST attribute.c_str()) !=
nullptr;
221 return xmlSetProp(impl_->node.get(), BAD_CAST attribute_name.c_str(),
222 BAD_CAST(absl::StrFormat(
"%" PRIu64, number).c_str())) !=
229 return xmlSetProp(impl_->node.get(), BAD_CAST attribute_name.c_str(),
230 BAD_CAST(FloatToXmlString(number).c_str())) !=
nullptr;
239 xmlNodeAddContent(impl_->node.get(), BAD_CAST content.c_str());
242void XmlNode::AddUrlEncodedContent(
const std::string& content) {
248 xmlNodeSetContent(impl_->node.get(), BAD_CAST content.c_str());
251void XmlNode::SetUrlEncodedContent(
const std::string& content) {
256 std::set<std::string> namespaces;
257 TraverseNodesAndCollectNamespaces(impl_->node.get(), &namespaces);
264 xml::scoped_xml_ptr<xmlDoc> doc(xmlNewDoc(BAD_CAST
"1.0"));
265 if (comment.empty()) {
266 xmlDocSetRootElement(doc.get(), xmlCopyNode(impl_->node.get(),
true));
268 xml::scoped_xml_ptr<xmlNode> comment_xml(
269 xmlNewDocComment(doc.get(), BAD_CAST comment.c_str()));
270 xmlDocSetRootElement(doc.get(), comment_xml.get());
271 xmlAddSibling(comment_xml.release(), xmlCopyNode(impl_->node.get(),
true));
275 static const int kNiceFormat = 1;
276 int doc_str_size = 0;
277 xmlChar* doc_str =
nullptr;
278 xmlDocDumpFormatMemoryEnc(doc.get(), &doc_str, &doc_str_size,
"UTF-8",
280 std::string output(doc_str, doc_str + doc_str_size);
286 xml::scoped_xml_ptr<xmlChar> str(
287 xmlGetProp(impl_->node.get(), BAD_CAST name.c_str()));
290 *value =
reinterpret_cast<const char*
>(str.get());
294xmlNode* XmlNode::GetRawPtr()
const {
295 return impl_->node.get();
298RepresentationBaseXmlNode::RepresentationBaseXmlNode(
const std::string& name)
300RepresentationBaseXmlNode::~RepresentationBaseXmlNode() {}
302bool RepresentationBaseXmlNode::AddContentProtectionElements(
303 const std::list<ContentProtectionElement>& content_protection_elements) {
304 for (
const auto& elem : content_protection_elements) {
305 RCHECK(AddContentProtectionElement(elem));
312 const std::string& scheme_id_uri,
313 const std::string& value) {
314 return AddDescriptor(
"SupplementalProperty", scheme_id_uri, value);
318 const std::string& scheme_id_uri,
319 const std::string& value) {
320 return AddDescriptor(
"EssentialProperty", scheme_id_uri, value);
324 const std::string& descriptor_name,
325 const std::string& scheme_id_uri,
326 const std::string& value) {
327 XmlNode descriptor(descriptor_name);
331 return AddChild(std::move(descriptor));
334bool RepresentationBaseXmlNode::AddContentProtectionElement(
336 XmlNode content_protection_node(
"ContentProtection");
339 if (!content_protection_element.value.empty()) {
340 RCHECK(content_protection_node.SetStringAttribute(
341 "value", content_protection_element.value));
343 RCHECK(content_protection_node.SetStringAttribute(
344 "schemeIdUri", content_protection_element.scheme_id_uri));
346 for (
const auto& pair : content_protection_element.additional_attributes) {
347 RCHECK(content_protection_node.SetStringAttribute(pair.first, pair.second));
350 RCHECK(content_protection_node.AddElements(
351 content_protection_element.subelements));
352 return AddChild(std::move(content_protection_node));
355AdaptationSetXmlNode::AdaptationSetXmlNode()
356 : RepresentationBaseXmlNode(
"AdaptationSet") {}
357AdaptationSetXmlNode::~AdaptationSetXmlNode() {}
360 const std::string& scheme_id_uri,
361 const std::string& value) {
366 const std::string& value) {
373 return AddChild(std::move(descriptor));
376RepresentationXmlNode::RepresentationXmlNode()
378RepresentationXmlNode::~RepresentationXmlNode() {}
383 bool set_frame_rate) {
384 if (!video_info.has_width() || !video_info.has_height()) {
385 LOG(ERROR) <<
"Missing width or height for adding a video info.";
389 if (video_info.has_pixel_width() && video_info.has_pixel_height()) {
391 absl::StrFormat(
"%d:%d", video_info.pixel_width(),
392 video_info.pixel_height())));
399 if (set_frame_rate) {
401 absl::StrFormat(
"%d/%d", video_info.time_scale(),
402 video_info.frame_duration())));
405 if (video_info.has_playback_rate()) {
407 "maxPlayoutRate", absl::StrFormat(
"%d", video_info.playback_rate())));
418 return AddAudioChannelInfo(audio_info) &&
419 AddAudioSamplingRateInfo(audio_info);
423 bool use_segment_list,
424 double target_segment_duration) {
425 if (media_info.has_media_file_url()) {
427 base_url.SetUrlEncodedContent(media_info.media_file_url());
429 RCHECK(
AddChild(std::move(base_url)));
435 const bool need_segment_base_or_list =
436 use_segment_list || media_info.has_index_range() ||
437 media_info.has_init_range() ||
438 (media_info.has_reference_time_scale() && !media_info.has_text_info()) ||
439 (media_info.has_text_info() && media_info.has_presentation_time_offset());
441 if (!need_segment_base_or_list) {
445 XmlNode child(use_segment_list ?
"SegmentList" :
"SegmentBase");
449 if (media_info.has_index_range() && !use_segment_list) {
451 RangeToString(media_info.index_range())));
454 if (media_info.has_reference_time_scale()) {
456 media_info.reference_time_scale()));
458 if (use_segment_list) {
459 const auto duration_seconds =
static_cast<int64_t
>(
460 floor(target_segment_duration * media_info.reference_time_scale()));
465 if (media_info.has_presentation_time_offset()) {
467 media_info.presentation_time_offset()));
470 if (media_info.has_init_range()) {
471 XmlNode initialization(
"Initialization");
473 "range", RangeToString(media_info.init_range())));
475 RCHECK(child.
AddChild(std::move(initialization)));
480 if (use_segment_list) {
481 for (
const Range& subsegment_range : media_info.subsegment_ranges()) {
482 XmlNode subsegment(
"SegmentURL");
484 RangeToString(subsegment_range)));
486 RCHECK(child.
AddChild(std::move(subsegment)));
495 const MediaInfo& media_info,
496 const std::list<SegmentInfo>& segment_infos,
497 bool low_latency_dash_mode) {
498 XmlNode segment_template(
"SegmentTemplate");
501 segment_infos.empty() ? 1 : segment_infos.begin()->start_segment_number;
503 if (media_info.has_reference_time_scale()) {
505 "timescale", media_info.reference_time_scale()));
508 if (media_info.has_segment_duration()) {
510 media_info.segment_duration()));
513 if (media_info.has_presentation_time_offset()) {
515 "presentationTimeOffset", media_info.presentation_time_offset()));
518 if (media_info.has_availability_time_offset()) {
520 "availabilityTimeOffset", media_info.availability_time_offset()));
523 if (low_latency_dash_mode) {
528 if (media_info.has_init_segment_url()) {
530 media_info.init_segment_url()));
533 if (media_info.has_segment_template_url()) {
535 "media", media_info.segment_template_url()));
539 if (!segment_infos.empty()) {
542 if (IsTimelineConstantDuration(segment_infos, start_number)) {
544 "duration", segment_infos.front().duration));
545 if (absl::GetFlag(FLAGS_dash_add_last_segment_number_when_needed)) {
546 uint32_t last_segment_number = start_number - 1;
547 for (
const auto& segment_info_element : segment_infos)
548 last_segment_number += segment_info_element.repeat + 1;
551 "http://dashif.org/guidelines/last-segment-number",
552 std::to_string(last_segment_number)));
555 if (!low_latency_dash_mode) {
556 XmlNode segment_timeline(
"SegmentTimeline");
557 RCHECK(PopulateSegmentTimeline(segment_infos, &segment_timeline));
558 RCHECK(segment_template.
AddChild(std::move(segment_timeline)));
562 return AddChild(std::move(segment_template));
565bool RepresentationXmlNode::AddAudioChannelInfo(
const AudioInfo& audio_info) {
566 std::string audio_channel_config_scheme;
567 std::string audio_channel_config_value;
569 if (audio_info.codec() == kEC3Codec) {
570 const auto& codec_data = audio_info.codec_specific_data();
574 const uint32_t ec3_channel_mpeg_value = codec_data.channel_mpeg_value();
575 const uint32_t NO_MAPPING = 0xFFFFFFFF;
576 if (ec3_channel_mpeg_value == NO_MAPPING) {
579 audio_channel_config_value =
580 absl::StrFormat(
"%04X", codec_data.channel_mask());
581 audio_channel_config_scheme =
582 "tag:dolby.com,2014:dash:audio_channel_configuration:2011";
587 audio_channel_config_value =
588 absl::StrFormat(
"%u", ec3_channel_mpeg_value);
589 audio_channel_config_scheme =
"urn:mpeg:mpegB:cicp:ChannelConfiguration";
592 AddDescriptor(
"AudioChannelConfiguration", audio_channel_config_scheme,
593 audio_channel_config_value);
597 if (codec_data.ec3_joc_complexity() != 0) {
598 std::string ec3_joc_complexity =
599 absl::StrFormat(
"%u", codec_data.ec3_joc_complexity());
601 "tag:dolby.com,2018:dash:EC3_ExtensionType:2018",
604 "tag:dolby.com,2018:dash:"
605 "EC3_ExtensionComplexityIndex:2018",
609 }
else if (audio_info.codec().substr(0, 4) == kAC4Codec) {
610 const auto& codec_data = audio_info.codec_specific_data();
611 const bool ac4_ims_flag = codec_data.ac4_ims_flag();
615 const uint32_t ac4_channel_mpeg_value = codec_data.channel_mpeg_value();
616 const uint32_t NO_MAPPING = 0xFFFFFFFF;
617 if (ac4_channel_mpeg_value == NO_MAPPING) {
623 audio_channel_config_value =
624 absl::StrFormat(
"%06X", codec_data.channel_mask());
627 audio_channel_config_scheme =
628 "tag:dolby.com,2015:dash:audio_channel_configuration:2015";
633 audio_channel_config_value =
634 absl::StrFormat(
"%u", ac4_channel_mpeg_value);
635 audio_channel_config_scheme =
"urn:mpeg:mpegB:cicp:ChannelConfiguration";
638 AddDescriptor(
"AudioChannelConfiguration", audio_channel_config_scheme,
639 audio_channel_config_value);
642 "tag:dolby.com,2016:dash:virtualized_content:2016",
646 }
else if (audio_info.codec() == kDTSCCodec ||
647 audio_info.codec() == kDTSECodec) {
648 audio_channel_config_value =
649 absl::StrFormat(
"%u", audio_info.num_channels());
650 audio_channel_config_scheme =
651 "tag:dts.com,2014:dash:audio_channel_configuration:2012";
652 }
else if (audio_info.codec() == kDTSXCodec) {
653 const auto& codec_data = audio_info.codec_specific_data();
654 audio_channel_config_value =
655 absl::StrFormat(
"%08X", codec_data.channel_mask());
656 audio_channel_config_scheme =
657 "tag:dts.com,2018:uhd:audio_channel_configuration";
659 audio_channel_config_value =
660 absl::StrFormat(
"%u", audio_info.num_channels());
661 audio_channel_config_scheme =
662 "urn:mpeg:dash:23003:3:audio_channel_configuration:2011";
665 return AddDescriptor(
"AudioChannelConfiguration", audio_channel_config_scheme,
666 audio_channel_config_value);
671bool RepresentationXmlNode::AddAudioSamplingRateInfo(
672 const AudioInfo& audio_info) {
673 return !audio_info.has_sampling_frequency() ||
675 audio_info.sampling_frequency());
bool AddAccessibilityElement(const std::string &scheme_id_uri, const std::string &value)
bool AddLabelElement(const std::string &value)
bool AddRoleElement(const std::string &scheme_id_uri, const std::string &value)
bool AddDescriptor(const std::string &descriptor_name, const std::string &scheme_id_uri, const std::string &value)
bool AddEssentialProperty(const std::string &scheme_id_uri, const std::string &value)
bool AddSupplementalProperty(const std::string &scheme_id_uri, const std::string &value)
bool AddVODOnlyInfo(const MediaInfo &media_info, bool use_segment_list, double target_segment_duration)
bool AddLiveOnlyInfo(const MediaInfo &media_info, const std::list< SegmentInfo > &segment_infos, bool low_latency_dash_mode)
bool AddAudioInfo(const MediaInfo::AudioInfo &audio_info)
bool AddVideoInfo(const MediaInfo::VideoInfo &video_info, bool set_width, bool set_height, bool set_frame_rate)
bool AddChild(XmlNode child)
bool AddElements(const std::vector< Element > &elements)
Adds Elements to this node using the Element struct.
std::set< std::string > ExtractReferencedNamespaces() const
void AddContent(const std::string &content)
Similar to SetContent, but appends to the end of existing content.
bool SetStringAttribute(const std::string &attribute_name, const std::string &attribute)
void SetContent(const std::string &content)
XmlNode(const std::string &name)
bool SetIntegerAttribute(const std::string &attribute_name, uint64_t number)
bool GetAttribute(const std::string &name, std::string *value) const
std::string ToString(const std::string &comment) const
bool SetFloatingPointAttribute(const std::string &attribute_name, double number)
All the methods that are virtual are virtual for mocking.