Rewrite RawKeySource::GetCryptoPeriodKey

To allow generating Widevine / Playready PSSHs if the corresponding
PSSH generator is specified.

Note that Key Rotation with RawKeySource is designed to be used for
testing only.

Change-Id: Icaf9e74955c082a7b000bd6a08f4561f2e01a2e2
This commit is contained in:
KongQun Yang 2018-05-03 10:32:01 -07:00
parent ba3e054e7e
commit 2ea347f45c
1 changed files with 16 additions and 29 deletions

View File

@ -11,6 +11,7 @@
#include "packager/base/strings/string_number_conversions.h" #include "packager/base/strings/string_number_conversions.h"
#include "packager/media/base/key_source.h" #include "packager/media/base/key_source.h"
#include "packager/media/base/raw_key_pssh_generator.h" #include "packager/media/base/raw_key_pssh_generator.h"
#include "packager/status_macros.h"
namespace { namespace {
const char kEmptyDrmLabel[] = ""; const char kEmptyDrmLabel[] = "";
@ -61,9 +62,7 @@ Status RawKeySource::GetKey(const std::vector<uint8_t>& key_id,
Status RawKeySource::GetCryptoPeriodKey(uint32_t crypto_period_index, Status RawKeySource::GetCryptoPeriodKey(uint32_t crypto_period_index,
const std::string& stream_label, const std::string& stream_label,
EncryptionKey* key) { EncryptionKey* key) {
Status status = GetKey(stream_label, key); RETURN_IF_ERROR(GetKey(stream_label, key));
if (!status.ok())
return status;
// A naive key rotation algorithm is implemented here by left rotating the // A naive key rotation algorithm is implemented here by left rotating the
// key, key_id and pssh. Note that this implementation is only intended for // key, key_id and pssh. Note that this implementation is only intended for
@ -78,33 +77,21 @@ Status RawKeySource::GetCryptoPeriodKey(uint32_t crypto_period_index,
key->key.begin() + (crypto_period_index % key->key.size()), key->key.begin() + (crypto_period_index % key->key.size()),
key->key.end()); key->key.end());
for (auto& key_system : key->key_system_info) { // Clear |key->key_system_info| to prepare for update. The original
if (key_system.system_id() != // |key_system_info| is saved as it may still be useful later.
std::vector<uint8_t>(std::begin(kCommonSystemId), std::vector<ProtectionSystemSpecificInfo> original_key_system_info;
std::end(kCommonSystemId))) { key->key_system_info.swap(original_key_system_info);
LOG(WARNING) << "For key rotation with raw key source, only common key "
"system is supported.";
}
std::vector<uint8_t> pssh_data = key_system.pssh_data();
if (!pssh_data.empty()) {
std::rotate(pssh_data.begin(),
pssh_data.begin() + (crypto_period_index % pssh_data.size()),
pssh_data.end());
key_system.set_pssh_data(pssh_data);
}
// Rotate the key_ids in pssh as well if exists. EncryptionKeyMap encryption_key_map;
// Save a local copy of the key ids before clearing the key ids in encryption_key_map[stream_label].reset(new EncryptionKey(*key));
// |key_system|. The key ids will be updated and added back later. RETURN_IF_ERROR(UpdateProtectionSystemInfo(&encryption_key_map));
std::vector<std::vector<uint8_t>> key_ids_copy = key_system.key_ids(); key->key_system_info = encryption_key_map[stream_label]->key_system_info;
key_system.clear_key_ids();
for (std::vector<uint8_t>& key_id : key_ids_copy) { // It is possible that the generated |key_system_info| is empty. This happens
std::rotate(key_id.begin(), // when RawKeyParams.pssh is specified. Restore the original key system info
key_id.begin() + (crypto_period_index % key_id.size()), // in this case.
key_id.end()); if (key->key_system_info.empty())
key_system.add_key_id(key_id); key->key_system_info.swap(original_key_system_info);
}
}
return Status::OK; return Status::OK;
} }