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 uint32_t crypto_period_duration_in_seconds,
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()),
87 std::vector<ProtectionSystemSpecificInfo> key_system_info;
88 if (!raw_key.
pssh.empty()) {
90 raw_key.
pssh.data(), raw_key.
pssh.size(), &key_system_info)) {
91 LOG(ERROR) <<
"--pssh argument should be full PSSH boxes.";
92 return std::unique_ptr<RawKeySource>();
96 std::vector<std::vector<uint8_t>> key_ids;
97 for (
const auto& entry : raw_key.
key_map)
98 key_ids.emplace_back(entry.second.key_id);
100 EncryptionKeyMap encryption_key_map;
101 for (
const auto& entry : raw_key.
key_map) {
102 const std::string& drm_label = entry.first;
105 if (key_pair.key_id.size() != 16) {
106 LOG(ERROR) <<
"Invalid key ID size '" << key_pair.key_id.size()
107 <<
"', must be 16 bytes.";
108 return std::unique_ptr<RawKeySource>();
110 if (key_pair.key.size() != 16) {
112 LOG(ERROR) <<
"Invalid key size '" << key_pair.key.size()
113 <<
"', must be 16 bytes.";
114 return std::unique_ptr<RawKeySource>();
116 if (!key_pair.iv.empty() && key_pair.iv.size() != 8 && key_pair.iv.size() != 16) {
117 LOG(ERROR) <<
"Invalid IV '" << key_pair.iv.size()
118 <<
"', must be 8 or 16 bytes.";
119 return std::unique_ptr<RawKeySource>();
122 std::unique_ptr<EncryptionKey> encryption_key(
new EncryptionKey);
123 encryption_key->key_id = key_pair.key_id;
124 encryption_key->key_ids = key_ids;
125 encryption_key->key = key_pair.key;
126 encryption_key->iv = (key_pair.iv.empty()) ? raw_key.
iv : key_pair.iv;
127 encryption_key->key_system_info = key_system_info;
128 encryption_key_map[drm_label] = std::move(encryption_key);
131 return std::unique_ptr<RawKeySource>(
135 RawKeySource::RawKeySource() {}
137 RawKeySource::RawKeySource(EncryptionKeyMap&& encryption_key_map)
138 : encryption_key_map_(std::move(encryption_key_map)) {}