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);
307 template <
typename ContentProtectionParent>
308 void AddContentProtectionElementsHelperTemplated(
309 const MediaInfo& media_info,
310 ContentProtectionParent* parent) {
312 if (!media_info.has_protected_content())
315 const MediaInfo::ProtectedContent& protected_content =
316 media_info.protected_content();
320 const bool is_mp4_container =
321 media_info.container_type() == MediaInfo::CONTAINER_MP4;
322 std::string key_id_uuid_format;
323 if (protected_content.has_default_key_id() &&
324 !IsKeyRotationDefaultKeyId(protected_content.default_key_id())) {
325 if (!
HexToUUID(protected_content.default_key_id(), &key_id_uuid_format)) {
326 LOG(ERROR) <<
"Failed to convert default key ID into UUID format.";
330 if (is_mp4_container) {
332 mp4_content_protection.scheme_id_uri = kEncryptedMp4Scheme;
333 mp4_content_protection.value = protected_content.protection_scheme();
334 if (!key_id_uuid_format.empty()) {
335 mp4_content_protection.additional_attributes[
"cenc:default_KID"] =
339 parent->AddContentProtectionElement(mp4_content_protection);
342 for (
const auto& entry : protected_content.content_protection_entry()) {
343 if (!entry.has_uuid()) {
345 <<
"ContentProtectionEntry was specified but no UUID is set for " 346 << entry.name_version() <<
", skipping.";
351 drm_content_protection.scheme_id_uri =
"urn:uuid:" + entry.uuid();
352 if (entry.has_name_version())
353 drm_content_protection.value = entry.name_version();
355 if (entry.has_pssh()) {
356 std::string base64_encoded_pssh;
358 base::StringPiece(entry.pssh().data(), entry.pssh().size()),
359 &base64_encoded_pssh);
361 cenc_pssh.name = kPsshElementName;
362 cenc_pssh.content = base64_encoded_pssh;
363 drm_content_protection.subelements.push_back(cenc_pssh);
366 if (!key_id_uuid_format.empty() && !is_mp4_container) {
367 drm_content_protection.additional_attributes[
"cenc:default_KID"] =
371 parent->AddContentProtectionElement(drm_content_protection);
374 VLOG_IF(1, protected_content.content_protection_entry().size() == 0)
375 <<
"The media is encrypted but no content protection specified (can " 376 "happen with key rotation).";
382 AddContentProtectionElementsHelperTemplated(media_info, parent);
387 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)