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