7#include <packager/mpd/base/mpd_utils.h>
18#include <absl/flags/flag.h>
19#include <absl/log/check.h>
20#include <absl/log/log.h>
21#include <absl/strings/ascii.h>
22#include <absl/strings/escaping.h>
23#include <absl/strings/numbers.h>
24#include <absl/strings/str_format.h>
25#include <libxml/tree.h>
26#include <libxml/xmlstring.h>
28#include <packager/macros/logging.h>
29#include <packager/media/base/fourccs.h>
30#include <packager/media/base/language_utils.h>
31#include <packager/media/base/protection_system_specific_info.h>
32#include <packager/mpd/base/adaptation_set.h>
33#include <packager/mpd/base/content_protection_element.h>
34#include <packager/mpd/base/media_info.pb.h>
35#include <packager/mpd/base/representation.h>
36#include <packager/mpd/base/xml/scoped_xml_ptr.h>
40 use_legacy_vp9_codec_string,
42 "Use legacy vp9 codec string 'vp9' if set to true; otherwise new style "
43 "vp09.xx.xx.xx... codec string will be used. Default to false as indicated "
44 "in https://github.com/shaka-project/shaka-packager/issues/406, all major "
45 "browsers and platforms already support the new 'vp09' codec string.");
50bool IsKeyRotationDefaultKeyId(
const std::string& key_id) {
51 for (
char c : key_id) {
58std::string TextCodecString(
const MediaInfo& media_info) {
59 CHECK(media_info.has_text_info());
60 const auto container_type = media_info.container_type();
64 if (container_type == MediaInfo::CONTAINER_TEXT) {
70 const std::string& codec = media_info.text_info().codec();
71 if (codec ==
"ttml" && container_type == MediaInfo::CONTAINER_MP4) {
80bool HasVODOnlyFields(
const MediaInfo& media_info) {
81 return media_info.has_init_range() || media_info.has_index_range() ||
82 media_info.has_media_file_url();
85bool HasLiveOnlyFields(
const MediaInfo& media_info) {
86 return media_info.has_init_segment_url() ||
87 media_info.has_segment_template_url();
90void RemoveDuplicateAttributes(
91 ContentProtectionElement* content_protection_element) {
92 DCHECK(content_protection_element);
93 typedef std::map<std::string, std::string> AttributesMap;
95 AttributesMap& attributes = content_protection_element->additional_attributes;
96 if (!content_protection_element->value.empty())
97 attributes.erase(
"value");
99 if (!content_protection_element->scheme_id_uri.empty())
100 attributes.erase(
"schemeIdUri");
103std::string GetLanguage(
const MediaInfo& media_info) {
105 if (media_info.has_audio_info()) {
106 lang = media_info.audio_info().language();
107 }
else if (media_info.has_text_info()) {
108 lang = media_info.text_info().language();
113std::string GetCodecs(
const MediaInfo& media_info) {
114 CHECK(OnlyOneTrue(media_info.has_video_info(), media_info.has_audio_info(),
115 media_info.has_text_info()));
117 if (media_info.has_video_info()) {
118 if (media_info.container_type() == MediaInfo::CONTAINER_WEBM) {
119 std::string codec = media_info.video_info().codec().substr(0, 4);
126 if (absl::GetFlag(FLAGS_use_legacy_vp9_codec_string)) {
131 return media_info.video_info().codec();
134 if (media_info.has_audio_info())
135 return media_info.audio_info().codec();
137 if (media_info.has_text_info())
138 return TextCodecString(media_info);
144std::string GetSupplementalCodecs(
const MediaInfo& media_info) {
145 CHECK(OnlyOneTrue(media_info.has_video_info(), media_info.has_audio_info(),
146 media_info.has_text_info()));
148 if (media_info.has_video_info() &&
149 media_info.video_info().has_supplemental_codec()) {
150 return media_info.video_info().supplemental_codec();
155std::string GetSupplementalProfiles(
const MediaInfo& media_info) {
156 CHECK(OnlyOneTrue(media_info.has_video_info(), media_info.has_audio_info(),
157 media_info.has_text_info()));
159 if (media_info.has_video_info() &&
160 media_info.video_info().has_compatible_brand()) {
161 return FourCCToString(
162 static_cast<media::FourCC
>(media_info.video_info().compatible_brand()));
167std::string GetBaseCodec(
const MediaInfo& media_info) {
169 if (media_info.has_video_info()) {
170 codec = media_info.video_info().codec();
171 }
else if (media_info.has_audio_info()) {
172 codec = media_info.audio_info().codec();
173 }
else if (media_info.has_text_info()) {
174 codec = media_info.text_info().codec();
178 size_t dot = codec.find(
'.');
179 if (dot != std::string::npos) {
185std::string GetAdaptationSetKey(
const MediaInfo& media_info,
189 if (media_info.has_video_info()) {
190 key.append(
"video:");
191 }
else if (media_info.has_audio_info()) {
192 key.append(
"audio:");
193 }
else if (media_info.has_text_info()) {
194 key.append(MediaInfo_TextInfo_TextType_Name(media_info.text_info().type()));
197 key.append(
"unknown:");
200 if (media_info.has_dash_label())
201 key.append(media_info.dash_label() +
":");
203 key.append(MediaInfo_ContainerType_Name(media_info.container_type()));
206 key.append(GetBaseCodec(media_info));
208 if (GetBaseCodec(media_info).find(
"dvh") == 0) {
212 key.append(std::to_string(kTransferFunctionPQ));
213 }
else if (media_info.video_info().has_transfer_characteristics()) {
216 std::to_string(media_info.video_info().transfer_characteristics()));
220 key.append(GetLanguage(media_info));
224 if (media_info.video_info().has_playback_rate()) {
225 key.append(
":trick_play");
228 if (!media_info.dash_accessibilities().empty()) {
229 key.append(
":accessibility_");
230 for (
const std::string& accessibility : media_info.dash_accessibilities())
231 key.append(accessibility);
234 if (!media_info.dash_roles().empty()) {
235 key.append(
":roles_");
236 for (
const std::string& role : media_info.dash_roles())
243std::string FloatToXmlString(
double number) {
245 std::string formatted = absl::StrFormat(
"%.6f", number);
246 size_t decimalPos = formatted.find(
'.');
247 if (decimalPos != std::string::npos) {
248 size_t lastNonZeroPos = formatted.find_last_not_of(
'0');
249 if (lastNonZeroPos >= decimalPos) {
250 formatted.erase(lastNonZeroPos + 1);
252 if (formatted.back() ==
'.') {
253 formatted.pop_back();
260std::string SecondsToXmlDuration(
double seconds) {
266 return absl::StrFormat(
"PT%sS", FloatToXmlString(seconds));
269bool GetDurationAttribute(xmlNodePtr node,
float* duration) {
272 static const char kDuration[] =
"duration";
273 xml::scoped_xml_ptr<xmlChar> duration_value(
274 xmlGetProp(node, BAD_CAST kDuration));
279 double duration_double_precision = 0.0;
280 if (!absl::SimpleAtod(
reinterpret_cast<const char*
>(duration_value.get()),
281 &duration_double_precision)) {
285 *duration =
static_cast<float>(duration_double_precision);
289bool MoreThanOneTrue(
bool b1,
bool b2,
bool b3) {
290 return (b1 && b2) || (b2 && b3) || (b3 && b1);
293bool AtLeastOneTrue(
bool b1,
bool b2,
bool b3) {
294 return b1 || b2 || b3;
297bool OnlyOneTrue(
bool b1,
bool b2,
bool b3) {
298 return !MoreThanOneTrue(b1, b2, b3) && AtLeastOneTrue(b1, b2, b3);
302bool HexToUUID(
const std::string& data, std::string* uuid_format) {
304 const size_t kExpectedUUIDSize = 16;
305 if (data.size() != kExpectedUUIDSize) {
306 LOG(ERROR) <<
"UUID size is expected to be " << kExpectedUUIDSize
307 <<
" but is " << data.size() <<
" and the data in hex is "
308 << absl::BytesToHexString(data);
312 const std::string hex_encoded =
313 absl::AsciiStrToLower(absl::BytesToHexString(data));
314 DCHECK_EQ(hex_encoded.size(), kExpectedUUIDSize * 2);
315 std::string_view all(hex_encoded);
319 std::string_view first = all.substr(0, 8);
320 std::string_view second = all.substr(8, 4);
321 std::string_view third = all.substr(12, 4);
322 std::string_view fourth = all.substr(16, 4);
323 std::string_view fifth = all.substr(20, 12);
326 const size_t kHumanReadableUUIDSize = 36;
327 uuid_format->reserve(kHumanReadableUUIDSize);
328 absl::StrAppendFormat(uuid_format,
"%s-%s-%s-%s-%s", first, second, third,
333void UpdateContentProtectionPsshHelper(
334 const std::string& drm_uuid,
335 const std::string& pssh,
336 std::list<ContentProtectionElement>* content_protection_elements) {
337 const std::string drm_uuid_schemd_id_uri_form =
"urn:uuid:" + drm_uuid;
338 for (std::list<ContentProtectionElement>::iterator protection =
339 content_protection_elements->begin();
340 protection != content_protection_elements->end(); ++protection) {
341 if (protection->scheme_id_uri != drm_uuid_schemd_id_uri_form) {
345 for (std::vector<Element>::iterator subelement =
346 protection->subelements.begin();
347 subelement != protection->subelements.end(); ++subelement) {
348 if (subelement->name == kPsshElementName) {
351 protection->subelements.erase(subelement);
372 ContentProtectionElement content_protection;
373 content_protection.scheme_id_uri = drm_uuid_schemd_id_uri_form;
379 content_protection_elements->push_back(content_protection);
387const char kMarlinUUID[] =
"5e629af5-38da-4063-8977-97ffbd9902d4";
389const char kFairPlayUUID[] =
"94ce86fb-07ff-4f43-adb8-93d2fa968ca2";
391const char kPlayReadyUUID[] =
"9a04f079-9840-4286-ab92-e65be0885f95";
395const char kContentProtectionValueMSPR20[] =
"MSPR 2.0";
397Element GenerateMarlinContentIds(
const std::string& key_id) {
399 static const char kMarlinContentIdName[] =
"mas:MarlinContentId";
400 static const char kMarlinContentIdPrefix[] =
"urn:marlin:kid:";
401 static const char kMarlinContentIdsName[] =
"mas:MarlinContentIds";
403 Element marlin_content_id;
404 marlin_content_id.name = kMarlinContentIdName;
405 marlin_content_id.content =
406 kMarlinContentIdPrefix +
407 absl::AsciiStrToLower(absl::BytesToHexString(key_id));
409 Element marlin_content_ids;
410 marlin_content_ids.name = kMarlinContentIdsName;
411 marlin_content_ids.subelements.push_back(marlin_content_id);
413 return marlin_content_ids;
416Element GenerateCencPsshElement(
const std::string& pssh) {
417 std::string base64_encoded_pssh;
418 absl::Base64Escape(std::string_view(pssh.data(), pssh.size()),
419 &base64_encoded_pssh);
421 cenc_pssh.name = kPsshElementName;
422 cenc_pssh.content = base64_encoded_pssh;
428Element GenerateMsprProElement(
const std::string& pssh) {
429 std::unique_ptr<media::PsshBoxBuilder> b =
431 reinterpret_cast<const uint8_t*
>(pssh.data()), pssh.size());
433 const std::vector<uint8_t>* p_pssh = &b->pssh_data();
434 std::string base64_encoded_mspr;
436 std::string_view(
reinterpret_cast<const char*
>(p_pssh->data()),
438 &base64_encoded_mspr);
440 mspr_pro.name = kMsproElementName;
441 mspr_pro.content = base64_encoded_mspr;
447template <
typename ContentProtectionParent>
448void AddContentProtectionElementsHelperTemplated(
449 const MediaInfo& media_info,
450 ContentProtectionParent* parent) {
452 if (!media_info.has_protected_content())
455 const MediaInfo::ProtectedContent& protected_content =
456 media_info.protected_content();
460 const bool is_mp4_container =
461 media_info.container_type() == MediaInfo::CONTAINER_MP4;
462 std::string key_id_uuid_format;
463 if (protected_content.has_default_key_id() &&
464 !IsKeyRotationDefaultKeyId(protected_content.default_key_id())) {
465 if (!
HexToUUID(protected_content.default_key_id(), &key_id_uuid_format)) {
466 LOG(ERROR) <<
"Failed to convert default key ID into UUID format.";
470 if (is_mp4_container) {
471 ContentProtectionElement mp4_content_protection;
472 mp4_content_protection.scheme_id_uri = kEncryptedMp4Scheme;
473 mp4_content_protection.value = protected_content.protection_scheme();
474 if (!key_id_uuid_format.empty()) {
475 mp4_content_protection.additional_attributes[
"cenc:default_KID"] =
479 parent->AddContentProtectionElement(mp4_content_protection);
482 for (
const auto& entry : protected_content.content_protection_entry()) {
483 if (!entry.has_uuid()) {
485 <<
"ContentProtectionEntry was specified but no UUID is set for "
486 << entry.name_version() <<
", skipping.";
490 ContentProtectionElement drm_content_protection;
492 if (entry.has_name_version())
493 drm_content_protection.value = entry.name_version();
495 if (entry.uuid() == kFairPlayUUID) {
496 VLOG(1) <<
"Skipping FairPlay ContentProtection element as FairPlay does "
497 "not support DASH signaling.";
499 }
else if (entry.uuid() == kMarlinUUID) {
501 drm_content_protection.scheme_id_uri =
502 "urn:uuid:" + absl::AsciiStrToUpper(entry.uuid());
503 drm_content_protection.subelements.push_back(
504 GenerateMarlinContentIds(protected_content.default_key_id()));
506 drm_content_protection.scheme_id_uri =
"urn:uuid:" + entry.uuid();
507 if (!entry.pssh().empty()) {
508 drm_content_protection.subelements.push_back(
509 GenerateCencPsshElement(entry.pssh()));
510 if (entry.uuid() == kPlayReadyUUID &&
511 protected_content.include_mspr_pro()) {
512 drm_content_protection.subelements.push_back(
513 GenerateMsprProElement(entry.pssh()));
514 drm_content_protection.value = kContentProtectionValueMSPR20;
519 if (!key_id_uuid_format.empty() && !is_mp4_container) {
520 drm_content_protection.additional_attributes[
"cenc:default_KID"] =
524 parent->AddContentProtectionElement(drm_content_protection);
527 if (protected_content.content_protection_entry().size() == 0) {
528 VLOG(1) <<
"The media is encrypted but no content protection specified "
529 <<
"(can happen with key rotation).";
536 AddContentProtectionElementsHelperTemplated(media_info, parent);
541 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)