Shaka Packager SDK
Loading...
Searching...
No Matches
mpd_utils.cc
1// Copyright 2014 Google LLC. 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 <absl/flags/flag.h>
10#include <absl/log/check.h>
11#include <absl/log/log.h>
12#include <absl/strings/escaping.h>
13#include <absl/strings/numbers.h>
14#include <absl/strings/str_format.h>
15#include <libxml/tree.h>
16
17#include <packager/macros/logging.h>
18#include <packager/media/base/fourccs.h>
19#include <packager/media/base/language_utils.h>
20#include <packager/media/base/protection_system_specific_info.h>
21#include <packager/mpd/base/adaptation_set.h>
22#include <packager/mpd/base/content_protection_element.h>
23#include <packager/mpd/base/representation.h>
24#include <packager/mpd/base/xml/scoped_xml_ptr.h>
25
26ABSL_FLAG(
27 bool,
28 use_legacy_vp9_codec_string,
29 false,
30 "Use legacy vp9 codec string 'vp9' if set to true; otherwise new style "
31 "vp09.xx.xx.xx... codec string will be used. Default to false as indicated "
32 "in https://github.com/shaka-project/shaka-packager/issues/406, all major "
33 "browsers and platforms already support the new 'vp09' codec string.");
34
35namespace shaka {
36namespace {
37
38bool IsKeyRotationDefaultKeyId(const std::string& key_id) {
39 for (char c : key_id) {
40 if (c != '\0')
41 return false;
42 }
43 return true;
44}
45
46std::string TextCodecString(const MediaInfo& media_info) {
47 CHECK(media_info.has_text_info());
48 const auto container_type = media_info.container_type();
49
50 // Codecs are not needed when mimeType is "text/*". Having a codec would be
51 // redundant.
52 if (container_type == MediaInfo::CONTAINER_TEXT) {
53 return "";
54 }
55
56 // DASH IOP mentions that the codec for ttml in mp4 is stpp, so override
57 // the default codec value.
58 const std::string& codec = media_info.text_info().codec();
59 if (codec == "ttml" && container_type == MediaInfo::CONTAINER_MP4) {
60 return "stpp";
61 }
62
63 return codec;
64}
65
66} // namespace
67
68bool HasVODOnlyFields(const MediaInfo& media_info) {
69 return media_info.has_init_range() || media_info.has_index_range() ||
70 media_info.has_media_file_url();
71}
72
73bool HasLiveOnlyFields(const MediaInfo& media_info) {
74 return media_info.has_init_segment_url() ||
75 media_info.has_segment_template_url();
76}
77
78void RemoveDuplicateAttributes(
79 ContentProtectionElement* content_protection_element) {
80 DCHECK(content_protection_element);
81 typedef std::map<std::string, std::string> AttributesMap;
82
83 AttributesMap& attributes = content_protection_element->additional_attributes;
84 if (!content_protection_element->value.empty())
85 attributes.erase("value");
86
87 if (!content_protection_element->scheme_id_uri.empty())
88 attributes.erase("schemeIdUri");
89}
90
91std::string GetLanguage(const MediaInfo& media_info) {
92 std::string lang;
93 if (media_info.has_audio_info()) {
94 lang = media_info.audio_info().language();
95 } else if (media_info.has_text_info()) {
96 lang = media_info.text_info().language();
97 }
98 return LanguageToShortestForm(lang);
99}
100
101std::string GetCodecs(const MediaInfo& media_info) {
102 CHECK(OnlyOneTrue(media_info.has_video_info(), media_info.has_audio_info(),
103 media_info.has_text_info()));
104
105 if (media_info.has_video_info()) {
106 if (media_info.container_type() == MediaInfo::CONTAINER_WEBM) {
107 std::string codec = media_info.video_info().codec().substr(0, 4);
108 // media_info.video_info().codec() contains new revised codec string
109 // specified by "VPx in ISO BMFF" document, which is not compatible to
110 // old codec strings in WebM. Hack it here before all browsers support
111 // new codec strings.
112 if (codec == "vp08")
113 return "vp8";
114 if (absl::GetFlag(FLAGS_use_legacy_vp9_codec_string)) {
115 if (codec == "vp09")
116 return "vp9";
117 }
118 }
119 return media_info.video_info().codec();
120 }
121
122 if (media_info.has_audio_info())
123 return media_info.audio_info().codec();
124
125 if (media_info.has_text_info())
126 return TextCodecString(media_info);
127
128 NOTIMPLEMENTED();
129 return "";
130}
131
132std::string GetSupplementalCodecs(const MediaInfo& media_info) {
133 CHECK(OnlyOneTrue(media_info.has_video_info(), media_info.has_audio_info(),
134 media_info.has_text_info()));
135
136 if (media_info.has_video_info() &&
137 media_info.video_info().has_supplemental_codec()) {
138 return media_info.video_info().supplemental_codec();
139 }
140 return "";
141}
142
143std::string GetSupplementalProfiles(const MediaInfo& media_info) {
144 CHECK(OnlyOneTrue(media_info.has_video_info(), media_info.has_audio_info(),
145 media_info.has_text_info()));
146
147 if (media_info.has_video_info() &&
148 media_info.video_info().has_compatible_brand()) {
149 return FourCCToString(
150 static_cast<media::FourCC>(media_info.video_info().compatible_brand()));
151 }
152 return "";
153}
154
155std::string GetBaseCodec(const MediaInfo& media_info) {
156 std::string codec;
157 if (media_info.has_video_info()) {
158 codec = media_info.video_info().codec();
159 } else if (media_info.has_audio_info()) {
160 codec = media_info.audio_info().codec();
161 } else if (media_info.has_text_info()) {
162 codec = media_info.text_info().codec();
163 }
164 // Convert, for example, "mp4a.40.2" to simply "mp4a".
165 // "mp4a.40.2" and "mp4a.40.5" can exist in the same AdaptationSet.
166 size_t dot = codec.find('.');
167 if (dot != std::string::npos) {
168 codec.erase(dot);
169 }
170 return codec;
171}
172
173std::string GetAdaptationSetKey(const MediaInfo& media_info,
174 bool ignore_codec) {
175 std::string key;
176
177 if (media_info.has_video_info()) {
178 key.append("video:");
179 } else if (media_info.has_audio_info()) {
180 key.append("audio:");
181 } else if (media_info.has_text_info()) {
182 key.append(MediaInfo_TextInfo_TextType_Name(media_info.text_info().type()));
183 key.append(":");
184 } else {
185 key.append("unknown:");
186 }
187
188 if (media_info.has_dash_label())
189 key.append(media_info.dash_label() + ":");
190
191 key.append(MediaInfo_ContainerType_Name(media_info.container_type()));
192 if (!ignore_codec) {
193 key.append(":");
194 key.append(GetBaseCodec(media_info));
195
196 if (GetBaseCodec(media_info).find("dvh") == 0) {
197 // Transfer characteristics for Dolby Vision (dvh1 or dvhe) must be PQ
198 // irrespective of value present in SPS VUI.
199 key.append(":");
200 key.append(std::to_string(kTransferFunctionPQ));
201 } else if (media_info.video_info().has_transfer_characteristics()) {
202 key.append(":");
203 key.append(
204 std::to_string(media_info.video_info().transfer_characteristics()));
205 }
206 }
207 key.append(":");
208 key.append(GetLanguage(media_info));
209
210 // Trick play streams of the same original stream, but possibly with
211 // different trick_play_factors, belong to the same trick play AdaptationSet.
212 if (media_info.video_info().has_playback_rate()) {
213 key.append(":trick_play");
214 }
215
216 if (!media_info.dash_accessibilities().empty()) {
217 key.append(":accessibility_");
218 for (const std::string& accessibility : media_info.dash_accessibilities())
219 key.append(accessibility);
220 }
221
222 if (!media_info.dash_roles().empty()) {
223 key.append(":roles_");
224 for (const std::string& role : media_info.dash_roles())
225 key.append(role);
226 }
227
228 return key;
229}
230
231std::string FloatToXmlString(double number) {
232 // Keep up to microsecond accuracy but trim trailing 0s
233 std::string formatted = absl::StrFormat("%.6f", number);
234 size_t decimalPos = formatted.find('.');
235 if (decimalPos != std::string::npos) {
236 size_t lastNonZeroPos = formatted.find_last_not_of('0');
237 if (lastNonZeroPos >= decimalPos) {
238 formatted.erase(lastNonZeroPos + 1);
239 }
240 if (formatted.back() == '.') {
241 formatted.pop_back();
242 }
243 }
244
245 return formatted;
246}
247
248std::string SecondsToXmlDuration(double seconds) {
249 // Chrome internally uses time accurate to microseconds, which is implemented
250 // per MSE spec (https://www.w3.org/TR/media-source/).
251 // We need a string formatter that has at least microseconds accuracy for a
252 // normal video (with duration up to 3 hours). FloatToXmlString
253 // implementation meets the requirement.
254 return absl::StrFormat("PT%sS", FloatToXmlString(seconds));
255}
256
257bool GetDurationAttribute(xmlNodePtr node, float* duration) {
258 DCHECK(node);
259 DCHECK(duration);
260 static const char kDuration[] = "duration";
261 xml::scoped_xml_ptr<xmlChar> duration_value(
262 xmlGetProp(node, BAD_CAST kDuration));
263
264 if (!duration_value)
265 return false;
266
267 double duration_double_precision = 0.0;
268 if (!absl::SimpleAtod(reinterpret_cast<const char*>(duration_value.get()),
269 &duration_double_precision)) {
270 return false;
271 }
272
273 *duration = static_cast<float>(duration_double_precision);
274 return true;
275}
276
277bool MoreThanOneTrue(bool b1, bool b2, bool b3) {
278 return (b1 && b2) || (b2 && b3) || (b3 && b1);
279}
280
281bool AtLeastOneTrue(bool b1, bool b2, bool b3) {
282 return b1 || b2 || b3;
283}
284
285bool OnlyOneTrue(bool b1, bool b2, bool b3) {
286 return !MoreThanOneTrue(b1, b2, b3) && AtLeastOneTrue(b1, b2, b3);
287}
288
289// Coverts binary data into human readable UUID format.
290bool HexToUUID(const std::string& data, std::string* uuid_format) {
291 DCHECK(uuid_format);
292 const size_t kExpectedUUIDSize = 16;
293 if (data.size() != kExpectedUUIDSize) {
294 LOG(ERROR) << "UUID size is expected to be " << kExpectedUUIDSize
295 << " but is " << data.size() << " and the data in hex is "
296 << absl::BytesToHexString(data);
297 return false;
298 }
299
300 const std::string hex_encoded =
301 absl::AsciiStrToLower(absl::BytesToHexString(data));
302 DCHECK_EQ(hex_encoded.size(), kExpectedUUIDSize * 2);
303 std::string_view all(hex_encoded);
304 // Note UUID has 5 parts separated with dashes.
305 // e.g. 123e4567-e89b-12d3-a456-426655440000
306 // These StringPieces have each part.
307 std::string_view first = all.substr(0, 8);
308 std::string_view second = all.substr(8, 4);
309 std::string_view third = all.substr(12, 4);
310 std::string_view fourth = all.substr(16, 4);
311 std::string_view fifth = all.substr(20, 12);
312
313 // 32 hexadecimal characters with 4 hyphens.
314 const size_t kHumanReadableUUIDSize = 36;
315 uuid_format->reserve(kHumanReadableUUIDSize);
316 absl::StrAppendFormat(uuid_format, "%s-%s-%s-%s-%s", first, second, third,
317 fourth, fifth);
318 return true;
319}
320
321void UpdateContentProtectionPsshHelper(
322 const std::string& drm_uuid,
323 const std::string& pssh,
324 std::list<ContentProtectionElement>* content_protection_elements) {
325 const std::string drm_uuid_schemd_id_uri_form = "urn:uuid:" + drm_uuid;
326 for (std::list<ContentProtectionElement>::iterator protection =
327 content_protection_elements->begin();
328 protection != content_protection_elements->end(); ++protection) {
329 if (protection->scheme_id_uri != drm_uuid_schemd_id_uri_form) {
330 continue;
331 }
332
333 for (std::vector<Element>::iterator subelement =
334 protection->subelements.begin();
335 subelement != protection->subelements.end(); ++subelement) {
336 if (subelement->name == kPsshElementName) {
337 // For now, we want to remove the PSSH element because some players do
338 // not support updating pssh.
339 protection->subelements.erase(subelement);
340
341 // TODO(rkuroiwa): Uncomment this and remove the line above when
342 // shaka-player supports updating PSSH.
343 // subelement->content = pssh;
344 return;
345 }
346 }
347
348 // Reaching here means <cenc:pssh> does not exist under the
349 // ContentProtection element. Add it.
350 // TODO(rkuroiwa): Uncomment this when shaka-player supports updating PSSH.
351 // Element cenc_pssh;
352 // cenc_pssh.name = kPsshElementName;
353 // cenc_pssh.content = pssh;
354 // protection->subelements.push_back(cenc_pssh);
355 return;
356 }
357
358 // Reaching here means that ContentProtection for the DRM does not exist.
359 // Add it.
360 ContentProtectionElement content_protection;
361 content_protection.scheme_id_uri = drm_uuid_schemd_id_uri_form;
362 // TODO(rkuroiwa): Uncomment this when shaka-player supports updating PSSH.
363 // Element cenc_pssh;
364 // cenc_pssh.name = kPsshElementName;
365 // cenc_pssh.content = pssh;
366 // content_protection.subelements.push_back(cenc_pssh);
367 content_protection_elements->push_back(content_protection);
368 return;
369}
370
371namespace {
372
373// UUID for Marlin Adaptive Streaming Specification – Simple Profile from
374// https://dashif.org/identifiers/content_protection/.
375const char kMarlinUUID[] = "5e629af5-38da-4063-8977-97ffbd9902d4";
376// String representation of media::kFairPlaySystemId.
377const char kFairPlayUUID[] = "94ce86fb-07ff-4f43-adb8-93d2fa968ca2";
378// String representation of media::kPlayReadySystemId.
379const char kPlayReadyUUID[] = "9a04f079-9840-4286-ab92-e65be0885f95";
380// It is RECOMMENDED to include the @value attribute with name and version
381// "MSPR 2.0". See
382// https://docs.microsoft.com/en-us/playready/specifications/mpeg-dash-playready#221-general.
383const char kContentProtectionValueMSPR20[] = "MSPR 2.0";
384
385Element GenerateMarlinContentIds(const std::string& key_id) {
386 // See https://github.com/shaka-project/shaka-packager/issues/381 for details.
387 static const char kMarlinContentIdName[] = "mas:MarlinContentId";
388 static const char kMarlinContentIdPrefix[] = "urn:marlin:kid:";
389 static const char kMarlinContentIdsName[] = "mas:MarlinContentIds";
390
391 Element marlin_content_id;
392 marlin_content_id.name = kMarlinContentIdName;
393 marlin_content_id.content =
394 kMarlinContentIdPrefix +
395 absl::AsciiStrToLower(absl::BytesToHexString(key_id));
396
397 Element marlin_content_ids;
398 marlin_content_ids.name = kMarlinContentIdsName;
399 marlin_content_ids.subelements.push_back(marlin_content_id);
400
401 return marlin_content_ids;
402}
403
404Element GenerateCencPsshElement(const std::string& pssh) {
405 std::string base64_encoded_pssh;
406 absl::Base64Escape(std::string_view(pssh.data(), pssh.size()),
407 &base64_encoded_pssh);
408 Element cenc_pssh;
409 cenc_pssh.name = kPsshElementName;
410 cenc_pssh.content = base64_encoded_pssh;
411 return cenc_pssh;
412}
413
414// Extract MS PlayReady Object from given PSSH
415// and encode it in base64.
416Element GenerateMsprProElement(const std::string& pssh) {
417 std::unique_ptr<media::PsshBoxBuilder> b =
419 reinterpret_cast<const uint8_t*>(pssh.data()),
420 pssh.size()
421 );
422
423 const std::vector<uint8_t> *p_pssh = &b->pssh_data();
424 std::string base64_encoded_mspr;
425 absl::Base64Escape(
426 std::string_view(reinterpret_cast<const char*>(p_pssh->data()),
427 p_pssh->size()),
428 &base64_encoded_mspr);
429 Element mspr_pro;
430 mspr_pro.name = kMsproElementName;
431 mspr_pro.content = base64_encoded_mspr;
432 return mspr_pro;
433}
434
435// Helper function. This works because Representation and AdaptationSet both
436// have AddContentProtectionElement().
437template <typename ContentProtectionParent>
438void AddContentProtectionElementsHelperTemplated(
439 const MediaInfo& media_info,
440 ContentProtectionParent* parent) {
441 DCHECK(parent);
442 if (!media_info.has_protected_content())
443 return;
444
445 const MediaInfo::ProtectedContent& protected_content =
446 media_info.protected_content();
447
448 // DASH MPD spec specifies a default ContentProtection element for ISO BMFF
449 // (MP4) files.
450 const bool is_mp4_container =
451 media_info.container_type() == MediaInfo::CONTAINER_MP4;
452 std::string key_id_uuid_format;
453 if (protected_content.has_default_key_id() &&
454 !IsKeyRotationDefaultKeyId(protected_content.default_key_id())) {
455 if (!HexToUUID(protected_content.default_key_id(), &key_id_uuid_format)) {
456 LOG(ERROR) << "Failed to convert default key ID into UUID format.";
457 }
458 }
459
460 if (is_mp4_container) {
461 ContentProtectionElement mp4_content_protection;
462 mp4_content_protection.scheme_id_uri = kEncryptedMp4Scheme;
463 mp4_content_protection.value = protected_content.protection_scheme();
464 if (!key_id_uuid_format.empty()) {
465 mp4_content_protection.additional_attributes["cenc:default_KID"] =
466 key_id_uuid_format;
467 }
468
469 parent->AddContentProtectionElement(mp4_content_protection);
470 }
471
472 for (const auto& entry : protected_content.content_protection_entry()) {
473 if (!entry.has_uuid()) {
474 LOG(WARNING)
475 << "ContentProtectionEntry was specified but no UUID is set for "
476 << entry.name_version() << ", skipping.";
477 continue;
478 }
479
480 ContentProtectionElement drm_content_protection;
481
482 if (entry.has_name_version())
483 drm_content_protection.value = entry.name_version();
484
485 if (entry.uuid() == kFairPlayUUID) {
486 VLOG(1) << "Skipping FairPlay ContentProtection element as FairPlay does "
487 "not support DASH signaling.";
488 continue;
489 } else if (entry.uuid() == kMarlinUUID) {
490 // Marlin requires its uuid to be in upper case. See #525 for details.
491 drm_content_protection.scheme_id_uri =
492 "urn:uuid:" + absl::AsciiStrToUpper(entry.uuid());
493 drm_content_protection.subelements.push_back(
494 GenerateMarlinContentIds(protected_content.default_key_id()));
495 } else {
496 drm_content_protection.scheme_id_uri = "urn:uuid:" + entry.uuid();
497 if (!entry.pssh().empty()) {
498 drm_content_protection.subelements.push_back(
499 GenerateCencPsshElement(entry.pssh()));
500 if(entry.uuid() == kPlayReadyUUID && protected_content.include_mspr_pro()) {
501 drm_content_protection.subelements.push_back(
502 GenerateMsprProElement(entry.pssh()));
503 drm_content_protection.value = kContentProtectionValueMSPR20;
504 }
505 }
506 }
507
508 if (!key_id_uuid_format.empty() && !is_mp4_container) {
509 drm_content_protection.additional_attributes["cenc:default_KID"] =
510 key_id_uuid_format;
511 }
512
513 parent->AddContentProtectionElement(drm_content_protection);
514 }
515
516 if (protected_content.content_protection_entry().size() == 0) {
517 VLOG(1) << "The media is encrypted but no content protection specified "
518 << "(can happen with key rotation).";
519 }
520}
521} // namespace
522
523void AddContentProtectionElements(const MediaInfo& media_info,
524 Representation* parent) {
525 AddContentProtectionElementsHelperTemplated(media_info, parent);
526}
527
528void AddContentProtectionElements(const MediaInfo& media_info,
529 AdaptationSet* parent) {
530 AddContentProtectionElementsHelperTemplated(media_info, parent);
531}
532
533} // namespace shaka
static std::unique_ptr< PsshBoxBuilder > ParseFromBox(const uint8_t *data, size_t data_size)
All the methods that are virtual are virtual for mocking.
bool HexToUUID(const std::string &data, std::string *uuid_format)
Definition mpd_utils.cc:290
std::string LanguageToShortestForm(const std::string &language)
void AddContentProtectionElements(const MediaInfo &media_info, Representation *parent)
Definition mpd_utils.cc:523