7 #include "packager/media/base/raw_key_source.h" 10 #include "packager/base/logging.h" 11 #include "packager/base/strings/string_number_conversions.h" 12 #include "packager/media/base/key_source.h" 13 #include "packager/media/base/raw_key_pssh_generator.h" 14 #include "packager/status_macros.h" 17 const char kEmptyDrmLabel[] =
"";
23 RawKeySource::~RawKeySource() {}
26 const std::vector<uint8_t>& init_data) {
36 auto iter = encryption_key_map_.find(stream_label);
37 if (iter == encryption_key_map_.end()) {
38 iter = encryption_key_map_.find(kEmptyDrmLabel);
39 if (iter == encryption_key_map_.end()) {
40 return Status(error::NOT_FOUND,
41 "Key for '" + stream_label +
"' was not found.");
51 for (
const auto& pair : encryption_key_map_) {
52 if (pair.second->key_id == key_id) {
57 return Status(error::INTERNAL_ERROR,
58 "Key for key_id=" + base::HexEncode(&key_id[0], key_id.size()) +
63 const std::string& stream_label,
65 RETURN_IF_ERROR(
GetKey(stream_label, key));
72 <<
"This naive key rotation algorithm should not be used in production.";
73 std::rotate(key->key_id.begin(),
74 key->key_id.begin() + (crypto_period_index % key->key_id.size()),
76 std::rotate(key->key.begin(),
77 key->key.begin() + (crypto_period_index % key->key.size()),
82 std::vector<ProtectionSystemSpecificInfo> original_key_system_info;
83 key->key_system_info.swap(original_key_system_info);
85 EncryptionKeyMap encryption_key_map;
86 encryption_key_map[stream_label].reset(
new EncryptionKey(*key));
88 key->key_system_info = encryption_key_map[stream_label]->key_system_info;
93 if (key->key_system_info.empty())
94 key->key_system_info.swap(original_key_system_info);
100 int protection_systems_flags,
101 FourCC protection_scheme) {
102 std::vector<ProtectionSystemSpecificInfo> key_system_info;
103 bool pssh_provided =
false;
104 if (!raw_key.
pssh.empty()) {
105 pssh_provided =
true;
107 raw_key.
pssh.data(), raw_key.
pssh.size(), &key_system_info)) {
108 LOG(ERROR) <<
"--pssh argument should be full PSSH boxes.";
109 return std::unique_ptr<RawKeySource>();
113 EncryptionKeyMap encryption_key_map;
114 for (
const auto& entry : raw_key.
key_map) {
115 const std::string& drm_label = entry.first;
118 if (key_pair.key_id.size() != 16) {
119 LOG(ERROR) <<
"Invalid key ID size '" << key_pair.key_id.size()
120 <<
"', must be 16 bytes.";
121 return std::unique_ptr<RawKeySource>();
123 if (key_pair.key.size() != 16) {
125 LOG(ERROR) <<
"Invalid key size '" << key_pair.key.size()
126 <<
"', must be 16 bytes.";
127 return std::unique_ptr<RawKeySource>();
130 std::unique_ptr<EncryptionKey> encryption_key(
new EncryptionKey);
131 encryption_key->key_id = key_pair.key_id;
132 encryption_key->key = key_pair.key;
133 encryption_key->iv = raw_key.
iv;
134 encryption_key->key_system_info = key_system_info;
135 encryption_key_map[drm_label] = std::move(encryption_key);
140 if (!pssh_provided && protection_systems_flags == NO_PROTECTION_SYSTEM_FLAG) {
141 protection_systems_flags = COMMON_PROTECTION_SYSTEM_FLAG;
144 return std::unique_ptr<RawKeySource>(
145 new RawKeySource(std::move(encryption_key_map), protection_systems_flags,
149 RawKeySource::RawKeySource()
150 :
KeySource(NO_PROTECTION_SYSTEM_FLAG, FOURCC_NULL) {}
152 RawKeySource::RawKeySource(EncryptionKeyMap&& encryption_key_map,
153 int protection_systems_flags,
154 FourCC protection_scheme)
155 :
KeySource(protection_systems_flags, protection_scheme),
156 encryption_key_map_(std::move(encryption_key_map)) {
All the methods that are virtual are virtual for mocking.
Raw key encryption/decryption parameters, i.e. with key parameters provided.
std::map< StreamLabel, KeyInfo > key_map
std::vector< uint8_t > iv
std::vector< uint8_t > pssh