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)) {
26 if (format ==
"vtt" &&
27 (media_info.container_type() == MediaInfo::CONTAINER_MP4)) {
38 bool HasVODOnlyFields(
const MediaInfo& media_info) {
39 return media_info.has_init_range() || media_info.has_index_range() ||
40 media_info.has_media_file_name();
43 bool HasLiveOnlyFields(
const MediaInfo& media_info) {
44 return media_info.has_init_segment_name() ||
45 media_info.has_segment_template() ||
46 media_info.has_segment_duration_seconds();
49 void RemoveDuplicateAttributes(
50 ContentProtectionElement* content_protection_element) {
51 DCHECK(content_protection_element);
52 typedef std::map<std::string, std::string> AttributesMap;
54 AttributesMap& attributes = content_protection_element->additional_attributes;
55 if (!content_protection_element->value.empty())
56 attributes.erase(
"value");
58 if (!content_protection_element->scheme_id_uri.empty())
59 attributes.erase(
"schemeIdUri");
62 std::string GetLanguage(
const MediaInfo& media_info) {
64 if (media_info.has_audio_info()) {
65 lang = media_info.audio_info().language();
66 }
else if (media_info.has_text_info()) {
67 lang = media_info.text_info().language();
72 std::string GetCodecs(
const MediaInfo& media_info) {
73 CHECK(OnlyOneTrue(media_info.has_video_info(), media_info.has_audio_info(),
74 media_info.has_text_info()));
76 if (media_info.has_video_info()) {
77 if (media_info.container_type() == MediaInfo::CONTAINER_WEBM) {
78 std::string codec = media_info.video_info().codec().substr(0, 4);
88 return media_info.video_info().codec();
91 if (media_info.has_audio_info())
92 return media_info.audio_info().codec();
94 if (media_info.has_text_info())
95 return TextCodecString(media_info);
101 std::string GetBaseCodec(
const MediaInfo& media_info) {
103 if (media_info.has_video_info()) {
104 codec = media_info.video_info().codec();
105 }
else if (media_info.has_audio_info()) {
106 codec = media_info.audio_info().codec();
107 }
else if (media_info.has_text_info()) {
108 codec = media_info.text_info().format();
112 size_t dot = codec.find(
'.');
113 if (dot != std::string::npos) {
119 std::string GetAdaptationSetKey(
const MediaInfo& media_info) {
122 if (media_info.has_video_info()) {
123 key.append(
"video:");
124 }
else if (media_info.has_audio_info()) {
125 key.append(
"audio:");
126 }
else if (media_info.has_text_info()) {
127 key.append(MediaInfo_TextInfo_TextType_Name(media_info.text_info().type()));
130 key.append(
"unknown:");
133 key.append(MediaInfo_ContainerType_Name(media_info.container_type()));
135 key.append(GetBaseCodec(media_info));
137 key.append(GetLanguage(media_info));
141 if (media_info.video_info().has_playback_rate()) {
142 key.append(
":trick_play");
148 std::string SecondsToXmlDuration(
double seconds) {
152 bool GetDurationAttribute(xmlNodePtr node,
float* duration) {
155 static const char kDuration[] =
"duration";
156 xml::scoped_xml_ptr<xmlChar> duration_value(
157 xmlGetProp(node, BAD_CAST kDuration));
162 double duration_double_precision = 0.0;
163 if (!base::StringToDouble(reinterpret_cast<const char*>(duration_value.get()),
164 &duration_double_precision)) {
168 *duration =
static_cast<float>(duration_double_precision);
172 bool MoreThanOneTrue(
bool b1,
bool b2,
bool b3) {
173 return (b1 && b2) || (b2 && b3) || (b3 && b1);
176 bool AtLeastOneTrue(
bool b1,
bool b2,
bool b3) {
return b1 || b2 || b3; }
178 bool OnlyOneTrue(
bool b1,
bool b2,
bool b3) {
179 return !MoreThanOneTrue(b1, b2, b3) && AtLeastOneTrue(b1, b2, b3);
185 std::ostringstream stringstream;
186 stringstream << value;
187 return stringstream.str();
191 bool HexToUUID(
const std::string& data, std::string* uuid_format) {
193 const size_t kExpectedUUIDSize = 16;
194 if (data.size() != kExpectedUUIDSize) {
195 LOG(ERROR) <<
"UUID size is expected to be " << kExpectedUUIDSize
196 <<
" but is " << data.size() <<
" and the data in hex is "
197 << base::HexEncode(data.data(), data.size());
201 const std::string hex_encoded =
202 base::ToLowerASCII(base::HexEncode(data.data(), data.size()));
203 DCHECK_EQ(hex_encoded.size(), kExpectedUUIDSize * 2);
204 base::StringPiece all(hex_encoded);
208 base::StringPiece first = all.substr(0, 8);
209 base::StringPiece second = all.substr(8, 4);
210 base::StringPiece third = all.substr(12, 4);
211 base::StringPiece fourth = all.substr(16, 4);
212 base::StringPiece fifth = all.substr(20, 12);
215 const size_t kHumanReadableUUIDSize = 36;
216 uuid_format->reserve(kHumanReadableUUIDSize);
217 first.CopyToString(uuid_format);
218 uuid_format->append(
"-");
219 second.AppendToString(uuid_format);
220 uuid_format->append(
"-");
221 third.AppendToString(uuid_format);
222 uuid_format->append(
"-");
223 fourth.AppendToString(uuid_format);
224 uuid_format->append(
"-");
225 fifth.AppendToString(uuid_format);
229 void UpdateContentProtectionPsshHelper(
230 const std::string& drm_uuid,
231 const std::string& pssh,
232 std::list<ContentProtectionElement>* content_protection_elements) {
233 const std::string drm_uuid_schemd_id_uri_form =
"urn:uuid:" + drm_uuid;
234 for (std::list<ContentProtectionElement>::iterator protection =
235 content_protection_elements->begin();
236 protection != content_protection_elements->end(); ++protection) {
237 if (protection->scheme_id_uri != drm_uuid_schemd_id_uri_form) {
241 for (std::vector<Element>::iterator subelement =
242 protection->subelements.begin();
243 subelement != protection->subelements.end(); ++subelement) {
244 if (subelement->name == kPsshElementName) {
247 protection->subelements.erase(subelement);
268 ContentProtectionElement content_protection;
269 content_protection.scheme_id_uri = drm_uuid_schemd_id_uri_form;
275 content_protection_elements->push_back(content_protection);
282 template <
typename ContentProtectionParent>
283 void AddContentProtectionElementsHelperTemplated(
284 const MediaInfo& media_info,
285 ContentProtectionParent* parent) {
287 if (!media_info.has_protected_content())
290 const MediaInfo::ProtectedContent& protected_content =
291 media_info.protected_content();
295 const bool is_mp4_container =
296 media_info.container_type() == MediaInfo::CONTAINER_MP4;
297 std::string key_id_uuid_format;
298 if (protected_content.has_default_key_id()) {
299 if (!
HexToUUID(protected_content.default_key_id(), &key_id_uuid_format)) {
300 LOG(ERROR) <<
"Failed to convert default key ID into UUID format.";
304 if (is_mp4_container) {
305 ContentProtectionElement mp4_content_protection;
306 mp4_content_protection.scheme_id_uri = kEncryptedMp4Scheme;
307 mp4_content_protection.value = protected_content.protection_scheme();
308 if (!key_id_uuid_format.empty()) {
309 mp4_content_protection.additional_attributes[
"cenc:default_KID"] =
313 parent->AddContentProtectionElement(mp4_content_protection);
316 for (
int i = 0; i < protected_content.content_protection_entry().size();
318 const MediaInfo::ProtectedContent::ContentProtectionEntry& entry =
319 protected_content.content_protection_entry(i);
320 if (!entry.has_uuid()) {
322 <<
"ContentProtectionEntry was specified but no UUID is set for "
323 << entry.name_version() <<
", skipping.";
327 ContentProtectionElement drm_content_protection;
328 drm_content_protection.scheme_id_uri =
"urn:uuid:" + entry.uuid();
329 if (entry.has_name_version())
330 drm_content_protection.value = entry.name_version();
332 if (entry.has_pssh()) {
333 std::string base64_encoded_pssh;
335 base::StringPiece(entry.pssh().data(), entry.pssh().size()),
336 &base64_encoded_pssh);
338 cenc_pssh.name = kPsshElementName;
339 cenc_pssh.content = base64_encoded_pssh;
340 drm_content_protection.subelements.push_back(cenc_pssh);
343 if (!key_id_uuid_format.empty() && !is_mp4_container) {
344 drm_content_protection.additional_attributes[
"cenc:default_KID"] =
348 parent->AddContentProtectionElement(drm_content_protection);
351 LOG_IF(WARNING, protected_content.content_protection_entry().size() == 0)
352 <<
"The media is encrypted but no content protection specified.";
358 AddContentProtectionElementsHelperTemplated(media_info, parent);
363 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)