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