2022-08-26 15:44:59 +00:00
|
|
|
// Copyright 2016 Google LLC. All rights reserved.
|
2016-03-25 08:40:15 +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-10-10 23:51:11 +00:00
|
|
|
#include <packager/hls/base/simple_hls_notifier.h>
|
2016-03-25 08:40:15 +00:00
|
|
|
|
2017-06-03 00:05:47 +00:00
|
|
|
#include <cmath>
|
2023-10-11 08:49:50 +00:00
|
|
|
#include <filesystem>
|
|
|
|
#include <optional>
|
2017-06-03 00:05:47 +00:00
|
|
|
|
2023-10-11 08:49:50 +00:00
|
|
|
#include <absl/flags/flag.h>
|
2023-10-13 19:42:47 +00:00
|
|
|
#include <absl/log/check.h>
|
|
|
|
#include <absl/log/log.h>
|
2023-08-22 01:41:18 +00:00
|
|
|
#include <absl/strings/escaping.h>
|
|
|
|
#include <absl/strings/numbers.h>
|
2023-10-11 08:49:50 +00:00
|
|
|
|
2023-10-10 23:51:11 +00:00
|
|
|
#include <packager/file/file_util.h>
|
|
|
|
#include <packager/media/base/protection_system_ids.h>
|
|
|
|
#include <packager/media/base/protection_system_specific_info.h>
|
|
|
|
#include <packager/media/base/proto_json_util.h>
|
|
|
|
#include <packager/media/base/widevine_pssh_data.pb.h>
|
2016-03-25 08:40:15 +00:00
|
|
|
|
2023-08-22 01:41:18 +00:00
|
|
|
ABSL_FLAG(bool,
|
|
|
|
enable_legacy_widevine_hls_signaling,
|
|
|
|
false,
|
|
|
|
"Specifies whether Legacy Widevine HLS, i.e. v1 is signalled in "
|
|
|
|
"the media playlist. Applies to Widevine protection system in HLS "
|
|
|
|
"with SAMPLE-AES only.");
|
2018-08-09 22:57:57 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2017-09-08 17:52:48 +00:00
|
|
|
|
2016-03-25 08:40:15 +00:00
|
|
|
namespace hls {
|
|
|
|
|
|
|
|
namespace {
|
2017-04-04 20:57:50 +00:00
|
|
|
|
|
|
|
const char kUriBase64Prefix[] = "data:text/plain;base64,";
|
2018-08-07 23:01:43 +00:00
|
|
|
const char kUriFairPlayPrefix[] = "skd://";
|
2017-04-04 20:57:50 +00:00
|
|
|
const char kWidevineDashIfIopUUID[] =
|
|
|
|
"urn:uuid:edef8ba9-79d6-4ace-a3c8-27dcd51d21ed";
|
|
|
|
|
2016-03-25 08:40:15 +00:00
|
|
|
bool IsWidevineSystemId(const std::vector<uint8_t>& system_id) {
|
2023-08-22 01:41:18 +00:00
|
|
|
return system_id.size() == std::size(media::kWidevineSystemId) &&
|
2016-06-24 00:22:37 +00:00
|
|
|
std::equal(system_id.begin(), system_id.end(),
|
|
|
|
media::kWidevineSystemId);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsCommonSystemId(const std::vector<uint8_t>& system_id) {
|
2023-08-22 01:41:18 +00:00
|
|
|
return system_id.size() == std::size(media::kCommonSystemId) &&
|
2016-06-24 00:22:37 +00:00
|
|
|
std::equal(system_id.begin(), system_id.end(), media::kCommonSystemId);
|
2016-03-25 08:40:15 +00:00
|
|
|
}
|
2016-04-16 22:58:47 +00:00
|
|
|
|
2018-08-07 23:01:43 +00:00
|
|
|
bool IsFairPlaySystemId(const std::vector<uint8_t>& system_id) {
|
2023-08-22 01:41:18 +00:00
|
|
|
return system_id.size() == std::size(media::kFairPlaySystemId) &&
|
2018-08-07 23:01:43 +00:00
|
|
|
std::equal(system_id.begin(), system_id.end(),
|
|
|
|
media::kFairPlaySystemId);
|
Fairplay key system support
Support generation of fairplay key system tag: "com.apple.streamingkeydelivery" when --pssh
includes fairplay key system id:
// Unofficial fairplay system id extracted from
// https://forums.developer.apple.com/thread/6185.
const uint8_t kFairplaySystemId[] = {0x29, 0x70, 0x1F, 0xE4, 0x3C, 0xC7,
0x4A, 0x34, 0x8C, 0x5B, 0xAE, 0x90,
0xC7, 0x43, 0x9A, 0x47};
Closes #258
2017-08-08 17:31:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Base64EncodeData(const std::string& prefix,
|
|
|
|
const std::string& data) {
|
|
|
|
std::string data_base64;
|
2023-08-22 01:41:18 +00:00
|
|
|
absl::Base64Escape(data, &data_base64);
|
Fairplay key system support
Support generation of fairplay key system tag: "com.apple.streamingkeydelivery" when --pssh
includes fairplay key system id:
// Unofficial fairplay system id extracted from
// https://forums.developer.apple.com/thread/6185.
const uint8_t kFairplaySystemId[] = {0x29, 0x70, 0x1F, 0xE4, 0x3C, 0xC7,
0x4A, 0x34, 0x8C, 0x5B, 0xAE, 0x90,
0xC7, 0x43, 0x9A, 0x47};
Closes #258
2017-08-08 17:31:40 +00:00
|
|
|
return prefix + data_base64;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string VectorToString(const std::vector<uint8_t>& v) {
|
|
|
|
return std::string(v.begin(), v.end());
|
|
|
|
}
|
|
|
|
|
2017-09-08 17:52:48 +00:00
|
|
|
// Segment URL is relative to either output directory or the directory
|
|
|
|
// containing the media playlist depends on whether base_url is set.
|
|
|
|
std::string GenerateSegmentUrl(const std::string& segment_name,
|
|
|
|
const std::string& base_url,
|
|
|
|
const std::string& output_dir,
|
|
|
|
const std::string& playlist_file_name) {
|
2023-08-22 01:41:18 +00:00
|
|
|
auto output_path = std::filesystem::u8path(output_dir);
|
2017-09-08 17:52:48 +00:00
|
|
|
if (!base_url.empty()) {
|
|
|
|
// Media segment URL is base_url + segment path relative to output
|
|
|
|
// directory.
|
|
|
|
return base_url + MakePathRelative(segment_name, output_path);
|
2016-04-16 22:58:47 +00:00
|
|
|
}
|
2017-09-08 17:52:48 +00:00
|
|
|
// Media segment URL is segment path relative to the directory containing the
|
|
|
|
// playlist.
|
2023-08-22 01:41:18 +00:00
|
|
|
const std::filesystem::path playlist_dir =
|
|
|
|
(output_path / playlist_file_name).parent_path() / "";
|
2017-09-08 17:52:48 +00:00
|
|
|
return MakePathRelative(segment_name, playlist_dir);
|
2016-04-16 22:58:47 +00:00
|
|
|
}
|
2016-06-24 00:22:37 +00:00
|
|
|
|
2018-04-14 01:26:02 +00:00
|
|
|
MediaInfo MakeMediaInfoPathsRelativeToPlaylist(
|
|
|
|
const MediaInfo& media_info,
|
|
|
|
const std::string& base_url,
|
|
|
|
const std::string& output_dir,
|
|
|
|
const std::string& playlist_name) {
|
|
|
|
MediaInfo media_info_copy = media_info;
|
|
|
|
if (media_info_copy.has_init_segment_name()) {
|
|
|
|
media_info_copy.set_init_segment_url(
|
|
|
|
GenerateSegmentUrl(media_info_copy.init_segment_name(), base_url,
|
|
|
|
output_dir, playlist_name));
|
|
|
|
}
|
|
|
|
if (media_info_copy.has_media_file_name()) {
|
|
|
|
media_info_copy.set_media_file_url(
|
|
|
|
GenerateSegmentUrl(media_info_copy.media_file_name(), base_url,
|
|
|
|
output_dir, playlist_name));
|
|
|
|
}
|
|
|
|
if (media_info_copy.has_segment_template()) {
|
|
|
|
media_info_copy.set_segment_template_url(
|
|
|
|
GenerateSegmentUrl(media_info_copy.segment_template(), base_url,
|
|
|
|
output_dir, playlist_name));
|
|
|
|
}
|
|
|
|
return media_info_copy;
|
|
|
|
}
|
|
|
|
|
2017-04-04 20:57:50 +00:00
|
|
|
bool WidevinePsshToJson(const std::vector<uint8_t>& pssh_box,
|
2016-06-24 00:22:37 +00:00
|
|
|
const std::vector<uint8_t>& key_id,
|
|
|
|
std::string* pssh_json) {
|
2018-05-03 00:54:10 +00:00
|
|
|
std::unique_ptr<media::PsshBoxBuilder> pssh_builder =
|
|
|
|
media::PsshBoxBuilder::ParseFromBox(pssh_box.data(), pssh_box.size());
|
|
|
|
if (!pssh_builder) {
|
2017-04-04 20:57:50 +00:00
|
|
|
LOG(ERROR) << "Failed to parse PSSH box.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-06-24 00:22:37 +00:00
|
|
|
media::WidevinePsshData pssh_proto;
|
2018-05-03 00:54:10 +00:00
|
|
|
if (!pssh_proto.ParseFromArray(pssh_builder->pssh_data().data(),
|
|
|
|
pssh_builder->pssh_data().size())) {
|
2016-06-24 00:22:37 +00:00
|
|
|
LOG(ERROR) << "Failed to parse protection_system_specific_data.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-04 21:26:39 +00:00
|
|
|
media::WidevineHeader widevine_header;
|
2018-08-09 22:57:57 +00:00
|
|
|
|
|
|
|
if (pssh_proto.has_provider()) {
|
|
|
|
widevine_header.set_provider(pssh_proto.provider());
|
|
|
|
} else {
|
|
|
|
LOG(WARNING) << "Missing provider in Widevine PSSH. The content may not "
|
|
|
|
"play in some devices.";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pssh_proto.has_content_id()) {
|
2018-05-04 21:26:39 +00:00
|
|
|
widevine_header.set_content_id(pssh_proto.content_id());
|
2018-08-09 22:57:57 +00:00
|
|
|
} else {
|
|
|
|
LOG(WARNING) << "Missing content_id in Widevine PSSH. The content may not "
|
|
|
|
"play in some devices.";
|
|
|
|
}
|
|
|
|
|
2018-05-04 21:26:39 +00:00
|
|
|
// Place the current |key_id| to the front and converts all key_id to hex
|
|
|
|
// format.
|
2023-08-22 01:41:18 +00:00
|
|
|
widevine_header.add_key_ids(absl::BytesToHexString(absl::string_view(
|
|
|
|
reinterpret_cast<const char*>(key_id.data()), key_id.size())));
|
2018-05-04 21:26:39 +00:00
|
|
|
for (const std::string& key_id_in_pssh : pssh_proto.key_id()) {
|
2023-08-22 01:41:18 +00:00
|
|
|
const std::string key_id_hex = absl::BytesToHexString(
|
|
|
|
absl::string_view(reinterpret_cast<const char*>(key_id_in_pssh.data()),
|
|
|
|
key_id_in_pssh.size()));
|
2018-05-04 21:26:39 +00:00
|
|
|
if (widevine_header.key_ids(0) != key_id_hex)
|
|
|
|
widevine_header.add_key_ids(key_id_hex);
|
2016-06-24 00:22:37 +00:00
|
|
|
}
|
|
|
|
|
2018-05-04 21:26:39 +00:00
|
|
|
*pssh_json = media::MessageToJsonString(widevine_header);
|
2016-06-24 00:22:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-08-22 01:41:18 +00:00
|
|
|
std::optional<MediaPlaylist::EncryptionMethod> StringToEncryptionMethod(
|
2017-04-04 20:57:50 +00:00
|
|
|
const std::string& method) {
|
|
|
|
if (method == "cenc") {
|
|
|
|
return MediaPlaylist::EncryptionMethod::kSampleAesCenc;
|
2019-07-15 18:03:42 +00:00
|
|
|
}
|
|
|
|
if (method == "cbcs") {
|
2017-04-04 20:57:50 +00:00
|
|
|
return MediaPlaylist::EncryptionMethod::kSampleAes;
|
2019-07-15 18:03:42 +00:00
|
|
|
}
|
|
|
|
if (method == "cbca") {
|
2017-04-04 20:57:50 +00:00
|
|
|
// cbca is a place holder for sample aes.
|
|
|
|
return MediaPlaylist::EncryptionMethod::kSampleAes;
|
|
|
|
}
|
2023-08-22 01:41:18 +00:00
|
|
|
return std::nullopt;
|
2017-04-04 20:57:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NotifyEncryptionToMediaPlaylist(
|
|
|
|
MediaPlaylist::EncryptionMethod encryption_method,
|
|
|
|
const std::string& uri,
|
|
|
|
const std::vector<uint8_t>& key_id,
|
|
|
|
const std::vector<uint8_t>& iv,
|
|
|
|
const std::string& key_format,
|
|
|
|
const std::string& key_format_version,
|
|
|
|
MediaPlaylist* media_playlist) {
|
|
|
|
std::string iv_string;
|
|
|
|
if (!iv.empty()) {
|
2023-08-22 01:41:18 +00:00
|
|
|
iv_string =
|
|
|
|
"0x" + absl::BytesToHexString(absl::string_view(
|
|
|
|
reinterpret_cast<const char*>(iv.data()), iv.size()));
|
2017-04-04 20:57:50 +00:00
|
|
|
}
|
|
|
|
std::string key_id_string;
|
|
|
|
if (!key_id.empty()) {
|
2023-08-22 01:41:18 +00:00
|
|
|
key_id_string = "0x" + absl::BytesToHexString(absl::string_view(
|
|
|
|
reinterpret_cast<const char*>(key_id.data()),
|
|
|
|
key_id.size()));
|
2017-04-04 20:57:50 +00:00
|
|
|
}
|
Fairplay key system support
Support generation of fairplay key system tag: "com.apple.streamingkeydelivery" when --pssh
includes fairplay key system id:
// Unofficial fairplay system id extracted from
// https://forums.developer.apple.com/thread/6185.
const uint8_t kFairplaySystemId[] = {0x29, 0x70, 0x1F, 0xE4, 0x3C, 0xC7,
0x4A, 0x34, 0x8C, 0x5B, 0xAE, 0x90,
0xC7, 0x43, 0x9A, 0x47};
Closes #258
2017-08-08 17:31:40 +00:00
|
|
|
|
2017-04-04 20:57:50 +00:00
|
|
|
media_playlist->AddEncryptionInfo(
|
|
|
|
encryption_method,
|
Fairplay key system support
Support generation of fairplay key system tag: "com.apple.streamingkeydelivery" when --pssh
includes fairplay key system id:
// Unofficial fairplay system id extracted from
// https://forums.developer.apple.com/thread/6185.
const uint8_t kFairplaySystemId[] = {0x29, 0x70, 0x1F, 0xE4, 0x3C, 0xC7,
0x4A, 0x34, 0x8C, 0x5B, 0xAE, 0x90,
0xC7, 0x43, 0x9A, 0x47};
Closes #258
2017-08-08 17:31:40 +00:00
|
|
|
uri, key_id_string, iv_string,
|
2017-04-04 20:57:50 +00:00
|
|
|
key_format, key_format_version);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates JSON format and the format similar to MPD.
|
|
|
|
bool HandleWidevineKeyFormats(
|
|
|
|
MediaPlaylist::EncryptionMethod encryption_method,
|
|
|
|
const std::vector<uint8_t>& key_id,
|
|
|
|
const std::vector<uint8_t>& iv,
|
|
|
|
const std::vector<uint8_t>& protection_system_specific_data,
|
|
|
|
MediaPlaylist* media_playlist) {
|
2023-08-22 01:41:18 +00:00
|
|
|
if (absl::GetFlag(FLAGS_enable_legacy_widevine_hls_signaling) &&
|
2018-08-09 22:57:57 +00:00
|
|
|
encryption_method == MediaPlaylist::EncryptionMethod::kSampleAes) {
|
2017-04-04 20:57:50 +00:00
|
|
|
// This format allows SAMPLE-AES only.
|
|
|
|
std::string key_uri_data;
|
|
|
|
if (!WidevinePsshToJson(protection_system_specific_data, key_id,
|
|
|
|
&key_uri_data)) {
|
|
|
|
return false;
|
|
|
|
}
|
Fairplay key system support
Support generation of fairplay key system tag: "com.apple.streamingkeydelivery" when --pssh
includes fairplay key system id:
// Unofficial fairplay system id extracted from
// https://forums.developer.apple.com/thread/6185.
const uint8_t kFairplaySystemId[] = {0x29, 0x70, 0x1F, 0xE4, 0x3C, 0xC7,
0x4A, 0x34, 0x8C, 0x5B, 0xAE, 0x90,
0xC7, 0x43, 0x9A, 0x47};
Closes #258
2017-08-08 17:31:40 +00:00
|
|
|
std::string key_uri_data_base64 =
|
|
|
|
Base64EncodeData(kUriBase64Prefix, key_uri_data);
|
|
|
|
NotifyEncryptionToMediaPlaylist(encryption_method, key_uri_data_base64,
|
2017-06-16 19:21:52 +00:00
|
|
|
std::vector<uint8_t>(), iv, "com.widevine",
|
|
|
|
"1", media_playlist);
|
2017-04-04 20:57:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string pssh_as_string(
|
|
|
|
reinterpret_cast<const char*>(protection_system_specific_data.data()),
|
|
|
|
protection_system_specific_data.size());
|
Fairplay key system support
Support generation of fairplay key system tag: "com.apple.streamingkeydelivery" when --pssh
includes fairplay key system id:
// Unofficial fairplay system id extracted from
// https://forums.developer.apple.com/thread/6185.
const uint8_t kFairplaySystemId[] = {0x29, 0x70, 0x1F, 0xE4, 0x3C, 0xC7,
0x4A, 0x34, 0x8C, 0x5B, 0xAE, 0x90,
0xC7, 0x43, 0x9A, 0x47};
Closes #258
2017-08-08 17:31:40 +00:00
|
|
|
std::string key_uri_data_base64 =
|
|
|
|
Base64EncodeData(kUriBase64Prefix, pssh_as_string);
|
|
|
|
NotifyEncryptionToMediaPlaylist(encryption_method, key_uri_data_base64,
|
|
|
|
key_id, iv, kWidevineDashIfIopUUID, "1",
|
|
|
|
media_playlist);
|
2017-04-04 20:57:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-03 00:05:47 +00:00
|
|
|
bool WriteMediaPlaylist(const std::string& output_dir,
|
|
|
|
MediaPlaylist* playlist) {
|
2023-08-22 01:41:18 +00:00
|
|
|
auto file_path = std::filesystem::u8path(output_dir) / playlist->file_name();
|
2017-06-03 00:05:47 +00:00
|
|
|
if (!playlist->WriteToFile(file_path)) {
|
2023-08-22 01:41:18 +00:00
|
|
|
LOG(ERROR) << "Failed to write playlist " << file_path.string();
|
2017-06-03 00:05:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-03-25 08:40:15 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
MediaPlaylistFactory::~MediaPlaylistFactory() {}
|
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<MediaPlaylist> MediaPlaylistFactory::Create(
|
2018-04-14 01:26:02 +00:00
|
|
|
const HlsParams& hls_params,
|
2016-03-25 08:40:15 +00:00
|
|
|
const std::string& file_name,
|
|
|
|
const std::string& name,
|
|
|
|
const std::string& group_id) {
|
2018-04-14 01:26:02 +00:00
|
|
|
return std::unique_ptr<MediaPlaylist>(
|
|
|
|
new MediaPlaylist(hls_params, file_name, name, group_id));
|
2016-03-25 08:40:15 +00:00
|
|
|
}
|
|
|
|
|
2018-01-17 23:43:41 +00:00
|
|
|
SimpleHlsNotifier::SimpleHlsNotifier(const HlsParams& hls_params)
|
2018-04-14 01:26:02 +00:00
|
|
|
: HlsNotifier(hls_params),
|
2018-01-17 23:43:41 +00:00
|
|
|
media_playlist_factory_(new MediaPlaylistFactory()) {
|
2023-08-22 01:41:18 +00:00
|
|
|
const auto master_playlist_path =
|
|
|
|
std::filesystem::u8path(hls_params.master_playlist_output);
|
|
|
|
master_playlist_dir_ = master_playlist_path.parent_path().string();
|
2018-11-20 00:09:24 +00:00
|
|
|
const std::string& default_audio_langauge = hls_params.default_language;
|
|
|
|
const std::string& default_text_language =
|
|
|
|
hls_params.default_text_language.empty()
|
|
|
|
? hls_params.default_language
|
|
|
|
: hls_params.default_text_language;
|
2023-08-22 01:41:18 +00:00
|
|
|
master_playlist_.reset(new MasterPlaylist(
|
|
|
|
master_playlist_path.filename(), default_audio_langauge,
|
|
|
|
default_text_language, hls_params.is_independent_segments));
|
2018-01-17 23:43:41 +00:00
|
|
|
}
|
2016-03-25 08:40:15 +00:00
|
|
|
|
|
|
|
SimpleHlsNotifier::~SimpleHlsNotifier() {}
|
|
|
|
|
|
|
|
bool SimpleHlsNotifier::Init() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SimpleHlsNotifier::NotifyNewStream(const MediaInfo& media_info,
|
|
|
|
const std::string& playlist_name,
|
|
|
|
const std::string& name,
|
|
|
|
const std::string& group_id,
|
|
|
|
uint32_t* stream_id) {
|
|
|
|
DCHECK(stream_id);
|
|
|
|
|
2019-04-30 22:29:37 +00:00
|
|
|
const std::string relative_playlist_path = MakePathRelative(
|
2023-08-22 01:41:18 +00:00
|
|
|
playlist_name, std::filesystem::u8path(master_playlist_dir_));
|
2019-04-30 22:29:37 +00:00
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<MediaPlaylist> media_playlist =
|
2019-04-30 22:29:37 +00:00
|
|
|
media_playlist_factory_->Create(hls_params(), relative_playlist_path,
|
|
|
|
name, group_id);
|
2018-04-14 01:26:02 +00:00
|
|
|
MediaInfo adjusted_media_info = MakeMediaInfoPathsRelativeToPlaylist(
|
2019-04-30 22:29:37 +00:00
|
|
|
media_info, hls_params().base_url, master_playlist_dir_,
|
2018-04-14 01:26:02 +00:00
|
|
|
media_playlist->file_name());
|
|
|
|
if (!media_playlist->SetMediaInfo(adjusted_media_info)) {
|
2016-03-25 08:40:15 +00:00
|
|
|
LOG(ERROR) << "Failed to set media info for playlist " << playlist_name;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-04 20:57:50 +00:00
|
|
|
MediaPlaylist::EncryptionMethod encryption_method =
|
|
|
|
MediaPlaylist::EncryptionMethod::kNone;
|
|
|
|
if (media_info.protected_content().has_protection_scheme()) {
|
|
|
|
const std::string& protection_scheme =
|
|
|
|
media_info.protected_content().protection_scheme();
|
2023-08-22 01:41:18 +00:00
|
|
|
std::optional<MediaPlaylist::EncryptionMethod> enc_method =
|
2017-07-28 00:03:19 +00:00
|
|
|
StringToEncryptionMethod(protection_scheme);
|
2017-04-04 20:57:50 +00:00
|
|
|
if (!enc_method) {
|
|
|
|
LOG(ERROR) << "Failed to recognize protection scheme "
|
|
|
|
<< protection_scheme;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
encryption_method = enc_method.value();
|
|
|
|
}
|
|
|
|
|
2023-08-22 01:41:18 +00:00
|
|
|
absl::MutexLock lock(&lock_);
|
2018-07-23 22:24:20 +00:00
|
|
|
*stream_id = sequence_number_++;
|
2018-02-02 17:14:46 +00:00
|
|
|
media_playlists_.push_back(media_playlist.get());
|
2017-04-04 20:57:50 +00:00
|
|
|
stream_map_[*stream_id].reset(
|
|
|
|
new StreamEntry{std::move(media_playlist), encryption_method});
|
2016-03-25 08:40:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-09-23 06:47:07 +00:00
|
|
|
bool SimpleHlsNotifier::NotifySampleDuration(uint32_t stream_id,
|
2021-08-04 18:56:44 +00:00
|
|
|
int32_t sample_duration) {
|
2023-08-22 01:41:18 +00:00
|
|
|
absl::MutexLock lock(&lock_);
|
2019-09-23 06:47:07 +00:00
|
|
|
auto stream_iterator = stream_map_.find(stream_id);
|
|
|
|
if (stream_iterator == stream_map_.end()) {
|
|
|
|
LOG(ERROR) << "Cannot find stream with ID: " << stream_id;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto& media_playlist = stream_iterator->second->media_playlist;
|
|
|
|
media_playlist->SetSampleDuration(sample_duration);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-03-25 08:40:15 +00:00
|
|
|
bool SimpleHlsNotifier::NotifyNewSegment(uint32_t stream_id,
|
|
|
|
const std::string& segment_name,
|
2021-08-04 18:56:44 +00:00
|
|
|
int64_t start_time,
|
|
|
|
int64_t duration,
|
2017-05-01 20:38:58 +00:00
|
|
|
uint64_t start_byte_offset,
|
2016-03-25 08:40:15 +00:00
|
|
|
uint64_t size) {
|
2023-08-22 01:41:18 +00:00
|
|
|
absl::MutexLock lock(&lock_);
|
2017-04-04 20:57:50 +00:00
|
|
|
auto stream_iterator = stream_map_.find(stream_id);
|
|
|
|
if (stream_iterator == stream_map_.end()) {
|
2016-03-25 08:40:15 +00:00
|
|
|
LOG(ERROR) << "Cannot find stream with ID: " << stream_id;
|
|
|
|
return false;
|
|
|
|
}
|
2017-04-04 20:57:50 +00:00
|
|
|
auto& media_playlist = stream_iterator->second->media_playlist;
|
2018-04-14 01:26:02 +00:00
|
|
|
const std::string& segment_url =
|
2019-04-30 22:29:37 +00:00
|
|
|
GenerateSegmentUrl(segment_name, hls_params().base_url,
|
|
|
|
master_playlist_dir_, media_playlist->file_name());
|
2017-09-08 17:52:48 +00:00
|
|
|
media_playlist->AddSegment(segment_url, start_time, duration,
|
|
|
|
start_byte_offset, size);
|
2017-06-03 00:05:47 +00:00
|
|
|
|
|
|
|
// Update target duration.
|
2021-08-04 18:56:44 +00:00
|
|
|
int32_t longest_segment_duration =
|
|
|
|
static_cast<int32_t>(ceil(media_playlist->GetLongestSegmentDuration()));
|
2017-06-03 00:05:47 +00:00
|
|
|
bool target_duration_updated = false;
|
|
|
|
if (longest_segment_duration > target_duration_) {
|
|
|
|
target_duration_ = longest_segment_duration;
|
|
|
|
target_duration_updated = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the playlists when there is new segments in live mode.
|
2018-04-14 01:26:02 +00:00
|
|
|
if (hls_params().playlist_type == HlsPlaylistType::kLive ||
|
|
|
|
hls_params().playlist_type == HlsPlaylistType::kEvent) {
|
2017-06-03 00:05:47 +00:00
|
|
|
// Update all playlists if target duration is updated.
|
|
|
|
if (target_duration_updated) {
|
2018-02-02 17:14:46 +00:00
|
|
|
for (MediaPlaylist* playlist : media_playlists_) {
|
2017-06-03 00:05:47 +00:00
|
|
|
playlist->SetTargetDuration(target_duration_);
|
2019-04-30 22:29:37 +00:00
|
|
|
if (!WriteMediaPlaylist(master_playlist_dir_, playlist))
|
2017-06-03 00:05:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2019-04-30 22:29:37 +00:00
|
|
|
if (!WriteMediaPlaylist(master_playlist_dir_, media_playlist.get()))
|
2018-02-05 19:21:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-04-30 22:29:37 +00:00
|
|
|
if (!master_playlist_->WriteMasterPlaylist(
|
|
|
|
hls_params().base_url, master_playlist_dir_, media_playlists_)) {
|
2018-02-05 19:21:28 +00:00
|
|
|
LOG(ERROR) << "Failed to write master playlist.";
|
|
|
|
return false;
|
2017-06-03 00:05:47 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-25 08:40:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-31 02:30:19 +00:00
|
|
|
bool SimpleHlsNotifier::NotifyKeyFrame(uint32_t stream_id,
|
2021-08-04 18:56:44 +00:00
|
|
|
int64_t timestamp,
|
2018-01-31 02:30:19 +00:00
|
|
|
uint64_t start_byte_offset,
|
|
|
|
uint64_t size) {
|
2023-08-22 01:41:18 +00:00
|
|
|
absl::MutexLock lock(&lock_);
|
2018-01-31 02:30:19 +00:00
|
|
|
auto stream_iterator = stream_map_.find(stream_id);
|
|
|
|
if (stream_iterator == stream_map_.end()) {
|
|
|
|
LOG(ERROR) << "Cannot find stream with ID: " << stream_id;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto& media_playlist = stream_iterator->second->media_playlist;
|
|
|
|
media_playlist->AddKeyFrame(timestamp, start_byte_offset, size);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-08-04 18:56:44 +00:00
|
|
|
bool SimpleHlsNotifier::NotifyCueEvent(uint32_t stream_id, int64_t timestamp) {
|
2023-08-22 01:41:18 +00:00
|
|
|
absl::MutexLock lock(&lock_);
|
2018-01-12 01:33:37 +00:00
|
|
|
auto stream_iterator = stream_map_.find(stream_id);
|
|
|
|
if (stream_iterator == stream_map_.end()) {
|
|
|
|
LOG(ERROR) << "Cannot find stream with ID: " << stream_id;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto& media_playlist = stream_iterator->second->media_playlist;
|
|
|
|
media_playlist->AddPlacementOpportunity();
|
|
|
|
return true;
|
2018-01-03 00:10:33 +00:00
|
|
|
}
|
|
|
|
|
2016-03-25 08:40:15 +00:00
|
|
|
bool SimpleHlsNotifier::NotifyEncryptionUpdate(
|
|
|
|
uint32_t stream_id,
|
|
|
|
const std::vector<uint8_t>& key_id,
|
|
|
|
const std::vector<uint8_t>& system_id,
|
|
|
|
const std::vector<uint8_t>& iv,
|
|
|
|
const std::vector<uint8_t>& protection_system_specific_data) {
|
2023-08-22 01:41:18 +00:00
|
|
|
absl::MutexLock lock(&lock_);
|
2017-04-04 20:57:50 +00:00
|
|
|
auto stream_iterator = stream_map_.find(stream_id);
|
|
|
|
if (stream_iterator == stream_map_.end()) {
|
2016-03-25 08:40:15 +00:00
|
|
|
LOG(ERROR) << "Cannot find stream with ID: " << stream_id;
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-24 00:22:37 +00:00
|
|
|
|
2017-04-04 20:57:50 +00:00
|
|
|
std::unique_ptr<MediaPlaylist>& media_playlist =
|
|
|
|
stream_iterator->second->media_playlist;
|
|
|
|
const MediaPlaylist::EncryptionMethod encryption_method =
|
|
|
|
stream_iterator->second->encryption_method;
|
|
|
|
LOG_IF(WARNING, encryption_method == MediaPlaylist::EncryptionMethod::kNone)
|
|
|
|
<< "Got encryption notification but the encryption method is NONE";
|
2016-06-24 00:22:37 +00:00
|
|
|
if (IsWidevineSystemId(system_id)) {
|
2017-04-04 20:57:50 +00:00
|
|
|
return HandleWidevineKeyFormats(encryption_method,
|
|
|
|
key_id, iv, protection_system_specific_data,
|
|
|
|
media_playlist.get());
|
|
|
|
}
|
2017-11-12 22:56:25 +00:00
|
|
|
|
|
|
|
// Key Id does not need to be specified with "identity" and "sdk".
|
|
|
|
const std::vector<uint8_t> empty_key_id;
|
|
|
|
|
2017-04-04 20:57:50 +00:00
|
|
|
if (IsCommonSystemId(system_id)) {
|
2018-04-14 01:26:02 +00:00
|
|
|
std::string key_uri = hls_params().key_uri;
|
|
|
|
if (key_uri.empty()) {
|
2017-11-12 22:56:25 +00:00
|
|
|
// Use key_id as the key_uri. The player needs to have custom logic to
|
|
|
|
// convert it to the actual key uri.
|
|
|
|
std::string key_uri_data = VectorToString(key_id);
|
|
|
|
key_uri = Base64EncodeData(kUriBase64Prefix, key_uri_data);
|
|
|
|
}
|
|
|
|
NotifyEncryptionToMediaPlaylist(encryption_method, key_uri, empty_key_id,
|
Fairplay key system support
Support generation of fairplay key system tag: "com.apple.streamingkeydelivery" when --pssh
includes fairplay key system id:
// Unofficial fairplay system id extracted from
// https://forums.developer.apple.com/thread/6185.
const uint8_t kFairplaySystemId[] = {0x29, 0x70, 0x1F, 0xE4, 0x3C, 0xC7,
0x4A, 0x34, 0x8C, 0x5B, 0xAE, 0x90,
0xC7, 0x43, 0x9A, 0x47};
Closes #258
2017-08-08 17:31:40 +00:00
|
|
|
iv, "identity", "", media_playlist.get());
|
2017-04-04 20:57:50 +00:00
|
|
|
return true;
|
2019-07-15 18:03:42 +00:00
|
|
|
}
|
|
|
|
if (IsFairPlaySystemId(system_id)) {
|
2018-04-14 01:26:02 +00:00
|
|
|
std::string key_uri = hls_params().key_uri;
|
|
|
|
if (key_uri.empty()) {
|
2017-11-12 22:56:25 +00:00
|
|
|
// Use key_id as the key_uri. The player needs to have custom logic to
|
|
|
|
// convert it to the actual key uri.
|
|
|
|
std::string key_uri_data = VectorToString(key_id);
|
2018-08-07 23:01:43 +00:00
|
|
|
key_uri = Base64EncodeData(kUriFairPlayPrefix, key_uri_data);
|
2017-11-12 22:56:25 +00:00
|
|
|
}
|
Fairplay key system support
Support generation of fairplay key system tag: "com.apple.streamingkeydelivery" when --pssh
includes fairplay key system id:
// Unofficial fairplay system id extracted from
// https://forums.developer.apple.com/thread/6185.
const uint8_t kFairplaySystemId[] = {0x29, 0x70, 0x1F, 0xE4, 0x3C, 0xC7,
0x4A, 0x34, 0x8C, 0x5B, 0xAE, 0x90,
0xC7, 0x43, 0x9A, 0x47};
Closes #258
2017-08-08 17:31:40 +00:00
|
|
|
|
2018-08-07 23:01:43 +00:00
|
|
|
// FairPlay defines IV to be carried with the key, not the playlist.
|
2017-11-12 22:56:25 +00:00
|
|
|
const std::vector<uint8_t> empty_iv;
|
|
|
|
NotifyEncryptionToMediaPlaylist(encryption_method, key_uri, empty_key_id,
|
|
|
|
empty_iv, "com.apple.streamingkeydelivery",
|
|
|
|
"1", media_playlist.get());
|
Fairplay key system support
Support generation of fairplay key system tag: "com.apple.streamingkeydelivery" when --pssh
includes fairplay key system id:
// Unofficial fairplay system id extracted from
// https://forums.developer.apple.com/thread/6185.
const uint8_t kFairplaySystemId[] = {0x29, 0x70, 0x1F, 0xE4, 0x3C, 0xC7,
0x4A, 0x34, 0x8C, 0x5B, 0xAE, 0x90,
0xC7, 0x43, 0x9A, 0x47};
Closes #258
2017-08-08 17:31:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-08-10 18:40:10 +00:00
|
|
|
LOG(WARNING) << "HLS: Ignore unknown or unsupported system ID: "
|
2023-08-22 01:41:18 +00:00
|
|
|
<< absl::BytesToHexString(absl::string_view(
|
|
|
|
reinterpret_cast<const char*>(system_id.data()),
|
|
|
|
system_id.size()));
|
2018-08-10 18:40:10 +00:00
|
|
|
return true;
|
2016-03-25 08:40:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SimpleHlsNotifier::Flush() {
|
2023-08-22 01:41:18 +00:00
|
|
|
absl::MutexLock lock(&lock_);
|
2018-02-02 17:14:46 +00:00
|
|
|
for (MediaPlaylist* playlist : media_playlists_) {
|
2017-06-03 00:05:47 +00:00
|
|
|
playlist->SetTargetDuration(target_duration_);
|
2019-04-30 22:29:37 +00:00
|
|
|
if (!WriteMediaPlaylist(master_playlist_dir_, playlist))
|
2017-06-03 00:05:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-04-30 22:29:37 +00:00
|
|
|
if (!master_playlist_->WriteMasterPlaylist(
|
|
|
|
hls_params().base_url, master_playlist_dir_, media_playlists_)) {
|
2018-02-05 19:21:28 +00:00
|
|
|
LOG(ERROR) << "Failed to write master playlist.";
|
|
|
|
return false;
|
|
|
|
}
|
2017-06-03 00:05:47 +00:00
|
|
|
return true;
|
2016-03-25 08:40:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace hls
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|