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 std::string TextCodecString(
const MediaInfo& media_info) {
19 CHECK(media_info.has_text_info());
20 const std::string& format = media_info.text_info().format();
22 if (format ==
"ttml" &&
23 (media_info.container_type() == MediaInfo::CONTAINER_MP4)) {
34 bool HasVODOnlyFields(
const MediaInfo& media_info) {
35 return media_info.has_init_range() || media_info.has_index_range() ||
36 media_info.has_media_file_name() ||
37 media_info.has_media_duration_seconds();
40 bool HasLiveOnlyFields(
const MediaInfo& media_info) {
41 return media_info.has_init_segment_name() ||
42 media_info.has_segment_template() ||
43 media_info.has_segment_duration_seconds();
46 void RemoveDuplicateAttributes(
47 ContentProtectionElement* content_protection_element) {
48 DCHECK(content_protection_element);
49 typedef std::map<std::string, std::string> AttributesMap;
51 AttributesMap& attributes = content_protection_element->additional_attributes;
52 if (!content_protection_element->value.empty())
53 attributes.erase(
"value");
55 if (!content_protection_element->scheme_id_uri.empty())
56 attributes.erase(
"schemeIdUri");
59 std::string GetLanguage(
const MediaInfo& media_info) {
61 if (media_info.has_audio_info()) {
62 lang = media_info.audio_info().language();
63 }
else if (media_info.has_text_info()) {
64 lang = media_info.text_info().language();
69 std::string GetCodecs(
const MediaInfo& media_info) {
70 CHECK(OnlyOneTrue(media_info.has_video_info(), media_info.has_audio_info(),
71 media_info.has_text_info()));
73 if (media_info.has_video_info()) {
74 if (media_info.container_type() == MediaInfo::CONTAINER_WEBM) {
75 std::string codec = media_info.video_info().codec().substr(0, 4);
85 return media_info.video_info().codec();
88 if (media_info.has_audio_info())
89 return media_info.audio_info().codec();
91 if (media_info.has_text_info())
92 return TextCodecString(media_info);
98 std::string GetBaseCodec(
const MediaInfo& media_info) {
100 if (media_info.has_video_info()) {
101 codec = media_info.video_info().codec();
102 }
else if (media_info.has_audio_info()) {
103 codec = media_info.audio_info().codec();
104 }
else if (media_info.has_text_info()) {
105 codec = media_info.text_info().format();
109 size_t dot = codec.find(
'.');
110 if (dot != std::string::npos) {
116 std::string GetAdaptationSetKey(
const MediaInfo& media_info) {
119 if (media_info.has_video_info()) {
120 key.append(
"video:");
121 }
else if (media_info.has_audio_info()) {
122 key.append(
"audio:");
123 }
else if (media_info.has_text_info()) {
124 key.append(MediaInfo_TextInfo_TextType_Name(media_info.text_info().type()));
127 key.append(
"unknown:");
130 key.append(MediaInfo_ContainerType_Name(media_info.container_type()));
132 key.append(GetBaseCodec(media_info));
134 key.append(GetLanguage(media_info));
139 std::string SecondsToXmlDuration(
double seconds) {
140 return "PT" + base::DoubleToString(seconds) +
"S";
143 bool GetDurationAttribute(xmlNodePtr node,
float* duration) {
146 static const char kDuration[] =
"duration";
147 xml::scoped_xml_ptr<xmlChar> duration_value(
148 xmlGetProp(node, BAD_CAST kDuration));
153 double duration_double_precision = 0.0;
154 if (!base::StringToDouble(reinterpret_cast<const char*>(duration_value.get()),
155 &duration_double_precision)) {
159 *duration =
static_cast<float>(duration_double_precision);
163 bool MoreThanOneTrue(
bool b1,
bool b2,
bool b3) {
164 return (b1 && b2) || (b2 && b3) || (b3 && b1);
167 bool AtLeastOneTrue(
bool b1,
bool b2,
bool b3) {
return b1 || b2 || b3; }
169 bool OnlyOneTrue(
bool b1,
bool b2,
bool b3) {
170 return !MoreThanOneTrue(b1, b2, b3) && AtLeastOneTrue(b1, b2, b3);
174 bool HexToUUID(
const std::string& data, std::string* uuid_format) {
176 const size_t kExpectedUUIDSize = 16;
177 if (data.size() != kExpectedUUIDSize) {
178 LOG(ERROR) <<
"UUID size is expected to be " << kExpectedUUIDSize
179 <<
" but is " << data.size() <<
" and the data in hex is "
180 << base::HexEncode(data.data(), data.size());
184 const std::string hex_encoded =
185 base::StringToLowerASCII(base::HexEncode(data.data(), data.size()));
186 DCHECK_EQ(hex_encoded.size(), kExpectedUUIDSize * 2);
187 base::StringPiece all(hex_encoded);
191 base::StringPiece first = all.substr(0, 8);
192 base::StringPiece second = all.substr(8, 4);
193 base::StringPiece third = all.substr(12, 4);
194 base::StringPiece fourth = all.substr(16, 4);
195 base::StringPiece fifth = all.substr(20, 12);
198 const size_t kHumanReadableUUIDSize = 36;
199 uuid_format->reserve(kHumanReadableUUIDSize);
200 first.CopyToString(uuid_format);
201 uuid_format->append(
"-");
202 second.AppendToString(uuid_format);
203 uuid_format->append(
"-");
204 third.AppendToString(uuid_format);
205 uuid_format->append(
"-");
206 fourth.AppendToString(uuid_format);
207 uuid_format->append(
"-");
208 fifth.AppendToString(uuid_format);
212 void UpdateContentProtectionPsshHelper(
213 const std::string& drm_uuid,
214 const std::string& pssh,
215 std::list<ContentProtectionElement>* content_protection_elements) {
216 const std::string drm_uuid_schemd_id_uri_form =
"urn:uuid:" + drm_uuid;
217 for (std::list<ContentProtectionElement>::iterator protection =
218 content_protection_elements->begin();
219 protection != content_protection_elements->end(); ++protection) {
220 if (protection->scheme_id_uri != drm_uuid_schemd_id_uri_form) {
224 for (std::vector<Element>::iterator subelement =
225 protection->subelements.begin();
226 subelement != protection->subelements.end(); ++subelement) {
227 if (subelement->name == kPsshElementName) {
230 protection->subelements.erase(subelement);
251 ContentProtectionElement content_protection;
252 content_protection.scheme_id_uri = drm_uuid_schemd_id_uri_form;
258 content_protection_elements->push_back(content_protection);
265 template <
typename ContentProtectionParent>
266 void AddContentProtectionElementsHelperTemplated(
267 const MediaInfo& media_info,
268 ContentProtectionParent* parent) {
270 if (!media_info.has_protected_content())
273 const MediaInfo::ProtectedContent& protected_content =
274 media_info.protected_content();
278 const bool is_mp4_container =
279 media_info.container_type() == MediaInfo::CONTAINER_MP4;
280 std::string key_id_uuid_format;
281 if (protected_content.has_default_key_id()) {
282 if (!
HexToUUID(protected_content.default_key_id(), &key_id_uuid_format)) {
283 LOG(ERROR) <<
"Failed to convert default key ID into UUID format.";
287 if (is_mp4_container) {
288 ContentProtectionElement mp4_content_protection;
289 mp4_content_protection.scheme_id_uri = kEncryptedMp4Scheme;
290 mp4_content_protection.value = protected_content.protection_scheme();
291 if (!key_id_uuid_format.empty()) {
292 mp4_content_protection.additional_attributes[
"cenc:default_KID"] =
296 parent->AddContentProtectionElement(mp4_content_protection);
299 for (
int i = 0; i < protected_content.content_protection_entry().size();
301 const MediaInfo::ProtectedContent::ContentProtectionEntry& entry =
302 protected_content.content_protection_entry(i);
303 if (!entry.has_uuid()) {
305 <<
"ContentProtectionEntry was specified but no UUID is set for "
306 << entry.name_version() <<
", skipping.";
310 ContentProtectionElement drm_content_protection;
311 drm_content_protection.scheme_id_uri =
"urn:uuid:" + entry.uuid();
312 if (entry.has_name_version())
313 drm_content_protection.value = entry.name_version();
315 if (entry.has_pssh()) {
316 std::string base64_encoded_pssh;
318 base::StringPiece(entry.pssh().data(), entry.pssh().size()),
319 &base64_encoded_pssh);
321 cenc_pssh.name = kPsshElementName;
322 cenc_pssh.content = base64_encoded_pssh;
323 drm_content_protection.subelements.push_back(cenc_pssh);
326 if (!key_id_uuid_format.empty() && !is_mp4_container) {
327 drm_content_protection.additional_attributes[
"cenc:default_KID"] =
331 parent->AddContentProtectionElement(drm_content_protection);
334 LOG_IF(WARNING, protected_content.content_protection_entry().size() == 0)
335 <<
"The media is encrypted but no content protection specified.";
341 AddContentProtectionElementsHelperTemplated(media_info, parent);
346 AddContentProtectionElementsHelperTemplated(media_info, parent);
void AddContentProtectionElements(const MediaInfo &media_info, Representation *parent)
bool HexToUUID(const std::string &data, std::string *uuid_format)