2023-12-01 17:32:19 +00:00
|
|
|
// Copyright 2014 Google LLC. All rights reserved.
|
2014-02-14 23:21:05 +00:00
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <packager/mpd/base/xml/xml_node.h>
|
2017-09-25 19:18:50 +00:00
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <cinttypes>
|
2021-05-25 19:08:58 +00:00
|
|
|
#include <cmath>
|
2014-09-30 23:52:58 +00:00
|
|
|
#include <limits>
|
2013-11-18 21:01:48 +00:00
|
|
|
#include <set>
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <absl/base/internal/endian.h>
|
|
|
|
#include <absl/flags/flag.h>
|
|
|
|
#include <absl/log/check.h>
|
|
|
|
#include <absl/log/log.h>
|
|
|
|
#include <absl/strings/escaping.h>
|
|
|
|
#include <absl/strings/numbers.h>
|
|
|
|
#include <absl/strings/str_format.h>
|
|
|
|
#include <libxml/tree.h>
|
|
|
|
|
|
|
|
#include <packager/macros/compiler.h>
|
|
|
|
#include <packager/media/base/rcheck.h>
|
|
|
|
#include <packager/mpd/base/media_info.pb.h>
|
|
|
|
#include <packager/mpd/base/mpd_utils.h>
|
|
|
|
#include <packager/mpd/base/segment_info.h>
|
|
|
|
#include <packager/mpd/base/xml/scoped_xml_ptr.h>
|
|
|
|
|
|
|
|
ABSL_FLAG(bool,
|
|
|
|
segment_template_constant_duration,
|
|
|
|
false,
|
|
|
|
"Generates SegmentTemplate@duration if all segments except the "
|
|
|
|
"last one has the same duration if this flag is set to true.");
|
|
|
|
|
|
|
|
ABSL_FLAG(bool,
|
|
|
|
dash_add_last_segment_number_when_needed,
|
|
|
|
false,
|
|
|
|
"Adds a Supplemental Descriptor with @schemeIdUri "
|
|
|
|
"set to http://dashif.org/guidelines/last-segment-number with "
|
|
|
|
"the @value set to the last segment number.");
|
2020-02-25 07:12:53 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2014-05-22 02:16:17 +00:00
|
|
|
|
2016-05-19 21:38:15 +00:00
|
|
|
using xml::XmlNode;
|
|
|
|
typedef MediaInfo::AudioInfo AudioInfo;
|
|
|
|
typedef MediaInfo::VideoInfo VideoInfo;
|
|
|
|
|
2013-11-18 21:01:48 +00:00
|
|
|
namespace {
|
2016-02-09 00:22:01 +00:00
|
|
|
const char kEC3Codec[] = "ec-3";
|
2020-07-04 21:55:28 +00:00
|
|
|
const char kAC4Codec[] = "ac-4";
|
2024-02-15 07:03:03 +00:00
|
|
|
const char kDTSCCodec[] = "dtsc";
|
|
|
|
const char kDTSECodec[] = "dtse";
|
|
|
|
const char kDTSXCodec[] = "dtsx";
|
2013-11-18 21:01:48 +00:00
|
|
|
|
2014-09-19 20:41:13 +00:00
|
|
|
std::string RangeToString(const Range& range) {
|
2023-12-01 17:32:19 +00:00
|
|
|
return absl::StrFormat("%u-%u", range.begin(), range.end());
|
2013-11-18 21:01:48 +00:00
|
|
|
}
|
|
|
|
|
2017-09-25 19:18:50 +00:00
|
|
|
// Check if segments are continuous and all segments except the last one are of
|
|
|
|
// the same duration.
|
|
|
|
bool IsTimelineConstantDuration(const std::list<SegmentInfo>& segment_infos,
|
|
|
|
uint32_t start_number) {
|
2023-12-01 17:32:19 +00:00
|
|
|
if (!absl::GetFlag(FLAGS_segment_template_constant_duration))
|
2017-09-25 19:18:50 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
DCHECK(!segment_infos.empty());
|
|
|
|
if (segment_infos.size() > 2)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const SegmentInfo& first_segment = segment_infos.front();
|
|
|
|
if (first_segment.start_time != first_segment.duration * (start_number - 1))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (segment_infos.size() == 1)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
const SegmentInfo& last_segment = segment_infos.back();
|
|
|
|
if (last_segment.repeat != 0)
|
|
|
|
return false;
|
|
|
|
|
2018-06-22 01:14:34 +00:00
|
|
|
const int64_t expected_last_segment_start_time =
|
2017-09-25 19:18:50 +00:00
|
|
|
first_segment.start_time +
|
|
|
|
first_segment.duration * (first_segment.repeat + 1);
|
|
|
|
return expected_last_segment_start_time == last_segment.start_time;
|
|
|
|
}
|
|
|
|
|
2014-05-22 02:16:17 +00:00
|
|
|
bool PopulateSegmentTimeline(const std::list<SegmentInfo>& segment_infos,
|
|
|
|
XmlNode* segment_timeline) {
|
2017-09-25 19:18:50 +00:00
|
|
|
for (const SegmentInfo& segment_info : segment_infos) {
|
2014-10-03 05:17:54 +00:00
|
|
|
XmlNode s_element("S");
|
2020-11-10 00:32:58 +00:00
|
|
|
RCHECK(s_element.SetIntegerAttribute("t", segment_info.start_time));
|
|
|
|
RCHECK(s_element.SetIntegerAttribute("d", segment_info.duration));
|
2017-09-25 19:18:50 +00:00
|
|
|
if (segment_info.repeat > 0)
|
2020-11-10 00:32:58 +00:00
|
|
|
RCHECK(s_element.SetIntegerAttribute("r", segment_info.repeat));
|
2014-05-22 02:16:17 +00:00
|
|
|
|
2020-11-10 00:32:58 +00:00
|
|
|
RCHECK(segment_timeline->AddChild(std::move(s_element)));
|
2014-05-22 02:16:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-18 19:05:11 +00:00
|
|
|
void CollectNamespaceFromName(const std::string& name,
|
|
|
|
std::set<std::string>* namespaces) {
|
|
|
|
const size_t pos = name.find(':');
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
namespaces->insert(name.substr(0, pos));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TraverseAttrsAndCollectNamespaces(const xmlAttr* attr,
|
|
|
|
std::set<std::string>* namespaces) {
|
|
|
|
for (const xmlAttr* cur_attr = attr; cur_attr; cur_attr = cur_attr->next) {
|
|
|
|
CollectNamespaceFromName(reinterpret_cast<const char*>(cur_attr->name),
|
|
|
|
namespaces);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TraverseNodesAndCollectNamespaces(const xmlNode* node,
|
|
|
|
std::set<std::string>* namespaces) {
|
|
|
|
for (const xmlNode* cur_node = node; cur_node; cur_node = cur_node->next) {
|
|
|
|
CollectNamespaceFromName(reinterpret_cast<const char*>(cur_node->name),
|
|
|
|
namespaces);
|
|
|
|
|
|
|
|
TraverseNodesAndCollectNamespaces(cur_node->children, namespaces);
|
|
|
|
TraverseAttrsAndCollectNamespaces(cur_node->properties, namespaces);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-18 21:01:48 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace xml {
|
|
|
|
|
2020-11-10 00:32:58 +00:00
|
|
|
class XmlNode::Impl {
|
|
|
|
public:
|
|
|
|
scoped_xml_ptr<xmlNode> node;
|
|
|
|
};
|
|
|
|
|
|
|
|
XmlNode::XmlNode(const std::string& name) : impl_(new Impl) {
|
|
|
|
impl_->node.reset(xmlNewNode(NULL, BAD_CAST name.c_str()));
|
|
|
|
DCHECK(impl_->node);
|
2013-11-18 21:01:48 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 00:32:58 +00:00
|
|
|
XmlNode::XmlNode(XmlNode&&) = default;
|
|
|
|
|
2013-11-18 21:01:48 +00:00
|
|
|
XmlNode::~XmlNode() {}
|
|
|
|
|
2020-11-10 00:32:58 +00:00
|
|
|
XmlNode& XmlNode::operator=(XmlNode&&) = default;
|
2013-11-18 21:01:48 +00:00
|
|
|
|
2020-11-10 00:32:58 +00:00
|
|
|
bool XmlNode::AddChild(XmlNode child) {
|
|
|
|
DCHECK(impl_->node);
|
|
|
|
DCHECK(child.impl_->node);
|
|
|
|
RCHECK(xmlAddChild(impl_->node.get(), child.impl_->node.get()));
|
|
|
|
|
|
|
|
// Reaching here means the ownership of |child| transfered to |node|.
|
2013-11-18 21:01:48 +00:00
|
|
|
// Release the pointer so that it doesn't get destructed in this scope.
|
2023-12-01 17:32:19 +00:00
|
|
|
UNUSED(child.impl_->node.release());
|
2013-11-18 21:01:48 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-15 21:57:47 +00:00
|
|
|
bool XmlNode::AddElements(const std::vector<Element>& elements) {
|
|
|
|
for (size_t element_index = 0; element_index < elements.size();
|
|
|
|
++element_index) {
|
|
|
|
const Element& child_element = elements[element_index];
|
2020-11-10 00:32:58 +00:00
|
|
|
XmlNode child_node(child_element.name);
|
2015-07-15 21:57:47 +00:00
|
|
|
for (std::map<std::string, std::string>::const_iterator attribute_it =
|
|
|
|
child_element.attributes.begin();
|
|
|
|
attribute_it != child_element.attributes.end(); ++attribute_it) {
|
2020-11-10 00:32:58 +00:00
|
|
|
RCHECK(child_node.SetStringAttribute(attribute_it->first,
|
|
|
|
attribute_it->second));
|
2015-07-15 21:57:47 +00:00
|
|
|
}
|
2018-09-18 00:27:02 +00:00
|
|
|
|
|
|
|
// Note that somehow |SetContent| needs to be called before |AddElements|
|
|
|
|
// otherwise the added children will be overwritten by the content.
|
|
|
|
child_node.SetContent(child_element.content);
|
|
|
|
|
2015-07-15 21:57:47 +00:00
|
|
|
// Recursively set children for the child.
|
2020-11-10 00:32:58 +00:00
|
|
|
RCHECK(child_node.AddElements(child_element.subelements));
|
2015-07-15 21:57:47 +00:00
|
|
|
|
2020-11-10 00:32:58 +00:00
|
|
|
if (!xmlAddChild(impl_->node.get(), child_node.impl_->node.get())) {
|
2015-07-15 21:57:47 +00:00
|
|
|
LOG(ERROR) << "Failed to set child " << child_element.name
|
|
|
|
<< " to parent element "
|
2020-11-10 00:32:58 +00:00
|
|
|
<< reinterpret_cast<const char*>(impl_->node->name);
|
2015-07-15 21:57:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-11-10 00:32:58 +00:00
|
|
|
// Reaching here means the ownership of |child_node| transfered to |node|.
|
2015-07-15 21:57:47 +00:00
|
|
|
// Release the pointer so that it doesn't get destructed in this scope.
|
2020-11-10 00:32:58 +00:00
|
|
|
child_node.impl_->node.release();
|
2015-07-15 21:57:47 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-11-10 00:32:58 +00:00
|
|
|
bool XmlNode::SetStringAttribute(const std::string& attribute_name,
|
2013-11-18 21:01:48 +00:00
|
|
|
const std::string& attribute) {
|
2020-11-10 00:32:58 +00:00
|
|
|
DCHECK(impl_->node);
|
|
|
|
return xmlSetProp(impl_->node.get(), BAD_CAST attribute_name.c_str(),
|
|
|
|
BAD_CAST attribute.c_str()) != nullptr;
|
2013-11-18 21:01:48 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 00:32:58 +00:00
|
|
|
bool XmlNode::SetIntegerAttribute(const std::string& attribute_name,
|
|
|
|
uint64_t number) {
|
|
|
|
DCHECK(impl_->node);
|
|
|
|
return xmlSetProp(impl_->node.get(), BAD_CAST attribute_name.c_str(),
|
2023-12-01 17:32:19 +00:00
|
|
|
BAD_CAST(absl::StrFormat("%" PRIu64, number).c_str())) !=
|
|
|
|
nullptr;
|
2013-11-18 21:01:48 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 00:32:58 +00:00
|
|
|
bool XmlNode::SetFloatingPointAttribute(const std::string& attribute_name,
|
2013-12-17 23:08:14 +00:00
|
|
|
double number) {
|
2020-11-10 00:32:58 +00:00
|
|
|
DCHECK(impl_->node);
|
|
|
|
return xmlSetProp(impl_->node.get(), BAD_CAST attribute_name.c_str(),
|
2023-12-01 17:32:19 +00:00
|
|
|
BAD_CAST(FloatToXmlString(number).c_str())) != nullptr;
|
2013-12-17 23:08:14 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 00:32:58 +00:00
|
|
|
bool XmlNode::SetId(uint32_t id) {
|
|
|
|
return SetIntegerAttribute("id", id);
|
2013-11-18 21:01:48 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 21:46:37 +00:00
|
|
|
void XmlNode::AddContent(const std::string& content) {
|
|
|
|
DCHECK(impl_->node);
|
|
|
|
xmlNodeAddContent(impl_->node.get(), BAD_CAST content.c_str());
|
|
|
|
}
|
|
|
|
|
2013-11-18 21:01:48 +00:00
|
|
|
void XmlNode::SetContent(const std::string& content) {
|
2020-11-10 00:32:58 +00:00
|
|
|
DCHECK(impl_->node);
|
|
|
|
xmlNodeSetContent(impl_->node.get(), BAD_CAST content.c_str());
|
2013-11-18 21:01:48 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 00:32:58 +00:00
|
|
|
std::set<std::string> XmlNode::ExtractReferencedNamespaces() const {
|
2018-09-18 19:05:11 +00:00
|
|
|
std::set<std::string> namespaces;
|
2020-11-10 00:32:58 +00:00
|
|
|
TraverseNodesAndCollectNamespaces(impl_->node.get(), &namespaces);
|
2018-09-18 19:05:11 +00:00
|
|
|
return namespaces;
|
|
|
|
}
|
|
|
|
|
2020-11-10 00:32:58 +00:00
|
|
|
std::string XmlNode::ToString(const std::string& comment) const {
|
|
|
|
// Create an xmlDoc from xmlNodePtr. The node is copied so ownership does not
|
|
|
|
// transfer.
|
|
|
|
xml::scoped_xml_ptr<xmlDoc> doc(xmlNewDoc(BAD_CAST "1.0"));
|
|
|
|
if (comment.empty()) {
|
|
|
|
xmlDocSetRootElement(doc.get(), xmlCopyNode(impl_->node.get(), true));
|
|
|
|
} else {
|
|
|
|
xml::scoped_xml_ptr<xmlNode> comment_xml(
|
|
|
|
xmlNewDocComment(doc.get(), BAD_CAST comment.c_str()));
|
|
|
|
xmlDocSetRootElement(doc.get(), comment_xml.get());
|
|
|
|
xmlAddSibling(comment_xml.release(), xmlCopyNode(impl_->node.get(), true));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Format the xmlDoc to string.
|
|
|
|
static const int kNiceFormat = 1;
|
|
|
|
int doc_str_size = 0;
|
|
|
|
xmlChar* doc_str = nullptr;
|
|
|
|
xmlDocDumpFormatMemoryEnc(doc.get(), &doc_str, &doc_str_size, "UTF-8",
|
|
|
|
kNiceFormat);
|
|
|
|
std::string output(doc_str, doc_str + doc_str_size);
|
|
|
|
xmlFree(doc_str);
|
|
|
|
return output;
|
2013-11-18 21:01:48 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 00:32:58 +00:00
|
|
|
bool XmlNode::GetAttribute(const std::string& name, std::string* value) const {
|
|
|
|
xml::scoped_xml_ptr<xmlChar> str(
|
|
|
|
xmlGetProp(impl_->node.get(), BAD_CAST name.c_str()));
|
|
|
|
if (!str)
|
|
|
|
return false;
|
|
|
|
*value = reinterpret_cast<const char*>(str.get());
|
|
|
|
return true;
|
2013-11-18 21:01:48 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 00:32:58 +00:00
|
|
|
xmlNode* XmlNode::GetRawPtr() const {
|
|
|
|
return impl_->node.get();
|
2013-11-18 21:01:48 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 00:32:58 +00:00
|
|
|
RepresentationBaseXmlNode::RepresentationBaseXmlNode(const std::string& name)
|
2013-11-18 21:01:48 +00:00
|
|
|
: XmlNode(name) {}
|
|
|
|
RepresentationBaseXmlNode::~RepresentationBaseXmlNode() {}
|
|
|
|
|
|
|
|
bool RepresentationBaseXmlNode::AddContentProtectionElements(
|
|
|
|
const std::list<ContentProtectionElement>& content_protection_elements) {
|
2020-11-10 00:32:58 +00:00
|
|
|
for (const auto& elem : content_protection_elements) {
|
|
|
|
RCHECK(AddContentProtectionElement(elem));
|
2013-11-18 21:01:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-11-10 00:32:58 +00:00
|
|
|
bool RepresentationBaseXmlNode::AddSupplementalProperty(
|
2016-12-13 23:48:54 +00:00
|
|
|
const std::string& scheme_id_uri,
|
|
|
|
const std::string& value) {
|
2020-11-10 00:32:58 +00:00
|
|
|
return AddDescriptor("SupplementalProperty", scheme_id_uri, value);
|
2016-12-13 23:48:54 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 00:32:58 +00:00
|
|
|
bool RepresentationBaseXmlNode::AddEssentialProperty(
|
2017-03-21 23:14:46 +00:00
|
|
|
const std::string& scheme_id_uri,
|
|
|
|
const std::string& value) {
|
2020-11-10 00:32:58 +00:00
|
|
|
return AddDescriptor("EssentialProperty", scheme_id_uri, value);
|
2019-06-25 01:02:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RepresentationBaseXmlNode::AddDescriptor(
|
|
|
|
const std::string& descriptor_name,
|
|
|
|
const std::string& scheme_id_uri,
|
|
|
|
const std::string& value) {
|
2020-11-10 00:32:58 +00:00
|
|
|
XmlNode descriptor(descriptor_name);
|
|
|
|
RCHECK(descriptor.SetStringAttribute("schemeIdUri", scheme_id_uri));
|
2019-06-25 01:02:19 +00:00
|
|
|
if (!value.empty())
|
2020-11-10 00:32:58 +00:00
|
|
|
RCHECK(descriptor.SetStringAttribute("value", value));
|
|
|
|
return AddChild(std::move(descriptor));
|
2017-03-21 23:14:46 +00:00
|
|
|
}
|
|
|
|
|
2013-11-18 21:01:48 +00:00
|
|
|
bool RepresentationBaseXmlNode::AddContentProtectionElement(
|
|
|
|
const ContentProtectionElement& content_protection_element) {
|
|
|
|
XmlNode content_protection_node("ContentProtection");
|
|
|
|
|
2015-07-15 21:57:47 +00:00
|
|
|
// @value is an optional attribute.
|
|
|
|
if (!content_protection_element.value.empty()) {
|
2020-11-10 00:32:58 +00:00
|
|
|
RCHECK(content_protection_node.SetStringAttribute(
|
|
|
|
"value", content_protection_element.value));
|
2013-11-18 21:01:48 +00:00
|
|
|
}
|
2020-11-10 00:32:58 +00:00
|
|
|
RCHECK(content_protection_node.SetStringAttribute(
|
|
|
|
"schemeIdUri", content_protection_element.scheme_id_uri));
|
2013-11-18 21:01:48 +00:00
|
|
|
|
2020-11-10 00:32:58 +00:00
|
|
|
for (const auto& pair : content_protection_element.additional_attributes) {
|
|
|
|
RCHECK(content_protection_node.SetStringAttribute(pair.first, pair.second));
|
2015-07-15 21:57:47 +00:00
|
|
|
}
|
2020-11-10 00:32:58 +00:00
|
|
|
|
|
|
|
RCHECK(content_protection_node.AddElements(
|
|
|
|
content_protection_element.subelements));
|
|
|
|
return AddChild(std::move(content_protection_node));
|
2013-11-18 21:01:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AdaptationSetXmlNode::AdaptationSetXmlNode()
|
|
|
|
: RepresentationBaseXmlNode("AdaptationSet") {}
|
|
|
|
AdaptationSetXmlNode::~AdaptationSetXmlNode() {}
|
|
|
|
|
2020-11-10 00:32:58 +00:00
|
|
|
bool AdaptationSetXmlNode::AddAccessibilityElement(
|
2019-06-13 06:01:16 +00:00
|
|
|
const std::string& scheme_id_uri,
|
|
|
|
const std::string& value) {
|
2020-11-10 00:32:58 +00:00
|
|
|
return AddDescriptor("Accessibility", scheme_id_uri, value);
|
2019-06-13 06:01:16 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 00:32:58 +00:00
|
|
|
bool AdaptationSetXmlNode::AddRoleElement(const std::string& scheme_id_uri,
|
2015-06-26 23:00:41 +00:00
|
|
|
const std::string& value) {
|
2020-11-10 00:32:58 +00:00
|
|
|
return AddDescriptor("Role", scheme_id_uri, value);
|
2015-06-26 23:00:41 +00:00
|
|
|
}
|
|
|
|
|
2024-02-14 18:36:08 +00:00
|
|
|
bool AdaptationSetXmlNode::AddLabelElement(const std::string& value) {
|
|
|
|
XmlNode descriptor("Label");
|
|
|
|
descriptor.SetContent(value);
|
|
|
|
return AddChild(std::move(descriptor));
|
|
|
|
}
|
|
|
|
|
2013-11-18 21:01:48 +00:00
|
|
|
RepresentationXmlNode::RepresentationXmlNode()
|
|
|
|
: RepresentationBaseXmlNode("Representation") {}
|
|
|
|
RepresentationXmlNode::~RepresentationXmlNode() {}
|
|
|
|
|
2016-01-05 00:33:53 +00:00
|
|
|
bool RepresentationXmlNode::AddVideoInfo(const VideoInfo& video_info,
|
|
|
|
bool set_width,
|
|
|
|
bool set_height,
|
|
|
|
bool set_frame_rate) {
|
2015-06-09 23:58:32 +00:00
|
|
|
if (!video_info.has_width() || !video_info.has_height()) {
|
|
|
|
LOG(ERROR) << "Missing width or height for adding a video info.";
|
|
|
|
return false;
|
2013-11-18 21:01:48 +00:00
|
|
|
}
|
|
|
|
|
2015-06-30 00:21:05 +00:00
|
|
|
if (video_info.has_pixel_width() && video_info.has_pixel_height()) {
|
2023-12-01 17:32:19 +00:00
|
|
|
RCHECK(SetStringAttribute("sar",
|
|
|
|
absl::StrFormat("%d:%d", video_info.pixel_width(),
|
|
|
|
video_info.pixel_height())));
|
2015-06-30 00:21:05 +00:00
|
|
|
}
|
|
|
|
|
2016-01-05 00:33:53 +00:00
|
|
|
if (set_width)
|
2020-11-10 00:32:58 +00:00
|
|
|
RCHECK(SetIntegerAttribute("width", video_info.width()));
|
2016-01-05 00:33:53 +00:00
|
|
|
if (set_height)
|
2020-11-10 00:32:58 +00:00
|
|
|
RCHECK(SetIntegerAttribute("height", video_info.height()));
|
2016-01-05 00:33:53 +00:00
|
|
|
if (set_frame_rate) {
|
2023-12-01 17:32:19 +00:00
|
|
|
RCHECK(SetStringAttribute("frameRate",
|
|
|
|
absl::StrFormat("%d/%d", video_info.time_scale(),
|
|
|
|
video_info.frame_duration())));
|
2016-01-05 00:33:53 +00:00
|
|
|
}
|
2017-03-21 23:14:46 +00:00
|
|
|
|
|
|
|
if (video_info.has_playback_rate()) {
|
2023-12-01 17:32:19 +00:00
|
|
|
RCHECK(SetStringAttribute(
|
|
|
|
"maxPlayoutRate", absl::StrFormat("%d", video_info.playback_rate())));
|
2017-03-21 23:14:46 +00:00
|
|
|
// Since the trick play stream contains only key frames, there is no coding
|
|
|
|
// dependency on the main stream. Simply set the codingDependency to false.
|
|
|
|
// TODO(hmchen): propagate this attribute up to the AdaptationSet, since
|
|
|
|
// all are set to false.
|
2020-11-10 00:32:58 +00:00
|
|
|
RCHECK(SetStringAttribute("codingDependency", "false"));
|
2017-03-21 23:14:46 +00:00
|
|
|
}
|
2013-11-18 21:01:48 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-09 23:58:32 +00:00
|
|
|
bool RepresentationXmlNode::AddAudioInfo(const AudioInfo& audio_info) {
|
2020-11-10 00:32:58 +00:00
|
|
|
return AddAudioChannelInfo(audio_info) &&
|
|
|
|
AddAudioSamplingRateInfo(audio_info);
|
2013-11-18 21:01:48 +00:00
|
|
|
}
|
|
|
|
|
2021-05-25 19:08:58 +00:00
|
|
|
bool RepresentationXmlNode::AddVODOnlyInfo(const MediaInfo& media_info,
|
|
|
|
bool use_segment_list,
|
|
|
|
double target_segment_duration) {
|
|
|
|
const bool use_single_segment_url_with_media =
|
2020-12-03 21:49:09 +00:00
|
|
|
media_info.has_text_info() && media_info.has_presentation_time_offset();
|
|
|
|
|
2021-05-25 19:08:58 +00:00
|
|
|
if (media_info.has_media_file_url() && !use_single_segment_url_with_media) {
|
2014-01-29 00:03:19 +00:00
|
|
|
XmlNode base_url("BaseURL");
|
2018-04-17 17:42:45 +00:00
|
|
|
base_url.SetContent(media_info.media_file_url());
|
2014-01-29 00:03:19 +00:00
|
|
|
|
2020-11-10 00:32:58 +00:00
|
|
|
RCHECK(AddChild(std::move(base_url)));
|
2014-01-29 00:03:19 +00:00
|
|
|
}
|
|
|
|
|
2021-05-25 19:08:58 +00:00
|
|
|
const bool need_segment_base_or_list =
|
|
|
|
use_segment_list || media_info.has_index_range() ||
|
|
|
|
media_info.has_init_range() ||
|
|
|
|
(media_info.has_reference_time_scale() && !media_info.has_text_info()) ||
|
|
|
|
use_single_segment_url_with_media;
|
2013-11-18 21:01:48 +00:00
|
|
|
|
2021-05-25 19:08:58 +00:00
|
|
|
if (!need_segment_base_or_list) {
|
|
|
|
return true;
|
|
|
|
}
|
2013-11-18 21:01:48 +00:00
|
|
|
|
2021-05-25 19:08:58 +00:00
|
|
|
XmlNode child(use_segment_list || use_single_segment_url_with_media
|
|
|
|
? "SegmentList"
|
|
|
|
: "SegmentBase");
|
2013-11-18 21:01:48 +00:00
|
|
|
|
2021-05-25 19:08:58 +00:00
|
|
|
// Forcing SegmentList for longer audio causes sidx atom to not be
|
|
|
|
// generated, therefore indexRange is not added to MPD if flag is set.
|
|
|
|
if (media_info.has_index_range() && !use_segment_list) {
|
|
|
|
RCHECK(child.SetStringAttribute("indexRange",
|
|
|
|
RangeToString(media_info.index_range())));
|
|
|
|
}
|
2018-01-03 00:10:54 +00:00
|
|
|
|
2021-05-25 19:08:58 +00:00
|
|
|
if (media_info.has_reference_time_scale()) {
|
|
|
|
RCHECK(child.SetIntegerAttribute("timescale",
|
|
|
|
media_info.reference_time_scale()));
|
2013-11-18 21:01:48 +00:00
|
|
|
|
2021-05-25 19:08:58 +00:00
|
|
|
if (use_segment_list && !use_single_segment_url_with_media) {
|
2023-12-01 17:32:19 +00:00
|
|
|
const auto duration_seconds = static_cast<int64_t>(
|
2021-05-25 19:08:58 +00:00
|
|
|
floor(target_segment_duration * media_info.reference_time_scale()));
|
|
|
|
RCHECK(child.SetIntegerAttribute("duration", duration_seconds));
|
2020-12-03 21:49:09 +00:00
|
|
|
}
|
2021-05-25 19:08:58 +00:00
|
|
|
}
|
2020-12-03 21:49:09 +00:00
|
|
|
|
2021-05-25 19:08:58 +00:00
|
|
|
if (media_info.has_presentation_time_offset()) {
|
|
|
|
RCHECK(child.SetIntegerAttribute("presentationTimeOffset",
|
|
|
|
media_info.presentation_time_offset()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (media_info.has_init_range()) {
|
|
|
|
XmlNode initialization("Initialization");
|
|
|
|
RCHECK(initialization.SetStringAttribute(
|
|
|
|
"range", RangeToString(media_info.init_range())));
|
|
|
|
|
|
|
|
RCHECK(child.AddChild(std::move(initialization)));
|
|
|
|
}
|
2013-11-18 21:01:48 +00:00
|
|
|
|
2021-05-25 19:08:58 +00:00
|
|
|
if (use_single_segment_url_with_media) {
|
|
|
|
XmlNode media_url("SegmentURL");
|
|
|
|
RCHECK(media_url.SetStringAttribute("media", media_info.media_file_url()));
|
|
|
|
RCHECK(child.AddChild(std::move(media_url)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since the SegmentURLs here do not have a @media element,
|
|
|
|
// BaseURL element is mapped to the @media attribute.
|
|
|
|
if (use_segment_list) {
|
|
|
|
for (const Range& subsegment_range : media_info.subsegment_ranges()) {
|
|
|
|
XmlNode subsegment("SegmentURL");
|
|
|
|
RCHECK(subsegment.SetStringAttribute("mediaRange",
|
|
|
|
RangeToString(subsegment_range)));
|
|
|
|
|
|
|
|
RCHECK(child.AddChild(std::move(subsegment)));
|
|
|
|
}
|
2013-11-18 21:01:48 +00:00
|
|
|
}
|
|
|
|
|
2021-05-25 19:08:58 +00:00
|
|
|
RCHECK(AddChild(std::move(child)));
|
2013-11-18 21:01:48 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-22 02:16:17 +00:00
|
|
|
bool RepresentationXmlNode::AddLiveOnlyInfo(
|
|
|
|
const MediaInfo& media_info,
|
2014-05-27 22:21:42 +00:00
|
|
|
const std::list<SegmentInfo>& segment_infos,
|
2021-08-25 15:38:05 +00:00
|
|
|
uint32_t start_number,
|
|
|
|
bool low_latency_dash_mode) {
|
2014-05-22 02:16:17 +00:00
|
|
|
XmlNode segment_template("SegmentTemplate");
|
|
|
|
if (media_info.has_reference_time_scale()) {
|
2020-11-10 00:32:58 +00:00
|
|
|
RCHECK(segment_template.SetIntegerAttribute(
|
|
|
|
"timescale", media_info.reference_time_scale()));
|
2014-05-22 02:16:17 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 15:38:05 +00:00
|
|
|
if (media_info.has_segment_duration()) {
|
|
|
|
RCHECK(segment_template.SetIntegerAttribute("duration",
|
|
|
|
media_info.segment_duration()));
|
|
|
|
}
|
|
|
|
|
2018-01-03 00:10:54 +00:00
|
|
|
if (media_info.has_presentation_time_offset()) {
|
2020-11-10 00:32:58 +00:00
|
|
|
RCHECK(segment_template.SetIntegerAttribute(
|
|
|
|
"presentationTimeOffset", media_info.presentation_time_offset()));
|
2018-01-03 00:10:54 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 15:38:05 +00:00
|
|
|
if (media_info.has_availability_time_offset()) {
|
|
|
|
RCHECK(segment_template.SetFloatingPointAttribute(
|
|
|
|
"availabilityTimeOffset", media_info.availability_time_offset()));
|
|
|
|
}
|
|
|
|
|
2023-07-05 21:33:51 +00:00
|
|
|
if (low_latency_dash_mode) {
|
|
|
|
RCHECK(segment_template.SetStringAttribute("availabilityTimeComplete",
|
|
|
|
"false"));
|
|
|
|
}
|
|
|
|
|
2018-04-17 17:42:45 +00:00
|
|
|
if (media_info.has_init_segment_url()) {
|
2020-11-10 00:32:58 +00:00
|
|
|
RCHECK(segment_template.SetStringAttribute("initialization",
|
|
|
|
media_info.init_segment_url()));
|
2014-05-22 02:16:17 +00:00
|
|
|
}
|
|
|
|
|
2018-04-17 17:42:45 +00:00
|
|
|
if (media_info.has_segment_template_url()) {
|
2020-11-10 00:32:58 +00:00
|
|
|
RCHECK(segment_template.SetStringAttribute(
|
|
|
|
"media", media_info.segment_template_url()));
|
|
|
|
RCHECK(segment_template.SetIntegerAttribute("startNumber", start_number));
|
2014-05-27 22:21:42 +00:00
|
|
|
}
|
|
|
|
|
2017-09-25 19:18:50 +00:00
|
|
|
if (!segment_infos.empty()) {
|
|
|
|
// Don't use SegmentTimeline if all segments except the last one are of
|
|
|
|
// the same duration.
|
|
|
|
if (IsTimelineConstantDuration(segment_infos, start_number)) {
|
2020-11-10 00:32:58 +00:00
|
|
|
RCHECK(segment_template.SetIntegerAttribute(
|
|
|
|
"duration", segment_infos.front().duration));
|
2023-12-01 17:32:19 +00:00
|
|
|
if (absl::GetFlag(FLAGS_dash_add_last_segment_number_when_needed)) {
|
2020-02-25 07:12:53 +00:00
|
|
|
uint32_t last_segment_number = start_number - 1;
|
2020-07-30 21:26:45 +00:00
|
|
|
for (const auto& segment_info_element : segment_infos)
|
2020-02-25 07:12:53 +00:00
|
|
|
last_segment_number += segment_info_element.repeat + 1;
|
2020-07-30 21:26:45 +00:00
|
|
|
|
2020-11-10 00:32:58 +00:00
|
|
|
RCHECK(AddSupplementalProperty(
|
2020-07-30 21:26:45 +00:00
|
|
|
"http://dashif.org/guidelines/last-segment-number",
|
2020-11-10 00:32:58 +00:00
|
|
|
std::to_string(last_segment_number)));
|
2020-02-25 07:12:53 +00:00
|
|
|
}
|
2017-09-25 19:18:50 +00:00
|
|
|
} else {
|
2021-08-25 15:38:05 +00:00
|
|
|
if (!low_latency_dash_mode) {
|
|
|
|
XmlNode segment_timeline("SegmentTimeline");
|
|
|
|
RCHECK(PopulateSegmentTimeline(segment_infos, &segment_timeline));
|
|
|
|
RCHECK(segment_template.AddChild(std::move(segment_timeline)));
|
|
|
|
}
|
2017-09-25 19:18:50 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-10 00:32:58 +00:00
|
|
|
return AddChild(std::move(segment_template));
|
2014-05-22 02:16:17 +00:00
|
|
|
}
|
|
|
|
|
2015-06-09 23:58:32 +00:00
|
|
|
bool RepresentationXmlNode::AddAudioChannelInfo(const AudioInfo& audio_info) {
|
2016-02-09 00:22:01 +00:00
|
|
|
std::string audio_channel_config_scheme;
|
|
|
|
std::string audio_channel_config_value;
|
|
|
|
|
|
|
|
if (audio_info.codec() == kEC3Codec) {
|
2020-06-04 05:02:49 +00:00
|
|
|
const auto& codec_data = audio_info.codec_specific_data();
|
|
|
|
// Use MPEG scheme if the mpeg value is available and valid, fallback to
|
|
|
|
// EC3 channel mapping otherwise.
|
|
|
|
// See https://github.com/Dash-Industry-Forum/DASH-IF-IOP/issues/268
|
2020-07-04 21:55:28 +00:00
|
|
|
const uint32_t ec3_channel_mpeg_value = codec_data.channel_mpeg_value();
|
2020-06-04 05:02:49 +00:00
|
|
|
const uint32_t NO_MAPPING = 0xFFFFFFFF;
|
|
|
|
if (ec3_channel_mpeg_value == NO_MAPPING) {
|
2023-12-01 17:32:19 +00:00
|
|
|
// Convert EC3 channel map into string of hexadecimal digits. Spec:
|
|
|
|
// DASH-IF Interoperability Points v3.0 9.2.1.2.
|
2020-06-04 05:02:49 +00:00
|
|
|
audio_channel_config_value =
|
2023-12-01 17:32:19 +00:00
|
|
|
absl::StrFormat("%04X", codec_data.channel_mask());
|
2020-06-04 05:02:49 +00:00
|
|
|
audio_channel_config_scheme =
|
2016-02-09 00:22:01 +00:00
|
|
|
"tag:dolby.com,2014:dash:audio_channel_configuration:2011";
|
2020-06-04 05:02:49 +00:00
|
|
|
} else {
|
|
|
|
// Calculate EC3 channel configuration descriptor value with MPEG scheme.
|
|
|
|
// Spec: ETSI TS 102 366 V1.4.1 Digital Audio Compression
|
|
|
|
// (AC-3, Enhanced AC-3) I.1.2.
|
2023-12-01 17:32:19 +00:00
|
|
|
audio_channel_config_value =
|
|
|
|
absl::StrFormat("%u", ec3_channel_mpeg_value);
|
2020-06-04 05:02:49 +00:00
|
|
|
audio_channel_config_scheme = "urn:mpeg:mpegB:cicp:ChannelConfiguration";
|
|
|
|
}
|
|
|
|
bool ret = AddDescriptor("AudioChannelConfiguration",
|
|
|
|
audio_channel_config_scheme,
|
|
|
|
audio_channel_config_value);
|
|
|
|
// Dolby Digital Plus JOC descriptor. Spec: ETSI TS 103 420 v1.2.1
|
|
|
|
// Backwards-compatible object audio carriage using Enhanced AC-3 Standard
|
|
|
|
// D.2.2.
|
|
|
|
if (codec_data.ec3_joc_complexity() != 0) {
|
|
|
|
std::string ec3_joc_complexity =
|
2023-12-01 17:32:19 +00:00
|
|
|
absl::StrFormat("%u", codec_data.ec3_joc_complexity());
|
2020-06-04 05:02:49 +00:00
|
|
|
ret &= AddDescriptor("SupplementalProperty",
|
|
|
|
"tag:dolby.com,2018:dash:EC3_ExtensionType:2018",
|
|
|
|
"JOC");
|
|
|
|
ret &= AddDescriptor("SupplementalProperty",
|
|
|
|
"tag:dolby.com,2018:dash:"
|
|
|
|
"EC3_ExtensionComplexityIndex:2018",
|
|
|
|
ec3_joc_complexity);
|
|
|
|
}
|
|
|
|
return ret;
|
2020-07-04 21:55:28 +00:00
|
|
|
} else if (audio_info.codec().substr(0, 4) == kAC4Codec) {
|
|
|
|
const auto& codec_data = audio_info.codec_specific_data();
|
|
|
|
const bool ac4_ims_flag = codec_data.ac4_ims_flag();
|
|
|
|
// Use MPEG scheme if the mpeg value is available and valid, fallback to
|
|
|
|
// AC4 channel mask otherwise.
|
|
|
|
// See https://github.com/Dash-Industry-Forum/DASH-IF-IOP/issues/268
|
|
|
|
const uint32_t ac4_channel_mpeg_value = codec_data.channel_mpeg_value();
|
|
|
|
const uint32_t NO_MAPPING = 0xFFFFFFFF;
|
|
|
|
if (ac4_channel_mpeg_value == NO_MAPPING) {
|
|
|
|
// Calculate AC-4 channel mask. Spec: ETSI TS 103 190-2 V1.2.1 Digital
|
|
|
|
// Audio Compression (AC-4) Standard; Part 2: Immersive and personalized
|
|
|
|
// audio G.3.1.
|
2023-12-01 17:32:19 +00:00
|
|
|
//
|
|
|
|
// this needs to print only 3 bytes of the 32-bit value
|
2020-07-04 21:55:28 +00:00
|
|
|
audio_channel_config_value =
|
2023-12-01 17:32:19 +00:00
|
|
|
absl::StrFormat("%06X", codec_data.channel_mask());
|
2020-07-04 21:55:28 +00:00
|
|
|
// Note that the channel config schemes for EC-3 and AC-4 are different.
|
|
|
|
// See https://github.com/Dash-Industry-Forum/DASH-IF-IOP/issues/268.
|
|
|
|
audio_channel_config_scheme =
|
|
|
|
"tag:dolby.com,2015:dash:audio_channel_configuration:2015";
|
|
|
|
} else {
|
|
|
|
// Calculate AC-4 channel configuration descriptor value with MPEG scheme.
|
|
|
|
// Spec: ETSI TS 103 190-2 V1.2.1 Digital Audio Compression (AC-4) Standard;
|
|
|
|
// Part 2: Immersive and personalized audio G.3.2.
|
2023-12-01 17:32:19 +00:00
|
|
|
audio_channel_config_value =
|
|
|
|
absl::StrFormat("%u", ac4_channel_mpeg_value);
|
2020-07-04 21:55:28 +00:00
|
|
|
audio_channel_config_scheme = "urn:mpeg:mpegB:cicp:ChannelConfiguration";
|
|
|
|
}
|
|
|
|
bool ret = AddDescriptor("AudioChannelConfiguration",
|
|
|
|
audio_channel_config_scheme,
|
|
|
|
audio_channel_config_value);
|
|
|
|
if (ac4_ims_flag) {
|
|
|
|
ret &= AddDescriptor("SupplementalProperty",
|
|
|
|
"tag:dolby.com,2016:dash:virtualized_content:2016",
|
|
|
|
"1");
|
|
|
|
}
|
|
|
|
return ret;
|
2024-02-15 07:03:03 +00:00
|
|
|
} else if (audio_info.codec() == kDTSCCodec ||
|
|
|
|
audio_info.codec() == kDTSECodec) {
|
|
|
|
audio_channel_config_value =
|
|
|
|
absl::StrFormat("%u", audio_info.num_channels());
|
|
|
|
audio_channel_config_scheme =
|
|
|
|
"tag:dts.com,2014:dash:audio_channel_configuration:2012";
|
|
|
|
} else if (audio_info.codec() == kDTSXCodec) {
|
|
|
|
const auto& codec_data = audio_info.codec_specific_data();
|
|
|
|
audio_channel_config_value =
|
|
|
|
absl::StrFormat("%08X", codec_data.channel_mask());
|
|
|
|
audio_channel_config_scheme =
|
|
|
|
"tag:dts.com,2018:uhd:audio_channel_configuration";
|
2016-02-09 00:22:01 +00:00
|
|
|
} else {
|
2023-12-01 17:32:19 +00:00
|
|
|
audio_channel_config_value =
|
|
|
|
absl::StrFormat("%u", audio_info.num_channels());
|
2016-02-09 00:22:01 +00:00
|
|
|
audio_channel_config_scheme =
|
|
|
|
"urn:mpeg:dash:23003:3:audio_channel_configuration:2011";
|
|
|
|
}
|
|
|
|
|
2019-06-25 01:02:19 +00:00
|
|
|
return AddDescriptor("AudioChannelConfiguration", audio_channel_config_scheme,
|
|
|
|
audio_channel_config_value);
|
2013-11-18 21:01:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MPD expects one number for sampling frequency, or if it is a range it should
|
|
|
|
// be space separated.
|
2020-11-10 00:32:58 +00:00
|
|
|
bool RepresentationXmlNode::AddAudioSamplingRateInfo(
|
2015-06-09 23:58:32 +00:00
|
|
|
const AudioInfo& audio_info) {
|
2020-11-10 00:32:58 +00:00
|
|
|
return !audio_info.has_sampling_frequency() ||
|
|
|
|
SetIntegerAttribute("audioSamplingRate",
|
|
|
|
audio_info.sampling_frequency());
|
2013-11-18 21:01:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace xml
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|