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