7 #include "packager/mpd/base/mpd_utils.h" 9 #include <libxml/tree.h> 11 #include "packager/base/base64.h" 12 #include "packager/base/logging.h" 13 #include "packager/base/strings/string_number_conversions.h" 14 #include "packager/base/strings/string_util.h" 15 #include "packager/media/base/language_utils.h" 16 #include "packager/mpd/base/adaptation_set.h" 17 #include "packager/mpd/base/content_protection_element.h" 18 #include "packager/mpd/base/representation.h" 19 #include "packager/mpd/base/xml/scoped_xml_ptr.h" 24 bool IsKeyRotationDefaultKeyId(
const std::string& key_id) {
25 for (
char c : key_id) {
32 std::string TextCodecString(
const MediaInfo& media_info) {
33 CHECK(media_info.has_text_info());
34 const auto container_type = media_info.container_type();
38 if (container_type == MediaInfo::CONTAINER_TEXT) {
44 const std::string& codec = media_info.text_info().codec();
45 if (codec ==
"ttml" && container_type == MediaInfo::CONTAINER_MP4) {
54 bool HasVODOnlyFields(
const MediaInfo& media_info) {
55 return media_info.has_init_range() || media_info.has_index_range() ||
56 media_info.has_media_file_url();
59 bool HasLiveOnlyFields(
const MediaInfo& media_info) {
60 return media_info.has_init_segment_url() ||
61 media_info.has_segment_template_url();
64 void RemoveDuplicateAttributes(
65 ContentProtectionElement* content_protection_element) {
66 DCHECK(content_protection_element);
67 typedef std::map<std::string, std::string> AttributesMap;
69 AttributesMap& attributes = content_protection_element->additional_attributes;
70 if (!content_protection_element->value.empty())
71 attributes.erase(
"value");
73 if (!content_protection_element->scheme_id_uri.empty())
74 attributes.erase(
"schemeIdUri");
77 std::string GetLanguage(
const MediaInfo& media_info) {
79 if (media_info.has_audio_info()) {
80 lang = media_info.audio_info().language();
81 }
else if (media_info.has_text_info()) {
82 lang = media_info.text_info().language();
87 std::string GetCodecs(
const MediaInfo& media_info) {
88 CHECK(OnlyOneTrue(media_info.has_video_info(), media_info.has_audio_info(),
89 media_info.has_text_info()));
91 if (media_info.has_video_info()) {
92 if (media_info.container_type() == MediaInfo::CONTAINER_WEBM) {
93 std::string codec = media_info.video_info().codec().substr(0, 4);
103 return media_info.video_info().codec();
106 if (media_info.has_audio_info())
107 return media_info.audio_info().codec();
109 if (media_info.has_text_info())
110 return TextCodecString(media_info);
116 std::string GetBaseCodec(
const MediaInfo& media_info) {
118 if (media_info.has_video_info()) {
119 codec = media_info.video_info().codec();
120 }
else if (media_info.has_audio_info()) {
121 codec = media_info.audio_info().codec();
122 }
else if (media_info.has_text_info()) {
123 codec = media_info.text_info().codec();
127 size_t dot = codec.find(
'.');
128 if (dot != std::string::npos) {
134 std::string GetAdaptationSetKey(
const MediaInfo& media_info) {
137 if (media_info.has_video_info()) {
138 key.append(
"video:");
139 }
else if (media_info.has_audio_info()) {
140 key.append(
"audio:");
141 }
else if (media_info.has_text_info()) {
142 key.append(MediaInfo_TextInfo_TextType_Name(media_info.text_info().type()));
145 key.append(
"unknown:");
148 key.append(MediaInfo_ContainerType_Name(media_info.container_type()));
150 key.append(GetBaseCodec(media_info));
152 key.append(GetLanguage(media_info));
156 if (media_info.video_info().has_playback_rate()) {
157 key.append(
":trick_play");
163 std::string SecondsToXmlDuration(
double seconds) {
169 return "PT" + base::DoubleToString(seconds) +
"S";
172 bool GetDurationAttribute(xmlNodePtr node,
float* duration) {
175 static const char kDuration[] =
"duration";
176 xml::scoped_xml_ptr<xmlChar> duration_value(
177 xmlGetProp(node, BAD_CAST kDuration));
182 double duration_double_precision = 0.0;
183 if (!base::StringToDouble(reinterpret_cast<const char*>(duration_value.get()),
184 &duration_double_precision)) {
188 *duration =
static_cast<float>(duration_double_precision);
192 bool MoreThanOneTrue(
bool b1,
bool b2,
bool b3) {
193 return (b1 && b2) || (b2 && b3) || (b3 && b1);
196 bool AtLeastOneTrue(
bool b1,
bool b2,
bool b3) {
197 return b1 || b2 || b3;
200 bool OnlyOneTrue(
bool b1,
bool b2,
bool b3) {
201 return !MoreThanOneTrue(b1, b2, b3) && AtLeastOneTrue(b1, b2, b3);
205 bool HexToUUID(
const std::string& data, std::string* uuid_format) {
207 const size_t kExpectedUUIDSize = 16;
208 if (data.size() != kExpectedUUIDSize) {
209 LOG(ERROR) <<
"UUID size is expected to be " << kExpectedUUIDSize
210 <<
" but is " << data.size() <<
" and the data in hex is " 211 << base::HexEncode(data.data(), data.size());
215 const std::string hex_encoded =
216 base::ToLowerASCII(base::HexEncode(data.data(), data.size()));
217 DCHECK_EQ(hex_encoded.size(), kExpectedUUIDSize * 2);
218 base::StringPiece all(hex_encoded);
222 base::StringPiece first = all.substr(0, 8);
223 base::StringPiece second = all.substr(8, 4);
224 base::StringPiece third = all.substr(12, 4);
225 base::StringPiece fourth = all.substr(16, 4);
226 base::StringPiece fifth = all.substr(20, 12);
229 const size_t kHumanReadableUUIDSize = 36;
230 uuid_format->reserve(kHumanReadableUUIDSize);
231 first.CopyToString(uuid_format);
232 uuid_format->append(
"-");
233 second.AppendToString(uuid_format);
234 uuid_format->append(
"-");
235 third.AppendToString(uuid_format);
236 uuid_format->append(
"-");
237 fourth.AppendToString(uuid_format);
238 uuid_format->append(
"-");
239 fifth.AppendToString(uuid_format);
243 void UpdateContentProtectionPsshHelper(
244 const std::string& drm_uuid,
245 const std::string& pssh,
246 std::list<ContentProtectionElement>* content_protection_elements) {
247 const std::string drm_uuid_schemd_id_uri_form =
"urn:uuid:" + drm_uuid;
248 for (std::list<ContentProtectionElement>::iterator protection =
249 content_protection_elements->begin();
250 protection != content_protection_elements->end(); ++protection) {
251 if (protection->scheme_id_uri != drm_uuid_schemd_id_uri_form) {
255 for (std::vector<Element>::iterator subelement =
256 protection->subelements.begin();
257 subelement != protection->subelements.end(); ++subelement) {
258 if (subelement->name == kPsshElementName) {
261 protection->subelements.erase(subelement);
283 content_protection.scheme_id_uri = drm_uuid_schemd_id_uri_form;
289 content_protection_elements->push_back(content_protection);
296 template <
typename ContentProtectionParent>
297 void AddContentProtectionElementsHelperTemplated(
298 const MediaInfo& media_info,
299 ContentProtectionParent* parent) {
301 if (!media_info.has_protected_content())
304 const MediaInfo::ProtectedContent& protected_content =
305 media_info.protected_content();
309 const bool is_mp4_container =
310 media_info.container_type() == MediaInfo::CONTAINER_MP4;
311 std::string key_id_uuid_format;
312 if (protected_content.has_default_key_id() &&
313 !IsKeyRotationDefaultKeyId(protected_content.default_key_id())) {
314 if (!
HexToUUID(protected_content.default_key_id(), &key_id_uuid_format)) {
315 LOG(ERROR) <<
"Failed to convert default key ID into UUID format.";
319 if (is_mp4_container) {
321 mp4_content_protection.scheme_id_uri = kEncryptedMp4Scheme;
322 mp4_content_protection.value = protected_content.protection_scheme();
323 if (!key_id_uuid_format.empty()) {
324 mp4_content_protection.additional_attributes[
"cenc:default_KID"] =
328 parent->AddContentProtectionElement(mp4_content_protection);
331 for (
const auto& entry : protected_content.content_protection_entry()) {
332 if (!entry.has_uuid()) {
334 <<
"ContentProtectionEntry was specified but no UUID is set for " 335 << entry.name_version() <<
", skipping.";
340 drm_content_protection.scheme_id_uri =
"urn:uuid:" + entry.uuid();
341 if (entry.has_name_version())
342 drm_content_protection.value = entry.name_version();
344 if (entry.has_pssh()) {
345 std::string base64_encoded_pssh;
347 base::StringPiece(entry.pssh().data(), entry.pssh().size()),
348 &base64_encoded_pssh);
350 cenc_pssh.name = kPsshElementName;
351 cenc_pssh.content = base64_encoded_pssh;
352 drm_content_protection.subelements.push_back(cenc_pssh);
355 if (!key_id_uuid_format.empty() && !is_mp4_container) {
356 drm_content_protection.additional_attributes[
"cenc:default_KID"] =
360 parent->AddContentProtectionElement(drm_content_protection);
363 VLOG_IF(1, protected_content.content_protection_entry().size() == 0)
364 <<
"The media is encrypted but no content protection specified (can " 365 "happen with key rotation).";
371 AddContentProtectionElementsHelperTemplated(media_info, parent);
376 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)