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