7 #include <packager/media/base/raw_key_source.h>
11 #include <absl/log/check.h>
12 #include <absl/log/log.h>
13 #include <absl/strings/escaping.h>
15 #include <packager/macros/compiler.h>
16 #include <packager/macros/status.h>
17 #include <packager/media/base/key_source.h>
18 #include <packager/utils/bytes_to_string_view.h>
21 const char kEmptyDrmLabel[] =
"";
27 RawKeySource::~RawKeySource() {}
30 const std::vector<uint8_t>& init_data) {
31 UNUSED(init_data_type);
42 auto iter = encryption_key_map_.find(stream_label);
43 if (iter == encryption_key_map_.end()) {
44 iter = encryption_key_map_.find(kEmptyDrmLabel);
45 if (iter == encryption_key_map_.end()) {
46 return Status(error::NOT_FOUND,
47 "Key for '" + stream_label +
"' was not found.");
57 for (
const auto& pair : encryption_key_map_) {
58 if (pair.second->key_id == key_id) {
63 return Status(error::INTERNAL_ERROR,
70 uint32_t crypto_period_index,
71 int32_t crypto_period_duration_in_seconds,
72 const std::string& stream_label,
74 UNUSED(crypto_period_duration_in_seconds);
76 RETURN_IF_ERROR(
GetKey(stream_label, key));
83 <<
"This naive key rotation algorithm should not be used in production.";
84 std::rotate(key->
key_id.begin(),
85 key->
key_id.begin() + (crypto_period_index % key->
key_id.size()),
87 std::rotate(key->key.begin(),
88 key->key.begin() + (crypto_period_index % key->key.size()),
97 const RawKeyParams& raw_key) {
98 std::vector<ProtectionSystemSpecificInfo> key_system_info;
99 if (!raw_key.pssh.empty()) {
101 raw_key.pssh.data(), raw_key.pssh.size(), &key_system_info)) {
102 LOG(ERROR) <<
"--pssh argument should be full PSSH boxes.";
103 return std::unique_ptr<RawKeySource>();
107 std::vector<std::vector<uint8_t>> key_ids;
108 for (
const auto& entry : raw_key.key_map)
109 key_ids.emplace_back(entry.second.key_id);
111 EncryptionKeyMap encryption_key_map;
112 for (
const auto& entry : raw_key.key_map) {
113 const std::string& drm_label = entry.first;
114 const RawKeyParams::KeyInfo& key_pair = entry.second;
116 if (key_pair.key_id.size() != 16) {
117 LOG(ERROR) <<
"Invalid key ID size '" << key_pair.key_id.size()
118 <<
"', must be 16 bytes.";
119 return std::unique_ptr<RawKeySource>();
121 if (key_pair.key.size() != 16) {
123 LOG(ERROR) <<
"Invalid key size '" << key_pair.key.size()
124 <<
"', must be 16 bytes.";
125 return std::unique_ptr<RawKeySource>();
127 if (!key_pair.iv.empty() && key_pair.iv.size() != 8 && key_pair.iv.size() != 16) {
128 LOG(ERROR) <<
"Invalid IV '" << key_pair.iv.size()
129 <<
"', must be 8 or 16 bytes.";
130 return std::unique_ptr<RawKeySource>();
133 std::unique_ptr<EncryptionKey> encryption_key(
new EncryptionKey);
134 encryption_key->key_id = key_pair.key_id;
135 encryption_key->key_ids = key_ids;
136 encryption_key->key = key_pair.key;
137 encryption_key->iv = (key_pair.iv.empty()) ? raw_key.iv : key_pair.iv;
138 encryption_key->key_system_info = key_system_info;
139 encryption_key_map[drm_label] = std::move(encryption_key);
142 return std::unique_ptr<RawKeySource>(
146 RawKeySource::RawKeySource() {}
148 RawKeySource::RawKeySource(EncryptionKeyMap&& encryption_key_map)
149 : encryption_key_map_(std::move(encryption_key_map)) {}
All the methods that are virtual are virtual for mocking.
std::string_view byte_vector_to_string_view(const std::vector< uint8_t > &bytes)
Convert byte vector to string_view.