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" + base::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 // Coverts binary data into human readable UUID format.
174 bool HexToUUID(const std::string& data, std::string* uuid_format) {
175  DCHECK(uuid_format);
176  const size_t kExpectedUUIDSize = 16;
177  if (data.size() != kExpectedUUIDSize) {
178  LOG(ERROR) << "UUID size is expected to be " << kExpectedUUIDSize
179  << " but is " << data.size() << " and the data in hex is "
180  << base::HexEncode(data.data(), data.size());
181  return false;
182  }
183 
184  const std::string hex_encoded =
185  base::ToLowerASCII(base::HexEncode(data.data(), data.size()));
186  DCHECK_EQ(hex_encoded.size(), kExpectedUUIDSize * 2);
187  base::StringPiece all(hex_encoded);
188  // Note UUID has 5 parts separated with dashes.
189  // e.g. 123e4567-e89b-12d3-a456-426655440000
190  // These StringPieces have each part.
191  base::StringPiece first = all.substr(0, 8);
192  base::StringPiece second = all.substr(8, 4);
193  base::StringPiece third = all.substr(12, 4);
194  base::StringPiece fourth = all.substr(16, 4);
195  base::StringPiece fifth = all.substr(20, 12);
196 
197  // 32 hexadecimal characters with 4 hyphens.
198  const size_t kHumanReadableUUIDSize = 36;
199  uuid_format->reserve(kHumanReadableUUIDSize);
200  first.CopyToString(uuid_format);
201  uuid_format->append("-");
202  second.AppendToString(uuid_format);
203  uuid_format->append("-");
204  third.AppendToString(uuid_format);
205  uuid_format->append("-");
206  fourth.AppendToString(uuid_format);
207  uuid_format->append("-");
208  fifth.AppendToString(uuid_format);
209  return true;
210 }
211 
212 void UpdateContentProtectionPsshHelper(
213  const std::string& drm_uuid,
214  const std::string& pssh,
215  std::list<ContentProtectionElement>* content_protection_elements) {
216  const std::string drm_uuid_schemd_id_uri_form = "urn:uuid:" + drm_uuid;
217  for (std::list<ContentProtectionElement>::iterator protection =
218  content_protection_elements->begin();
219  protection != content_protection_elements->end(); ++protection) {
220  if (protection->scheme_id_uri != drm_uuid_schemd_id_uri_form) {
221  continue;
222  }
223 
224  for (std::vector<Element>::iterator subelement =
225  protection->subelements.begin();
226  subelement != protection->subelements.end(); ++subelement) {
227  if (subelement->name == kPsshElementName) {
228  // For now, we want to remove the PSSH element because some players do
229  // not support updating pssh.
230  protection->subelements.erase(subelement);
231 
232  // TODO(rkuroiwa): Uncomment this and remove the line above when
233  // shaka-player supports updating PSSH.
234  // subelement->content = pssh;
235  return;
236  }
237  }
238 
239  // Reaching here means <cenc:pssh> does not exist under the
240  // ContentProtection element. Add it.
241  // TODO(rkuroiwa): Uncomment this when shaka-player supports updating PSSH.
242  // Element cenc_pssh;
243  // cenc_pssh.name = kPsshElementName;
244  // cenc_pssh.content = pssh;
245  // protection->subelements.push_back(cenc_pssh);
246  return;
247  }
248 
249  // Reaching here means that ContentProtection for the DRM does not exist.
250  // Add it.
251  ContentProtectionElement content_protection;
252  content_protection.scheme_id_uri = drm_uuid_schemd_id_uri_form;
253  // TODO(rkuroiwa): Uncomment this when shaka-player supports updating PSSH.
254  // Element cenc_pssh;
255  // cenc_pssh.name = kPsshElementName;
256  // cenc_pssh.content = pssh;
257  // content_protection.subelements.push_back(cenc_pssh);
258  content_protection_elements->push_back(content_protection);
259  return;
260 }
261 
262 namespace {
263 // Helper function. This works because Representation and AdaptationSet both
264 // have AddContentProtectionElement().
265 template <typename ContentProtectionParent>
266 void AddContentProtectionElementsHelperTemplated(
267  const MediaInfo& media_info,
268  ContentProtectionParent* parent) {
269  DCHECK(parent);
270  if (!media_info.has_protected_content())
271  return;
272 
273  const MediaInfo::ProtectedContent& protected_content =
274  media_info.protected_content();
275 
276  // DASH MPD spec specifies a default ContentProtection element for ISO BMFF
277  // (MP4) files.
278  const bool is_mp4_container =
279  media_info.container_type() == MediaInfo::CONTAINER_MP4;
280  std::string key_id_uuid_format;
281  if (protected_content.has_default_key_id()) {
282  if (!HexToUUID(protected_content.default_key_id(), &key_id_uuid_format)) {
283  LOG(ERROR) << "Failed to convert default key ID into UUID format.";
284  }
285  }
286 
287  if (is_mp4_container) {
288  ContentProtectionElement mp4_content_protection;
289  mp4_content_protection.scheme_id_uri = kEncryptedMp4Scheme;
290  mp4_content_protection.value = protected_content.protection_scheme();
291  if (!key_id_uuid_format.empty()) {
292  mp4_content_protection.additional_attributes["cenc:default_KID"] =
293  key_id_uuid_format;
294  }
295 
296  parent->AddContentProtectionElement(mp4_content_protection);
297  }
298 
299  for (int i = 0; i < protected_content.content_protection_entry().size();
300  ++i) {
301  const MediaInfo::ProtectedContent::ContentProtectionEntry& entry =
302  protected_content.content_protection_entry(i);
303  if (!entry.has_uuid()) {
304  LOG(WARNING)
305  << "ContentProtectionEntry was specified but no UUID is set for "
306  << entry.name_version() << ", skipping.";
307  continue;
308  }
309 
310  ContentProtectionElement drm_content_protection;
311  drm_content_protection.scheme_id_uri = "urn:uuid:" + entry.uuid();
312  if (entry.has_name_version())
313  drm_content_protection.value = entry.name_version();
314 
315  if (entry.has_pssh()) {
316  std::string base64_encoded_pssh;
317  base::Base64Encode(
318  base::StringPiece(entry.pssh().data(), entry.pssh().size()),
319  &base64_encoded_pssh);
320  Element cenc_pssh;
321  cenc_pssh.name = kPsshElementName;
322  cenc_pssh.content = base64_encoded_pssh;
323  drm_content_protection.subelements.push_back(cenc_pssh);
324  }
325 
326  if (!key_id_uuid_format.empty() && !is_mp4_container) {
327  drm_content_protection.additional_attributes["cenc:default_KID"] =
328  key_id_uuid_format;
329  }
330 
331  parent->AddContentProtectionElement(drm_content_protection);
332  }
333 
334  LOG_IF(WARNING, protected_content.content_protection_entry().size() == 0)
335  << "The media is encrypted but no content protection specified.";
336 }
337 } // namespace
338 
339 void AddContentProtectionElements(const MediaInfo& media_info,
340  Representation* parent) {
341  AddContentProtectionElementsHelperTemplated(media_info, parent);
342 }
343 
344 void AddContentProtectionElements(const MediaInfo& media_info,
345  AdaptationSet* parent) {
346  AddContentProtectionElementsHelperTemplated(media_info, parent);
347 }
348 
349 
350 } // namespace shaka
void AddContentProtectionElements(const MediaInfo &media_info, Representation *parent)
Definition: mpd_utils.cc:339
bool HexToUUID(const std::string &data, std::string *uuid_format)
Definition: mpd_utils.cc:174