DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerator
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 namespace {
17 
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();
22  // DASH IOP mentions that the codec for ttml in mp4 is stpp.
23  if (format == "ttml" &&
24  (media_info.container_type() == MediaInfo::CONTAINER_MP4)) {
25  return "stpp";
26  }
27 
28  // Otherwise codec doesn't need to be specified, e.g. vtt and ttml+xml are
29  // obvious from the mime type.
30  return "";
31 }
32 
33 } // namespace
34 
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();
39 }
40 
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();
45 }
46 
47 void RemoveDuplicateAttributes(
48  ContentProtectionElement* content_protection_element) {
49  DCHECK(content_protection_element);
50  typedef std::map<std::string, std::string> AttributesMap;
51 
52  AttributesMap& attributes = content_protection_element->additional_attributes;
53  if (!content_protection_element->value.empty())
54  attributes.erase("value");
55 
56  if (!content_protection_element->scheme_id_uri.empty())
57  attributes.erase("schemeIdUri");
58 }
59 
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()));
63 
64  if (media_info.has_video_info()) {
65  if (media_info.container_type() == MediaInfo::CONTAINER_WEBM) {
66  std::string codec = media_info.video_info().codec().substr(0, 4);
67  // media_info.video_info().codec() contains new revised codec string
68  // specified by "VPx in ISO BMFF" document, which is not compatible to
69  // old codec strings in WebM. Hack it here before all browsers support
70  // new codec strings.
71  if (codec == "vp08")
72  return "vp8";
73  if (codec == "vp09")
74  return "vp9";
75  }
76  return media_info.video_info().codec();
77  }
78 
79  if (media_info.has_audio_info())
80  return media_info.audio_info().codec();
81 
82  if (media_info.has_text_info())
83  return TextCodecString(media_info);
84 
85  NOTREACHED();
86  return "";
87 }
88 
89 std::string SecondsToXmlDuration(double seconds) {
90  return "PT" + base::DoubleToString(seconds) + "S";
91 }
92 
93 bool GetDurationAttribute(xmlNodePtr node, float* duration) {
94  DCHECK(node);
95  DCHECK(duration);
96  static const char kDuration[] = "duration";
97  xml::scoped_xml_ptr<xmlChar> duration_value(
98  xmlGetProp(node, BAD_CAST kDuration));
99 
100  if (!duration_value)
101  return false;
102 
103  double duration_double_precision = 0.0;
104  if (!base::StringToDouble(reinterpret_cast<const char*>(duration_value.get()),
105  &duration_double_precision)) {
106  return false;
107  }
108 
109  *duration = static_cast<float>(duration_double_precision);
110  return true;
111 }
112 
113 bool MoreThanOneTrue(bool b1, bool b2, bool b3) {
114  return (b1 && b2) || (b2 && b3) || (b3 && b1);
115 }
116 
117 bool AtLeastOneTrue(bool b1, bool b2, bool b3) { return b1 || b2 || b3; }
118 
119 bool OnlyOneTrue(bool b1, bool b2, bool b3) {
120  return !MoreThanOneTrue(b1, b2, b3) && AtLeastOneTrue(b1, b2, b3);
121 }
122 
123 // Coverts binary data into human readable UUID format.
124 bool HexToUUID(const std::string& data, std::string* uuid_format) {
125  DCHECK(uuid_format);
126  const size_t kExpectedUUIDSize = 16;
127  if (data.size() != kExpectedUUIDSize) {
128  LOG(ERROR) << "UUID size is expected to be " << kExpectedUUIDSize
129  << " but is " << data.size() << " and the data in hex is "
130  << base::HexEncode(data.data(), data.size());
131  return false;
132  }
133 
134  const std::string hex_encoded =
135  base::StringToLowerASCII(base::HexEncode(data.data(), data.size()));
136  DCHECK_EQ(hex_encoded.size(), kExpectedUUIDSize * 2);
137  base::StringPiece all(hex_encoded);
138  // Note UUID has 5 parts separated with dashes.
139  // e.g. 123e4567-e89b-12d3-a456-426655440000
140  // These StringPieces have each part.
141  base::StringPiece first = all.substr(0, 8);
142  base::StringPiece second = all.substr(8, 4);
143  base::StringPiece third = all.substr(12, 4);
144  base::StringPiece fourth = all.substr(16, 4);
145  base::StringPiece fifth = all.substr(20, 12);
146 
147  // 32 hexadecimal characters with 4 hyphens.
148  const size_t kHumanReadableUUIDSize = 36;
149  uuid_format->reserve(kHumanReadableUUIDSize);
150  first.CopyToString(uuid_format);
151  uuid_format->append("-");
152  second.AppendToString(uuid_format);
153  uuid_format->append("-");
154  third.AppendToString(uuid_format);
155  uuid_format->append("-");
156  fourth.AppendToString(uuid_format);
157  uuid_format->append("-");
158  fifth.AppendToString(uuid_format);
159  return true;
160 }
161 
162 void UpdateContentProtectionPsshHelper(
163  const std::string& drm_uuid,
164  const std::string& pssh,
165  std::list<ContentProtectionElement>* content_protection_elements) {
166  const std::string drm_uuid_schemd_id_uri_form = "urn:uuid:" + drm_uuid;
167  for (std::list<ContentProtectionElement>::iterator protection =
168  content_protection_elements->begin();
169  protection != content_protection_elements->end(); ++protection) {
170  if (protection->scheme_id_uri != drm_uuid_schemd_id_uri_form) {
171  continue;
172  }
173 
174  for (std::vector<Element>::iterator subelement =
175  protection->subelements.begin();
176  subelement != protection->subelements.end(); ++subelement) {
177  if (subelement->name == kPsshElementName) {
178  // For now, we want to remove the PSSH element because some players do
179  // not support updating pssh.
180  protection->subelements.erase(subelement);
181 
182  // TODO(rkuroiwa): Uncomment this and remove the line above when
183  // shaka-player supports updating PSSH.
184  // subelement->content = pssh;
185  return;
186  }
187  }
188 
189  // Reaching here means <cenc:pssh> does not exist under the
190  // ContentProtection element. Add it.
191  // TODO(rkuroiwa): Uncomment this when shaka-player supports updating PSSH.
192  // Element cenc_pssh;
193  // cenc_pssh.name = kPsshElementName;
194  // cenc_pssh.content = pssh;
195  // protection->subelements.push_back(cenc_pssh);
196  return;
197  }
198 
199  // Reaching here means that ContentProtection for the DRM does not exist.
200  // Add it.
201  ContentProtectionElement content_protection;
202  content_protection.scheme_id_uri = drm_uuid_schemd_id_uri_form;
203  // TODO(rkuroiwa): Uncomment this when shaka-player supports updating PSSH.
204  // Element cenc_pssh;
205  // cenc_pssh.name = kPsshElementName;
206  // cenc_pssh.content = pssh;
207  // content_protection.subelements.push_back(cenc_pssh);
208  content_protection_elements->push_back(content_protection);
209  return;
210 }
211 
212 namespace {
213 // Helper function. This works because Representation and AdaptationSet both
214 // have AddContentProtectionElement().
215 template <typename ContentProtectionParent>
216 void AddContentProtectionElementsHelperTemplated(
217  const MediaInfo& media_info,
218  ContentProtectionParent* parent) {
219  DCHECK(parent);
220  if (!media_info.has_protected_content())
221  return;
222 
223  const MediaInfo::ProtectedContent& protected_content =
224  media_info.protected_content();
225 
226  // DASH MPD spec specifies a default ContentProtection element for ISO BMFF
227  // (MP4) files.
228  const bool is_mp4_container =
229  media_info.container_type() == MediaInfo::CONTAINER_MP4;
230  std::string key_id_uuid_format;
231  if (protected_content.has_default_key_id()) {
232  if (!HexToUUID(protected_content.default_key_id(), &key_id_uuid_format)) {
233  LOG(ERROR) << "Failed to convert default key ID into UUID format.";
234  }
235  }
236 
237  if (is_mp4_container) {
238  ContentProtectionElement mp4_content_protection;
239  mp4_content_protection.scheme_id_uri = kEncryptedMp4Scheme;
240  mp4_content_protection.value = kEncryptedMp4Value;
241  if (!key_id_uuid_format.empty()) {
242  mp4_content_protection.additional_attributes["cenc:default_KID"] =
243  key_id_uuid_format;
244  }
245 
246  parent->AddContentProtectionElement(mp4_content_protection);
247  }
248 
249  for (int i = 0; i < protected_content.content_protection_entry().size();
250  ++i) {
251  const MediaInfo::ProtectedContent::ContentProtectionEntry& entry =
252  protected_content.content_protection_entry(i);
253  if (!entry.has_uuid()) {
254  LOG(WARNING)
255  << "ContentProtectionEntry was specified but no UUID is set for "
256  << entry.name_version() << ", skipping.";
257  continue;
258  }
259 
260  ContentProtectionElement drm_content_protection;
261  drm_content_protection.scheme_id_uri = "urn:uuid:" + entry.uuid();
262  if (entry.has_name_version())
263  drm_content_protection.value = entry.name_version();
264 
265  if (entry.has_pssh()) {
266  std::string base64_encoded_pssh;
267  base::Base64Encode(entry.pssh(), &base64_encoded_pssh);
268  Element cenc_pssh;
269  cenc_pssh.name = kPsshElementName;
270  cenc_pssh.content = base64_encoded_pssh;
271  drm_content_protection.subelements.push_back(cenc_pssh);
272  }
273 
274  if (!key_id_uuid_format.empty() && !is_mp4_container) {
275  drm_content_protection.additional_attributes["cenc:default_KID"] =
276  key_id_uuid_format;
277  }
278 
279  parent->AddContentProtectionElement(drm_content_protection);
280  }
281 
282  LOG_IF(WARNING, protected_content.content_protection_entry().size() == 0)
283  << "The media is encrypted but no content protection specified.";
284 }
285 } // namespace
286 
287 void AddContentProtectionElements(const MediaInfo& media_info,
288  Representation* parent) {
289  AddContentProtectionElementsHelperTemplated(media_info, parent);
290 }
291 
292 void AddContentProtectionElements(const MediaInfo& media_info,
293  AdaptationSet* parent) {
294  AddContentProtectionElementsHelperTemplated(media_info, parent);
295 }
296 
297 
298 } // namespace edash_packager
void AddContentProtectionElements(const MediaInfo &media_info, Representation *parent)
Definition: mpd_utils.cc:287
bool HexToUUID(const std::string &data, std::string *uuid_format)
Definition: mpd_utils.cc:124