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