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 {
18 std::string TextCodecString(
19 const edash_packager::MediaInfo& media_info) {
20 CHECK(media_info.has_text_info());
21 const std::string& format = media_info.text_info().format();
23 if (format ==
"ttml" &&
24 (media_info.container_type() == MediaInfo::CONTAINER_MP4)) {
35 bool HasVODOnlyFields(
const MediaInfo& media_info) {
36 return media_info.has_init_range() || media_info.has_index_range() ||
37 media_info.has_media_file_name() ||
38 media_info.has_media_duration_seconds();
41 bool HasLiveOnlyFields(
const MediaInfo& media_info) {
42 return media_info.has_init_segment_name() ||
43 media_info.has_segment_template() ||
44 media_info.has_segment_duration_seconds();
47 void RemoveDuplicateAttributes(
48 ContentProtectionElement* content_protection_element) {
49 DCHECK(content_protection_element);
50 typedef std::map<std::string, std::string> AttributesMap;
52 AttributesMap& attributes = content_protection_element->additional_attributes;
53 if (!content_protection_element->value.empty())
54 attributes.erase(
"value");
56 if (!content_protection_element->scheme_id_uri.empty())
57 attributes.erase(
"schemeIdUri");
60 std::string GetCodecs(
const MediaInfo& media_info) {
61 CHECK(OnlyOneTrue(media_info.has_video_info(), media_info.has_audio_info(),
62 media_info.has_text_info()));
64 if (media_info.has_video_info())
65 return media_info.video_info().codec();
67 if (media_info.has_audio_info())
68 return media_info.audio_info().codec();
70 if (media_info.has_text_info())
71 return TextCodecString(media_info);
77 std::string SecondsToXmlDuration(
double seconds) {
78 return "PT" + base::DoubleToString(seconds) +
"S";
81 bool GetDurationAttribute(xmlNodePtr node,
float* duration) {
84 static const char kDuration[] =
"duration";
85 xml::scoped_xml_ptr<xmlChar> duration_value(
86 xmlGetProp(node, BAD_CAST kDuration));
91 double duration_double_precision = 0.0;
92 if (!base::StringToDouble(reinterpret_cast<const char*>(duration_value.get()),
93 &duration_double_precision)) {
97 *duration =
static_cast<float>(duration_double_precision);
101 bool MoreThanOneTrue(
bool b1,
bool b2,
bool b3) {
102 return (b1 && b2) || (b2 && b3) || (b3 && b1);
105 bool AtLeastOneTrue(
bool b1,
bool b2,
bool b3) {
return b1 || b2 || b3; }
107 bool OnlyOneTrue(
bool b1,
bool b2,
bool b3) {
108 return !MoreThanOneTrue(b1, b2, b3) && AtLeastOneTrue(b1, b2, b3);
112 bool HexToUUID(
const std::string& data, std::string* uuid_format) {
114 const size_t kExpectedUUIDSize = 16;
115 if (data.size() != kExpectedUUIDSize) {
116 LOG(ERROR) <<
"UUID size is expected to be " << kExpectedUUIDSize
117 <<
" but is " << data.size() <<
" and the data in hex is "
118 << base::HexEncode(data.data(), data.size());
122 const std::string hex_encoded =
123 base::StringToLowerASCII(base::HexEncode(data.data(), data.size()));
124 DCHECK_EQ(hex_encoded.size(), kExpectedUUIDSize * 2);
125 base::StringPiece all(hex_encoded);
129 base::StringPiece first = all.substr(0, 8);
130 base::StringPiece second = all.substr(8, 4);
131 base::StringPiece third = all.substr(12, 4);
132 base::StringPiece fourth = all.substr(16, 4);
133 base::StringPiece fifth = all.substr(20, 12);
136 const size_t kHumanReadableUUIDSize = 36;
137 uuid_format->reserve(kHumanReadableUUIDSize);
138 first.CopyToString(uuid_format);
139 uuid_format->append(
"-");
140 second.AppendToString(uuid_format);
141 uuid_format->append(
"-");
142 third.AppendToString(uuid_format);
143 uuid_format->append(
"-");
144 fourth.AppendToString(uuid_format);
145 uuid_format->append(
"-");
146 fifth.AppendToString(uuid_format);
150 void UpdateContentProtectionPsshHelper(
151 const std::string& drm_uuid,
152 const std::string& pssh,
153 std::list<ContentProtectionElement>* content_protection_elements) {
154 const std::string drm_uuid_schemd_id_uri_form =
"urn:uuid:" + drm_uuid;
155 for (std::list<ContentProtectionElement>::iterator protection =
156 content_protection_elements->begin();
157 protection != content_protection_elements->end(); ++protection) {
158 if (protection->scheme_id_uri != drm_uuid_schemd_id_uri_form) {
162 for (std::vector<Element>::iterator subelement =
163 protection->subelements.begin();
164 subelement != protection->subelements.end(); ++subelement) {
165 if (subelement->name == kPsshElementName) {
168 protection->subelements.erase(subelement);
189 ContentProtectionElement content_protection;
190 content_protection.scheme_id_uri = drm_uuid_schemd_id_uri_form;
196 content_protection_elements->push_back(content_protection);
203 template <
typename ContentProtectionParent>
204 void AddContentProtectionElementsHelperTemplated(
205 const MediaInfo& media_info,
206 ContentProtectionParent* parent) {
208 if (!media_info.has_protected_content())
211 const MediaInfo::ProtectedContent& protected_content =
212 media_info.protected_content();
216 const bool is_mp4_container =
217 media_info.container_type() == MediaInfo::CONTAINER_MP4;
218 if (is_mp4_container) {
219 ContentProtectionElement mp4_content_protection;
220 mp4_content_protection.scheme_id_uri = kEncryptedMp4Scheme;
221 mp4_content_protection.value = kEncryptedMp4Value;
222 if (protected_content.has_default_key_id()) {
223 std::string key_id_uuid_format;
224 if (
HexToUUID(protected_content.default_key_id(), &key_id_uuid_format)) {
225 mp4_content_protection.additional_attributes[
"cenc:default_KID"] =
228 LOG(ERROR) <<
"Failed to convert default key ID into UUID format.";
232 parent->AddContentProtectionElement(mp4_content_protection);
235 for (
int i = 0; i < protected_content.content_protection_entry().size();
237 const MediaInfo::ProtectedContent::ContentProtectionEntry& entry =
238 protected_content.content_protection_entry(i);
239 if (!entry.has_uuid()) {
241 <<
"ContentProtectionEntry was specified but no UUID is set for "
242 << entry.name_version() <<
", skipping.";
246 ContentProtectionElement drm_content_protection;
247 drm_content_protection.scheme_id_uri =
"urn:uuid:" + entry.uuid();
248 if (entry.has_name_version())
249 drm_content_protection.value = entry.name_version();
251 if (entry.has_pssh()) {
252 std::string base64_encoded_pssh;
253 base::Base64Encode(entry.pssh(), &base64_encoded_pssh);
255 cenc_pssh.name = kPsshElementName;
256 cenc_pssh.content = base64_encoded_pssh;
257 drm_content_protection.subelements.push_back(cenc_pssh);
260 parent->AddContentProtectionElement(drm_content_protection);
263 LOG_IF(WARNING, protected_content.content_protection_entry().size() == 0)
264 <<
"The media is encrypted but no content protection specified.";
270 AddContentProtectionElementsHelperTemplated(media_info, parent);
275 AddContentProtectionElementsHelperTemplated(media_info, parent);
void AddContentProtectionElements(const MediaInfo &media_info, Representation *parent)
bool HexToUUID(const std::string &data, std::string *uuid_format)