7 #include "packager/mpd/base/mpd_utils.h" 9 #include <gflags/gflags.h> 10 #include <libxml/tree.h> 12 #include "packager/base/base64.h" 13 #include "packager/base/logging.h" 14 #include "packager/base/strings/string_number_conversions.h" 15 #include "packager/base/strings/string_util.h" 16 #include "packager/media/base/language_utils.h" 17 #include "packager/mpd/base/adaptation_set.h" 18 #include "packager/mpd/base/content_protection_element.h" 19 #include "packager/mpd/base/representation.h" 20 #include "packager/mpd/base/xml/scoped_xml_ptr.h" 23 use_legacy_vp9_codec_string,
25 "Use legacy vp9 codec string 'vp9' if set to true; otherwise new style " 26 "vp09.xx.xx.xx... codec string will be used. Default to false as indicated " 27 "in https://github.com/google/shaka-packager/issues/406, all major " 28 "browsers and platforms already support the new 'vp09' codec string.");
33 bool IsKeyRotationDefaultKeyId(
const std::string& key_id) {
34 for (
char c : key_id) {
41 std::string TextCodecString(
const MediaInfo& media_info) {
42 CHECK(media_info.has_text_info());
43 const auto container_type = media_info.container_type();
47 if (container_type == MediaInfo::CONTAINER_TEXT) {
53 const std::string& codec = media_info.text_info().codec();
54 if (codec ==
"ttml" && container_type == MediaInfo::CONTAINER_MP4) {
63 bool HasVODOnlyFields(
const MediaInfo& media_info) {
64 return media_info.has_init_range() || media_info.has_index_range() ||
65 media_info.has_media_file_url();
68 bool HasLiveOnlyFields(
const MediaInfo& media_info) {
69 return media_info.has_init_segment_url() ||
70 media_info.has_segment_template_url();
73 void RemoveDuplicateAttributes(
74 ContentProtectionElement* content_protection_element) {
75 DCHECK(content_protection_element);
76 typedef std::map<std::string, std::string> AttributesMap;
78 AttributesMap& attributes = content_protection_element->additional_attributes;
79 if (!content_protection_element->value.empty())
80 attributes.erase(
"value");
82 if (!content_protection_element->scheme_id_uri.empty())
83 attributes.erase(
"schemeIdUri");
86 std::string GetLanguage(
const MediaInfo& media_info) {
88 if (media_info.has_audio_info()) {
89 lang = media_info.audio_info().language();
90 }
else if (media_info.has_text_info()) {
91 lang = media_info.text_info().language();
96 std::string GetCodecs(
const MediaInfo& media_info) {
97 CHECK(OnlyOneTrue(media_info.has_video_info(), media_info.has_audio_info(),
98 media_info.has_text_info()));
100 if (media_info.has_video_info()) {
101 if (media_info.container_type() == MediaInfo::CONTAINER_WEBM) {
102 std::string codec = media_info.video_info().codec().substr(0, 4);
109 if (FLAGS_use_legacy_vp9_codec_string) {
114 return media_info.video_info().codec();
117 if (media_info.has_audio_info())
118 return media_info.audio_info().codec();
120 if (media_info.has_text_info())
121 return TextCodecString(media_info);
127 std::string GetBaseCodec(
const MediaInfo& media_info) {
129 if (media_info.has_video_info()) {
130 codec = media_info.video_info().codec();
131 }
else if (media_info.has_audio_info()) {
132 codec = media_info.audio_info().codec();
133 }
else if (media_info.has_text_info()) {
134 codec = media_info.text_info().codec();
138 size_t dot = codec.find(
'.');
139 if (dot != std::string::npos) {
145 std::string GetAdaptationSetKey(
const MediaInfo& media_info) {
148 if (media_info.has_video_info()) {
149 key.append(
"video:");
150 }
else if (media_info.has_audio_info()) {
151 key.append(
"audio:");
152 }
else if (media_info.has_text_info()) {
153 key.append(MediaInfo_TextInfo_TextType_Name(media_info.text_info().type()));
156 key.append(
"unknown:");
159 key.append(MediaInfo_ContainerType_Name(media_info.container_type()));
161 key.append(GetBaseCodec(media_info));
163 key.append(GetLanguage(media_info));
167 if (media_info.video_info().has_playback_rate()) {
168 key.append(
":trick_play");
174 std::string SecondsToXmlDuration(
double seconds) {
180 return "PT" + base::DoubleToString(seconds) +
"S";
183 bool GetDurationAttribute(xmlNodePtr node,
float* duration) {
186 static const char kDuration[] =
"duration";
187 xml::scoped_xml_ptr<xmlChar> duration_value(
188 xmlGetProp(node, BAD_CAST kDuration));
193 double duration_double_precision = 0.0;
194 if (!base::StringToDouble(reinterpret_cast<const char*>(duration_value.get()),
195 &duration_double_precision)) {
199 *duration =
static_cast<float>(duration_double_precision);
203 bool MoreThanOneTrue(
bool b1,
bool b2,
bool b3) {
204 return (b1 && b2) || (b2 && b3) || (b3 && b1);
207 bool AtLeastOneTrue(
bool b1,
bool b2,
bool b3) {
208 return b1 || b2 || b3;
211 bool OnlyOneTrue(
bool b1,
bool b2,
bool b3) {
212 return !MoreThanOneTrue(b1, b2, b3) && AtLeastOneTrue(b1, b2, b3);
216 bool HexToUUID(
const std::string& data, std::string* uuid_format) {
218 const size_t kExpectedUUIDSize = 16;
219 if (data.size() != kExpectedUUIDSize) {
220 LOG(ERROR) <<
"UUID size is expected to be " << kExpectedUUIDSize
221 <<
" but is " << data.size() <<
" and the data in hex is " 222 << base::HexEncode(data.data(), data.size());
226 const std::string hex_encoded =
227 base::ToLowerASCII(base::HexEncode(data.data(), data.size()));
228 DCHECK_EQ(hex_encoded.size(), kExpectedUUIDSize * 2);
229 base::StringPiece all(hex_encoded);
233 base::StringPiece first = all.substr(0, 8);
234 base::StringPiece second = all.substr(8, 4);
235 base::StringPiece third = all.substr(12, 4);
236 base::StringPiece fourth = all.substr(16, 4);
237 base::StringPiece fifth = all.substr(20, 12);
240 const size_t kHumanReadableUUIDSize = 36;
241 uuid_format->reserve(kHumanReadableUUIDSize);
242 first.CopyToString(uuid_format);
243 uuid_format->append(
"-");
244 second.AppendToString(uuid_format);
245 uuid_format->append(
"-");
246 third.AppendToString(uuid_format);
247 uuid_format->append(
"-");
248 fourth.AppendToString(uuid_format);
249 uuid_format->append(
"-");
250 fifth.AppendToString(uuid_format);
254 void UpdateContentProtectionPsshHelper(
255 const std::string& drm_uuid,
256 const std::string& pssh,
257 std::list<ContentProtectionElement>* content_protection_elements) {
258 const std::string drm_uuid_schemd_id_uri_form =
"urn:uuid:" + drm_uuid;
259 for (std::list<ContentProtectionElement>::iterator protection =
260 content_protection_elements->begin();
261 protection != content_protection_elements->end(); ++protection) {
262 if (protection->scheme_id_uri != drm_uuid_schemd_id_uri_form) {
266 for (std::vector<Element>::iterator subelement =
267 protection->subelements.begin();
268 subelement != protection->subelements.end(); ++subelement) {
269 if (subelement->name == kPsshElementName) {
272 protection->subelements.erase(subelement);
294 content_protection.scheme_id_uri = drm_uuid_schemd_id_uri_form;
300 content_protection_elements->push_back(content_protection);
308 const char kMarlinUUID[] =
"5e629af5-38da-4063-8977-97ffbd9902d4";
311 const char kFairPlayUUID[] =
"29701fe4-3cc7-4a34-8c5b-ae90c7439a47";
313 Element GenerateMarlinContentIds(
const std::string& key_id) {
315 static const char kMarlinContentIdName[] =
"mas:MarlinContentId";
316 static const char kMarlinContentIdPrefix[] =
"urn:marlin:kid:";
317 static const char kMarlinContentIdsName[] =
"mas:MarlinContentIds";
320 marlin_content_id.name = kMarlinContentIdName;
321 marlin_content_id.content =
322 kMarlinContentIdPrefix +
323 base::ToLowerASCII(base::HexEncode(key_id.data(), key_id.size()));
326 marlin_content_ids.name = kMarlinContentIdsName;
327 marlin_content_ids.subelements.push_back(marlin_content_id);
329 return marlin_content_ids;
332 Element GenerateCencPsshElement(
const std::string& pssh) {
333 std::string base64_encoded_pssh;
334 base::Base64Encode(base::StringPiece(pssh.data(), pssh.size()),
335 &base64_encoded_pssh);
337 cenc_pssh.name = kPsshElementName;
338 cenc_pssh.content = base64_encoded_pssh;
344 template <
typename ContentProtectionParent>
345 void AddContentProtectionElementsHelperTemplated(
346 const MediaInfo& media_info,
347 ContentProtectionParent* parent) {
349 if (!media_info.has_protected_content())
352 const MediaInfo::ProtectedContent& protected_content =
353 media_info.protected_content();
357 const bool is_mp4_container =
358 media_info.container_type() == MediaInfo::CONTAINER_MP4;
359 std::string key_id_uuid_format;
360 if (protected_content.has_default_key_id() &&
361 !IsKeyRotationDefaultKeyId(protected_content.default_key_id())) {
362 if (!
HexToUUID(protected_content.default_key_id(), &key_id_uuid_format)) {
363 LOG(ERROR) <<
"Failed to convert default key ID into UUID format.";
367 if (is_mp4_container) {
369 mp4_content_protection.scheme_id_uri = kEncryptedMp4Scheme;
370 mp4_content_protection.value = protected_content.protection_scheme();
371 if (!key_id_uuid_format.empty()) {
372 mp4_content_protection.additional_attributes[
"cenc:default_KID"] =
376 parent->AddContentProtectionElement(mp4_content_protection);
379 for (
const auto& entry : protected_content.content_protection_entry()) {
380 if (!entry.has_uuid()) {
382 <<
"ContentProtectionEntry was specified but no UUID is set for " 383 << entry.name_version() <<
", skipping.";
389 if (entry.has_name_version())
390 drm_content_protection.value = entry.name_version();
392 if (entry.uuid() == kFairPlayUUID) {
393 VLOG(1) <<
"Skipping FairPlay ContentProtection element as FairPlay does " 394 "not support DASH signaling.";
396 }
else if (entry.uuid() == kMarlinUUID) {
398 drm_content_protection.scheme_id_uri =
399 "urn:uuid:" + base::ToUpperASCII(entry.uuid());
400 drm_content_protection.subelements.push_back(
401 GenerateMarlinContentIds(protected_content.default_key_id()));
403 drm_content_protection.scheme_id_uri =
"urn:uuid:" + entry.uuid();
404 if (!entry.pssh().empty()) {
405 drm_content_protection.subelements.push_back(
406 GenerateCencPsshElement(entry.pssh()));
410 if (!key_id_uuid_format.empty() && !is_mp4_container) {
411 drm_content_protection.additional_attributes[
"cenc:default_KID"] =
415 parent->AddContentProtectionElement(drm_content_protection);
418 VLOG_IF(1, protected_content.content_protection_entry().size() == 0)
419 <<
"The media is encrypted but no content protection specified (can " 420 "happen with key rotation).";
426 AddContentProtectionElementsHelperTemplated(media_info, parent);
431 AddContentProtectionElementsHelperTemplated(media_info, parent);
std::string LanguageToShortestForm(const std::string &language)
All the methods that are virtual are virtual for mocking.
void AddContentProtectionElements(const MediaInfo &media_info, Representation *parent)
bool HexToUUID(const std::string &data, std::string *uuid_format)