DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs
mpd_utils.cc
1 // Copyright 2014 Google Inc. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #include "packager/mpd/base/mpd_utils.h"
8 
9 #include <libxml/tree.h>
10 
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"
14 
15 namespace edash_packager {
16 
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();
21 }
22 
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();
27 }
28 
29 void RemoveDuplicateAttributes(
30  ContentProtectionElement* content_protection_element) {
31  DCHECK(content_protection_element);
32  typedef std::map<std::string, std::string> AttributesMap;
33 
34  AttributesMap& attributes = content_protection_element->additional_attributes;
35  if (!content_protection_element->value.empty())
36  attributes.erase("value");
37 
38  if (!content_protection_element->scheme_id_uri.empty())
39  attributes.erase("schemeIdUri");
40 }
41 
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();
46 
47  std::string audio_codec;
48  if (media_info.has_audio_info())
49  audio_codec = media_info.audio_info().codec();
50 
51  if (!video_codec.empty() && !audio_codec.empty()) {
52  return video_codec + "," + audio_codec;
53  } else if (!video_codec.empty()) {
54  return video_codec;
55  } else if (!audio_codec.empty()) {
56  return audio_codec;
57  }
58 
59  return "";
60 }
61 
62 std::string SecondsToXmlDuration(double seconds) {
63  return "PT" + base::DoubleToString(seconds) + "S";
64 }
65 
66 bool GetDurationAttribute(xmlNodePtr node, float* duration) {
67  DCHECK(node);
68  DCHECK(duration);
69  static const char kDuration[] = "duration";
70  xml::ScopedXmlPtr<xmlChar>::type duration_value(
71  xmlGetProp(node, BAD_CAST kDuration));
72 
73  if (!duration_value)
74  return false;
75 
76  double duration_double_precision = 0.0;
77  if (!base::StringToDouble(reinterpret_cast<const char*>(duration_value.get()),
78  &duration_double_precision)) {
79  return false;
80  }
81 
82  *duration = static_cast<float>(duration_double_precision);
83  return true;
84 }
85 
86 bool MoreThanOneTrue(bool b1, bool b2, bool b3) {
87  return (b1 && b2) || (b2 && b3) || (b3 && b1);
88 }
89 
90 bool AtLeastOneTrue(bool b1, bool b2, bool b3) { return b1 || b2 || b3; }
91 
92 bool OnlyOneTrue(bool b1, bool b2, bool b3) {
93  return !MoreThanOneTrue(b1, b2, b3) && AtLeastOneTrue(b1, b2, b3);
94 }
95 
96 // Coverts binary data into human readable UUID format.
97 bool HexToUUID(const std::string& data, std::string* uuid_format) {
98  DCHECK(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());
104  return false;
105  }
106 
107  const std::string hex_encoded =
108  StringToLowerASCII(base::HexEncode(data.data(), data.size()));
109  DCHECK_EQ(hex_encoded.size(), kExpectedUUIDSize * 2);
110  base::StringPiece all(hex_encoded);
111  // Note UUID has 5 parts separated with dashes.
112  // e.g. 123e4567-e89b-12d3-a456-426655440000
113  // These StringPieces have each part.
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);
119 
120  // 32 hexadecimal characters with 4 hyphens.
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);
132  return true;
133 }
134 
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) {
144  continue;
145  }
146 
147  for (std::vector<Element>::iterator subelement =
148  protection->subelements.begin();
149  subelement != protection->subelements.end(); ++subelement) {
150  if (subelement->name == kPsshElementName) {
151  // For now, we want to remove the PSSH element because some players do
152  // not support updating pssh.
153  protection->subelements.erase(subelement);
154 
155  // TODO(rkuroiwa): Uncomment this and remove the line above when
156  // shaka-player supports updating PSSH.
157  // subelement->content = pssh;
158  return;
159  }
160  }
161 
162  // Reaching here means <cenc:pssh> does not exist under the
163  // ContentProtection element. Add it.
164  // TODO(rkuroiwa): Uncomment this when shaka-player supports updating PSSH.
165  // Element cenc_pssh;
166  // cenc_pssh.name = kPsshElementName;
167  // cenc_pssh.content = pssh;
168  // protection->subelements.push_back(cenc_pssh);
169  return;
170  }
171 
172  // Reaching here means that ContentProtection for the DRM does not exist.
173  // Add it.
174  ContentProtectionElement content_protection;
175  content_protection.scheme_id_uri = drm_uuid_schemd_id_uri_form;
176  // TODO(rkuroiwa): Uncomment this when shaka-player supports updating PSSH.
177  // Element cenc_pssh;
178  // cenc_pssh.name = kPsshElementName;
179  // cenc_pssh.content = pssh;
180  // content_protection.subelements.push_back(cenc_pssh);
181  content_protection_elements->push_back(content_protection);
182  return;
183 }
184 
185 namespace {
186 // Helper function. This works because Representation and AdaptationSet both
187 // have AddContentProtectionElement().
188 template <typename ContentProtectionParent>
189 void AddContentProtectionElementsHelperTemplated(
190  const MediaInfo& media_info,
191  ContentProtectionParent* parent) {
192  DCHECK(parent);
193  if (!media_info.has_protected_content())
194  return;
195 
196  const MediaInfo::ProtectedContent& protected_content =
197  media_info.protected_content();
198 
199  // DASH MPD spec specifies a default ContentProtection element for ISO BMFF
200  // (MP4) files.
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"] =
211  key_id_uuid_format;
212  } else {
213  LOG(ERROR) << "Failed to convert default key ID into UUID format.";
214  }
215  }
216 
217  parent->AddContentProtectionElement(mp4_content_protection);
218  }
219 
220  for (int i = 0; i < protected_content.content_protection_entry().size();
221  ++i) {
222  const MediaInfo::ProtectedContent::ContentProtectionEntry& entry =
223  protected_content.content_protection_entry(i);
224  if (!entry.has_uuid()) {
225  LOG(WARNING)
226  << "ContentProtectionEntry was specified but no UUID is set for "
227  << entry.name_version() << ", skipping.";
228  continue;
229  }
230 
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();
235 
236  if (entry.has_pssh()) {
237  std::string base64_encoded_pssh;
238  base::Base64Encode(entry.pssh(), &base64_encoded_pssh);
239  Element cenc_pssh;
240  cenc_pssh.name = kPsshElementName;
241  cenc_pssh.content = base64_encoded_pssh;
242  drm_content_protection.subelements.push_back(cenc_pssh);
243  }
244 
245  parent->AddContentProtectionElement(drm_content_protection);
246  }
247 
248  LOG_IF(WARNING, protected_content.content_protection_entry().size() == 0)
249  << "The media is encrypted but no content protection specified.";
250 }
251 } // namespace
252 
253 void AddContentProtectionElements(const MediaInfo& media_info,
254  Representation* parent) {
255  AddContentProtectionElementsHelperTemplated(media_info, parent);
256 }
257 
258 void AddContentProtectionElements(const MediaInfo& media_info,
259  AdaptationSet* parent) {
260  AddContentProtectionElementsHelperTemplated(media_info, parent);
261 }
262 
263 
264 } // namespace edash_packager
void AddContentProtectionElements(const MediaInfo &media_info, Representation *parent)
Definition: mpd_utils.cc:253
bool HexToUUID(const std::string &data, std::string *uuid_format)
Definition: mpd_utils.cc:97