2022-08-26 15:44:59 +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
|
2013-12-17 00:52:13 +00:00
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/base/widevine_key_source.h"
|
2013-12-17 00:52:13 +00:00
|
|
|
|
2022-11-04 22:46:41 +00:00
|
|
|
#include <functional>
|
|
|
|
#include <iterator>
|
2019-04-16 14:55:08 +00:00
|
|
|
|
2023-10-09 23:21:41 +00:00
|
|
|
#include <absl/base/internal/endian.h>
|
|
|
|
#include <absl/flags/flag.h>
|
|
|
|
#include <absl/strings/escaping.h>
|
2014-10-07 21:33:08 +00:00
|
|
|
#include "packager/media/base/http_key_fetcher.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/base/producer_consumer_queue.h"
|
2018-09-17 22:23:38 +00:00
|
|
|
#include "packager/media/base/protection_system_ids.h"
|
2016-02-19 18:24:33 +00:00
|
|
|
#include "packager/media/base/protection_system_specific_info.h"
|
2018-05-04 21:31:33 +00:00
|
|
|
#include "packager/media/base/proto_json_util.h"
|
2017-09-12 19:22:39 +00:00
|
|
|
#include "packager/media/base/pssh_generator_util.h"
|
2016-04-06 23:21:45 +00:00
|
|
|
#include "packager/media/base/rcheck.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/base/request_signer.h"
|
2018-05-04 21:31:33 +00:00
|
|
|
#include "packager/media/base/widevine_common_encryption.pb.h"
|
2013-12-17 00:52:13 +00:00
|
|
|
|
2022-11-04 22:46:41 +00:00
|
|
|
ABSL_FLAG(std::string,
|
|
|
|
video_feature,
|
|
|
|
"",
|
|
|
|
"Specify the optional video feature, e.g. HDR.");
|
2019-04-16 14:55:08 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2017-04-05 20:53:58 +00:00
|
|
|
namespace media {
|
2013-12-17 00:52:13 +00:00
|
|
|
namespace {
|
|
|
|
|
2014-06-30 15:40:02 +00:00
|
|
|
const bool kEnableKeyRotation = true;
|
|
|
|
|
2014-01-14 18:36:41 +00:00
|
|
|
// Number of times to retry requesting keys in case of a transient error from
|
|
|
|
// the server.
|
|
|
|
const int kNumTransientErrorRetries = 5;
|
|
|
|
const int kFirstRetryDelayMilliseconds = 1000;
|
|
|
|
|
2014-04-24 16:59:07 +00:00
|
|
|
// Default crypto period count, which is the number of keys to fetch on every
|
|
|
|
// key rotation enabled request.
|
|
|
|
const int kDefaultCryptoPeriodCount = 10;
|
|
|
|
const int kGetKeyTimeoutInSeconds = 5 * 60; // 5 minutes.
|
2014-10-07 21:33:08 +00:00
|
|
|
const int kKeyFetchTimeoutInSeconds = 60; // 1 minute.
|
2014-04-24 16:59:07 +00:00
|
|
|
|
2018-05-04 21:31:33 +00:00
|
|
|
CommonEncryptionRequest::ProtectionScheme ToCommonEncryptionProtectionScheme(
|
|
|
|
FourCC protection_scheme) {
|
|
|
|
switch (protection_scheme) {
|
|
|
|
case FOURCC_cenc:
|
|
|
|
return CommonEncryptionRequest::CENC;
|
|
|
|
case FOURCC_cbcs:
|
|
|
|
case kAppleSampleAesProtectionScheme:
|
|
|
|
// Treat sample aes as a variant of cbcs.
|
|
|
|
return CommonEncryptionRequest::CBCS;
|
|
|
|
case FOURCC_cbc1:
|
|
|
|
return CommonEncryptionRequest::CBC1;
|
|
|
|
case FOURCC_cens:
|
|
|
|
return CommonEncryptionRequest::CENS;
|
|
|
|
default:
|
|
|
|
LOG(WARNING) << "Ignore unrecognized protection scheme "
|
|
|
|
<< FourCCToString(protection_scheme);
|
|
|
|
return CommonEncryptionRequest::UNSPECIFIED;
|
2013-12-17 00:52:13 +00:00
|
|
|
}
|
2017-04-05 20:53:58 +00:00
|
|
|
}
|
2013-12-17 00:52:13 +00:00
|
|
|
|
2018-05-11 00:19:28 +00:00
|
|
|
ProtectionSystemSpecificInfo ProtectionSystemInfoFromPsshProto(
|
|
|
|
const CommonEncryptionResponse::Track::Pssh& pssh_proto) {
|
|
|
|
PsshBoxBuilder pssh_builder;
|
2022-11-04 22:46:41 +00:00
|
|
|
pssh_builder.set_system_id(kWidevineSystemId, std::size(kWidevineSystemId));
|
2018-05-11 00:19:28 +00:00
|
|
|
|
|
|
|
if (pssh_proto.has_boxes()) {
|
|
|
|
return {pssh_builder.system_id(),
|
|
|
|
std::vector<uint8_t>(pssh_proto.boxes().begin(),
|
|
|
|
pssh_proto.boxes().end())};
|
|
|
|
} else {
|
|
|
|
pssh_builder.set_pssh_box_version(0);
|
|
|
|
const std::vector<uint8_t> pssh_data(pssh_proto.data().begin(),
|
|
|
|
pssh_proto.data().end());
|
|
|
|
pssh_builder.set_pssh_data(pssh_data);
|
|
|
|
return {pssh_builder.system_id(), pssh_builder.CreateBox()};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-05 20:53:58 +00:00
|
|
|
} // namespace
|
2013-12-17 00:52:13 +00:00
|
|
|
|
2016-03-21 18:09:24 +00:00
|
|
|
WidevineKeySource::WidevineKeySource(const std::string& server_url,
|
2020-05-29 18:45:57 +00:00
|
|
|
ProtectionSystem protection_systems,
|
2018-08-07 21:43:42 +00:00
|
|
|
FourCC protection_scheme)
|
2017-09-12 19:22:39 +00:00
|
|
|
// Widevine PSSH is fetched from Widevine license server.
|
2020-06-01 22:26:31 +00:00
|
|
|
: generate_widevine_protection_system_(
|
2018-08-10 20:31:17 +00:00
|
|
|
// Generate Widevine protection system if there are no other
|
|
|
|
// protection system specified.
|
2020-05-29 18:45:57 +00:00
|
|
|
protection_systems == ProtectionSystem::kNone ||
|
|
|
|
has_flag(protection_systems, ProtectionSystem::kWidevine)),
|
2014-10-07 21:33:08 +00:00
|
|
|
key_fetcher_(new HttpKeyFetcher(kKeyFetchTimeoutInSeconds)),
|
2014-02-20 22:38:28 +00:00
|
|
|
server_url_(server_url),
|
2014-04-24 16:59:07 +00:00
|
|
|
crypto_period_count_(kDefaultCryptoPeriodCount),
|
2018-08-07 21:43:42 +00:00
|
|
|
protection_scheme_(protection_scheme),
|
2022-11-04 22:46:41 +00:00
|
|
|
key_production_thread_(
|
|
|
|
std::bind(&WidevineKeySource::FetchKeysTask, this)) {}
|
2014-04-24 16:59:07 +00:00
|
|
|
|
2014-08-20 23:51:15 +00:00
|
|
|
WidevineKeySource::~WidevineKeySource() {
|
2014-06-30 15:40:02 +00:00
|
|
|
if (key_pool_)
|
|
|
|
key_pool_->Stop();
|
2022-11-04 22:46:41 +00:00
|
|
|
// Signal the production thread to start key production if it is not
|
|
|
|
// signaled yet so the thread can be joined.
|
|
|
|
if (!start_key_production_.HasBeenNotified())
|
|
|
|
start_key_production_.Notify();
|
|
|
|
key_production_thread_.join();
|
2014-05-08 23:34:45 +00:00
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
Status WidevineKeySource::FetchKeys(const std::vector<uint8_t>& content_id,
|
2014-08-20 23:51:15 +00:00
|
|
|
const std::string& policy) {
|
2022-11-04 22:46:41 +00:00
|
|
|
absl::MutexLock scoped_lock(&mutex_);
|
2018-05-04 21:31:33 +00:00
|
|
|
common_encryption_request_.reset(new CommonEncryptionRequest);
|
|
|
|
common_encryption_request_->set_content_id(content_id.data(),
|
|
|
|
content_id.size());
|
|
|
|
common_encryption_request_->set_policy(policy);
|
|
|
|
common_encryption_request_->set_protection_scheme(
|
|
|
|
ToCommonEncryptionProtectionScheme(protection_scheme_));
|
2018-05-11 00:19:28 +00:00
|
|
|
if (enable_entitlement_license_)
|
|
|
|
common_encryption_request_->set_enable_entitlement_license(true);
|
2017-04-05 20:53:58 +00:00
|
|
|
|
2014-10-15 21:56:12 +00:00
|
|
|
return FetchKeysInternal(!kEnableKeyRotation, 0, false);
|
2014-08-20 23:51:15 +00:00
|
|
|
}
|
|
|
|
|
2017-04-04 19:43:41 +00:00
|
|
|
Status WidevineKeySource::FetchKeys(EmeInitDataType init_data_type,
|
|
|
|
const std::vector<uint8_t>& init_data) {
|
|
|
|
std::vector<uint8_t> pssh_data;
|
|
|
|
uint32_t asset_id = 0;
|
|
|
|
switch (init_data_type) {
|
|
|
|
case EmeInitDataType::CENC: {
|
|
|
|
const std::vector<uint8_t> widevine_system_id(
|
2022-11-04 22:46:41 +00:00
|
|
|
kWidevineSystemId, kWidevineSystemId + std::size(kWidevineSystemId));
|
2017-04-04 19:43:41 +00:00
|
|
|
std::vector<ProtectionSystemSpecificInfo> protection_systems_info;
|
|
|
|
if (!ProtectionSystemSpecificInfo::ParseBoxes(
|
|
|
|
init_data.data(), init_data.size(), &protection_systems_info)) {
|
|
|
|
return Status(error::PARSER_FAILURE, "Error parsing the PSSH boxes.");
|
|
|
|
}
|
2017-09-12 19:22:39 +00:00
|
|
|
for (const auto& info : protection_systems_info) {
|
2018-05-03 00:54:10 +00:00
|
|
|
std::unique_ptr<PsshBoxBuilder> pssh_builder =
|
|
|
|
PsshBoxBuilder::ParseFromBox(info.psshs.data(), info.psshs.size());
|
|
|
|
if (!pssh_builder)
|
|
|
|
return Status(error::PARSER_FAILURE, "Error parsing the PSSH box.");
|
2017-04-04 19:43:41 +00:00
|
|
|
// Use Widevine PSSH if available otherwise construct a Widevine PSSH
|
|
|
|
// from the first available key ids.
|
2018-05-03 00:54:10 +00:00
|
|
|
if (info.system_id == widevine_system_id) {
|
|
|
|
pssh_data = pssh_builder->pssh_data();
|
2017-04-04 19:43:41 +00:00
|
|
|
break;
|
2018-05-03 00:54:10 +00:00
|
|
|
} else if (pssh_data.empty() && !pssh_builder->key_ids().empty()) {
|
|
|
|
pssh_data =
|
|
|
|
GenerateWidevinePsshDataFromKeyIds(pssh_builder->key_ids());
|
2017-04-04 19:43:41 +00:00
|
|
|
// Continue to see if there is any Widevine PSSH. The KeyId generated
|
|
|
|
// PSSH is only used if a Widevine PSSH could not be found.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pssh_data.empty())
|
|
|
|
return Status(error::INVALID_ARGUMENT, "No supported PSSHs found.");
|
|
|
|
break;
|
|
|
|
}
|
2017-09-12 19:22:39 +00:00
|
|
|
case EmeInitDataType::WEBM: {
|
|
|
|
pssh_data = GenerateWidevinePsshDataFromKeyIds({init_data});
|
2017-04-04 19:43:41 +00:00
|
|
|
break;
|
2017-09-12 19:22:39 +00:00
|
|
|
}
|
2017-04-04 19:43:41 +00:00
|
|
|
case EmeInitDataType::WIDEVINE_CLASSIC:
|
|
|
|
if (init_data.size() < sizeof(asset_id))
|
|
|
|
return Status(error::INVALID_ARGUMENT, "Invalid asset id.");
|
2022-11-04 22:46:41 +00:00
|
|
|
asset_id = absl::big_endian::Load32(init_data.data());
|
2017-04-04 19:43:41 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG(ERROR) << "Init data type " << static_cast<int>(init_data_type)
|
|
|
|
<< " not supported.";
|
|
|
|
return Status(error::INVALID_ARGUMENT, "Unsupported init data type.");
|
2016-02-19 18:24:33 +00:00
|
|
|
}
|
2017-04-04 19:43:41 +00:00
|
|
|
const bool widevine_classic =
|
|
|
|
init_data_type == EmeInitDataType::WIDEVINE_CLASSIC;
|
2022-11-04 22:46:41 +00:00
|
|
|
absl::MutexLock scoped_lock(&mutex_);
|
2018-05-04 21:31:33 +00:00
|
|
|
common_encryption_request_.reset(new CommonEncryptionRequest);
|
2017-04-04 19:43:41 +00:00
|
|
|
if (widevine_classic) {
|
2018-05-04 21:31:33 +00:00
|
|
|
common_encryption_request_->set_asset_id(asset_id);
|
2017-04-04 19:43:41 +00:00
|
|
|
} else {
|
2018-05-04 21:31:33 +00:00
|
|
|
common_encryption_request_->set_pssh_data(pssh_data.data(),
|
|
|
|
pssh_data.size());
|
2016-02-19 18:24:33 +00:00
|
|
|
}
|
2017-04-04 19:43:41 +00:00
|
|
|
return FetchKeysInternal(!kEnableKeyRotation, 0, widevine_classic);
|
2014-04-15 22:18:26 +00:00
|
|
|
}
|
2013-12-17 00:52:13 +00:00
|
|
|
|
2017-06-13 21:54:12 +00:00
|
|
|
Status WidevineKeySource::GetKey(const std::string& stream_label,
|
|
|
|
EncryptionKey* key) {
|
2014-06-30 15:40:02 +00:00
|
|
|
DCHECK(key);
|
2017-06-13 21:54:12 +00:00
|
|
|
if (encryption_key_map_.find(stream_label) == encryption_key_map_.end()) {
|
2014-06-30 15:40:02 +00:00
|
|
|
return Status(error::INTERNAL_ERROR,
|
2017-06-13 21:54:12 +00:00
|
|
|
"Cannot find key for '" + stream_label + "'.");
|
2014-06-30 15:40:02 +00:00
|
|
|
}
|
2017-06-13 21:54:12 +00:00
|
|
|
*key = *encryption_key_map_[stream_label];
|
2014-06-30 15:40:02 +00:00
|
|
|
return Status::OK;
|
2014-04-24 16:59:07 +00:00
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
Status WidevineKeySource::GetKey(const std::vector<uint8_t>& key_id,
|
2014-08-20 23:51:15 +00:00
|
|
|
EncryptionKey* key) {
|
|
|
|
DCHECK(key);
|
2016-08-30 23:01:19 +00:00
|
|
|
for (const auto& pair : encryption_key_map_) {
|
|
|
|
if (pair.second->key_id == key_id) {
|
|
|
|
*key = *pair.second;
|
2014-08-20 23:51:15 +00:00
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Status(error::INTERNAL_ERROR,
|
|
|
|
"Cannot find key with specified key ID");
|
|
|
|
}
|
|
|
|
|
2021-08-04 18:56:44 +00:00
|
|
|
Status WidevineKeySource::GetCryptoPeriodKey(
|
|
|
|
uint32_t crypto_period_index,
|
|
|
|
int32_t crypto_period_duration_in_seconds,
|
|
|
|
const std::string& stream_label,
|
|
|
|
EncryptionKey* key) {
|
2014-06-30 15:40:02 +00:00
|
|
|
// TODO(kqyang): This is not elegant. Consider refactoring later.
|
|
|
|
{
|
2022-11-04 22:46:41 +00:00
|
|
|
absl::MutexLock scoped_lock(&mutex_);
|
2014-06-30 15:40:02 +00:00
|
|
|
if (!key_production_started_) {
|
2019-01-24 18:39:54 +00:00
|
|
|
crypto_period_duration_in_seconds_ = crypto_period_duration_in_seconds;
|
2014-06-30 15:40:02 +00:00
|
|
|
// Another client may have a slightly smaller starting crypto period
|
|
|
|
// index. Set the initial value to account for that.
|
2014-10-15 21:56:12 +00:00
|
|
|
first_crypto_period_index_ =
|
|
|
|
crypto_period_index ? crypto_period_index - 1 : 0;
|
2014-06-30 15:40:02 +00:00
|
|
|
DCHECK(!key_pool_);
|
2018-07-27 22:50:41 +00:00
|
|
|
const size_t queue_size = crypto_period_count_ * 10;
|
|
|
|
key_pool_.reset(
|
|
|
|
new EncryptionKeyQueue(queue_size, first_crypto_period_index_));
|
2022-11-04 22:46:41 +00:00
|
|
|
start_key_production_.Notify();
|
2014-06-30 15:40:02 +00:00
|
|
|
key_production_started_ = true;
|
2019-01-24 18:39:54 +00:00
|
|
|
} else if (crypto_period_duration_in_seconds_ !=
|
|
|
|
crypto_period_duration_in_seconds) {
|
|
|
|
return Status(error::INVALID_ARGUMENT,
|
|
|
|
"Crypto period duration should not change.");
|
2014-06-30 15:40:02 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-13 21:54:12 +00:00
|
|
|
return GetKeyInternal(crypto_period_index, stream_label, key);
|
2014-04-24 16:59:07 +00:00
|
|
|
}
|
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
void WidevineKeySource::set_signer(std::unique_ptr<RequestSigner> signer) {
|
|
|
|
signer_ = std::move(signer);
|
2014-10-15 00:47:25 +00:00
|
|
|
}
|
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
void WidevineKeySource::set_key_fetcher(
|
|
|
|
std::unique_ptr<KeyFetcher> key_fetcher) {
|
|
|
|
key_fetcher_ = std::move(key_fetcher);
|
2014-04-24 16:59:07 +00:00
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
Status WidevineKeySource::GetKeyInternal(uint32_t crypto_period_index,
|
2017-06-13 21:54:12 +00:00
|
|
|
const std::string& stream_label,
|
2014-09-30 21:52:21 +00:00
|
|
|
EncryptionKey* key) {
|
2014-06-30 15:40:02 +00:00
|
|
|
DCHECK(key_pool_);
|
|
|
|
DCHECK(key);
|
2014-04-24 16:59:07 +00:00
|
|
|
|
2017-01-24 00:55:02 +00:00
|
|
|
std::shared_ptr<EncryptionKeyMap> encryption_key_map;
|
|
|
|
Status status = key_pool_->Peek(crypto_period_index, &encryption_key_map,
|
|
|
|
kGetKeyTimeoutInSeconds * 1000);
|
2014-04-24 16:59:07 +00:00
|
|
|
if (!status.ok()) {
|
|
|
|
if (status.error_code() == error::STOPPED) {
|
|
|
|
CHECK(!common_encryption_request_status_.ok());
|
|
|
|
return common_encryption_request_status_;
|
2014-04-15 22:18:26 +00:00
|
|
|
}
|
|
|
|
return status;
|
2014-04-24 16:59:07 +00:00
|
|
|
}
|
|
|
|
|
2017-06-13 21:54:12 +00:00
|
|
|
if (encryption_key_map->find(stream_label) == encryption_key_map->end()) {
|
2014-04-15 22:18:26 +00:00
|
|
|
return Status(error::INTERNAL_ERROR,
|
2017-06-13 21:54:12 +00:00
|
|
|
"Cannot find key for '" + stream_label + "'.");
|
2014-04-15 22:18:26 +00:00
|
|
|
}
|
2017-06-13 21:54:12 +00:00
|
|
|
*key = *encryption_key_map->at(stream_label);
|
2014-04-15 22:18:26 +00:00
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
2014-08-20 23:51:15 +00:00
|
|
|
void WidevineKeySource::FetchKeysTask() {
|
2014-06-30 15:40:02 +00:00
|
|
|
// Wait until key production is signaled.
|
2022-11-04 22:46:41 +00:00
|
|
|
start_key_production_.WaitForNotification();
|
2014-06-30 15:40:02 +00:00
|
|
|
if (!key_pool_ || key_pool_->Stopped())
|
|
|
|
return;
|
|
|
|
|
2014-08-20 23:51:15 +00:00
|
|
|
Status status = FetchKeysInternal(kEnableKeyRotation,
|
|
|
|
first_crypto_period_index_,
|
|
|
|
false);
|
2014-06-30 15:40:02 +00:00
|
|
|
while (status.ok()) {
|
|
|
|
first_crypto_period_index_ += crypto_period_count_;
|
2014-08-20 23:51:15 +00:00
|
|
|
status = FetchKeysInternal(kEnableKeyRotation,
|
|
|
|
first_crypto_period_index_,
|
|
|
|
false);
|
2014-04-24 16:59:07 +00:00
|
|
|
}
|
|
|
|
common_encryption_request_status_ = status;
|
2014-06-30 15:40:02 +00:00
|
|
|
key_pool_->Stop();
|
2014-04-15 22:18:26 +00:00
|
|
|
}
|
|
|
|
|
2014-08-20 23:51:15 +00:00
|
|
|
Status WidevineKeySource::FetchKeysInternal(bool enable_key_rotation,
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t first_crypto_period_index,
|
2014-08-20 23:51:15 +00:00
|
|
|
bool widevine_classic) {
|
2018-05-04 21:31:33 +00:00
|
|
|
CommonEncryptionRequest request;
|
|
|
|
FillRequest(enable_key_rotation, first_crypto_period_index, &request);
|
2013-12-17 00:52:13 +00:00
|
|
|
|
|
|
|
std::string message;
|
2014-10-10 22:36:40 +00:00
|
|
|
Status status = GenerateKeyMessage(request, &message);
|
2013-12-17 00:52:13 +00:00
|
|
|
if (!status.ok())
|
|
|
|
return status;
|
2014-01-14 18:36:41 +00:00
|
|
|
VLOG(1) << "Message: " << message;
|
2013-12-17 00:52:13 +00:00
|
|
|
|
|
|
|
std::string raw_response;
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t sleep_duration = kFirstRetryDelayMilliseconds;
|
2014-01-14 18:36:41 +00:00
|
|
|
|
|
|
|
// Perform client side retries if seeing server transient error to workaround
|
|
|
|
// server limitation.
|
|
|
|
for (int i = 0; i < kNumTransientErrorRetries; ++i) {
|
2014-10-07 21:33:08 +00:00
|
|
|
status = key_fetcher_->FetchKeys(server_url_, message, &raw_response);
|
2014-06-18 19:23:34 +00:00
|
|
|
if (status.ok()) {
|
|
|
|
VLOG(1) << "Retry [" << i << "] Response:" << raw_response;
|
|
|
|
|
|
|
|
bool transient_error = false;
|
2018-05-04 21:31:33 +00:00
|
|
|
if (ExtractEncryptionKey(enable_key_rotation, widevine_classic,
|
|
|
|
raw_response, &transient_error))
|
2014-06-18 19:23:34 +00:00
|
|
|
return Status::OK;
|
|
|
|
|
|
|
|
if (!transient_error) {
|
|
|
|
return Status(
|
|
|
|
error::SERVER_ERROR,
|
2018-05-04 21:31:33 +00:00
|
|
|
"Failed to extract encryption key from '" + raw_response + "'.");
|
2014-06-18 19:23:34 +00:00
|
|
|
}
|
|
|
|
} else if (status.error_code() != error::TIME_OUT) {
|
2014-01-14 18:36:41 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exponential backoff.
|
|
|
|
if (i != kNumTransientErrorRetries - 1) {
|
2022-11-04 22:46:41 +00:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(sleep_duration));
|
2014-01-14 18:36:41 +00:00
|
|
|
sleep_duration *= 2;
|
|
|
|
}
|
2013-12-17 00:52:13 +00:00
|
|
|
}
|
2014-01-14 18:36:41 +00:00
|
|
|
return Status(error::SERVER_ERROR,
|
|
|
|
"Failed to recover from server internal error.");
|
2013-12-17 00:52:13 +00:00
|
|
|
}
|
|
|
|
|
2014-08-20 23:51:15 +00:00
|
|
|
void WidevineKeySource::FillRequest(bool enable_key_rotation,
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t first_crypto_period_index,
|
2018-05-04 21:31:33 +00:00
|
|
|
CommonEncryptionRequest* request) {
|
|
|
|
DCHECK(common_encryption_request_);
|
2013-12-17 00:52:13 +00:00
|
|
|
DCHECK(request);
|
2018-05-04 21:31:33 +00:00
|
|
|
*request = *common_encryption_request_;
|
|
|
|
|
|
|
|
request->add_tracks()->set_type("SD");
|
|
|
|
request->add_tracks()->set_type("HD");
|
|
|
|
request->add_tracks()->set_type("UHD1");
|
|
|
|
request->add_tracks()->set_type("UHD2");
|
|
|
|
request->add_tracks()->set_type("AUDIO");
|
|
|
|
|
|
|
|
request->add_drm_types(ModularDrmType::WIDEVINE);
|
2014-04-24 16:59:07 +00:00
|
|
|
|
2018-05-04 21:31:33 +00:00
|
|
|
if (enable_key_rotation) {
|
|
|
|
request->set_first_crypto_period_index(first_crypto_period_index);
|
|
|
|
request->set_crypto_period_count(crypto_period_count_);
|
2019-01-24 18:39:54 +00:00
|
|
|
request->set_crypto_period_seconds(crypto_period_duration_in_seconds_);
|
2017-07-18 21:30:02 +00:00
|
|
|
}
|
|
|
|
|
2018-05-04 21:31:33 +00:00
|
|
|
if (!group_id_.empty())
|
|
|
|
request->set_group_id(group_id_.data(), group_id_.size());
|
2019-04-16 14:55:08 +00:00
|
|
|
|
2022-11-04 22:46:41 +00:00
|
|
|
std::string video_feature = absl::GetFlag(FLAGS_video_feature);
|
|
|
|
if (!video_feature.empty())
|
|
|
|
request->set_video_feature(video_feature);
|
2013-12-17 00:52:13 +00:00
|
|
|
}
|
|
|
|
|
2018-05-04 21:31:33 +00:00
|
|
|
Status WidevineKeySource::GenerateKeyMessage(
|
|
|
|
const CommonEncryptionRequest& request,
|
|
|
|
std::string* message) {
|
2014-10-10 22:36:40 +00:00
|
|
|
DCHECK(message);
|
2013-12-17 00:52:13 +00:00
|
|
|
|
2018-05-04 21:31:33 +00:00
|
|
|
SignedModularDrmRequest signed_request;
|
|
|
|
signed_request.set_request(MessageToJsonString(request));
|
2014-10-10 22:36:40 +00:00
|
|
|
|
|
|
|
// Sign the request.
|
|
|
|
if (signer_) {
|
|
|
|
std::string signature;
|
2018-05-04 21:31:33 +00:00
|
|
|
if (!signer_->GenerateSignature(signed_request.request(), &signature))
|
2014-10-10 22:36:40 +00:00
|
|
|
return Status(error::INTERNAL_ERROR, "Signature generation failed.");
|
|
|
|
|
2018-05-04 21:31:33 +00:00
|
|
|
signed_request.set_signature(signature);
|
|
|
|
signed_request.set_signer(signer_->signer_name());
|
2014-10-10 22:36:40 +00:00
|
|
|
}
|
2013-12-17 00:52:13 +00:00
|
|
|
|
2018-05-04 21:31:33 +00:00
|
|
|
*message = MessageToJsonString(signed_request);
|
2013-12-17 00:52:13 +00:00
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
2014-08-20 23:51:15 +00:00
|
|
|
bool WidevineKeySource::ExtractEncryptionKey(
|
2014-06-30 15:40:02 +00:00
|
|
|
bool enable_key_rotation,
|
2014-08-20 23:51:15 +00:00
|
|
|
bool widevine_classic,
|
2014-04-16 01:09:32 +00:00
|
|
|
const std::string& response,
|
|
|
|
bool* transient_error) {
|
2014-01-14 18:36:41 +00:00
|
|
|
DCHECK(transient_error);
|
|
|
|
*transient_error = false;
|
|
|
|
|
2018-05-04 21:31:33 +00:00
|
|
|
SignedModularDrmResponse signed_response_proto;
|
|
|
|
if (!JsonStringToMessage(response, &signed_response_proto)) {
|
|
|
|
LOG(ERROR) << "Failed to convert JSON to proto: " << response;
|
2013-12-17 00:52:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-04 21:31:33 +00:00
|
|
|
CommonEncryptionResponse response_proto;
|
|
|
|
if (!JsonStringToMessage(signed_response_proto.response(), &response_proto)) {
|
|
|
|
LOG(ERROR) << "Failed to convert JSON to proto: "
|
|
|
|
<< signed_response_proto.response();
|
|
|
|
return false;
|
|
|
|
}
|
2013-12-17 00:52:13 +00:00
|
|
|
|
2018-05-04 21:31:33 +00:00
|
|
|
if (response_proto.status() != CommonEncryptionResponse::OK) {
|
2013-12-17 00:52:13 +00:00
|
|
|
LOG(ERROR) << "Received non-OK license response: " << response;
|
2018-05-04 21:31:33 +00:00
|
|
|
// Server may return INTERNAL_ERROR intermittently, which is a transient
|
|
|
|
// error and the next client request may succeed without problem.
|
|
|
|
*transient_error =
|
|
|
|
(response_proto.status() == CommonEncryptionResponse::INTERNAL_ERROR);
|
2013-12-17 00:52:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-04 21:31:33 +00:00
|
|
|
RCHECK(enable_key_rotation
|
|
|
|
? response_proto.tracks_size() >= crypto_period_count_
|
|
|
|
: response_proto.tracks_size() >= 1);
|
2014-04-24 16:59:07 +00:00
|
|
|
|
2018-05-04 21:31:33 +00:00
|
|
|
uint32_t current_crypto_period_index = first_crypto_period_index_;
|
2013-12-17 00:52:13 +00:00
|
|
|
|
2020-06-01 22:26:31 +00:00
|
|
|
std::vector<std::vector<uint8_t>> key_ids;
|
|
|
|
for (const auto& track : response_proto.tracks()) {
|
|
|
|
if (!widevine_classic)
|
|
|
|
key_ids.emplace_back(track.key_id().begin(), track.key_id().end());
|
|
|
|
}
|
|
|
|
|
2014-04-24 16:59:07 +00:00
|
|
|
EncryptionKeyMap encryption_key_map;
|
2018-05-04 21:31:33 +00:00
|
|
|
for (const auto& track : response_proto.tracks()) {
|
|
|
|
VLOG(2) << "track " << track.ShortDebugString();
|
2014-04-15 22:18:26 +00:00
|
|
|
|
2014-06-30 15:40:02 +00:00
|
|
|
if (enable_key_rotation) {
|
2018-05-04 21:31:33 +00:00
|
|
|
if (track.crypto_period_index() != current_crypto_period_index) {
|
|
|
|
if (track.crypto_period_index() != current_crypto_period_index + 1) {
|
2014-04-24 16:59:07 +00:00
|
|
|
LOG(ERROR) << "Expecting crypto period index "
|
|
|
|
<< current_crypto_period_index << " or "
|
|
|
|
<< current_crypto_period_index + 1 << "; Seen "
|
2018-05-04 21:31:33 +00:00
|
|
|
<< track.crypto_period_index();
|
2014-04-24 16:59:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!PushToKeyPool(&encryption_key_map))
|
|
|
|
return false;
|
|
|
|
++current_crypto_period_index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-04 21:31:33 +00:00
|
|
|
const std::string& stream_label = track.type();
|
2017-06-13 21:54:12 +00:00
|
|
|
RCHECK(encryption_key_map.find(stream_label) == encryption_key_map.end());
|
2014-04-15 22:18:26 +00:00
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<EncryptionKey> encryption_key(new EncryptionKey());
|
2018-05-04 21:31:33 +00:00
|
|
|
encryption_key->key.assign(track.key().begin(), track.key().end());
|
2014-08-20 23:51:15 +00:00
|
|
|
|
|
|
|
// Get key ID and PSSH data for CENC content only.
|
|
|
|
if (!widevine_classic) {
|
2018-05-04 21:31:33 +00:00
|
|
|
encryption_key->key_id.assign(track.key_id().begin(),
|
|
|
|
track.key_id().end());
|
2019-02-13 00:34:24 +00:00
|
|
|
encryption_key->iv.assign(track.iv().begin(), track.iv().end());
|
2020-06-01 22:26:31 +00:00
|
|
|
encryption_key->key_ids = key_ids;
|
2014-08-20 23:51:15 +00:00
|
|
|
|
2018-08-10 20:31:17 +00:00
|
|
|
if (generate_widevine_protection_system_) {
|
|
|
|
if (track.pssh_size() != 1) {
|
|
|
|
LOG(ERROR) << "Expecting one and only one pssh, seeing "
|
|
|
|
<< track.pssh_size();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
encryption_key->key_system_info.push_back(
|
|
|
|
ProtectionSystemInfoFromPsshProto(track.pssh(0)));
|
2018-05-04 21:31:33 +00:00
|
|
|
}
|
2014-08-20 23:51:15 +00:00
|
|
|
}
|
2017-06-13 21:54:12 +00:00
|
|
|
encryption_key_map[stream_label] = std::move(encryption_key);
|
2014-04-24 16:59:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DCHECK(!encryption_key_map.empty());
|
2014-06-30 15:40:02 +00:00
|
|
|
if (!enable_key_rotation) {
|
2017-04-04 19:43:41 +00:00
|
|
|
// Merge with previously requested keys.
|
|
|
|
for (auto& pair : encryption_key_map)
|
|
|
|
encryption_key_map_[pair.first] = std::move(pair.second);
|
2014-06-30 15:40:02 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-06-01 22:26:31 +00:00
|
|
|
|
2014-04-24 16:59:07 +00:00
|
|
|
return PushToKeyPool(&encryption_key_map);
|
|
|
|
}
|
|
|
|
|
2014-08-20 23:51:15 +00:00
|
|
|
bool WidevineKeySource::PushToKeyPool(
|
2014-04-24 16:59:07 +00:00
|
|
|
EncryptionKeyMap* encryption_key_map) {
|
2014-06-30 15:40:02 +00:00
|
|
|
DCHECK(key_pool_);
|
2014-04-24 16:59:07 +00:00
|
|
|
DCHECK(encryption_key_map);
|
2017-01-24 00:55:02 +00:00
|
|
|
auto encryption_key_map_shared = std::make_shared<EncryptionKeyMap>();
|
|
|
|
encryption_key_map_shared->swap(*encryption_key_map);
|
|
|
|
Status status = key_pool_->Push(encryption_key_map_shared, kInfiniteTimeout);
|
2014-04-24 16:59:07 +00:00
|
|
|
if (!status.ok()) {
|
|
|
|
DCHECK_EQ(error::STOPPED, status.error_code());
|
|
|
|
return false;
|
2013-12-17 00:52:13 +00:00
|
|
|
}
|
2014-04-15 22:18:26 +00:00
|
|
|
return true;
|
2013-12-17 00:52:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|