7 #include "packager/mpd/base/mpd_utils.h"
9 #include <libxml/tree.h>
11 #include "packager/base/logging.h"
12 #include "packager/base/strings/string_number_conversions.h"
13 #include "packager/mpd/base/xml/scoped_xml_ptr.h"
18 bool IsKeyRotationDefaultKeyId(
const std::string& key_id) {
19 for (
char c : key_id) {
26 std::string TextCodecString(
const MediaInfo& media_info) {
27 CHECK(media_info.has_text_info());
28 const std::string& format = media_info.text_info().format();
30 if (format ==
"ttml" &&
31 (media_info.container_type() == MediaInfo::CONTAINER_MP4)) {
34 if (format ==
"vtt" &&
35 (media_info.container_type() == MediaInfo::CONTAINER_MP4)) {
46 bool HasVODOnlyFields(
const MediaInfo& media_info) {
47 return media_info.has_init_range() || media_info.has_index_range() ||
48 media_info.has_media_file_name();
51 bool HasLiveOnlyFields(
const MediaInfo& media_info) {
52 return media_info.has_init_segment_name() ||
53 media_info.has_segment_template() ||
54 media_info.has_segment_duration_seconds();
57 void RemoveDuplicateAttributes(
58 ContentProtectionElement* content_protection_element) {
59 DCHECK(content_protection_element);
60 typedef std::map<std::string, std::string> AttributesMap;
62 AttributesMap& attributes = content_protection_element->additional_attributes;
63 if (!content_protection_element->value.empty())
64 attributes.erase(
"value");
66 if (!content_protection_element->scheme_id_uri.empty())
67 attributes.erase(
"schemeIdUri");
70 std::string GetLanguage(
const MediaInfo& media_info) {
72 if (media_info.has_audio_info()) {
73 lang = media_info.audio_info().language();
74 }
else if (media_info.has_text_info()) {
75 lang = media_info.text_info().language();
80 std::string GetCodecs(
const MediaInfo& media_info) {
81 CHECK(OnlyOneTrue(media_info.has_video_info(), media_info.has_audio_info(),
82 media_info.has_text_info()));
84 if (media_info.has_video_info()) {
85 if (media_info.container_type() == MediaInfo::CONTAINER_WEBM) {
86 std::string codec = media_info.video_info().codec().substr(0, 4);
96 return media_info.video_info().codec();
99 if (media_info.has_audio_info())
100 return media_info.audio_info().codec();
102 if (media_info.has_text_info())
103 return TextCodecString(media_info);
109 std::string GetBaseCodec(
const MediaInfo& media_info) {
111 if (media_info.has_video_info()) {
112 codec = media_info.video_info().codec();
113 }
else if (media_info.has_audio_info()) {
114 codec = media_info.audio_info().codec();
115 }
else if (media_info.has_text_info()) {
116 codec = media_info.text_info().format();
120 size_t dot = codec.find(
'.');
121 if (dot != std::string::npos) {
127 std::string GetAdaptationSetKey(
const MediaInfo& media_info) {
130 if (media_info.has_video_info()) {
131 key.append(
"video:");
132 }
else if (media_info.has_audio_info()) {
133 key.append(
"audio:");
134 }
else if (media_info.has_text_info()) {
135 key.append(MediaInfo_TextInfo_TextType_Name(media_info.text_info().type()));
138 key.append(
"unknown:");
141 key.append(MediaInfo_ContainerType_Name(media_info.container_type()));
143 key.append(GetBaseCodec(media_info));
145 key.append(GetLanguage(media_info));
149 if (media_info.video_info().has_playback_rate()) {
150 key.append(
":trick_play");
156 std::string SecondsToXmlDuration(
double seconds) {
160 bool GetDurationAttribute(xmlNodePtr node,
float* duration) {
163 static const char kDuration[] =
"duration";
164 xml::scoped_xml_ptr<xmlChar> duration_value(
165 xmlGetProp(node, BAD_CAST kDuration));
170 double duration_double_precision = 0.0;
171 if (!base::StringToDouble(reinterpret_cast<const char*>(duration_value.get()),
172 &duration_double_precision)) {
176 *duration =
static_cast<float>(duration_double_precision);
180 bool MoreThanOneTrue(
bool b1,
bool b2,
bool b3) {
181 return (b1 && b2) || (b2 && b3) || (b3 && b1);
184 bool AtLeastOneTrue(
bool b1,
bool b2,
bool b3) {
return b1 || b2 || b3; }
186 bool OnlyOneTrue(
bool b1,
bool b2,
bool b3) {
187 return !MoreThanOneTrue(b1, b2, b3) && AtLeastOneTrue(b1, b2, b3);
193 std::ostringstream stringstream;
194 stringstream << value;
195 return stringstream.str();
199 bool HexToUUID(
const std::string& data, std::string* uuid_format) {
201 const size_t kExpectedUUIDSize = 16;
202 if (data.size() != kExpectedUUIDSize) {
203 LOG(ERROR) <<
"UUID size is expected to be " << kExpectedUUIDSize
204 <<
" but is " << data.size() <<
" and the data in hex is "
205 << base::HexEncode(data.data(), data.size());
209 const std::string hex_encoded =
210 base::ToLowerASCII(base::HexEncode(data.data(), data.size()));
211 DCHECK_EQ(hex_encoded.size(), kExpectedUUIDSize * 2);
212 base::StringPiece all(hex_encoded);
216 base::StringPiece first = all.substr(0, 8);
217 base::StringPiece second = all.substr(8, 4);
218 base::StringPiece third = all.substr(12, 4);
219 base::StringPiece fourth = all.substr(16, 4);
220 base::StringPiece fifth = all.substr(20, 12);
223 const size_t kHumanReadableUUIDSize = 36;
224 uuid_format->reserve(kHumanReadableUUIDSize);
225 first.CopyToString(uuid_format);
226 uuid_format->append(
"-");
227 second.AppendToString(uuid_format);
228 uuid_format->append(
"-");
229 third.AppendToString(uuid_format);
230 uuid_format->append(
"-");
231 fourth.AppendToString(uuid_format);
232 uuid_format->append(
"-");
233 fifth.AppendToString(uuid_format);
237 void UpdateContentProtectionPsshHelper(
238 const std::string& drm_uuid,
239 const std::string& pssh,
240 std::list<ContentProtectionElement>* content_protection_elements) {
241 const std::string drm_uuid_schemd_id_uri_form =
"urn:uuid:" + drm_uuid;
242 for (std::list<ContentProtectionElement>::iterator protection =
243 content_protection_elements->begin();
244 protection != content_protection_elements->end(); ++protection) {
245 if (protection->scheme_id_uri != drm_uuid_schemd_id_uri_form) {
249 for (std::vector<Element>::iterator subelement =
250 protection->subelements.begin();
251 subelement != protection->subelements.end(); ++subelement) {
252 if (subelement->name == kPsshElementName) {
255 protection->subelements.erase(subelement);
276 ContentProtectionElement content_protection;
277 content_protection.scheme_id_uri = drm_uuid_schemd_id_uri_form;
283 content_protection_elements->push_back(content_protection);
290 template <
typename ContentProtectionParent>
291 void AddContentProtectionElementsHelperTemplated(
292 const MediaInfo& media_info,
293 ContentProtectionParent* parent) {
295 if (!media_info.has_protected_content())
298 const MediaInfo::ProtectedContent& protected_content =
299 media_info.protected_content();
303 const bool is_mp4_container =
304 media_info.container_type() == MediaInfo::CONTAINER_MP4;
305 std::string key_id_uuid_format;
306 if (protected_content.has_default_key_id() &&
307 !IsKeyRotationDefaultKeyId(protected_content.default_key_id())) {
308 if (!
HexToUUID(protected_content.default_key_id(), &key_id_uuid_format)) {
309 LOG(ERROR) <<
"Failed to convert default key ID into UUID format.";
313 if (is_mp4_container) {
314 ContentProtectionElement mp4_content_protection;
315 mp4_content_protection.scheme_id_uri = kEncryptedMp4Scheme;
316 mp4_content_protection.value = protected_content.protection_scheme();
317 if (!key_id_uuid_format.empty()) {
318 mp4_content_protection.additional_attributes[
"cenc:default_KID"] =
322 parent->AddContentProtectionElement(mp4_content_protection);
325 for (
const auto& entry : protected_content.content_protection_entry()) {
326 if (!entry.has_uuid()) {
328 <<
"ContentProtectionEntry was specified but no UUID is set for "
329 << entry.name_version() <<
", skipping.";
333 ContentProtectionElement drm_content_protection;
334 drm_content_protection.scheme_id_uri =
"urn:uuid:" + entry.uuid();
335 if (entry.has_name_version())
336 drm_content_protection.value = entry.name_version();
338 if (entry.has_pssh()) {
339 std::string base64_encoded_pssh;
341 base::StringPiece(entry.pssh().data(), entry.pssh().size()),
342 &base64_encoded_pssh);
344 cenc_pssh.name = kPsshElementName;
345 cenc_pssh.content = base64_encoded_pssh;
346 drm_content_protection.subelements.push_back(cenc_pssh);
349 if (!key_id_uuid_format.empty() && !is_mp4_container) {
350 drm_content_protection.additional_attributes[
"cenc:default_KID"] =
354 parent->AddContentProtectionElement(drm_content_protection);
357 VLOG_IF(1, protected_content.content_protection_entry().size() == 0)
358 <<
"The media is encrypted but no content protection specified (can "
359 "happen with key rotation).";
365 AddContentProtectionElementsHelperTemplated(media_info, parent);
370 AddContentProtectionElementsHelperTemplated(media_info, parent);
void AddContentProtectionElements(const MediaInfo &media_info, Representation *parent)
bool HexToUUID(const std::string &data, std::string *uuid_format)
std::string DoubleToString(double value)