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 // Helper function. This works because Representation and AdaptationSet both
306 // have AddContentProtectionElement().
307 template <typename ContentProtectionParent>
308 void AddContentProtectionElementsHelperTemplated(
309  const MediaInfo& media_info,
310  ContentProtectionParent* parent) {
311  DCHECK(parent);
312  if (!media_info.has_protected_content())
313  return;
314 
315  const MediaInfo::ProtectedContent& protected_content =
316  media_info.protected_content();
317 
318  // DASH MPD spec specifies a default ContentProtection element for ISO BMFF
319  // (MP4) files.
320  const bool is_mp4_container =
321  media_info.container_type() == MediaInfo::CONTAINER_MP4;
322  std::string key_id_uuid_format;
323  if (protected_content.has_default_key_id() &&
324  !IsKeyRotationDefaultKeyId(protected_content.default_key_id())) {
325  if (!HexToUUID(protected_content.default_key_id(), &key_id_uuid_format)) {
326  LOG(ERROR) << "Failed to convert default key ID into UUID format.";
327  }
328  }
329 
330  if (is_mp4_container) {
331  ContentProtectionElement mp4_content_protection;
332  mp4_content_protection.scheme_id_uri = kEncryptedMp4Scheme;
333  mp4_content_protection.value = protected_content.protection_scheme();
334  if (!key_id_uuid_format.empty()) {
335  mp4_content_protection.additional_attributes["cenc:default_KID"] =
336  key_id_uuid_format;
337  }
338 
339  parent->AddContentProtectionElement(mp4_content_protection);
340  }
341 
342  for (const auto& entry : protected_content.content_protection_entry()) {
343  if (!entry.has_uuid()) {
344  LOG(WARNING)
345  << "ContentProtectionEntry was specified but no UUID is set for "
346  << entry.name_version() << ", skipping.";
347  continue;
348  }
349 
350  ContentProtectionElement drm_content_protection;
351  drm_content_protection.scheme_id_uri = "urn:uuid:" + entry.uuid();
352  if (entry.has_name_version())
353  drm_content_protection.value = entry.name_version();
354 
355  if (entry.has_pssh()) {
356  std::string base64_encoded_pssh;
357  base::Base64Encode(
358  base::StringPiece(entry.pssh().data(), entry.pssh().size()),
359  &base64_encoded_pssh);
360  Element cenc_pssh;
361  cenc_pssh.name = kPsshElementName;
362  cenc_pssh.content = base64_encoded_pssh;
363  drm_content_protection.subelements.push_back(cenc_pssh);
364  }
365 
366  if (!key_id_uuid_format.empty() && !is_mp4_container) {
367  drm_content_protection.additional_attributes["cenc:default_KID"] =
368  key_id_uuid_format;
369  }
370 
371  parent->AddContentProtectionElement(drm_content_protection);
372  }
373 
374  VLOG_IF(1, protected_content.content_protection_entry().size() == 0)
375  << "The media is encrypted but no content protection specified (can "
376  "happen with key rotation).";
377 }
378 } // namespace
379 
380 void AddContentProtectionElements(const MediaInfo& media_info,
381  Representation* parent) {
382  AddContentProtectionElementsHelperTemplated(media_info, parent);
383 }
384 
385 void AddContentProtectionElements(const MediaInfo& media_info,
386  AdaptationSet* parent) {
387  AddContentProtectionElementsHelperTemplated(media_info, parent);
388 }
389 
390 } // 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:380
bool HexToUUID(const std::string &data, std::string *uuid_format)
Definition: mpd_utils.cc:216