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