2016-03-25 08:40:15 +00:00
|
|
|
// Copyright 2016 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
|
|
|
|
#include "packager/hls/base/simple_hls_notifier.h"
|
|
|
|
|
2018-08-09 22:57:57 +00:00
|
|
|
#include <gflags/gflags.h>
|
2017-06-03 00:05:47 +00:00
|
|
|
#include <cmath>
|
|
|
|
|
2016-03-25 08:40:15 +00:00
|
|
|
#include "packager/base/base64.h"
|
2016-04-16 22:58:47 +00:00
|
|
|
#include "packager/base/files/file_path.h"
|
2016-03-25 08:40:15 +00:00
|
|
|
#include "packager/base/logging.h"
|
2017-04-04 20:57:50 +00:00
|
|
|
#include "packager/base/optional.h"
|
2016-03-25 08:40:15 +00:00
|
|
|
#include "packager/base/strings/string_number_conversions.h"
|
|
|
|
#include "packager/base/strings/stringprintf.h"
|
|
|
|
#include "packager/hls/base/media_playlist.h"
|
2018-09-17 22:23:38 +00:00
|
|
|
#include "packager/media/base/protection_system_ids.h"
|
2017-04-04 20:57:50 +00:00
|
|
|
#include "packager/media/base/protection_system_specific_info.h"
|
2018-05-04 21:26:39 +00:00
|
|
|
#include "packager/media/base/proto_json_util.h"
|
2016-03-25 08:40:15 +00:00
|
|
|
#include "packager/media/base/widevine_pssh_data.pb.h"
|
|
|
|
|
2018-08-09 22:57:57 +00:00
|
|
|
DEFINE_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.");
|
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2017-09-08 17:52:48 +00:00
|
|
|
|
|
|
|
using base::FilePath;
|
|
|
|
|
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) {
|
2016-06-24 00:22:37 +00:00
|
|
|
return system_id.size() == arraysize(media::kWidevineSystemId) &&
|
|
|
|
std::equal(system_id.begin(), system_id.end(),
|
|
|
|
media::kWidevineSystemId);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsCommonSystemId(const std::vector<uint8_t>& system_id) {
|
|
|
|
return system_id.size() == arraysize(media::kCommonSystemId) &&
|
|
|
|
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) {
|
|
|
|
return system_id.size() == arraysize(media::kFairPlaySystemId) &&
|
|
|
|
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;
|
|
|
|
base::Base64Encode(data, &data_base64);
|
|
|
|
return prefix + data_base64;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string VectorToString(const std::vector<uint8_t>& v) {
|
|
|
|
return std::string(v.begin(), v.end());
|
|
|
|
}
|
|
|
|
|
2016-04-16 22:58:47 +00:00
|
|
|
// TODO(rkuroiwa): Dedup these with the functions in MpdBuilder.
|
2017-09-08 17:52:48 +00:00
|
|
|
// If |media_path| is contained in |parent_path|, then
|
|
|
|
// Strips the common path and keep only the relative part of |media_path|.
|
|
|
|
// e.g. if |parent_path| is /some/parent/ and
|
|
|
|
// |media_path| is /some/parent/abc/child/item.ext,
|
|
|
|
// abc/child/item.ext is returned.
|
|
|
|
// else
|
|
|
|
// Returns |media_path|.
|
|
|
|
// The path separator of the output is also changed to "/" if it is not.
|
|
|
|
std::string MakePathRelative(const std::string& media_path,
|
|
|
|
const FilePath& parent_path) {
|
|
|
|
FilePath relative_path;
|
|
|
|
const FilePath child_path = FilePath::FromUTF8Unsafe(media_path);
|
|
|
|
const bool is_child =
|
|
|
|
parent_path.AppendRelativePath(child_path, &relative_path);
|
|
|
|
if (!is_child)
|
|
|
|
relative_path = child_path;
|
|
|
|
return relative_path.NormalizePathSeparatorsTo('/').AsUTF8Unsafe();
|
2016-04-16 22:58:47 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
FilePath output_path = FilePath::FromUTF8Unsafe(output_dir);
|
|
|
|
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.
|
|
|
|
const FilePath playlist_dir =
|
|
|
|
output_path.Append(FilePath::FromUTF8Unsafe(playlist_file_name))
|
|
|
|
.DirName()
|
|
|
|
.AsEndingWithSeparator();
|
|
|
|
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.
|
|
|
|
widevine_header.add_key_ids(base::HexEncode(key_id.data(), key_id.size()));
|
|
|
|
for (const std::string& key_id_in_pssh : pssh_proto.key_id()) {
|
|
|
|
const std::string key_id_hex =
|
|
|
|
base::HexEncode(key_id_in_pssh.data(), key_id_in_pssh.size());
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-07-28 00:03:19 +00:00
|
|
|
base::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;
|
|
|
|
}
|
2019-07-15 18:03:42 +00:00
|
|
|
return base::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()) {
|
|
|
|
iv_string = "0x" + base::HexEncode(iv.data(), iv.size());
|
|
|
|
}
|
|
|
|
std::string key_id_string;
|
|
|
|
if (!key_id.empty()) {
|
|
|
|
key_id_string = "0x" + base::HexEncode(key_id.data(), key_id.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
|
|
|
|
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) {
|
2018-08-09 22:57:57 +00:00
|
|
|
if (FLAGS_enable_legacy_widevine_hls_signaling &&
|
|
|
|
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) {
|
|
|
|
std::string file_path =
|
2017-09-08 17:52:48 +00:00
|
|
|
FilePath::FromUTF8Unsafe(output_dir)
|
|
|
|
.Append(FilePath::FromUTF8Unsafe(playlist->file_name()))
|
2017-06-03 00:05:47 +00:00
|
|
|
.AsUTF8Unsafe();
|
|
|
|
if (!playlist->WriteToFile(file_path)) {
|
|
|
|
LOG(ERROR) << "Failed to write playlist " << file_path;
|
|
|
|
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()) {
|
|
|
|
const base::FilePath master_playlist_path(
|
|
|
|
base::FilePath::FromUTF8Unsafe(hls_params.master_playlist_output));
|
2019-04-30 22:29:37 +00:00
|
|
|
master_playlist_dir_ = master_playlist_path.DirName().AsUTF8Unsafe();
|
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;
|
2018-01-17 23:43:41 +00:00
|
|
|
master_playlist_.reset(
|
|
|
|
new MasterPlaylist(master_playlist_path.BaseName().AsUTF8Unsafe(),
|
2020-08-19 18:17:21 +00:00
|
|
|
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(
|
|
|
|
playlist_name, FilePath::FromUTF8Unsafe(master_playlist_dir_));
|
|
|
|
|
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();
|
|
|
|
base::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();
|
|
|
|
}
|
|
|
|
|
2016-03-25 08:40:15 +00:00
|
|
|
base::AutoLock auto_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,
|
|
|
|
uint32_t sample_duration) {
|
|
|
|
base::AutoLock auto_lock(lock_);
|
|
|
|
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,
|
|
|
|
uint64_t start_time,
|
|
|
|
uint64_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) {
|
|
|
|
base::AutoLock auto_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.
|
|
|
|
uint32_t longest_segment_duration =
|
|
|
|
static_cast<uint32_t>(ceil(media_playlist->GetLongestSegmentDuration()));
|
|
|
|
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,
|
|
|
|
uint64_t timestamp,
|
|
|
|
uint64_t start_byte_offset,
|
|
|
|
uint64_t size) {
|
|
|
|
base::AutoLock auto_lock(lock_);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-01-12 01:33:37 +00:00
|
|
|
bool SimpleHlsNotifier::NotifyCueEvent(uint32_t stream_id, uint64_t timestamp) {
|
|
|
|
base::AutoLock auto_lock(lock_);
|
|
|
|
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) {
|
|
|
|
base::AutoLock auto_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: "
|
|
|
|
<< base::HexEncode(system_id.data(), system_id.size());
|
|
|
|
return true;
|
2016-03-25 08:40:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SimpleHlsNotifier::Flush() {
|
|
|
|
base::AutoLock auto_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
|