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/status_macros.h" 16 const char kEmptyDrmLabel[] =
"";
22 RawKeySource::~RawKeySource() {}
25 const std::vector<uint8_t>& init_data) {
35 auto iter = encryption_key_map_.find(stream_label);
36 if (iter == encryption_key_map_.end()) {
37 iter = encryption_key_map_.find(kEmptyDrmLabel);
38 if (iter == encryption_key_map_.end()) {
39 return Status(error::NOT_FOUND,
40 "Key for '" + stream_label +
"' was not found.");
50 for (
const auto& pair : encryption_key_map_) {
51 if (pair.second->key_id == key_id) {
56 return Status(error::INTERNAL_ERROR,
57 "Key for key_id=" + base::HexEncode(&key_id[0], key_id.size()) +
62 const std::string& stream_label,
64 RETURN_IF_ERROR(
GetKey(stream_label, key));
71 <<
"This naive key rotation algorithm should not be used in production.";
72 std::rotate(key->key_id.begin(),
73 key->key_id.begin() + (crypto_period_index % key->key_id.size()),
75 std::rotate(key->key.begin(),
76 key->key.begin() + (crypto_period_index % key->key.size()),
81 std::vector<ProtectionSystemSpecificInfo> original_key_system_info;
82 key->key_system_info.swap(original_key_system_info);
84 EncryptionKeyMap encryption_key_map;
85 encryption_key_map[stream_label].reset(
new EncryptionKey(*key));
87 key->key_system_info = encryption_key_map[stream_label]->key_system_info;
92 if (key->key_system_info.empty())
93 key->key_system_info.swap(original_key_system_info);
99 int protection_systems_flags,
100 FourCC protection_scheme) {
101 std::vector<ProtectionSystemSpecificInfo> key_system_info;
102 bool pssh_provided =
false;
103 if (!raw_key.
pssh.empty()) {
104 pssh_provided =
true;
106 raw_key.
pssh.data(), raw_key.
pssh.size(), &key_system_info)) {
107 LOG(ERROR) <<
"--pssh argument should be full PSSH boxes.";
108 return std::unique_ptr<RawKeySource>();
112 EncryptionKeyMap encryption_key_map;
113 for (
const auto& entry : raw_key.
key_map) {
114 const std::string& drm_label = entry.first;
117 if (key_pair.key_id.size() != 16) {
118 LOG(ERROR) <<
"Invalid key ID size '" << key_pair.key_id.size()
119 <<
"', must be 16 bytes.";
120 return std::unique_ptr<RawKeySource>();
122 if (key_pair.key.size() != 16) {
124 LOG(ERROR) <<
"Invalid key size '" << key_pair.key.size()
125 <<
"', must be 16 bytes.";
126 return std::unique_ptr<RawKeySource>();
129 std::unique_ptr<EncryptionKey> encryption_key(
new EncryptionKey);
130 encryption_key->key_id = key_pair.key_id;
131 encryption_key->key = key_pair.key;
132 encryption_key->iv = raw_key.
iv;
133 encryption_key->key_system_info = key_system_info;
134 encryption_key_map[drm_label] = std::move(encryption_key);
139 if (!pssh_provided && protection_systems_flags == NO_PROTECTION_SYSTEM_FLAG) {
140 protection_systems_flags = COMMON_PROTECTION_SYSTEM_FLAG;
143 return std::unique_ptr<RawKeySource>(
144 new RawKeySource(std::move(encryption_key_map), protection_systems_flags,
148 RawKeySource::RawKeySource()
149 :
KeySource(NO_PROTECTION_SYSTEM_FLAG, FOURCC_NULL) {}
151 RawKeySource::RawKeySource(EncryptionKeyMap&& encryption_key_map,
152 int protection_systems_flags,
153 FourCC protection_scheme)
154 :
KeySource(protection_systems_flags, protection_scheme),
155 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