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"
15 namespace edash_packager {
17 bool HasVODOnlyFields(
const MediaInfo& media_info) {
18 return media_info.has_init_range() || media_info.has_index_range() ||
19 media_info.has_media_file_name() ||
20 media_info.has_media_duration_seconds();
23 bool HasLiveOnlyFields(
const MediaInfo& media_info) {
24 return media_info.has_init_segment_name() ||
25 media_info.has_segment_template() ||
26 media_info.has_segment_duration_seconds();
29 void RemoveDuplicateAttributes(
30 ContentProtectionElement* content_protection_element) {
31 DCHECK(content_protection_element);
32 typedef std::map<std::string, std::string> AttributesMap;
34 AttributesMap& attributes = content_protection_element->additional_attributes;
35 if (!content_protection_element->value.empty())
36 attributes.erase(
"value");
38 if (!content_protection_element->scheme_id_uri.empty())
39 attributes.erase(
"schemeIdUri");
42 std::string GetCodecs(
const MediaInfo& media_info) {
43 std::string video_codec;
44 if (media_info.has_video_info())
45 video_codec = media_info.video_info().codec();
47 std::string audio_codec;
48 if (media_info.has_audio_info())
49 audio_codec = media_info.audio_info().codec();
51 if (!video_codec.empty() && !audio_codec.empty()) {
52 return video_codec +
"," + audio_codec;
53 }
else if (!video_codec.empty()) {
55 }
else if (!audio_codec.empty()) {
62 std::string SecondsToXmlDuration(
double seconds) {
63 return "PT" + base::DoubleToString(seconds) +
"S";
66 bool GetDurationAttribute(xmlNodePtr node,
float* duration) {
69 static const char kDuration[] =
"duration";
70 xml::ScopedXmlPtr<xmlChar>::type duration_value(
71 xmlGetProp(node, BAD_CAST kDuration));
76 double duration_double_precision = 0.0;
77 if (!base::StringToDouble(reinterpret_cast<const char*>(duration_value.get()),
78 &duration_double_precision)) {
82 *duration =
static_cast<float>(duration_double_precision);
86 bool MoreThanOneTrue(
bool b1,
bool b2,
bool b3) {
87 return (b1 && b2) || (b2 && b3) || (b3 && b1);
90 bool AtLeastOneTrue(
bool b1,
bool b2,
bool b3) {
return b1 || b2 || b3; }
92 bool OnlyOneTrue(
bool b1,
bool b2,
bool b3) {
93 return !MoreThanOneTrue(b1, b2, b3) && AtLeastOneTrue(b1, b2, b3);
97 bool HexToUUID(
const std::string& data, std::string* uuid_format) {
99 const size_t kExpectedUUIDSize = 16;
100 if (data.size() != kExpectedUUIDSize) {
101 LOG(ERROR) <<
"UUID size is expected to be " << kExpectedUUIDSize
102 <<
" but is " << data.size() <<
" and the data in hex is "
103 << base::HexEncode(data.data(), data.size());
107 const std::string hex_encoded =
108 base::StringToLowerASCII(base::HexEncode(data.data(), data.size()));
109 DCHECK_EQ(hex_encoded.size(), kExpectedUUIDSize * 2);
110 base::StringPiece all(hex_encoded);
114 base::StringPiece first = all.substr(0, 8);
115 base::StringPiece second = all.substr(8, 4);
116 base::StringPiece third = all.substr(12, 4);
117 base::StringPiece fourth = all.substr(16, 4);
118 base::StringPiece fifth = all.substr(20, 12);
121 const size_t kHumanReadableUUIDSize = 36;
122 uuid_format->reserve(kHumanReadableUUIDSize);
123 first.CopyToString(uuid_format);
124 uuid_format->append(
"-");
125 second.AppendToString(uuid_format);
126 uuid_format->append(
"-");
127 third.AppendToString(uuid_format);
128 uuid_format->append(
"-");
129 fourth.AppendToString(uuid_format);
130 uuid_format->append(
"-");
131 fifth.AppendToString(uuid_format);
135 void UpdateContentProtectionPsshHelper(
136 const std::string& drm_uuid,
137 const std::string& pssh,
138 std::list<ContentProtectionElement>* content_protection_elements) {
139 const std::string drm_uuid_schemd_id_uri_form =
"urn:uuid:" + drm_uuid;
140 for (std::list<ContentProtectionElement>::iterator protection =
141 content_protection_elements->begin();
142 protection != content_protection_elements->end(); ++protection) {
143 if (protection->scheme_id_uri != drm_uuid_schemd_id_uri_form) {
147 for (std::vector<Element>::iterator subelement =
148 protection->subelements.begin();
149 subelement != protection->subelements.end(); ++subelement) {
150 if (subelement->name == kPsshElementName) {
153 protection->subelements.erase(subelement);
174 ContentProtectionElement content_protection;
175 content_protection.scheme_id_uri = drm_uuid_schemd_id_uri_form;
181 content_protection_elements->push_back(content_protection);
188 template <
typename ContentProtectionParent>
189 void AddContentProtectionElementsHelperTemplated(
190 const MediaInfo& media_info,
191 ContentProtectionParent* parent) {
193 if (!media_info.has_protected_content())
196 const MediaInfo::ProtectedContent& protected_content =
197 media_info.protected_content();
201 const bool is_mp4_container =
202 media_info.container_type() == MediaInfo::CONTAINER_MP4;
203 if (is_mp4_container) {
204 ContentProtectionElement mp4_content_protection;
205 mp4_content_protection.scheme_id_uri = kEncryptedMp4Scheme;
206 mp4_content_protection.value = kEncryptedMp4Value;
207 if (protected_content.has_default_key_id()) {
208 std::string key_id_uuid_format;
209 if (
HexToUUID(protected_content.default_key_id(), &key_id_uuid_format)) {
210 mp4_content_protection.additional_attributes[
"cenc:default_KID"] =
213 LOG(ERROR) <<
"Failed to convert default key ID into UUID format.";
217 parent->AddContentProtectionElement(mp4_content_protection);
220 for (
int i = 0; i < protected_content.content_protection_entry().size();
222 const MediaInfo::ProtectedContent::ContentProtectionEntry& entry =
223 protected_content.content_protection_entry(i);
224 if (!entry.has_uuid()) {
226 <<
"ContentProtectionEntry was specified but no UUID is set for "
227 << entry.name_version() <<
", skipping.";
231 ContentProtectionElement drm_content_protection;
232 drm_content_protection.scheme_id_uri =
"urn:uuid:" + entry.uuid();
233 if (entry.has_name_version())
234 drm_content_protection.value = entry.name_version();
236 if (entry.has_pssh()) {
237 std::string base64_encoded_pssh;
238 base::Base64Encode(entry.pssh(), &base64_encoded_pssh);
240 cenc_pssh.name = kPsshElementName;
241 cenc_pssh.content = base64_encoded_pssh;
242 drm_content_protection.subelements.push_back(cenc_pssh);
245 parent->AddContentProtectionElement(drm_content_protection);
248 LOG_IF(WARNING, protected_content.content_protection_entry().size() == 0)
249 <<
"The media is encrypted but no content protection specified.";
255 AddContentProtectionElementsHelperTemplated(media_info, parent);
260 AddContentProtectionElementsHelperTemplated(media_info, parent);
void AddContentProtectionElements(const MediaInfo &media_info, Representation *parent)
bool HexToUUID(const std::string &data, std::string *uuid_format)