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  return key;
172 }
173 
174 std::string SecondsToXmlDuration(double seconds) {
175  // Chrome internally uses time accurate to microseconds, which is implemented
176  // per MSE spec (https://www.w3.org/TR/media-source/).
177  // We need a string formatter that has at least microseconds accuracy for a
178  // normal video (with duration up to 3 hours). Chrome's DoubleToString
179  // implementation meets the requirement.
180  return "PT" + base::DoubleToString(seconds) + "S";
181 }
182 
183 bool GetDurationAttribute(xmlNodePtr node, float* duration) {
184  DCHECK(node);
185  DCHECK(duration);
186  static const char kDuration[] = "duration";
187  xml::scoped_xml_ptr<xmlChar> duration_value(
188  xmlGetProp(node, BAD_CAST kDuration));
189 
190  if (!duration_value)
191  return false;
192 
193  double duration_double_precision = 0.0;
194  if (!base::StringToDouble(reinterpret_cast<const char*>(duration_value.get()),
195  &duration_double_precision)) {
196  return false;
197  }
198 
199  *duration = static_cast<float>(duration_double_precision);
200  return true;
201 }
202 
203 bool MoreThanOneTrue(bool b1, bool b2, bool b3) {
204  return (b1 && b2) || (b2 && b3) || (b3 && b1);
205 }
206 
207 bool AtLeastOneTrue(bool b1, bool b2, bool b3) {
208  return b1 || b2 || b3;
209 }
210 
211 bool OnlyOneTrue(bool b1, bool b2, bool b3) {
212  return !MoreThanOneTrue(b1, b2, b3) && AtLeastOneTrue(b1, b2, b3);
213 }
214 
215 // Coverts binary data into human readable UUID format.
216 bool HexToUUID(const std::string& data, std::string* uuid_format) {
217  DCHECK(uuid_format);
218  const size_t kExpectedUUIDSize = 16;
219  if (data.size() != kExpectedUUIDSize) {
220  LOG(ERROR) << "UUID size is expected to be " << kExpectedUUIDSize
221  << " but is " << data.size() << " and the data in hex is "
222  << base::HexEncode(data.data(), data.size());
223  return false;
224  }
225 
226  const std::string hex_encoded =
227  base::ToLowerASCII(base::HexEncode(data.data(), data.size()));
228  DCHECK_EQ(hex_encoded.size(), kExpectedUUIDSize * 2);
229  base::StringPiece all(hex_encoded);
230  // Note UUID has 5 parts separated with dashes.
231  // e.g. 123e4567-e89b-12d3-a456-426655440000
232  // These StringPieces have each part.
233  base::StringPiece first = all.substr(0, 8);
234  base::StringPiece second = all.substr(8, 4);
235  base::StringPiece third = all.substr(12, 4);
236  base::StringPiece fourth = all.substr(16, 4);
237  base::StringPiece fifth = all.substr(20, 12);
238 
239  // 32 hexadecimal characters with 4 hyphens.
240  const size_t kHumanReadableUUIDSize = 36;
241  uuid_format->reserve(kHumanReadableUUIDSize);
242  first.CopyToString(uuid_format);
243  uuid_format->append("-");
244  second.AppendToString(uuid_format);
245  uuid_format->append("-");
246  third.AppendToString(uuid_format);
247  uuid_format->append("-");
248  fourth.AppendToString(uuid_format);
249  uuid_format->append("-");
250  fifth.AppendToString(uuid_format);
251  return true;
252 }
253 
254 void UpdateContentProtectionPsshHelper(
255  const std::string& drm_uuid,
256  const std::string& pssh,
257  std::list<ContentProtectionElement>* content_protection_elements) {
258  const std::string drm_uuid_schemd_id_uri_form = "urn:uuid:" + drm_uuid;
259  for (std::list<ContentProtectionElement>::iterator protection =
260  content_protection_elements->begin();
261  protection != content_protection_elements->end(); ++protection) {
262  if (protection->scheme_id_uri != drm_uuid_schemd_id_uri_form) {
263  continue;
264  }
265 
266  for (std::vector<Element>::iterator subelement =
267  protection->subelements.begin();
268  subelement != protection->subelements.end(); ++subelement) {
269  if (subelement->name == kPsshElementName) {
270  // For now, we want to remove the PSSH element because some players do
271  // not support updating pssh.
272  protection->subelements.erase(subelement);
273 
274  // TODO(rkuroiwa): Uncomment this and remove the line above when
275  // shaka-player supports updating PSSH.
276  // subelement->content = pssh;
277  return;
278  }
279  }
280 
281  // Reaching here means <cenc:pssh> does not exist under the
282  // ContentProtection element. Add it.
283  // TODO(rkuroiwa): Uncomment this when shaka-player supports updating PSSH.
284  // Element cenc_pssh;
285  // cenc_pssh.name = kPsshElementName;
286  // cenc_pssh.content = pssh;
287  // protection->subelements.push_back(cenc_pssh);
288  return;
289  }
290 
291  // Reaching here means that ContentProtection for the DRM does not exist.
292  // Add it.
293  ContentProtectionElement content_protection;
294  content_protection.scheme_id_uri = drm_uuid_schemd_id_uri_form;
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  // content_protection.subelements.push_back(cenc_pssh);
300  content_protection_elements->push_back(content_protection);
301  return;
302 }
303 
304 namespace {
305 
306 // UUID for Marlin Adaptive Streaming Specification – Simple Profile from
307 // https://dashif.org/identifiers/content_protection/.
308 const char kMarlinUUID[] = "5e629af5-38da-4063-8977-97ffbd9902d4";
309 // Unofficial FairPlay system id extracted from
310 // https://forums.developer.apple.com/thread/6185.
311 const char kFairPlayUUID[] = "29701fe4-3cc7-4a34-8c5b-ae90c7439a47";
312 
313 Element GenerateMarlinContentIds(const std::string& key_id) {
314  // See https://github.com/google/shaka-packager/issues/381 for details.
315  static const char kMarlinContentIdName[] = "mas:MarlinContentId";
316  static const char kMarlinContentIdPrefix[] = "urn:marlin:kid:";
317  static const char kMarlinContentIdsName[] = "mas:MarlinContentIds";
318 
319  Element marlin_content_id;
320  marlin_content_id.name = kMarlinContentIdName;
321  marlin_content_id.content =
322  kMarlinContentIdPrefix +
323  base::ToLowerASCII(base::HexEncode(key_id.data(), key_id.size()));
324 
325  Element marlin_content_ids;
326  marlin_content_ids.name = kMarlinContentIdsName;
327  marlin_content_ids.subelements.push_back(marlin_content_id);
328 
329  return marlin_content_ids;
330 }
331 
332 Element GenerateCencPsshElement(const std::string& pssh) {
333  std::string base64_encoded_pssh;
334  base::Base64Encode(base::StringPiece(pssh.data(), pssh.size()),
335  &base64_encoded_pssh);
336  Element cenc_pssh;
337  cenc_pssh.name = kPsshElementName;
338  cenc_pssh.content = base64_encoded_pssh;
339  return cenc_pssh;
340 }
341 
342 // Helper function. This works because Representation and AdaptationSet both
343 // have AddContentProtectionElement().
344 template <typename ContentProtectionParent>
345 void AddContentProtectionElementsHelperTemplated(
346  const MediaInfo& media_info,
347  ContentProtectionParent* parent) {
348  DCHECK(parent);
349  if (!media_info.has_protected_content())
350  return;
351 
352  const MediaInfo::ProtectedContent& protected_content =
353  media_info.protected_content();
354 
355  // DASH MPD spec specifies a default ContentProtection element for ISO BMFF
356  // (MP4) files.
357  const bool is_mp4_container =
358  media_info.container_type() == MediaInfo::CONTAINER_MP4;
359  std::string key_id_uuid_format;
360  if (protected_content.has_default_key_id() &&
361  !IsKeyRotationDefaultKeyId(protected_content.default_key_id())) {
362  if (!HexToUUID(protected_content.default_key_id(), &key_id_uuid_format)) {
363  LOG(ERROR) << "Failed to convert default key ID into UUID format.";
364  }
365  }
366 
367  if (is_mp4_container) {
368  ContentProtectionElement mp4_content_protection;
369  mp4_content_protection.scheme_id_uri = kEncryptedMp4Scheme;
370  mp4_content_protection.value = protected_content.protection_scheme();
371  if (!key_id_uuid_format.empty()) {
372  mp4_content_protection.additional_attributes["cenc:default_KID"] =
373  key_id_uuid_format;
374  }
375 
376  parent->AddContentProtectionElement(mp4_content_protection);
377  }
378 
379  for (const auto& entry : protected_content.content_protection_entry()) {
380  if (!entry.has_uuid()) {
381  LOG(WARNING)
382  << "ContentProtectionEntry was specified but no UUID is set for "
383  << entry.name_version() << ", skipping.";
384  continue;
385  }
386 
387  ContentProtectionElement drm_content_protection;
388 
389  if (entry.has_name_version())
390  drm_content_protection.value = entry.name_version();
391 
392  if (entry.uuid() == kFairPlayUUID) {
393  VLOG(1) << "Skipping FairPlay ContentProtection element as FairPlay does "
394  "not support DASH signaling.";
395  continue;
396  } else if (entry.uuid() == kMarlinUUID) {
397  // Marlin requires its uuid to be in upper case. See #525 for details.
398  drm_content_protection.scheme_id_uri =
399  "urn:uuid:" + base::ToUpperASCII(entry.uuid());
400  drm_content_protection.subelements.push_back(
401  GenerateMarlinContentIds(protected_content.default_key_id()));
402  } else {
403  drm_content_protection.scheme_id_uri = "urn:uuid:" + entry.uuid();
404  if (!entry.pssh().empty()) {
405  drm_content_protection.subelements.push_back(
406  GenerateCencPsshElement(entry.pssh()));
407  }
408  }
409 
410  if (!key_id_uuid_format.empty() && !is_mp4_container) {
411  drm_content_protection.additional_attributes["cenc:default_KID"] =
412  key_id_uuid_format;
413  }
414 
415  parent->AddContentProtectionElement(drm_content_protection);
416  }
417 
418  VLOG_IF(1, protected_content.content_protection_entry().size() == 0)
419  << "The media is encrypted but no content protection specified (can "
420  "happen with key rotation).";
421 }
422 } // namespace
423 
424 void AddContentProtectionElements(const MediaInfo& media_info,
425  Representation* parent) {
426  AddContentProtectionElementsHelperTemplated(media_info, parent);
427 }
428 
429 void AddContentProtectionElements(const MediaInfo& media_info,
430  AdaptationSet* parent) {
431  AddContentProtectionElementsHelperTemplated(media_info, parent);
432 }
433 
434 } // 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:424
bool HexToUUID(const std::string &data, std::string *uuid_format)
Definition: mpd_utils.cc:216