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) {
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);
176 std::ostringstream stringstream;
177 stringstream << value;
178 return stringstream.str();
182 bool HexToUUID(
const std::string& data, std::string* uuid_format) {
184 const size_t kExpectedUUIDSize = 16;
185 if (data.size() != kExpectedUUIDSize) {
186 LOG(ERROR) <<
"UUID size is expected to be " << kExpectedUUIDSize
187 <<
" but is " << data.size() <<
" and the data in hex is "
188 << base::HexEncode(data.data(), data.size());
192 const std::string hex_encoded =
193 base::ToLowerASCII(base::HexEncode(data.data(), data.size()));
194 DCHECK_EQ(hex_encoded.size(), kExpectedUUIDSize * 2);
195 base::StringPiece all(hex_encoded);
199 base::StringPiece first = all.substr(0, 8);
200 base::StringPiece second = all.substr(8, 4);
201 base::StringPiece third = all.substr(12, 4);
202 base::StringPiece fourth = all.substr(16, 4);
203 base::StringPiece fifth = all.substr(20, 12);
206 const size_t kHumanReadableUUIDSize = 36;
207 uuid_format->reserve(kHumanReadableUUIDSize);
208 first.CopyToString(uuid_format);
209 uuid_format->append(
"-");
210 second.AppendToString(uuid_format);
211 uuid_format->append(
"-");
212 third.AppendToString(uuid_format);
213 uuid_format->append(
"-");
214 fourth.AppendToString(uuid_format);
215 uuid_format->append(
"-");
216 fifth.AppendToString(uuid_format);
220 void UpdateContentProtectionPsshHelper(
221 const std::string& drm_uuid,
222 const std::string& pssh,
223 std::list<ContentProtectionElement>* content_protection_elements) {
224 const std::string drm_uuid_schemd_id_uri_form =
"urn:uuid:" + drm_uuid;
225 for (std::list<ContentProtectionElement>::iterator protection =
226 content_protection_elements->begin();
227 protection != content_protection_elements->end(); ++protection) {
228 if (protection->scheme_id_uri != drm_uuid_schemd_id_uri_form) {
232 for (std::vector<Element>::iterator subelement =
233 protection->subelements.begin();
234 subelement != protection->subelements.end(); ++subelement) {
235 if (subelement->name == kPsshElementName) {
238 protection->subelements.erase(subelement);
259 ContentProtectionElement content_protection;
260 content_protection.scheme_id_uri = drm_uuid_schemd_id_uri_form;
266 content_protection_elements->push_back(content_protection);
273 template <
typename ContentProtectionParent>
274 void AddContentProtectionElementsHelperTemplated(
275 const MediaInfo& media_info,
276 ContentProtectionParent* parent) {
278 if (!media_info.has_protected_content())
281 const MediaInfo::ProtectedContent& protected_content =
282 media_info.protected_content();
286 const bool is_mp4_container =
287 media_info.container_type() == MediaInfo::CONTAINER_MP4;
288 std::string key_id_uuid_format;
289 if (protected_content.has_default_key_id()) {
290 if (!
HexToUUID(protected_content.default_key_id(), &key_id_uuid_format)) {
291 LOG(ERROR) <<
"Failed to convert default key ID into UUID format.";
295 if (is_mp4_container) {
296 ContentProtectionElement mp4_content_protection;
297 mp4_content_protection.scheme_id_uri = kEncryptedMp4Scheme;
298 mp4_content_protection.value = protected_content.protection_scheme();
299 if (!key_id_uuid_format.empty()) {
300 mp4_content_protection.additional_attributes[
"cenc:default_KID"] =
304 parent->AddContentProtectionElement(mp4_content_protection);
307 for (
int i = 0; i < protected_content.content_protection_entry().size();
309 const MediaInfo::ProtectedContent::ContentProtectionEntry& entry =
310 protected_content.content_protection_entry(i);
311 if (!entry.has_uuid()) {
313 <<
"ContentProtectionEntry was specified but no UUID is set for "
314 << entry.name_version() <<
", skipping.";
318 ContentProtectionElement drm_content_protection;
319 drm_content_protection.scheme_id_uri =
"urn:uuid:" + entry.uuid();
320 if (entry.has_name_version())
321 drm_content_protection.value = entry.name_version();
323 if (entry.has_pssh()) {
324 std::string base64_encoded_pssh;
326 base::StringPiece(entry.pssh().data(), entry.pssh().size()),
327 &base64_encoded_pssh);
329 cenc_pssh.name = kPsshElementName;
330 cenc_pssh.content = base64_encoded_pssh;
331 drm_content_protection.subelements.push_back(cenc_pssh);
334 if (!key_id_uuid_format.empty() && !is_mp4_container) {
335 drm_content_protection.additional_attributes[
"cenc:default_KID"] =
339 parent->AddContentProtectionElement(drm_content_protection);
342 LOG_IF(WARNING, protected_content.content_protection_entry().size() == 0)
343 <<
"The media is encrypted but no content protection specified.";
349 AddContentProtectionElementsHelperTemplated(media_info, parent);
354 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)