Shaka Packager SDK
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 <gflags/gflags.h>
10 #include <libxml/tree.h>
11 
12 #include "packager/base/base64.h"
13 #include "packager/base/logging.h"
14 #include "packager/base/strings/string_number_conversions.h"
15 #include "packager/base/strings/string_util.h"
16 #include "packager/media/base/language_utils.h"
17 #include "packager/media/base/protection_system_specific_info.h"
18 #include "packager/mpd/base/adaptation_set.h"
19 #include "packager/mpd/base/content_protection_element.h"
20 #include "packager/mpd/base/representation.h"
21 #include "packager/mpd/base/xml/scoped_xml_ptr.h"
22 
23 DEFINE_bool(
24  use_legacy_vp9_codec_string,
25  false,
26  "Use legacy vp9 codec string 'vp9' if set to true; otherwise new style "
27  "vp09.xx.xx.xx... codec string will be used. Default to false as indicated "
28  "in https://github.com/google/shaka-packager/issues/406, all major "
29  "browsers and platforms already support the new 'vp09' codec string.");
30 
31 namespace shaka {
32 namespace {
33 
34 bool IsKeyRotationDefaultKeyId(const std::string& key_id) {
35  for (char c : key_id) {
36  if (c != '\0')
37  return false;
38  }
39  return true;
40 }
41 
42 std::string TextCodecString(const MediaInfo& media_info) {
43  CHECK(media_info.has_text_info());
44  const auto container_type = media_info.container_type();
45 
46  // Codecs are not needed when mimeType is "text/*". Having a codec would be
47  // redundant.
48  if (container_type == MediaInfo::CONTAINER_TEXT) {
49  return "";
50  }
51 
52  // DASH IOP mentions that the codec for ttml in mp4 is stpp, so override
53  // the default codec value.
54  const std::string& codec = media_info.text_info().codec();
55  if (codec == "ttml" && container_type == MediaInfo::CONTAINER_MP4) {
56  return "stpp";
57  }
58 
59  return codec;
60 }
61 
62 } // namespace
63 
64 bool HasVODOnlyFields(const MediaInfo& media_info) {
65  return media_info.has_init_range() || media_info.has_index_range() ||
66  media_info.has_media_file_url();
67 }
68 
69 bool HasLiveOnlyFields(const MediaInfo& media_info) {
70  return media_info.has_init_segment_url() ||
71  media_info.has_segment_template_url();
72 }
73 
74 void RemoveDuplicateAttributes(
75  ContentProtectionElement* content_protection_element) {
76  DCHECK(content_protection_element);
77  typedef std::map<std::string, std::string> AttributesMap;
78 
79  AttributesMap& attributes = content_protection_element->additional_attributes;
80  if (!content_protection_element->value.empty())
81  attributes.erase("value");
82 
83  if (!content_protection_element->scheme_id_uri.empty())
84  attributes.erase("schemeIdUri");
85 }
86 
87 std::string GetLanguage(const MediaInfo& media_info) {
88  std::string lang;
89  if (media_info.has_audio_info()) {
90  lang = media_info.audio_info().language();
91  } else if (media_info.has_text_info()) {
92  lang = media_info.text_info().language();
93  }
94  return LanguageToShortestForm(lang);
95 }
96 
97 std::string GetCodecs(const MediaInfo& media_info) {
98  CHECK(OnlyOneTrue(media_info.has_video_info(), media_info.has_audio_info(),
99  media_info.has_text_info()));
100 
101  if (media_info.has_video_info()) {
102  if (media_info.container_type() == MediaInfo::CONTAINER_WEBM) {
103  std::string codec = media_info.video_info().codec().substr(0, 4);
104  // media_info.video_info().codec() contains new revised codec string
105  // specified by "VPx in ISO BMFF" document, which is not compatible to
106  // old codec strings in WebM. Hack it here before all browsers support
107  // new codec strings.
108  if (codec == "vp08")
109  return "vp8";
110  if (FLAGS_use_legacy_vp9_codec_string) {
111  if (codec == "vp09")
112  return "vp9";
113  }
114  }
115  return media_info.video_info().codec();
116  }
117 
118  if (media_info.has_audio_info())
119  return media_info.audio_info().codec();
120 
121  if (media_info.has_text_info())
122  return TextCodecString(media_info);
123 
124  NOTREACHED();
125  return "";
126 }
127 
128 std::string GetBaseCodec(const MediaInfo& media_info) {
129  std::string codec;
130  if (media_info.has_video_info()) {
131  codec = media_info.video_info().codec();
132  } else if (media_info.has_audio_info()) {
133  codec = media_info.audio_info().codec();
134  } else if (media_info.has_text_info()) {
135  codec = media_info.text_info().codec();
136  }
137  // Convert, for example, "mp4a.40.2" to simply "mp4a".
138  // "mp4a.40.2" and "mp4a.40.5" can exist in the same AdaptationSet.
139  size_t dot = codec.find('.');
140  if (dot != std::string::npos) {
141  codec.erase(dot);
142  }
143  return codec;
144 }
145 
146 std::string GetAdaptationSetKey(const MediaInfo& media_info,
147  bool ignore_codec) {
148  std::string key;
149 
150  if (media_info.has_video_info()) {
151  key.append("video:");
152  } else if (media_info.has_audio_info()) {
153  key.append("audio:");
154  } else if (media_info.has_text_info()) {
155  key.append(MediaInfo_TextInfo_TextType_Name(media_info.text_info().type()));
156  key.append(":");
157  } else {
158  key.append("unknown:");
159  }
160 
161  key.append(MediaInfo_ContainerType_Name(media_info.container_type()));
162  if (!ignore_codec) {
163  key.append(":");
164  key.append(GetBaseCodec(media_info));
165  }
166  key.append(":");
167  key.append(GetLanguage(media_info));
168 
169  // Trick play streams of the same original stream, but possibly with
170  // different trick_play_factors, belong to the same trick play AdaptationSet.
171  if (media_info.video_info().has_playback_rate()) {
172  key.append(":trick_play");
173  }
174 
175  if (!media_info.dash_accessibilities().empty()) {
176  key.append(":accessibility_");
177  for (const std::string& accessibility : media_info.dash_accessibilities())
178  key.append(accessibility);
179  }
180 
181  if (!media_info.dash_roles().empty()) {
182  key.append(":roles_");
183  for (const std::string& role : media_info.dash_roles())
184  key.append(role);
185  }
186 
187  return key;
188 }
189 
190 std::string SecondsToXmlDuration(double seconds) {
191  // Chrome internally uses time accurate to microseconds, which is implemented
192  // per MSE spec (https://www.w3.org/TR/media-source/).
193  // We need a string formatter that has at least microseconds accuracy for a
194  // normal video (with duration up to 3 hours). Chrome's DoubleToString
195  // implementation meets the requirement.
196  return "PT" + base::DoubleToString(seconds) + "S";
197 }
198 
199 bool GetDurationAttribute(xmlNodePtr node, float* duration) {
200  DCHECK(node);
201  DCHECK(duration);
202  static const char kDuration[] = "duration";
203  xml::scoped_xml_ptr<xmlChar> duration_value(
204  xmlGetProp(node, BAD_CAST kDuration));
205 
206  if (!duration_value)
207  return false;
208 
209  double duration_double_precision = 0.0;
210  if (!base::StringToDouble(reinterpret_cast<const char*>(duration_value.get()),
211  &duration_double_precision)) {
212  return false;
213  }
214 
215  *duration = static_cast<float>(duration_double_precision);
216  return true;
217 }
218 
219 bool MoreThanOneTrue(bool b1, bool b2, bool b3) {
220  return (b1 && b2) || (b2 && b3) || (b3 && b1);
221 }
222 
223 bool AtLeastOneTrue(bool b1, bool b2, bool b3) {
224  return b1 || b2 || b3;
225 }
226 
227 bool OnlyOneTrue(bool b1, bool b2, bool b3) {
228  return !MoreThanOneTrue(b1, b2, b3) && AtLeastOneTrue(b1, b2, b3);
229 }
230 
231 // Coverts binary data into human readable UUID format.
232 bool HexToUUID(const std::string& data, std::string* uuid_format) {
233  DCHECK(uuid_format);
234  const size_t kExpectedUUIDSize = 16;
235  if (data.size() != kExpectedUUIDSize) {
236  LOG(ERROR) << "UUID size is expected to be " << kExpectedUUIDSize
237  << " but is " << data.size() << " and the data in hex is "
238  << base::HexEncode(data.data(), data.size());
239  return false;
240  }
241 
242  const std::string hex_encoded =
243  base::ToLowerASCII(base::HexEncode(data.data(), data.size()));
244  DCHECK_EQ(hex_encoded.size(), kExpectedUUIDSize * 2);
245  base::StringPiece all(hex_encoded);
246  // Note UUID has 5 parts separated with dashes.
247  // e.g. 123e4567-e89b-12d3-a456-426655440000
248  // These StringPieces have each part.
249  base::StringPiece first = all.substr(0, 8);
250  base::StringPiece second = all.substr(8, 4);
251  base::StringPiece third = all.substr(12, 4);
252  base::StringPiece fourth = all.substr(16, 4);
253  base::StringPiece fifth = all.substr(20, 12);
254 
255  // 32 hexadecimal characters with 4 hyphens.
256  const size_t kHumanReadableUUIDSize = 36;
257  uuid_format->reserve(kHumanReadableUUIDSize);
258  first.CopyToString(uuid_format);
259  uuid_format->append("-");
260  second.AppendToString(uuid_format);
261  uuid_format->append("-");
262  third.AppendToString(uuid_format);
263  uuid_format->append("-");
264  fourth.AppendToString(uuid_format);
265  uuid_format->append("-");
266  fifth.AppendToString(uuid_format);
267  return true;
268 }
269 
270 void UpdateContentProtectionPsshHelper(
271  const std::string& drm_uuid,
272  const std::string& pssh,
273  std::list<ContentProtectionElement>* content_protection_elements) {
274  const std::string drm_uuid_schemd_id_uri_form = "urn:uuid:" + drm_uuid;
275  for (std::list<ContentProtectionElement>::iterator protection =
276  content_protection_elements->begin();
277  protection != content_protection_elements->end(); ++protection) {
278  if (protection->scheme_id_uri != drm_uuid_schemd_id_uri_form) {
279  continue;
280  }
281 
282  for (std::vector<Element>::iterator subelement =
283  protection->subelements.begin();
284  subelement != protection->subelements.end(); ++subelement) {
285  if (subelement->name == kPsshElementName) {
286  // For now, we want to remove the PSSH element because some players do
287  // not support updating pssh.
288  protection->subelements.erase(subelement);
289 
290  // TODO(rkuroiwa): Uncomment this and remove the line above when
291  // shaka-player supports updating PSSH.
292  // subelement->content = pssh;
293  return;
294  }
295  }
296 
297  // Reaching here means <cenc:pssh> does not exist under the
298  // ContentProtection element. Add it.
299  // TODO(rkuroiwa): Uncomment this when shaka-player supports updating PSSH.
300  // Element cenc_pssh;
301  // cenc_pssh.name = kPsshElementName;
302  // cenc_pssh.content = pssh;
303  // protection->subelements.push_back(cenc_pssh);
304  return;
305  }
306 
307  // Reaching here means that ContentProtection for the DRM does not exist.
308  // Add it.
309  ContentProtectionElement content_protection;
310  content_protection.scheme_id_uri = drm_uuid_schemd_id_uri_form;
311  // TODO(rkuroiwa): Uncomment this when shaka-player supports updating PSSH.
312  // Element cenc_pssh;
313  // cenc_pssh.name = kPsshElementName;
314  // cenc_pssh.content = pssh;
315  // content_protection.subelements.push_back(cenc_pssh);
316  content_protection_elements->push_back(content_protection);
317  return;
318 }
319 
320 namespace {
321 
322 // UUID for Marlin Adaptive Streaming Specification – Simple Profile from
323 // https://dashif.org/identifiers/content_protection/.
324 const char kMarlinUUID[] = "5e629af5-38da-4063-8977-97ffbd9902d4";
325 // Unofficial FairPlay system id extracted from
326 // https://forums.developer.apple.com/thread/6185.
327 const char kFairPlayUUID[] = "29701fe4-3cc7-4a34-8c5b-ae90c7439a47";
328 // String representation of media::kPlayReadySystemId.
329 const char kPlayReadyUUID[] = "9a04f079-9840-4286-ab92-e65be0885f95";
330 // It is RECOMMENDED to include the @value attribute with name and version "MSPR 2.0".
331 // See https://docs.microsoft.com/en-us/playready/specifications/mpeg-dash-playready#221-general.
332 const char kContentProtectionValueMSPR20[] = "MSPR 2.0";
333 
334 Element GenerateMarlinContentIds(const std::string& key_id) {
335  // See https://github.com/google/shaka-packager/issues/381 for details.
336  static const char kMarlinContentIdName[] = "mas:MarlinContentId";
337  static const char kMarlinContentIdPrefix[] = "urn:marlin:kid:";
338  static const char kMarlinContentIdsName[] = "mas:MarlinContentIds";
339 
340  Element marlin_content_id;
341  marlin_content_id.name = kMarlinContentIdName;
342  marlin_content_id.content =
343  kMarlinContentIdPrefix +
344  base::ToLowerASCII(base::HexEncode(key_id.data(), key_id.size()));
345 
346  Element marlin_content_ids;
347  marlin_content_ids.name = kMarlinContentIdsName;
348  marlin_content_ids.subelements.push_back(marlin_content_id);
349 
350  return marlin_content_ids;
351 }
352 
353 Element GenerateCencPsshElement(const std::string& pssh) {
354  std::string base64_encoded_pssh;
355  base::Base64Encode(base::StringPiece(pssh.data(), pssh.size()),
356  &base64_encoded_pssh);
357  Element cenc_pssh;
358  cenc_pssh.name = kPsshElementName;
359  cenc_pssh.content = base64_encoded_pssh;
360  return cenc_pssh;
361 }
362 
363 // Extract MS PlayReady Object from given PSSH
364 // and encode it in base64.
365 Element GenerateMsprProElement(const std::string& pssh) {
366  std::unique_ptr<media::PsshBoxBuilder> b =
368  reinterpret_cast<const uint8_t*>(pssh.data()),
369  pssh.size()
370  );
371 
372  const std::vector<uint8_t> *p_pssh = &b->pssh_data();
373  std::string base64_encoded_mspr;
374  base::Base64Encode(
375  base::StringPiece(
376  reinterpret_cast<const char*>(p_pssh->data()),
377  p_pssh->size()),
378  &base64_encoded_mspr
379  );
380  Element mspr_pro;
381  mspr_pro.name = kMsproElementName;
382  mspr_pro.content = base64_encoded_mspr;
383  return mspr_pro;
384 }
385 
386 // Helper function. This works because Representation and AdaptationSet both
387 // have AddContentProtectionElement().
388 template <typename ContentProtectionParent>
389 void AddContentProtectionElementsHelperTemplated(
390  const MediaInfo& media_info,
391  ContentProtectionParent* parent) {
392  DCHECK(parent);
393  if (!media_info.has_protected_content())
394  return;
395 
396  const MediaInfo::ProtectedContent& protected_content =
397  media_info.protected_content();
398 
399  // DASH MPD spec specifies a default ContentProtection element for ISO BMFF
400  // (MP4) files.
401  const bool is_mp4_container =
402  media_info.container_type() == MediaInfo::CONTAINER_MP4;
403  std::string key_id_uuid_format;
404  if (protected_content.has_default_key_id() &&
405  !IsKeyRotationDefaultKeyId(protected_content.default_key_id())) {
406  if (!HexToUUID(protected_content.default_key_id(), &key_id_uuid_format)) {
407  LOG(ERROR) << "Failed to convert default key ID into UUID format.";
408  }
409  }
410 
411  if (is_mp4_container) {
412  ContentProtectionElement mp4_content_protection;
413  mp4_content_protection.scheme_id_uri = kEncryptedMp4Scheme;
414  mp4_content_protection.value = protected_content.protection_scheme();
415  if (!key_id_uuid_format.empty()) {
416  mp4_content_protection.additional_attributes["cenc:default_KID"] =
417  key_id_uuid_format;
418  }
419 
420  parent->AddContentProtectionElement(mp4_content_protection);
421  }
422 
423  for (const auto& entry : protected_content.content_protection_entry()) {
424  if (!entry.has_uuid()) {
425  LOG(WARNING)
426  << "ContentProtectionEntry was specified but no UUID is set for "
427  << entry.name_version() << ", skipping.";
428  continue;
429  }
430 
431  ContentProtectionElement drm_content_protection;
432 
433  if (entry.has_name_version())
434  drm_content_protection.value = entry.name_version();
435 
436  if (entry.uuid() == kFairPlayUUID) {
437  VLOG(1) << "Skipping FairPlay ContentProtection element as FairPlay does "
438  "not support DASH signaling.";
439  continue;
440  } else if (entry.uuid() == kMarlinUUID) {
441  // Marlin requires its uuid to be in upper case. See #525 for details.
442  drm_content_protection.scheme_id_uri =
443  "urn:uuid:" + base::ToUpperASCII(entry.uuid());
444  drm_content_protection.subelements.push_back(
445  GenerateMarlinContentIds(protected_content.default_key_id()));
446  } else {
447  drm_content_protection.scheme_id_uri = "urn:uuid:" + entry.uuid();
448  if (!entry.pssh().empty()) {
449  drm_content_protection.subelements.push_back(
450  GenerateCencPsshElement(entry.pssh()));
451  if(entry.uuid() == kPlayReadyUUID && protected_content.include_mspr_pro()) {
452  drm_content_protection.subelements.push_back(
453  GenerateMsprProElement(entry.pssh()));
454  drm_content_protection.value = kContentProtectionValueMSPR20;
455  }
456  }
457  }
458 
459  if (!key_id_uuid_format.empty() && !is_mp4_container) {
460  drm_content_protection.additional_attributes["cenc:default_KID"] =
461  key_id_uuid_format;
462  }
463 
464  parent->AddContentProtectionElement(drm_content_protection);
465  }
466 
467  VLOG_IF(1, protected_content.content_protection_entry().size() == 0)
468  << "The media is encrypted but no content protection specified (can "
469  "happen with key rotation).";
470 }
471 } // namespace
472 
473 void AddContentProtectionElements(const MediaInfo& media_info,
474  Representation* parent) {
475  AddContentProtectionElementsHelperTemplated(media_info, parent);
476 }
477 
478 void AddContentProtectionElements(const MediaInfo& media_info,
479  AdaptationSet* parent) {
480  AddContentProtectionElementsHelperTemplated(media_info, parent);
481 }
482 
483 } // namespace shaka
static std::unique_ptr< PsshBoxBuilder > ParseFromBox(const uint8_t *data, size_t data_size)
All the methods that are virtual are virtual for mocking.
bool HexToUUID(const std::string &data, std::string *uuid_format)
Definition: mpd_utils.cc:232
std::string LanguageToShortestForm(const std::string &language)
void AddContentProtectionElements(const MediaInfo &media_info, Representation *parent)
Definition: mpd_utils.cc:473