7 #include <packager/mpd/base/mpd_utils.h>
9 #include <absl/flags/flag.h>
10 #include <absl/log/check.h>
11 #include <absl/log/log.h>
12 #include <absl/strings/escaping.h>
13 #include <absl/strings/numbers.h>
14 #include <absl/strings/str_format.h>
15 #include <libxml/tree.h>
17 #include <packager/macros/logging.h>
18 #include <packager/media/base/fourccs.h>
19 #include <packager/media/base/language_utils.h>
20 #include <packager/media/base/protection_system_specific_info.h>
21 #include <packager/mpd/base/adaptation_set.h>
22 #include <packager/mpd/base/content_protection_element.h>
23 #include <packager/mpd/base/representation.h>
24 #include <packager/mpd/base/xml/scoped_xml_ptr.h>
28 use_legacy_vp9_codec_string,
30 "Use legacy vp9 codec string 'vp9' if set to true; otherwise new style "
31 "vp09.xx.xx.xx... codec string will be used. Default to false as indicated "
32 "in https://github.com/shaka-project/shaka-packager/issues/406, all major "
33 "browsers and platforms already support the new 'vp09' codec string.");
38 bool IsKeyRotationDefaultKeyId(
const std::string& key_id) {
39 for (
char c : key_id) {
46 std::string TextCodecString(
const MediaInfo& media_info) {
47 CHECK(media_info.has_text_info());
48 const auto container_type = media_info.container_type();
52 if (container_type == MediaInfo::CONTAINER_TEXT) {
58 const std::string& codec = media_info.text_info().codec();
59 if (codec ==
"ttml" && container_type == MediaInfo::CONTAINER_MP4) {
68 bool HasVODOnlyFields(
const MediaInfo& media_info) {
69 return media_info.has_init_range() || media_info.has_index_range() ||
70 media_info.has_media_file_url();
73 bool HasLiveOnlyFields(
const MediaInfo& media_info) {
74 return media_info.has_init_segment_url() ||
75 media_info.has_segment_template_url();
78 void RemoveDuplicateAttributes(
79 ContentProtectionElement* content_protection_element) {
80 DCHECK(content_protection_element);
81 typedef std::map<std::string, std::string> AttributesMap;
83 AttributesMap& attributes = content_protection_element->additional_attributes;
84 if (!content_protection_element->value.empty())
85 attributes.erase(
"value");
87 if (!content_protection_element->scheme_id_uri.empty())
88 attributes.erase(
"schemeIdUri");
91 std::string GetLanguage(
const MediaInfo& media_info) {
93 if (media_info.has_audio_info()) {
94 lang = media_info.audio_info().language();
95 }
else if (media_info.has_text_info()) {
96 lang = media_info.text_info().language();
101 std::string GetCodecs(
const MediaInfo& media_info) {
102 CHECK(OnlyOneTrue(media_info.has_video_info(), media_info.has_audio_info(),
103 media_info.has_text_info()));
105 if (media_info.has_video_info()) {
106 if (media_info.container_type() == MediaInfo::CONTAINER_WEBM) {
107 std::string codec = media_info.video_info().codec().substr(0, 4);
114 if (absl::GetFlag(FLAGS_use_legacy_vp9_codec_string)) {
119 return media_info.video_info().codec();
122 if (media_info.has_audio_info())
123 return media_info.audio_info().codec();
125 if (media_info.has_text_info())
126 return TextCodecString(media_info);
132 std::string GetSupplementalCodecs(
const MediaInfo& media_info) {
133 CHECK(OnlyOneTrue(media_info.has_video_info(), media_info.has_audio_info(),
134 media_info.has_text_info()));
136 if (media_info.has_video_info() &&
137 media_info.video_info().has_supplemental_codec()) {
138 return media_info.video_info().supplemental_codec();
143 std::string GetSupplementalProfiles(
const MediaInfo& media_info) {
144 CHECK(OnlyOneTrue(media_info.has_video_info(), media_info.has_audio_info(),
145 media_info.has_text_info()));
147 if (media_info.has_video_info() &&
148 media_info.video_info().has_compatible_brand()) {
149 return FourCCToString(
150 static_cast<media::FourCC
>(media_info.video_info().compatible_brand()));
155 std::string GetBaseCodec(
const MediaInfo& media_info) {
157 if (media_info.has_video_info()) {
158 codec = media_info.video_info().codec();
159 }
else if (media_info.has_audio_info()) {
160 codec = media_info.audio_info().codec();
161 }
else if (media_info.has_text_info()) {
162 codec = media_info.text_info().codec();
166 size_t dot = codec.find(
'.');
167 if (dot != std::string::npos) {
173 std::string GetAdaptationSetKey(
const MediaInfo& media_info,
177 if (media_info.has_video_info()) {
178 key.append(
"video:");
179 }
else if (media_info.has_audio_info()) {
180 key.append(
"audio:");
181 }
else if (media_info.has_text_info()) {
182 key.append(MediaInfo_TextInfo_TextType_Name(media_info.text_info().type()));
185 key.append(
"unknown:");
188 if (media_info.has_dash_label())
189 key.append(media_info.dash_label() +
":");
191 key.append(MediaInfo_ContainerType_Name(media_info.container_type()));
194 key.append(GetBaseCodec(media_info));
196 if (GetBaseCodec(media_info).find(
"dvh") == 0) {
200 key.append(std::to_string(kTransferFunctionPQ));
201 }
else if (media_info.video_info().has_transfer_characteristics()) {
204 std::to_string(media_info.video_info().transfer_characteristics()));
208 key.append(GetLanguage(media_info));
212 if (media_info.video_info().has_playback_rate()) {
213 key.append(
":trick_play");
216 if (!media_info.dash_accessibilities().empty()) {
217 key.append(
":accessibility_");
218 for (
const std::string& accessibility : media_info.dash_accessibilities())
219 key.append(accessibility);
222 if (!media_info.dash_roles().empty()) {
223 key.append(
":roles_");
224 for (
const std::string& role : media_info.dash_roles())
231 std::string FloatToXmlString(
double number) {
233 std::string formatted = absl::StrFormat(
"%.6f", number);
234 size_t decimalPos = formatted.find(
'.');
235 if (decimalPos != std::string::npos) {
236 size_t lastNonZeroPos = formatted.find_last_not_of(
'0');
237 if (lastNonZeroPos >= decimalPos) {
238 formatted.erase(lastNonZeroPos + 1);
240 if (formatted.back() ==
'.') {
241 formatted.pop_back();
248 std::string SecondsToXmlDuration(
double seconds) {
254 return absl::StrFormat(
"PT%sS", FloatToXmlString(seconds));
257 bool GetDurationAttribute(xmlNodePtr node,
float* duration) {
260 static const char kDuration[] =
"duration";
261 xml::scoped_xml_ptr<xmlChar> duration_value(
262 xmlGetProp(node, BAD_CAST kDuration));
267 double duration_double_precision = 0.0;
268 if (!absl::SimpleAtod(
reinterpret_cast<const char*
>(duration_value.get()),
269 &duration_double_precision)) {
273 *duration =
static_cast<float>(duration_double_precision);
277 bool MoreThanOneTrue(
bool b1,
bool b2,
bool b3) {
278 return (b1 && b2) || (b2 && b3) || (b3 && b1);
281 bool AtLeastOneTrue(
bool b1,
bool b2,
bool b3) {
282 return b1 || b2 || b3;
285 bool OnlyOneTrue(
bool b1,
bool b2,
bool b3) {
286 return !MoreThanOneTrue(b1, b2, b3) && AtLeastOneTrue(b1, b2, b3);
290 bool HexToUUID(
const std::string& data, std::string* uuid_format) {
292 const size_t kExpectedUUIDSize = 16;
293 if (data.size() != kExpectedUUIDSize) {
294 LOG(ERROR) <<
"UUID size is expected to be " << kExpectedUUIDSize
295 <<
" but is " << data.size() <<
" and the data in hex is "
296 << absl::BytesToHexString(data);
300 const std::string hex_encoded =
301 absl::AsciiStrToLower(absl::BytesToHexString(data));
302 DCHECK_EQ(hex_encoded.size(), kExpectedUUIDSize * 2);
303 std::string_view all(hex_encoded);
307 std::string_view first = all.substr(0, 8);
308 std::string_view second = all.substr(8, 4);
309 std::string_view third = all.substr(12, 4);
310 std::string_view fourth = all.substr(16, 4);
311 std::string_view fifth = all.substr(20, 12);
314 const size_t kHumanReadableUUIDSize = 36;
315 uuid_format->reserve(kHumanReadableUUIDSize);
316 absl::StrAppendFormat(uuid_format,
"%s-%s-%s-%s-%s", first, second, third,
321 void UpdateContentProtectionPsshHelper(
322 const std::string& drm_uuid,
323 const std::string& pssh,
324 std::list<ContentProtectionElement>* content_protection_elements) {
325 const std::string drm_uuid_schemd_id_uri_form =
"urn:uuid:" + drm_uuid;
326 for (std::list<ContentProtectionElement>::iterator protection =
327 content_protection_elements->begin();
328 protection != content_protection_elements->end(); ++protection) {
329 if (protection->scheme_id_uri != drm_uuid_schemd_id_uri_form) {
333 for (std::vector<Element>::iterator subelement =
334 protection->subelements.begin();
335 subelement != protection->subelements.end(); ++subelement) {
336 if (subelement->name == kPsshElementName) {
339 protection->subelements.erase(subelement);
360 ContentProtectionElement content_protection;
361 content_protection.scheme_id_uri = drm_uuid_schemd_id_uri_form;
367 content_protection_elements->push_back(content_protection);
375 const char kMarlinUUID[] =
"5e629af5-38da-4063-8977-97ffbd9902d4";
377 const char kFairPlayUUID[] =
"94ce86fb-07ff-4f43-adb8-93d2fa968ca2";
379 const char kPlayReadyUUID[] =
"9a04f079-9840-4286-ab92-e65be0885f95";
383 const char kContentProtectionValueMSPR20[] =
"MSPR 2.0";
385 Element GenerateMarlinContentIds(
const std::string& key_id) {
387 static const char kMarlinContentIdName[] =
"mas:MarlinContentId";
388 static const char kMarlinContentIdPrefix[] =
"urn:marlin:kid:";
389 static const char kMarlinContentIdsName[] =
"mas:MarlinContentIds";
391 Element marlin_content_id;
392 marlin_content_id.name = kMarlinContentIdName;
393 marlin_content_id.content =
394 kMarlinContentIdPrefix +
395 absl::AsciiStrToLower(absl::BytesToHexString(key_id));
397 Element marlin_content_ids;
398 marlin_content_ids.name = kMarlinContentIdsName;
399 marlin_content_ids.subelements.push_back(marlin_content_id);
401 return marlin_content_ids;
404 Element GenerateCencPsshElement(
const std::string& pssh) {
405 std::string base64_encoded_pssh;
406 absl::Base64Escape(std::string_view(pssh.data(), pssh.size()),
407 &base64_encoded_pssh);
409 cenc_pssh.name = kPsshElementName;
410 cenc_pssh.content = base64_encoded_pssh;
416 Element GenerateMsprProElement(
const std::string& pssh) {
417 std::unique_ptr<media::PsshBoxBuilder> b =
419 reinterpret_cast<const uint8_t*
>(pssh.data()),
423 const std::vector<uint8_t> *p_pssh = &b->pssh_data();
424 std::string base64_encoded_mspr;
426 std::string_view(
reinterpret_cast<const char*
>(p_pssh->data()),
428 &base64_encoded_mspr);
430 mspr_pro.name = kMsproElementName;
431 mspr_pro.content = base64_encoded_mspr;
437 template <
typename ContentProtectionParent>
438 void AddContentProtectionElementsHelperTemplated(
439 const MediaInfo& media_info,
440 ContentProtectionParent* parent) {
442 if (!media_info.has_protected_content())
445 const MediaInfo::ProtectedContent& protected_content =
446 media_info.protected_content();
450 const bool is_mp4_container =
451 media_info.container_type() == MediaInfo::CONTAINER_MP4;
452 std::string key_id_uuid_format;
453 if (protected_content.has_default_key_id() &&
454 !IsKeyRotationDefaultKeyId(protected_content.default_key_id())) {
455 if (!
HexToUUID(protected_content.default_key_id(), &key_id_uuid_format)) {
456 LOG(ERROR) <<
"Failed to convert default key ID into UUID format.";
460 if (is_mp4_container) {
461 ContentProtectionElement mp4_content_protection;
462 mp4_content_protection.scheme_id_uri = kEncryptedMp4Scheme;
463 mp4_content_protection.value = protected_content.protection_scheme();
464 if (!key_id_uuid_format.empty()) {
465 mp4_content_protection.additional_attributes[
"cenc:default_KID"] =
469 parent->AddContentProtectionElement(mp4_content_protection);
472 for (
const auto& entry : protected_content.content_protection_entry()) {
473 if (!entry.has_uuid()) {
475 <<
"ContentProtectionEntry was specified but no UUID is set for "
476 << entry.name_version() <<
", skipping.";
480 ContentProtectionElement drm_content_protection;
482 if (entry.has_name_version())
483 drm_content_protection.value = entry.name_version();
485 if (entry.uuid() == kFairPlayUUID) {
486 VLOG(1) <<
"Skipping FairPlay ContentProtection element as FairPlay does "
487 "not support DASH signaling.";
489 }
else if (entry.uuid() == kMarlinUUID) {
491 drm_content_protection.scheme_id_uri =
492 "urn:uuid:" + absl::AsciiStrToUpper(entry.uuid());
493 drm_content_protection.subelements.push_back(
494 GenerateMarlinContentIds(protected_content.default_key_id()));
496 drm_content_protection.scheme_id_uri =
"urn:uuid:" + entry.uuid();
497 if (!entry.pssh().empty()) {
498 drm_content_protection.subelements.push_back(
499 GenerateCencPsshElement(entry.pssh()));
500 if(entry.uuid() == kPlayReadyUUID && protected_content.include_mspr_pro()) {
501 drm_content_protection.subelements.push_back(
502 GenerateMsprProElement(entry.pssh()));
503 drm_content_protection.value = kContentProtectionValueMSPR20;
508 if (!key_id_uuid_format.empty() && !is_mp4_container) {
509 drm_content_protection.additional_attributes[
"cenc:default_KID"] =
513 parent->AddContentProtectionElement(drm_content_protection);
516 if (protected_content.content_protection_entry().size() == 0) {
517 VLOG(1) <<
"The media is encrypted but no content protection specified "
518 <<
"(can happen with key rotation).";
525 AddContentProtectionElementsHelperTemplated(media_info, parent);
530 AddContentProtectionElementsHelperTemplated(media_info, parent);
All the methods that are virtual are virtual for mocking.
bool HexToUUID(const std::string &data, std::string *uuid_format)
std::string LanguageToShortestForm(const std::string &language)
void AddContentProtectionElements(const MediaInfo &media_info, Representation *parent)