2023-12-01 17:32:19 +00:00
|
|
|
|
// Copyright 2014 Google LLC. All rights reserved.
|
2014-02-14 23:21:05 +00:00
|
|
|
|
//
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
|
#include <packager/mpd/base/mpd_utils.h>
|
2013-11-18 23:39:20 +00:00
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
|
#include <absl/flags/flag.h>
|
|
|
|
|
#include <absl/log/check.h>
|
|
|
|
|
#include <absl/log/log.h>
|
|
|
|
|
#include <absl/strings/escaping.h>
|
|
|
|
|
#include <absl/strings/numbers.h>
|
|
|
|
|
#include <absl/strings/str_format.h>
|
2014-08-28 18:35:15 +00:00
|
|
|
|
#include <libxml/tree.h>
|
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
|
#include <packager/macros/logging.h>
|
2024-05-11 00:42:34 +00:00
|
|
|
|
#include <packager/media/base/fourccs.h>
|
2023-12-01 17:32:19 +00:00
|
|
|
|
#include <packager/media/base/language_utils.h>
|
|
|
|
|
#include <packager/media/base/protection_system_specific_info.h>
|
|
|
|
|
#include <packager/mpd/base/adaptation_set.h>
|
|
|
|
|
#include <packager/mpd/base/content_protection_element.h>
|
|
|
|
|
#include <packager/mpd/base/representation.h>
|
|
|
|
|
#include <packager/mpd/base/xml/scoped_xml_ptr.h>
|
|
|
|
|
|
|
|
|
|
ABSL_FLAG(
|
|
|
|
|
bool,
|
2018-06-06 01:31:21 +00:00
|
|
|
|
use_legacy_vp9_codec_string,
|
|
|
|
|
false,
|
|
|
|
|
"Use legacy vp9 codec string 'vp9' if set to true; otherwise new style "
|
|
|
|
|
"vp09.xx.xx.xx... codec string will be used. Default to false as indicated "
|
2022-03-07 19:56:34 +00:00
|
|
|
|
"in https://github.com/shaka-project/shaka-packager/issues/406, all major "
|
2018-06-06 01:31:21 +00:00
|
|
|
|
"browsers and platforms already support the new 'vp09' codec string.");
|
|
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
|
namespace shaka {
|
2015-10-29 20:58:36 +00:00
|
|
|
|
namespace {
|
|
|
|
|
|
2017-07-28 00:03:19 +00:00
|
|
|
|
bool IsKeyRotationDefaultKeyId(const std::string& key_id) {
|
|
|
|
|
for (char c : key_id) {
|
|
|
|
|
if (c != '\0')
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-19 21:38:15 +00:00
|
|
|
|
std::string TextCodecString(const MediaInfo& media_info) {
|
2015-10-29 20:58:36 +00:00
|
|
|
|
CHECK(media_info.has_text_info());
|
2018-02-26 20:13:28 +00:00
|
|
|
|
const auto container_type = media_info.container_type();
|
|
|
|
|
|
|
|
|
|
// Codecs are not needed when mimeType is "text/*". Having a codec would be
|
|
|
|
|
// redundant.
|
|
|
|
|
if (container_type == MediaInfo::CONTAINER_TEXT) {
|
|
|
|
|
return "";
|
2015-10-29 20:58:36 +00:00
|
|
|
|
}
|
2018-02-26 20:13:28 +00:00
|
|
|
|
|
|
|
|
|
// DASH IOP mentions that the codec for ttml in mp4 is stpp, so override
|
|
|
|
|
// the default codec value.
|
|
|
|
|
const std::string& codec = media_info.text_info().codec();
|
|
|
|
|
if (codec == "ttml" && container_type == MediaInfo::CONTAINER_MP4) {
|
|
|
|
|
return "stpp";
|
2015-11-23 23:12:04 +00:00
|
|
|
|
}
|
2015-10-29 20:58:36 +00:00
|
|
|
|
|
2018-02-26 20:13:28 +00:00
|
|
|
|
return codec;
|
2015-10-29 20:58:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace
|
2013-11-18 23:39:20 +00:00
|
|
|
|
|
|
|
|
|
bool HasVODOnlyFields(const MediaInfo& media_info) {
|
|
|
|
|
return media_info.has_init_range() || media_info.has_index_range() ||
|
2018-04-17 17:42:45 +00:00
|
|
|
|
media_info.has_media_file_url();
|
2013-11-18 23:39:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool HasLiveOnlyFields(const MediaInfo& media_info) {
|
2018-04-17 17:42:45 +00:00
|
|
|
|
return media_info.has_init_segment_url() ||
|
2017-09-25 19:18:50 +00:00
|
|
|
|
media_info.has_segment_template_url();
|
2013-11-18 23:39:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RemoveDuplicateAttributes(
|
|
|
|
|
ContentProtectionElement* content_protection_element) {
|
|
|
|
|
DCHECK(content_protection_element);
|
|
|
|
|
typedef std::map<std::string, std::string> AttributesMap;
|
|
|
|
|
|
|
|
|
|
AttributesMap& attributes = content_protection_element->additional_attributes;
|
|
|
|
|
if (!content_protection_element->value.empty())
|
|
|
|
|
attributes.erase("value");
|
|
|
|
|
|
|
|
|
|
if (!content_protection_element->scheme_id_uri.empty())
|
|
|
|
|
attributes.erase("schemeIdUri");
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-08 19:19:12 +00:00
|
|
|
|
std::string GetLanguage(const MediaInfo& media_info) {
|
|
|
|
|
std::string lang;
|
|
|
|
|
if (media_info.has_audio_info()) {
|
|
|
|
|
lang = media_info.audio_info().language();
|
|
|
|
|
} else if (media_info.has_text_info()) {
|
|
|
|
|
lang = media_info.text_info().language();
|
|
|
|
|
}
|
2018-04-05 01:27:57 +00:00
|
|
|
|
return LanguageToShortestForm(lang);
|
2016-03-08 19:19:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-11-18 23:39:20 +00:00
|
|
|
|
std::string GetCodecs(const MediaInfo& media_info) {
|
2015-10-29 20:58:36 +00:00
|
|
|
|
CHECK(OnlyOneTrue(media_info.has_video_info(), media_info.has_audio_info(),
|
|
|
|
|
media_info.has_text_info()));
|
|
|
|
|
|
2016-01-11 21:36:44 +00:00
|
|
|
|
if (media_info.has_video_info()) {
|
|
|
|
|
if (media_info.container_type() == MediaInfo::CONTAINER_WEBM) {
|
|
|
|
|
std::string codec = media_info.video_info().codec().substr(0, 4);
|
|
|
|
|
// media_info.video_info().codec() contains new revised codec string
|
|
|
|
|
// specified by "VPx in ISO BMFF" document, which is not compatible to
|
|
|
|
|
// old codec strings in WebM. Hack it here before all browsers support
|
|
|
|
|
// new codec strings.
|
|
|
|
|
if (codec == "vp08")
|
|
|
|
|
return "vp8";
|
2023-12-01 17:32:19 +00:00
|
|
|
|
if (absl::GetFlag(FLAGS_use_legacy_vp9_codec_string)) {
|
2018-06-06 01:31:21 +00:00
|
|
|
|
if (codec == "vp09")
|
|
|
|
|
return "vp9";
|
|
|
|
|
}
|
2016-01-11 21:36:44 +00:00
|
|
|
|
}
|
2015-10-29 20:58:36 +00:00
|
|
|
|
return media_info.video_info().codec();
|
2016-01-11 21:36:44 +00:00
|
|
|
|
}
|
2015-06-09 23:58:32 +00:00
|
|
|
|
|
|
|
|
|
if (media_info.has_audio_info())
|
2015-10-29 20:58:36 +00:00
|
|
|
|
return media_info.audio_info().codec();
|
|
|
|
|
|
|
|
|
|
if (media_info.has_text_info())
|
|
|
|
|
return TextCodecString(media_info);
|
2013-11-18 23:39:20 +00:00
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
|
NOTIMPLEMENTED();
|
2013-11-18 23:39:20 +00:00
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-11 00:42:34 +00:00
|
|
|
|
std::string GetSupplementalCodecs(const MediaInfo& media_info) {
|
|
|
|
|
CHECK(OnlyOneTrue(media_info.has_video_info(), media_info.has_audio_info(),
|
|
|
|
|
media_info.has_text_info()));
|
|
|
|
|
|
|
|
|
|
if (media_info.has_video_info() &&
|
|
|
|
|
media_info.video_info().has_supplemental_codec()) {
|
|
|
|
|
return media_info.video_info().supplemental_codec();
|
|
|
|
|
}
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string GetSupplementalProfiles(const MediaInfo& media_info) {
|
|
|
|
|
CHECK(OnlyOneTrue(media_info.has_video_info(), media_info.has_audio_info(),
|
|
|
|
|
media_info.has_text_info()));
|
|
|
|
|
|
|
|
|
|
if (media_info.has_video_info() &&
|
|
|
|
|
media_info.video_info().has_compatible_brand()) {
|
|
|
|
|
return FourCCToString(
|
|
|
|
|
static_cast<media::FourCC>(media_info.video_info().compatible_brand()));
|
|
|
|
|
}
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-08 19:19:12 +00:00
|
|
|
|
std::string GetBaseCodec(const MediaInfo& media_info) {
|
|
|
|
|
std::string codec;
|
|
|
|
|
if (media_info.has_video_info()) {
|
|
|
|
|
codec = media_info.video_info().codec();
|
|
|
|
|
} else if (media_info.has_audio_info()) {
|
|
|
|
|
codec = media_info.audio_info().codec();
|
|
|
|
|
} else if (media_info.has_text_info()) {
|
2018-02-26 20:13:28 +00:00
|
|
|
|
codec = media_info.text_info().codec();
|
2016-03-08 19:19:12 +00:00
|
|
|
|
}
|
|
|
|
|
// Convert, for example, "mp4a.40.2" to simply "mp4a".
|
|
|
|
|
// "mp4a.40.2" and "mp4a.40.5" can exist in the same AdaptationSet.
|
|
|
|
|
size_t dot = codec.find('.');
|
|
|
|
|
if (dot != std::string::npos) {
|
|
|
|
|
codec.erase(dot);
|
|
|
|
|
}
|
|
|
|
|
return codec;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-18 02:33:44 +00:00
|
|
|
|
std::string GetAdaptationSetKey(const MediaInfo& media_info,
|
|
|
|
|
bool ignore_codec) {
|
2016-03-08 19:19:12 +00:00
|
|
|
|
std::string key;
|
|
|
|
|
|
|
|
|
|
if (media_info.has_video_info()) {
|
|
|
|
|
key.append("video:");
|
|
|
|
|
} else if (media_info.has_audio_info()) {
|
|
|
|
|
key.append("audio:");
|
|
|
|
|
} else if (media_info.has_text_info()) {
|
|
|
|
|
key.append(MediaInfo_TextInfo_TextType_Name(media_info.text_info().type()));
|
|
|
|
|
key.append(":");
|
|
|
|
|
} else {
|
|
|
|
|
key.append("unknown:");
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-14 18:36:08 +00:00
|
|
|
|
if (media_info.has_dash_label())
|
|
|
|
|
key.append(media_info.dash_label() + ":");
|
|
|
|
|
|
2016-03-08 19:19:12 +00:00
|
|
|
|
key.append(MediaInfo_ContainerType_Name(media_info.container_type()));
|
2020-03-18 02:33:44 +00:00
|
|
|
|
if (!ignore_codec) {
|
|
|
|
|
key.append(":");
|
|
|
|
|
key.append(GetBaseCodec(media_info));
|
2023-09-08 21:41:41 +00:00
|
|
|
|
|
|
|
|
|
if (GetBaseCodec(media_info).find("dvh") == 0) {
|
|
|
|
|
// Transfer characteristics for Dolby Vision (dvh1 or dvhe) must be PQ
|
|
|
|
|
// irrespective of value present in SPS VUI.
|
|
|
|
|
key.append(":");
|
|
|
|
|
key.append(std::to_string(kTransferFunctionPQ));
|
|
|
|
|
} else if (media_info.video_info().has_transfer_characteristics()) {
|
|
|
|
|
key.append(":");
|
|
|
|
|
key.append(
|
|
|
|
|
std::to_string(media_info.video_info().transfer_characteristics()));
|
|
|
|
|
}
|
2020-03-18 02:33:44 +00:00
|
|
|
|
}
|
2016-03-08 19:19:12 +00:00
|
|
|
|
key.append(":");
|
|
|
|
|
key.append(GetLanguage(media_info));
|
|
|
|
|
|
2017-03-21 23:14:46 +00:00
|
|
|
|
// Trick play streams of the same original stream, but possibly with
|
2017-05-15 16:22:06 +00:00
|
|
|
|
// different trick_play_factors, belong to the same trick play AdaptationSet.
|
2017-05-16 16:59:52 +00:00
|
|
|
|
if (media_info.video_info().has_playback_rate()) {
|
2017-03-21 23:14:46 +00:00
|
|
|
|
key.append(":trick_play");
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-13 06:01:16 +00:00
|
|
|
|
if (!media_info.dash_accessibilities().empty()) {
|
|
|
|
|
key.append(":accessibility_");
|
|
|
|
|
for (const std::string& accessibility : media_info.dash_accessibilities())
|
|
|
|
|
key.append(accessibility);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!media_info.dash_roles().empty()) {
|
|
|
|
|
key.append(":roles_");
|
|
|
|
|
for (const std::string& role : media_info.dash_roles())
|
|
|
|
|
key.append(role);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-08 19:19:12 +00:00
|
|
|
|
return key;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
|
std::string FloatToXmlString(double number) {
|
|
|
|
|
// Keep up to microsecond accuracy but trim trailing 0s
|
2024-02-08 17:48:14 +00:00
|
|
|
|
std::string formatted = absl::StrFormat("%.6f", number);
|
2023-12-01 17:32:19 +00:00
|
|
|
|
size_t decimalPos = formatted.find('.');
|
|
|
|
|
if (decimalPos != std::string::npos) {
|
|
|
|
|
size_t lastNonZeroPos = formatted.find_last_not_of('0');
|
|
|
|
|
if (lastNonZeroPos >= decimalPos) {
|
|
|
|
|
formatted.erase(lastNonZeroPos + 1);
|
|
|
|
|
}
|
|
|
|
|
if (formatted.back() == '.') {
|
|
|
|
|
formatted.pop_back();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return formatted;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-22 02:16:17 +00:00
|
|
|
|
std::string SecondsToXmlDuration(double seconds) {
|
2018-04-02 23:53:29 +00:00
|
|
|
|
// Chrome internally uses time accurate to microseconds, which is implemented
|
|
|
|
|
// per MSE spec (https://www.w3.org/TR/media-source/).
|
|
|
|
|
// We need a string formatter that has at least microseconds accuracy for a
|
2023-12-01 17:32:19 +00:00
|
|
|
|
// normal video (with duration up to 3 hours). FloatToXmlString
|
2018-04-02 23:53:29 +00:00
|
|
|
|
// implementation meets the requirement.
|
2023-12-01 17:32:19 +00:00
|
|
|
|
return absl::StrFormat("PT%sS", FloatToXmlString(seconds));
|
2013-11-18 23:39:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-03 00:59:16 +00:00
|
|
|
|
bool GetDurationAttribute(xmlNodePtr node, float* duration) {
|
2013-11-18 23:39:20 +00:00
|
|
|
|
DCHECK(node);
|
|
|
|
|
DCHECK(duration);
|
|
|
|
|
static const char kDuration[] = "duration";
|
2015-11-17 00:06:17 +00:00
|
|
|
|
xml::scoped_xml_ptr<xmlChar> duration_value(
|
2013-11-18 23:39:20 +00:00
|
|
|
|
xmlGetProp(node, BAD_CAST kDuration));
|
|
|
|
|
|
|
|
|
|
if (!duration_value)
|
|
|
|
|
return false;
|
|
|
|
|
|
2014-01-03 00:59:16 +00:00
|
|
|
|
double duration_double_precision = 0.0;
|
2023-12-01 17:32:19 +00:00
|
|
|
|
if (!absl::SimpleAtod(reinterpret_cast<const char*>(duration_value.get()),
|
|
|
|
|
&duration_double_precision)) {
|
2014-01-03 00:59:16 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*duration = static_cast<float>(duration_double_precision);
|
|
|
|
|
return true;
|
2013-11-18 23:39:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-06 08:52:06 +00:00
|
|
|
|
bool MoreThanOneTrue(bool b1, bool b2, bool b3) {
|
|
|
|
|
return (b1 && b2) || (b2 && b3) || (b3 && b1);
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-26 20:13:28 +00:00
|
|
|
|
bool AtLeastOneTrue(bool b1, bool b2, bool b3) {
|
|
|
|
|
return b1 || b2 || b3;
|
|
|
|
|
}
|
2014-06-06 08:52:06 +00:00
|
|
|
|
|
|
|
|
|
bool OnlyOneTrue(bool b1, bool b2, bool b3) {
|
2018-02-26 20:13:28 +00:00
|
|
|
|
return !MoreThanOneTrue(b1, b2, b3) && AtLeastOneTrue(b1, b2, b3);
|
2014-06-06 08:52:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-08-26 20:25:29 +00:00
|
|
|
|
// Coverts binary data into human readable UUID format.
|
|
|
|
|
bool HexToUUID(const std::string& data, std::string* uuid_format) {
|
|
|
|
|
DCHECK(uuid_format);
|
|
|
|
|
const size_t kExpectedUUIDSize = 16;
|
|
|
|
|
if (data.size() != kExpectedUUIDSize) {
|
|
|
|
|
LOG(ERROR) << "UUID size is expected to be " << kExpectedUUIDSize
|
|
|
|
|
<< " but is " << data.size() << " and the data in hex is "
|
2023-12-01 17:32:19 +00:00
|
|
|
|
<< absl::BytesToHexString(data);
|
2015-08-26 20:25:29 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const std::string hex_encoded =
|
2023-12-01 17:32:19 +00:00
|
|
|
|
absl::AsciiStrToLower(absl::BytesToHexString(data));
|
2015-08-26 20:25:29 +00:00
|
|
|
|
DCHECK_EQ(hex_encoded.size(), kExpectedUUIDSize * 2);
|
2023-12-01 17:32:19 +00:00
|
|
|
|
std::string_view all(hex_encoded);
|
2015-08-26 20:25:29 +00:00
|
|
|
|
// Note UUID has 5 parts separated with dashes.
|
|
|
|
|
// e.g. 123e4567-e89b-12d3-a456-426655440000
|
|
|
|
|
// These StringPieces have each part.
|
2023-12-01 17:32:19 +00:00
|
|
|
|
std::string_view first = all.substr(0, 8);
|
|
|
|
|
std::string_view second = all.substr(8, 4);
|
|
|
|
|
std::string_view third = all.substr(12, 4);
|
|
|
|
|
std::string_view fourth = all.substr(16, 4);
|
|
|
|
|
std::string_view fifth = all.substr(20, 12);
|
2015-08-26 20:25:29 +00:00
|
|
|
|
|
|
|
|
|
// 32 hexadecimal characters with 4 hyphens.
|
|
|
|
|
const size_t kHumanReadableUUIDSize = 36;
|
|
|
|
|
uuid_format->reserve(kHumanReadableUUIDSize);
|
2023-12-01 17:32:19 +00:00
|
|
|
|
absl::StrAppendFormat(uuid_format, "%s-%s-%s-%s-%s", first, second, third,
|
|
|
|
|
fourth, fifth);
|
2015-08-26 20:25:29 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UpdateContentProtectionPsshHelper(
|
2015-09-24 22:03:52 +00:00
|
|
|
|
const std::string& drm_uuid,
|
2015-08-26 20:25:29 +00:00
|
|
|
|
const std::string& pssh,
|
2015-09-24 22:03:52 +00:00
|
|
|
|
std::list<ContentProtectionElement>* content_protection_elements) {
|
|
|
|
|
const std::string drm_uuid_schemd_id_uri_form = "urn:uuid:" + drm_uuid;
|
2015-08-26 20:25:29 +00:00
|
|
|
|
for (std::list<ContentProtectionElement>::iterator protection =
|
2015-09-24 22:03:52 +00:00
|
|
|
|
content_protection_elements->begin();
|
|
|
|
|
protection != content_protection_elements->end(); ++protection) {
|
|
|
|
|
if (protection->scheme_id_uri != drm_uuid_schemd_id_uri_form) {
|
2015-08-26 20:25:29 +00:00
|
|
|
|
continue;
|
2015-09-24 22:03:52 +00:00
|
|
|
|
}
|
2015-08-26 20:25:29 +00:00
|
|
|
|
|
|
|
|
|
for (std::vector<Element>::iterator subelement =
|
|
|
|
|
protection->subelements.begin();
|
|
|
|
|
subelement != protection->subelements.end(); ++subelement) {
|
|
|
|
|
if (subelement->name == kPsshElementName) {
|
2015-09-24 22:03:52 +00:00
|
|
|
|
// For now, we want to remove the PSSH element because some players do
|
|
|
|
|
// not support updating pssh.
|
|
|
|
|
protection->subelements.erase(subelement);
|
|
|
|
|
|
|
|
|
|
// TODO(rkuroiwa): Uncomment this and remove the line above when
|
|
|
|
|
// shaka-player supports updating PSSH.
|
|
|
|
|
// subelement->content = pssh;
|
2015-08-26 20:25:29 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-09-24 22:03:52 +00:00
|
|
|
|
|
|
|
|
|
// Reaching here means <cenc:pssh> does not exist under the
|
|
|
|
|
// ContentProtection element. Add it.
|
|
|
|
|
// TODO(rkuroiwa): Uncomment this when shaka-player supports updating PSSH.
|
|
|
|
|
// Element cenc_pssh;
|
|
|
|
|
// cenc_pssh.name = kPsshElementName;
|
|
|
|
|
// cenc_pssh.content = pssh;
|
|
|
|
|
// protection->subelements.push_back(cenc_pssh);
|
2015-08-26 20:25:29 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-24 22:03:52 +00:00
|
|
|
|
// Reaching here means that ContentProtection for the DRM does not exist.
|
2015-08-26 20:25:29 +00:00
|
|
|
|
// Add it.
|
|
|
|
|
ContentProtectionElement content_protection;
|
2015-09-24 22:03:52 +00:00
|
|
|
|
content_protection.scheme_id_uri = drm_uuid_schemd_id_uri_form;
|
|
|
|
|
// TODO(rkuroiwa): Uncomment this when shaka-player supports updating PSSH.
|
|
|
|
|
// Element cenc_pssh;
|
|
|
|
|
// cenc_pssh.name = kPsshElementName;
|
|
|
|
|
// cenc_pssh.content = pssh;
|
|
|
|
|
// content_protection.subelements.push_back(cenc_pssh);
|
|
|
|
|
content_protection_elements->push_back(content_protection);
|
2015-08-26 20:25:29 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace {
|
2018-09-18 00:27:02 +00:00
|
|
|
|
|
|
|
|
|
// UUID for Marlin Adaptive Streaming Specification – Simple Profile from
|
|
|
|
|
// https://dashif.org/identifiers/content_protection/.
|
|
|
|
|
const char kMarlinUUID[] = "5e629af5-38da-4063-8977-97ffbd9902d4";
|
2023-11-30 06:33:13 +00:00
|
|
|
|
// String representation of media::kFairPlaySystemId.
|
|
|
|
|
const char kFairPlayUUID[] = "94ce86fb-07ff-4f43-adb8-93d2fa968ca2";
|
2020-04-17 17:20:03 +00:00
|
|
|
|
// String representation of media::kPlayReadySystemId.
|
|
|
|
|
const char kPlayReadyUUID[] = "9a04f079-9840-4286-ab92-e65be0885f95";
|
2023-12-01 17:32:19 +00:00
|
|
|
|
// It is RECOMMENDED to include the @value attribute with name and version
|
|
|
|
|
// "MSPR 2.0". See
|
|
|
|
|
// https://docs.microsoft.com/en-us/playready/specifications/mpeg-dash-playready#221-general.
|
2020-04-17 17:20:03 +00:00
|
|
|
|
const char kContentProtectionValueMSPR20[] = "MSPR 2.0";
|
2018-09-18 00:27:02 +00:00
|
|
|
|
|
|
|
|
|
Element GenerateMarlinContentIds(const std::string& key_id) {
|
2022-03-07 19:56:34 +00:00
|
|
|
|
// See https://github.com/shaka-project/shaka-packager/issues/381 for details.
|
2018-09-18 00:27:02 +00:00
|
|
|
|
static const char kMarlinContentIdName[] = "mas:MarlinContentId";
|
|
|
|
|
static const char kMarlinContentIdPrefix[] = "urn:marlin:kid:";
|
|
|
|
|
static const char kMarlinContentIdsName[] = "mas:MarlinContentIds";
|
|
|
|
|
|
|
|
|
|
Element marlin_content_id;
|
|
|
|
|
marlin_content_id.name = kMarlinContentIdName;
|
|
|
|
|
marlin_content_id.content =
|
2018-11-17 00:44:44 +00:00
|
|
|
|
kMarlinContentIdPrefix +
|
2023-12-01 17:32:19 +00:00
|
|
|
|
absl::AsciiStrToLower(absl::BytesToHexString(key_id));
|
2018-09-18 00:27:02 +00:00
|
|
|
|
|
|
|
|
|
Element marlin_content_ids;
|
|
|
|
|
marlin_content_ids.name = kMarlinContentIdsName;
|
|
|
|
|
marlin_content_ids.subelements.push_back(marlin_content_id);
|
|
|
|
|
|
|
|
|
|
return marlin_content_ids;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Element GenerateCencPsshElement(const std::string& pssh) {
|
|
|
|
|
std::string base64_encoded_pssh;
|
2023-12-01 17:32:19 +00:00
|
|
|
|
absl::Base64Escape(std::string_view(pssh.data(), pssh.size()),
|
2018-09-18 00:27:02 +00:00
|
|
|
|
&base64_encoded_pssh);
|
|
|
|
|
Element cenc_pssh;
|
|
|
|
|
cenc_pssh.name = kPsshElementName;
|
|
|
|
|
cenc_pssh.content = base64_encoded_pssh;
|
|
|
|
|
return cenc_pssh;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-17 17:20:03 +00:00
|
|
|
|
// Extract MS PlayReady Object from given PSSH
|
|
|
|
|
// and encode it in base64.
|
|
|
|
|
Element GenerateMsprProElement(const std::string& pssh) {
|
|
|
|
|
std::unique_ptr<media::PsshBoxBuilder> b =
|
|
|
|
|
media::PsshBoxBuilder::ParseFromBox(
|
|
|
|
|
reinterpret_cast<const uint8_t*>(pssh.data()),
|
|
|
|
|
pssh.size()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const std::vector<uint8_t> *p_pssh = &b->pssh_data();
|
|
|
|
|
std::string base64_encoded_mspr;
|
2023-12-01 17:32:19 +00:00
|
|
|
|
absl::Base64Escape(
|
|
|
|
|
std::string_view(reinterpret_cast<const char*>(p_pssh->data()),
|
|
|
|
|
p_pssh->size()),
|
|
|
|
|
&base64_encoded_mspr);
|
2020-04-17 17:20:03 +00:00
|
|
|
|
Element mspr_pro;
|
|
|
|
|
mspr_pro.name = kMsproElementName;
|
|
|
|
|
mspr_pro.content = base64_encoded_mspr;
|
|
|
|
|
return mspr_pro;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-26 20:25:29 +00:00
|
|
|
|
// Helper function. This works because Representation and AdaptationSet both
|
|
|
|
|
// have AddContentProtectionElement().
|
|
|
|
|
template <typename ContentProtectionParent>
|
|
|
|
|
void AddContentProtectionElementsHelperTemplated(
|
|
|
|
|
const MediaInfo& media_info,
|
|
|
|
|
ContentProtectionParent* parent) {
|
|
|
|
|
DCHECK(parent);
|
|
|
|
|
if (!media_info.has_protected_content())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
const MediaInfo::ProtectedContent& protected_content =
|
|
|
|
|
media_info.protected_content();
|
|
|
|
|
|
|
|
|
|
// DASH MPD spec specifies a default ContentProtection element for ISO BMFF
|
|
|
|
|
// (MP4) files.
|
|
|
|
|
const bool is_mp4_container =
|
|
|
|
|
media_info.container_type() == MediaInfo::CONTAINER_MP4;
|
2016-01-21 00:14:24 +00:00
|
|
|
|
std::string key_id_uuid_format;
|
2017-07-28 00:03:19 +00:00
|
|
|
|
if (protected_content.has_default_key_id() &&
|
|
|
|
|
!IsKeyRotationDefaultKeyId(protected_content.default_key_id())) {
|
2016-01-21 00:14:24 +00:00
|
|
|
|
if (!HexToUUID(protected_content.default_key_id(), &key_id_uuid_format)) {
|
|
|
|
|
LOG(ERROR) << "Failed to convert default key ID into UUID format.";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-26 20:25:29 +00:00
|
|
|
|
if (is_mp4_container) {
|
|
|
|
|
ContentProtectionElement mp4_content_protection;
|
|
|
|
|
mp4_content_protection.scheme_id_uri = kEncryptedMp4Scheme;
|
2016-04-21 23:21:16 +00:00
|
|
|
|
mp4_content_protection.value = protected_content.protection_scheme();
|
2016-01-21 00:14:24 +00:00
|
|
|
|
if (!key_id_uuid_format.empty()) {
|
|
|
|
|
mp4_content_protection.additional_attributes["cenc:default_KID"] =
|
|
|
|
|
key_id_uuid_format;
|
2015-08-26 20:25:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parent->AddContentProtectionElement(mp4_content_protection);
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-28 00:03:19 +00:00
|
|
|
|
for (const auto& entry : protected_content.content_protection_entry()) {
|
2015-08-26 20:25:29 +00:00
|
|
|
|
if (!entry.has_uuid()) {
|
|
|
|
|
LOG(WARNING)
|
|
|
|
|
<< "ContentProtectionEntry was specified but no UUID is set for "
|
|
|
|
|
<< entry.name_version() << ", skipping.";
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ContentProtectionElement drm_content_protection;
|
2018-09-18 00:27:02 +00:00
|
|
|
|
|
2015-08-26 20:25:29 +00:00
|
|
|
|
if (entry.has_name_version())
|
|
|
|
|
drm_content_protection.value = entry.name_version();
|
|
|
|
|
|
2018-09-18 00:27:02 +00:00
|
|
|
|
if (entry.uuid() == kFairPlayUUID) {
|
|
|
|
|
VLOG(1) << "Skipping FairPlay ContentProtection element as FairPlay does "
|
|
|
|
|
"not support DASH signaling.";
|
|
|
|
|
continue;
|
|
|
|
|
} else if (entry.uuid() == kMarlinUUID) {
|
2018-12-05 21:03:07 +00:00
|
|
|
|
// Marlin requires its uuid to be in upper case. See #525 for details.
|
|
|
|
|
drm_content_protection.scheme_id_uri =
|
2023-12-01 17:32:19 +00:00
|
|
|
|
"urn:uuid:" + absl::AsciiStrToUpper(entry.uuid());
|
2018-09-18 00:27:02 +00:00
|
|
|
|
drm_content_protection.subelements.push_back(
|
|
|
|
|
GenerateMarlinContentIds(protected_content.default_key_id()));
|
2018-12-05 21:03:07 +00:00
|
|
|
|
} else {
|
|
|
|
|
drm_content_protection.scheme_id_uri = "urn:uuid:" + entry.uuid();
|
|
|
|
|
if (!entry.pssh().empty()) {
|
|
|
|
|
drm_content_protection.subelements.push_back(
|
|
|
|
|
GenerateCencPsshElement(entry.pssh()));
|
2020-04-17 17:20:03 +00:00
|
|
|
|
if(entry.uuid() == kPlayReadyUUID && protected_content.include_mspr_pro()) {
|
|
|
|
|
drm_content_protection.subelements.push_back(
|
|
|
|
|
GenerateMsprProElement(entry.pssh()));
|
|
|
|
|
drm_content_protection.value = kContentProtectionValueMSPR20;
|
|
|
|
|
}
|
2018-12-05 21:03:07 +00:00
|
|
|
|
}
|
2015-08-26 20:25:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-21 00:14:24 +00:00
|
|
|
|
if (!key_id_uuid_format.empty() && !is_mp4_container) {
|
|
|
|
|
drm_content_protection.additional_attributes["cenc:default_KID"] =
|
|
|
|
|
key_id_uuid_format;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-26 20:25:29 +00:00
|
|
|
|
parent->AddContentProtectionElement(drm_content_protection);
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
|
if (protected_content.content_protection_entry().size() == 0) {
|
|
|
|
|
VLOG(1) << "The media is encrypted but no content protection specified "
|
|
|
|
|
<< "(can happen with key rotation).";
|
|
|
|
|
}
|
2015-08-26 20:25:29 +00:00
|
|
|
|
}
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
void AddContentProtectionElements(const MediaInfo& media_info,
|
|
|
|
|
Representation* parent) {
|
|
|
|
|
AddContentProtectionElementsHelperTemplated(media_info, parent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AddContentProtectionElements(const MediaInfo& media_info,
|
|
|
|
|
AdaptationSet* parent) {
|
|
|
|
|
AddContentProtectionElementsHelperTemplated(media_info, parent);
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
|
} // namespace shaka
|