Shaka Packager SDK
Loading...
Searching...
No Matches
xml_node.cc
1// Copyright 2014 Google LLC. All rights reserved.
2//
3// Use of this source code is governed by a BSD-style
4// license that can be found in the LICENSE file or at
5// https://developers.google.com/open-source/licenses/bsd
6
7#include <packager/mpd/base/xml/xml_node.h>
8
9#include <cinttypes>
10#include <cmath>
11#include <limits>
12#include <set>
13
14#include <absl/base/internal/endian.h>
15#include <absl/flags/flag.h>
16#include <absl/log/check.h>
17#include <absl/log/log.h>
18#include <absl/strings/escaping.h>
19#include <absl/strings/numbers.h>
20#include <absl/strings/str_format.h>
21#include <curl/curl.h>
22#include <libxml/tree.h>
23
24#include <packager/macros/compiler.h>
25#include <packager/media/base/rcheck.h>
26#include <packager/mpd/base/media_info.pb.h>
27#include <packager/mpd/base/mpd_utils.h>
28#include <packager/mpd/base/segment_info.h>
29#include <packager/mpd/base/xml/scoped_xml_ptr.h>
30
31ABSL_FLAG(bool,
32 segment_template_constant_duration,
33 false,
34 "Generates SegmentTemplate@duration if all segments except the "
35 "last one has the same duration if this flag is set to true.");
36
37ABSL_FLAG(bool,
38 dash_add_last_segment_number_when_needed,
39 false,
40 "Adds a Supplemental Descriptor with @schemeIdUri "
41 "set to http://dashif.org/guidelines/last-segment-number with "
42 "the @value set to the last segment number.");
43
44namespace shaka {
45
46using xml::XmlNode;
47typedef MediaInfo::AudioInfo AudioInfo;
48typedef MediaInfo::VideoInfo VideoInfo;
49
50namespace {
51const char kEC3Codec[] = "ec-3";
52const char kAC4Codec[] = "ac-4";
53const char kDTSCCodec[] = "dtsc";
54const char kDTSECodec[] = "dtse";
55const char kDTSXCodec[] = "dtsx";
56
57std::string urlEncode(const std::string& input) {
58 // NOTE: According to the docs, "Since 7.82.0, the curl parameter is ignored".
59 CURL* curl = NULL;
60 char* output = curl_easy_escape(curl, input.c_str(), input.length());
61 if (output) {
62 std::string encodedUrl(output);
63 curl_free(output); // Free the output string when done
64 return encodedUrl;
65 }
66 return ""; // Return empty string if initialization fails
67}
68
69std::string RangeToString(const Range& range) {
70 return absl::StrFormat("%u-%u", range.begin(), range.end());
71}
72
73// Check if segments are continuous and all segments except the last one are of
74// the same duration.
75bool IsTimelineConstantDuration(const std::list<SegmentInfo>& segment_infos,
76 uint32_t start_number) {
77 if (!absl::GetFlag(FLAGS_segment_template_constant_duration))
78 return false;
79
80 DCHECK(!segment_infos.empty());
81 if (segment_infos.size() > 2)
82 return false;
83
84 const SegmentInfo& first_segment = segment_infos.front();
85 if (first_segment.start_time != first_segment.duration * (start_number - 1))
86 return false;
87
88 if (segment_infos.size() == 1)
89 return true;
90
91 const SegmentInfo& last_segment = segment_infos.back();
92 if (last_segment.repeat != 0)
93 return false;
94
95 const int64_t expected_last_segment_start_time =
96 first_segment.start_time +
97 first_segment.duration * (first_segment.repeat + 1);
98 return expected_last_segment_start_time == last_segment.start_time;
99}
100
101bool PopulateSegmentTimeline(const std::list<SegmentInfo>& segment_infos,
102 XmlNode* segment_timeline) {
103 for (const SegmentInfo& segment_info : segment_infos) {
104 XmlNode s_element("S");
105 RCHECK(s_element.SetIntegerAttribute("t", segment_info.start_time));
106 RCHECK(s_element.SetIntegerAttribute("d", segment_info.duration));
107 if (segment_info.repeat > 0)
108 RCHECK(s_element.SetIntegerAttribute("r", segment_info.repeat));
109
110 RCHECK(segment_timeline->AddChild(std::move(s_element)));
111 }
112
113 return true;
114}
115
116void CollectNamespaceFromName(const std::string& name,
117 std::set<std::string>* namespaces) {
118 const size_t pos = name.find(':');
119 if (pos != std::string::npos)
120 namespaces->insert(name.substr(0, pos));
121}
122
123void TraverseAttrsAndCollectNamespaces(const xmlAttr* attr,
124 std::set<std::string>* namespaces) {
125 for (const xmlAttr* cur_attr = attr; cur_attr; cur_attr = cur_attr->next) {
126 CollectNamespaceFromName(reinterpret_cast<const char*>(cur_attr->name),
127 namespaces);
128 }
129}
130
131void TraverseNodesAndCollectNamespaces(const xmlNode* node,
132 std::set<std::string>* namespaces) {
133 for (const xmlNode* cur_node = node; cur_node; cur_node = cur_node->next) {
134 CollectNamespaceFromName(reinterpret_cast<const char*>(cur_node->name),
135 namespaces);
136
137 TraverseNodesAndCollectNamespaces(cur_node->children, namespaces);
138 TraverseAttrsAndCollectNamespaces(cur_node->properties, namespaces);
139 }
140}
141
142} // namespace
143
144namespace xml {
145
146class XmlNode::Impl {
147 public:
148 scoped_xml_ptr<xmlNode> node;
149};
150
151XmlNode::XmlNode(const std::string& name) : impl_(new Impl) {
152 impl_->node.reset(xmlNewNode(NULL, BAD_CAST name.c_str()));
153 DCHECK(impl_->node);
154}
155
156XmlNode::XmlNode(XmlNode&&) = default;
157
158XmlNode::~XmlNode() {}
159
160XmlNode& XmlNode::operator=(XmlNode&&) = default;
161
163 DCHECK(impl_->node);
164 DCHECK(child.impl_->node);
165 RCHECK(xmlAddChild(impl_->node.get(), child.impl_->node.get()));
166
167 // Reaching here means the ownership of |child| transfered to |node|.
168 // Release the pointer so that it doesn't get destructed in this scope.
169 UNUSED(child.impl_->node.release());
170 return true;
171}
172
173bool XmlNode::AddElements(const std::vector<Element>& elements) {
174 for (size_t element_index = 0; element_index < elements.size();
175 ++element_index) {
176 const Element& child_element = elements[element_index];
177 XmlNode child_node(child_element.name);
178 for (std::map<std::string, std::string>::const_iterator attribute_it =
179 child_element.attributes.begin();
180 attribute_it != child_element.attributes.end(); ++attribute_it) {
181 RCHECK(child_node.SetStringAttribute(attribute_it->first,
182 attribute_it->second));
183 }
184
185 // Note that somehow |SetContent| needs to be called before |AddElements|
186 // otherwise the added children will be overwritten by the content.
187 child_node.SetContent(child_element.content);
188
189 // Recursively set children for the child.
190 RCHECK(child_node.AddElements(child_element.subelements));
191
192 if (!xmlAddChild(impl_->node.get(), child_node.impl_->node.get())) {
193 LOG(ERROR) << "Failed to set child " << child_element.name
194 << " to parent element "
195 << reinterpret_cast<const char*>(impl_->node->name);
196 return false;
197 }
198 // Reaching here means the ownership of |child_node| transfered to |node|.
199 // Release the pointer so that it doesn't get destructed in this scope.
200 child_node.impl_->node.release();
201 }
202 return true;
203}
204
205bool XmlNode::SetStringAttribute(const std::string& attribute_name,
206 const std::string& attribute) {
207 DCHECK(impl_->node);
208 return xmlSetProp(impl_->node.get(), BAD_CAST attribute_name.c_str(),
209 BAD_CAST attribute.c_str()) != nullptr;
210}
211
212bool XmlNode::SetIntegerAttribute(const std::string& attribute_name,
213 uint64_t number) {
214 DCHECK(impl_->node);
215 return xmlSetProp(impl_->node.get(), BAD_CAST attribute_name.c_str(),
216 BAD_CAST(absl::StrFormat("%" PRIu64, number).c_str())) !=
217 nullptr;
218}
219
220bool XmlNode::SetFloatingPointAttribute(const std::string& attribute_name,
221 double number) {
222 DCHECK(impl_->node);
223 return xmlSetProp(impl_->node.get(), BAD_CAST attribute_name.c_str(),
224 BAD_CAST(FloatToXmlString(number).c_str())) != nullptr;
225}
226
227bool XmlNode::SetId(uint32_t id) {
228 return SetIntegerAttribute("id", id);
229}
230
231void XmlNode::AddContent(const std::string& content) {
232 DCHECK(impl_->node);
233 xmlNodeAddContent(impl_->node.get(), BAD_CAST content.c_str());
234}
235
236void XmlNode::AddUrlEncodedContent(const std::string& content) {
237 AddContent(urlEncode(content));
238}
239
240void XmlNode::SetContent(const std::string& content) {
241 DCHECK(impl_->node);
242 xmlNodeSetContent(impl_->node.get(), BAD_CAST content.c_str());
243}
244
245void XmlNode::SetUrlEncodedContent(const std::string& content) {
246 SetContent(urlEncode(content));
247}
248
249std::set<std::string> XmlNode::ExtractReferencedNamespaces() const {
250 std::set<std::string> namespaces;
251 TraverseNodesAndCollectNamespaces(impl_->node.get(), &namespaces);
252 return namespaces;
253}
254
255std::string XmlNode::ToString(const std::string& comment) const {
256 // Create an xmlDoc from xmlNodePtr. The node is copied so ownership does not
257 // transfer.
258 xml::scoped_xml_ptr<xmlDoc> doc(xmlNewDoc(BAD_CAST "1.0"));
259 if (comment.empty()) {
260 xmlDocSetRootElement(doc.get(), xmlCopyNode(impl_->node.get(), true));
261 } else {
262 xml::scoped_xml_ptr<xmlNode> comment_xml(
263 xmlNewDocComment(doc.get(), BAD_CAST comment.c_str()));
264 xmlDocSetRootElement(doc.get(), comment_xml.get());
265 xmlAddSibling(comment_xml.release(), xmlCopyNode(impl_->node.get(), true));
266 }
267
268 // Format the xmlDoc to string.
269 static const int kNiceFormat = 1;
270 int doc_str_size = 0;
271 xmlChar* doc_str = nullptr;
272 xmlDocDumpFormatMemoryEnc(doc.get(), &doc_str, &doc_str_size, "UTF-8",
273 kNiceFormat);
274 std::string output(doc_str, doc_str + doc_str_size);
275 xmlFree(doc_str);
276 return output;
277}
278
279bool XmlNode::GetAttribute(const std::string& name, std::string* value) const {
280 xml::scoped_xml_ptr<xmlChar> str(
281 xmlGetProp(impl_->node.get(), BAD_CAST name.c_str()));
282 if (!str)
283 return false;
284 *value = reinterpret_cast<const char*>(str.get());
285 return true;
286}
287
288xmlNode* XmlNode::GetRawPtr() const {
289 return impl_->node.get();
290}
291
292RepresentationBaseXmlNode::RepresentationBaseXmlNode(const std::string& name)
293 : XmlNode(name) {}
294RepresentationBaseXmlNode::~RepresentationBaseXmlNode() {}
295
296bool RepresentationBaseXmlNode::AddContentProtectionElements(
297 const std::list<ContentProtectionElement>& content_protection_elements) {
298 for (const auto& elem : content_protection_elements) {
299 RCHECK(AddContentProtectionElement(elem));
300 }
301
302 return true;
303}
304
306 const std::string& scheme_id_uri,
307 const std::string& value) {
308 return AddDescriptor("SupplementalProperty", scheme_id_uri, value);
309}
310
312 const std::string& scheme_id_uri,
313 const std::string& value) {
314 return AddDescriptor("EssentialProperty", scheme_id_uri, value);
315}
316
318 const std::string& descriptor_name,
319 const std::string& scheme_id_uri,
320 const std::string& value) {
321 XmlNode descriptor(descriptor_name);
322 RCHECK(descriptor.SetStringAttribute("schemeIdUri", scheme_id_uri));
323 if (!value.empty())
324 RCHECK(descriptor.SetStringAttribute("value", value));
325 return AddChild(std::move(descriptor));
326}
327
328bool RepresentationBaseXmlNode::AddContentProtectionElement(
329 const ContentProtectionElement& content_protection_element) {
330 XmlNode content_protection_node("ContentProtection");
331
332 // @value is an optional attribute.
333 if (!content_protection_element.value.empty()) {
334 RCHECK(content_protection_node.SetStringAttribute(
335 "value", content_protection_element.value));
336 }
337 RCHECK(content_protection_node.SetStringAttribute(
338 "schemeIdUri", content_protection_element.scheme_id_uri));
339
340 for (const auto& pair : content_protection_element.additional_attributes) {
341 RCHECK(content_protection_node.SetStringAttribute(pair.first, pair.second));
342 }
343
344 RCHECK(content_protection_node.AddElements(
345 content_protection_element.subelements));
346 return AddChild(std::move(content_protection_node));
347}
348
349AdaptationSetXmlNode::AdaptationSetXmlNode()
350 : RepresentationBaseXmlNode("AdaptationSet") {}
351AdaptationSetXmlNode::~AdaptationSetXmlNode() {}
352
354 const std::string& scheme_id_uri,
355 const std::string& value) {
356 return AddDescriptor("Accessibility", scheme_id_uri, value);
357}
358
359bool AdaptationSetXmlNode::AddRoleElement(const std::string& scheme_id_uri,
360 const std::string& value) {
361 return AddDescriptor("Role", scheme_id_uri, value);
362}
363
364bool AdaptationSetXmlNode::AddLabelElement(const std::string& value) {
365 XmlNode descriptor("Label");
366 descriptor.SetContent(value);
367 return AddChild(std::move(descriptor));
368}
369
370RepresentationXmlNode::RepresentationXmlNode()
371 : RepresentationBaseXmlNode("Representation") {}
372RepresentationXmlNode::~RepresentationXmlNode() {}
373
374bool RepresentationXmlNode::AddVideoInfo(const VideoInfo& video_info,
375 bool set_width,
376 bool set_height,
377 bool set_frame_rate) {
378 if (!video_info.has_width() || !video_info.has_height()) {
379 LOG(ERROR) << "Missing width or height for adding a video info.";
380 return false;
381 }
382
383 if (video_info.has_pixel_width() && video_info.has_pixel_height()) {
384 RCHECK(SetStringAttribute("sar",
385 absl::StrFormat("%d:%d", video_info.pixel_width(),
386 video_info.pixel_height())));
387 }
388
389 if (set_width)
390 RCHECK(SetIntegerAttribute("width", video_info.width()));
391 if (set_height)
392 RCHECK(SetIntegerAttribute("height", video_info.height()));
393 if (set_frame_rate) {
394 RCHECK(SetStringAttribute("frameRate",
395 absl::StrFormat("%d/%d", video_info.time_scale(),
396 video_info.frame_duration())));
397 }
398
399 if (video_info.has_playback_rate()) {
400 RCHECK(SetStringAttribute(
401 "maxPlayoutRate", absl::StrFormat("%d", video_info.playback_rate())));
402 // Since the trick play stream contains only key frames, there is no coding
403 // dependency on the main stream. Simply set the codingDependency to false.
404 // TODO(hmchen): propagate this attribute up to the AdaptationSet, since
405 // all are set to false.
406 RCHECK(SetStringAttribute("codingDependency", "false"));
407 }
408 return true;
409}
410
411bool RepresentationXmlNode::AddAudioInfo(const AudioInfo& audio_info) {
412 return AddAudioChannelInfo(audio_info) &&
413 AddAudioSamplingRateInfo(audio_info);
414}
415
416bool RepresentationXmlNode::AddVODOnlyInfo(const MediaInfo& media_info,
417 bool use_segment_list,
418 double target_segment_duration) {
419 if (media_info.has_media_file_url()) {
420 XmlNode base_url("BaseURL");
421 base_url.SetUrlEncodedContent(media_info.media_file_url());
422
423 RCHECK(AddChild(std::move(base_url)));
424 }
425
426 // For single-file text tracks with a presentationTimeOffset we still need a
427 // SegmentBase element to carry the offset — SegmentBase is correct here
428 // because the track is a single segment, not a segmented stream.
429 const bool need_segment_base_or_list =
430 use_segment_list || media_info.has_index_range() ||
431 media_info.has_init_range() ||
432 (media_info.has_reference_time_scale() && !media_info.has_text_info()) ||
433 (media_info.has_text_info() && media_info.has_presentation_time_offset());
434
435 if (!need_segment_base_or_list) {
436 return true;
437 }
438
439 XmlNode child(use_segment_list ? "SegmentList" : "SegmentBase");
440
441 // Forcing SegmentList for longer audio causes sidx atom to not be
442 // generated, therefore indexRange is not added to MPD if flag is set.
443 if (media_info.has_index_range() && !use_segment_list) {
444 RCHECK(child.SetStringAttribute("indexRange",
445 RangeToString(media_info.index_range())));
446 }
447
448 if (media_info.has_reference_time_scale()) {
449 RCHECK(child.SetIntegerAttribute("timescale",
450 media_info.reference_time_scale()));
451
452 if (use_segment_list) {
453 const auto duration_seconds = static_cast<int64_t>(
454 floor(target_segment_duration * media_info.reference_time_scale()));
455 RCHECK(child.SetIntegerAttribute("duration", duration_seconds));
456 }
457 }
458
459 if (media_info.has_presentation_time_offset()) {
460 RCHECK(child.SetIntegerAttribute("presentationTimeOffset",
461 media_info.presentation_time_offset()));
462 }
463
464 if (media_info.has_init_range()) {
465 XmlNode initialization("Initialization");
466 RCHECK(initialization.SetStringAttribute(
467 "range", RangeToString(media_info.init_range())));
468
469 RCHECK(child.AddChild(std::move(initialization)));
470 }
471
472 // Since the SegmentURLs here do not have a @media element,
473 // BaseURL element is mapped to the @media attribute.
474 if (use_segment_list) {
475 for (const Range& subsegment_range : media_info.subsegment_ranges()) {
476 XmlNode subsegment("SegmentURL");
477 RCHECK(subsegment.SetStringAttribute("mediaRange",
478 RangeToString(subsegment_range)));
479
480 RCHECK(child.AddChild(std::move(subsegment)));
481 }
482 }
483
484 RCHECK(AddChild(std::move(child)));
485 return true;
486}
487
489 const MediaInfo& media_info,
490 const std::list<SegmentInfo>& segment_infos,
491 bool low_latency_dash_mode) {
492 XmlNode segment_template("SegmentTemplate");
493
494 int start_number =
495 segment_infos.empty() ? 1 : segment_infos.begin()->start_segment_number;
496
497 if (media_info.has_reference_time_scale()) {
498 RCHECK(segment_template.SetIntegerAttribute(
499 "timescale", media_info.reference_time_scale()));
500 }
501
502 if (media_info.has_segment_duration()) {
503 RCHECK(segment_template.SetIntegerAttribute("duration",
504 media_info.segment_duration()));
505 }
506
507 if (media_info.has_presentation_time_offset()) {
508 RCHECK(segment_template.SetIntegerAttribute(
509 "presentationTimeOffset", media_info.presentation_time_offset()));
510 }
511
512 if (media_info.has_availability_time_offset()) {
513 RCHECK(segment_template.SetFloatingPointAttribute(
514 "availabilityTimeOffset", media_info.availability_time_offset()));
515 }
516
517 if (low_latency_dash_mode) {
518 RCHECK(segment_template.SetStringAttribute("availabilityTimeComplete",
519 "false"));
520 }
521
522 if (media_info.has_init_segment_url()) {
523 RCHECK(segment_template.SetStringAttribute("initialization",
524 media_info.init_segment_url()));
525 }
526
527 if (media_info.has_segment_template_url()) {
528 RCHECK(segment_template.SetStringAttribute(
529 "media", media_info.segment_template_url()));
530 RCHECK(segment_template.SetIntegerAttribute("startNumber", start_number));
531 }
532
533 if (!segment_infos.empty()) {
534 // Don't use SegmentTimeline if all segments except the last one are of
535 // the same duration.
536 if (IsTimelineConstantDuration(segment_infos, start_number)) {
537 RCHECK(segment_template.SetIntegerAttribute(
538 "duration", segment_infos.front().duration));
539 if (absl::GetFlag(FLAGS_dash_add_last_segment_number_when_needed)) {
540 uint32_t last_segment_number = start_number - 1;
541 for (const auto& segment_info_element : segment_infos)
542 last_segment_number += segment_info_element.repeat + 1;
543
545 "http://dashif.org/guidelines/last-segment-number",
546 std::to_string(last_segment_number)));
547 }
548 } else {
549 if (!low_latency_dash_mode) {
550 XmlNode segment_timeline("SegmentTimeline");
551 RCHECK(PopulateSegmentTimeline(segment_infos, &segment_timeline));
552 RCHECK(segment_template.AddChild(std::move(segment_timeline)));
553 }
554 }
555 }
556 return AddChild(std::move(segment_template));
557}
558
559bool RepresentationXmlNode::AddAudioChannelInfo(const AudioInfo& audio_info) {
560 std::string audio_channel_config_scheme;
561 std::string audio_channel_config_value;
562
563 if (audio_info.codec() == kEC3Codec) {
564 const auto& codec_data = audio_info.codec_specific_data();
565 // Use MPEG scheme if the mpeg value is available and valid, fallback to
566 // EC3 channel mapping otherwise.
567 // See https://github.com/Dash-Industry-Forum/DASH-IF-IOP/issues/268
568 const uint32_t ec3_channel_mpeg_value = codec_data.channel_mpeg_value();
569 const uint32_t NO_MAPPING = 0xFFFFFFFF;
570 if (ec3_channel_mpeg_value == NO_MAPPING) {
571 // Convert EC3 channel map into string of hexadecimal digits. Spec:
572 // DASH-IF Interoperability Points v3.0 9.2.1.2.
573 audio_channel_config_value =
574 absl::StrFormat("%04X", codec_data.channel_mask());
575 audio_channel_config_scheme =
576 "tag:dolby.com,2014:dash:audio_channel_configuration:2011";
577 } else {
578 // Calculate EC3 channel configuration descriptor value with MPEG scheme.
579 // Spec: ETSI TS 102 366 V1.4.1 Digital Audio Compression
580 // (AC-3, Enhanced AC-3) I.1.2.
581 audio_channel_config_value =
582 absl::StrFormat("%u", ec3_channel_mpeg_value);
583 audio_channel_config_scheme = "urn:mpeg:mpegB:cicp:ChannelConfiguration";
584 }
585 bool ret =
586 AddDescriptor("AudioChannelConfiguration", audio_channel_config_scheme,
587 audio_channel_config_value);
588 // Dolby Digital Plus JOC descriptor. Spec: ETSI TS 103 420 v1.2.1
589 // Backwards-compatible object audio carriage using Enhanced AC-3 Standard
590 // D.2.2.
591 if (codec_data.ec3_joc_complexity() != 0) {
592 std::string ec3_joc_complexity =
593 absl::StrFormat("%u", codec_data.ec3_joc_complexity());
594 ret &= AddDescriptor("SupplementalProperty",
595 "tag:dolby.com,2018:dash:EC3_ExtensionType:2018",
596 "JOC");
597 ret &= AddDescriptor("SupplementalProperty",
598 "tag:dolby.com,2018:dash:"
599 "EC3_ExtensionComplexityIndex:2018",
600 ec3_joc_complexity);
601 }
602 return ret;
603 } else if (audio_info.codec().substr(0, 4) == kAC4Codec) {
604 const auto& codec_data = audio_info.codec_specific_data();
605 const bool ac4_ims_flag = codec_data.ac4_ims_flag();
606 // Use MPEG scheme if the mpeg value is available and valid, fallback to
607 // AC4 channel mask otherwise.
608 // See https://github.com/Dash-Industry-Forum/DASH-IF-IOP/issues/268
609 const uint32_t ac4_channel_mpeg_value = codec_data.channel_mpeg_value();
610 const uint32_t NO_MAPPING = 0xFFFFFFFF;
611 if (ac4_channel_mpeg_value == NO_MAPPING) {
612 // Calculate AC-4 channel mask. Spec: ETSI TS 103 190-2 V1.2.1 Digital
613 // Audio Compression (AC-4) Standard; Part 2: Immersive and personalized
614 // audio G.3.1.
615 //
616 // this needs to print only 3 bytes of the 32-bit value
617 audio_channel_config_value =
618 absl::StrFormat("%06X", codec_data.channel_mask());
619 // Note that the channel config schemes for EC-3 and AC-4 are different.
620 // See https://github.com/Dash-Industry-Forum/DASH-IF-IOP/issues/268.
621 audio_channel_config_scheme =
622 "tag:dolby.com,2015:dash:audio_channel_configuration:2015";
623 } else {
624 // Calculate AC-4 channel configuration descriptor value with MPEG scheme.
625 // Spec: ETSI TS 103 190-2 V1.2.1 Digital Audio Compression (AC-4)
626 // Standard; Part 2: Immersive and personalized audio G.3.2.
627 audio_channel_config_value =
628 absl::StrFormat("%u", ac4_channel_mpeg_value);
629 audio_channel_config_scheme = "urn:mpeg:mpegB:cicp:ChannelConfiguration";
630 }
631 bool ret =
632 AddDescriptor("AudioChannelConfiguration", audio_channel_config_scheme,
633 audio_channel_config_value);
634 if (ac4_ims_flag) {
635 ret &= AddDescriptor("SupplementalProperty",
636 "tag:dolby.com,2016:dash:virtualized_content:2016",
637 "1");
638 }
639 return ret;
640 } else if (audio_info.codec() == kDTSCCodec ||
641 audio_info.codec() == kDTSECodec) {
642 audio_channel_config_value =
643 absl::StrFormat("%u", audio_info.num_channels());
644 audio_channel_config_scheme =
645 "tag:dts.com,2014:dash:audio_channel_configuration:2012";
646 } else if (audio_info.codec() == kDTSXCodec) {
647 const auto& codec_data = audio_info.codec_specific_data();
648 audio_channel_config_value =
649 absl::StrFormat("%08X", codec_data.channel_mask());
650 audio_channel_config_scheme =
651 "tag:dts.com,2018:uhd:audio_channel_configuration";
652 } else {
653 audio_channel_config_value =
654 absl::StrFormat("%u", audio_info.num_channels());
655 audio_channel_config_scheme =
656 "urn:mpeg:dash:23003:3:audio_channel_configuration:2011";
657 }
658
659 return AddDescriptor("AudioChannelConfiguration", audio_channel_config_scheme,
660 audio_channel_config_value);
661}
662
663// MPD expects one number for sampling frequency, or if it is a range it should
664// be space separated.
665bool RepresentationXmlNode::AddAudioSamplingRateInfo(
666 const AudioInfo& audio_info) {
667 return !audio_info.has_sampling_frequency() ||
668 SetIntegerAttribute("audioSamplingRate",
669 audio_info.sampling_frequency());
670}
671
672} // namespace xml
673} // namespace shaka
bool AddAccessibilityElement(const std::string &scheme_id_uri, const std::string &value)
Definition xml_node.cc:353
bool AddLabelElement(const std::string &value)
Definition xml_node.cc:364
bool AddRoleElement(const std::string &scheme_id_uri, const std::string &value)
Definition xml_node.cc:359
bool AddDescriptor(const std::string &descriptor_name, const std::string &scheme_id_uri, const std::string &value)
Definition xml_node.cc:317
bool AddEssentialProperty(const std::string &scheme_id_uri, const std::string &value)
Definition xml_node.cc:311
bool AddSupplementalProperty(const std::string &scheme_id_uri, const std::string &value)
Definition xml_node.cc:305
bool AddVODOnlyInfo(const MediaInfo &media_info, bool use_segment_list, double target_segment_duration)
Definition xml_node.cc:416
bool AddLiveOnlyInfo(const MediaInfo &media_info, const std::list< SegmentInfo > &segment_infos, bool low_latency_dash_mode)
Definition xml_node.cc:488
bool AddAudioInfo(const MediaInfo::AudioInfo &audio_info)
Definition xml_node.cc:411
bool AddVideoInfo(const MediaInfo::VideoInfo &video_info, bool set_width, bool set_height, bool set_frame_rate)
Definition xml_node.cc:374
bool AddChild(XmlNode child)
Definition xml_node.cc:162
bool AddElements(const std::vector< Element > &elements)
Adds Elements to this node using the Element struct.
Definition xml_node.cc:173
std::set< std::string > ExtractReferencedNamespaces() const
Definition xml_node.cc:249
void AddContent(const std::string &content)
Similar to SetContent, but appends to the end of existing content.
Definition xml_node.cc:231
bool SetStringAttribute(const std::string &attribute_name, const std::string &attribute)
Definition xml_node.cc:205
void SetContent(const std::string &content)
Definition xml_node.cc:240
bool SetId(uint32_t id)
Definition xml_node.cc:227
XmlNode(const std::string &name)
Definition xml_node.cc:151
bool SetIntegerAttribute(const std::string &attribute_name, uint64_t number)
Definition xml_node.cc:212
bool GetAttribute(const std::string &name, std::string *value) const
Definition xml_node.cc:279
std::string ToString(const std::string &comment) const
Definition xml_node.cc:255
bool SetFloatingPointAttribute(const std::string &attribute_name, double number)
Definition xml_node.cc:220
All the methods that are virtual are virtual for mocking.