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/strings/string_number_conversions.h"
16 #include "packager/mpd/base/media_info.pb.h"
17 #include "packager/mpd/base/segment_info.h"
21 using edash_packager::MediaInfo;
22 typedef edash_packager::MediaInfo::AudioInfo AudioInfo;
23 typedef edash_packager::MediaInfo::VideoInfo VideoInfo;
24 typedef MediaInfo::ContentProtectionXml ContentProtectionXml;
25 typedef ContentProtectionXml::AttributeNameValuePair AttributeNameValuePair;
27 namespace edash_packager {
31 std::string RangeToString(
const Range& range) {
32 return base::Uint64ToString(range.begin()) +
"-" +
33 base::Uint64ToString(range.end());
36 bool PopulateSegmentTimeline(
const std::list<SegmentInfo>& segment_infos,
38 for (std::list<SegmentInfo>::const_iterator it = segment_infos.begin();
39 it != segment_infos.end();
42 s_element.SetIntegerAttribute(
"t", it->start_time);
43 s_element.SetIntegerAttribute(
"d", it->duration);
45 s_element.SetIntegerAttribute(
"r", it->repeat);
47 CHECK(segment_timeline->
AddChild(s_element.PassScopedPtr()));
62 XmlNode::~XmlNode() {}
67 if (!xmlAddChild(node_.get(), child.get()))
72 ignore_result(child.release());
77 for (
size_t element_index = 0; element_index < elements.size();
79 const Element& child_element = elements[element_index];
80 XmlNode child_node(child_element.name.c_str());
81 for (std::map<std::string, std::string>::const_iterator attribute_it =
82 child_element.attributes.begin();
83 attribute_it != child_element.attributes.end(); ++attribute_it) {
85 attribute_it->second);
88 if (!child_node.AddElements(child_element.subelements))
91 child_node.SetContent(child_element.content);
93 if (!xmlAddChild(node_.get(), child_node.GetRawPtr())) {
94 LOG(ERROR) <<
"Failed to set child " << child_element.name
95 <<
" to parent element "
96 <<
reinterpret_cast<const char*
>(node_->name);
101 ignore_result(child_node.Release());
107 const std::string& attribute) {
109 DCHECK(attribute_name);
110 xmlSetProp(node_.get(), BAD_CAST attribute_name, BAD_CAST attribute.c_str());
115 DCHECK(attribute_name);
116 xmlSetProp(node_.get(),
117 BAD_CAST attribute_name,
118 BAD_CAST (base::Uint64ToString(number).c_str()));
124 DCHECK(attribute_name);
125 xmlSetProp(node_.get(),
126 BAD_CAST attribute_name,
127 BAD_CAST (base::DoubleToString(number).c_str()));
136 xmlNodeSetContent(node_.get(), BAD_CAST content.c_str());
140 DVLOG(2) <<
"Passing node_.";
146 DVLOG(2) <<
"Releasing node_.";
148 return node_.release();
155 RepresentationBaseXmlNode::RepresentationBaseXmlNode(
const char* name)
157 RepresentationBaseXmlNode::~RepresentationBaseXmlNode() {}
159 bool RepresentationBaseXmlNode::AddContentProtectionElements(
160 const std::list<ContentProtectionElement>& content_protection_elements) {
161 std::list<ContentProtectionElement>::const_iterator content_protection_it =
162 content_protection_elements.begin();
163 for (; content_protection_it != content_protection_elements.end();
164 ++content_protection_it) {
165 if (!AddContentProtectionElement(*content_protection_it))
172 bool RepresentationBaseXmlNode::AddContentProtectionElement(
173 const ContentProtectionElement& content_protection_element) {
174 XmlNode content_protection_node(
"ContentProtection");
177 if (!content_protection_element.value.empty()) {
178 content_protection_node.SetStringAttribute(
179 "value", content_protection_element.value);
181 content_protection_node.SetStringAttribute(
182 "schemeIdUri", content_protection_element.scheme_id_uri);
184 typedef std::map<std::string, std::string> AttributesMapType;
185 const AttributesMapType& additional_attributes =
186 content_protection_element.additional_attributes;
188 AttributesMapType::const_iterator attributes_it =
189 additional_attributes.begin();
190 for (; attributes_it != additional_attributes.end(); ++attributes_it) {
191 content_protection_node.SetStringAttribute(attributes_it->first.c_str(),
192 attributes_it->second);
195 if (!content_protection_node.AddElements(
196 content_protection_element.subelements)) {
199 return AddChild(content_protection_node.PassScopedPtr());
202 AdaptationSetXmlNode::AdaptationSetXmlNode()
203 : RepresentationBaseXmlNode(
"AdaptationSet") {}
204 AdaptationSetXmlNode::~AdaptationSetXmlNode() {}
207 const std::string& value) {
214 RepresentationXmlNode::RepresentationXmlNode()
216 RepresentationXmlNode::~RepresentationXmlNode() {}
221 bool set_frame_rate) {
222 if (!video_info.has_width() || !video_info.has_height()) {
223 LOG(ERROR) <<
"Missing width or height for adding a video info.";
227 if (video_info.has_pixel_width() && video_info.has_pixel_height()) {
230 base::IntToString(video_info.pixel_height()));
237 if (set_frame_rate) {
239 base::IntToString(video_info.time_scale()) +
"/" +
240 base::IntToString(video_info.frame_duration()));
246 if (!AddAudioChannelInfo(audio_info))
249 AddAudioSamplingRateInfo(audio_info);
254 if (media_info.has_media_file_name()) {
256 base_url.
SetContent(media_info.media_file_name());
262 const bool need_segment_base = media_info.has_index_range() ||
263 media_info.has_init_range() ||
264 media_info.has_reference_time_scale();
266 if (need_segment_base) {
267 XmlNode segment_base(
"SegmentBase");
268 if (media_info.has_index_range()) {
270 RangeToString(media_info.index_range()));
273 if (media_info.has_reference_time_scale()) {
275 media_info.reference_time_scale());
278 if (media_info.has_init_range()) {
279 XmlNode initialization(
"Initialization");
281 RangeToString(media_info.init_range()));
291 if (media_info.has_media_duration_seconds()) {
301 const MediaInfo& media_info,
302 const std::list<SegmentInfo>& segment_infos,
303 uint32_t start_number) {
304 XmlNode segment_template(
"SegmentTemplate");
305 if (media_info.has_reference_time_scale()) {
307 media_info.reference_time_scale());
310 if (media_info.has_init_segment_name()) {
315 const std::string& init_segment_name = media_info.init_segment_name();
316 if (init_segment_name.find(
"$Number$") != std::string::npos ||
317 init_segment_name.find(
"$Time$") != std::string::npos) {
318 LOG(ERROR) <<
"$Number$ and $Time$ cannot be used for "
319 "SegmentTemplate@initialization";
323 media_info.init_segment_name());
326 if (media_info.has_segment_template()) {
331 if (media_info.segment_template().find(
"$Number") != std::string::npos) {
332 DCHECK_GE(start_number, 1u);
338 XmlNode segment_timeline(
"SegmentTimeline");
340 return PopulateSegmentTimeline(segment_infos, &segment_timeline) &&
345 bool RepresentationXmlNode::AddAudioChannelInfo(
const AudioInfo& audio_info) {
346 const uint32_t num_channels = audio_info.num_channels();
347 XmlNode audio_channel_config(
"AudioChannelConfiguration");
348 const char kAudioChannelConfigScheme[] =
349 "urn:mpeg:dash:23003:3:audio_channel_configuration:2011";
350 audio_channel_config.SetStringAttribute(
"schemeIdUri",
351 kAudioChannelConfigScheme);
352 audio_channel_config.SetIntegerAttribute(
"value", num_channels);
354 return AddChild(audio_channel_config.PassScopedPtr());
359 void RepresentationXmlNode::AddAudioSamplingRateInfo(
360 const AudioInfo& audio_info) {
361 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)