7 #include "packager/media/base/key_source.h"
9 #include "packager/base/strings/string_number_conversions.h"
10 #include "packager/media/base/aes_encryptor.h"
11 #include "packager/media/base/buffer_writer.h"
13 namespace edash_packager {
16 EncryptionKey::EncryptionKey() {}
17 EncryptionKey::~EncryptionKey() {}
19 KeySource::~KeySource() {}
22 const std::string& policy) {
44 DCHECK(encryption_key_);
45 *key = *encryption_key_;
52 DCHECK(encryption_key_);
53 if (key_id != encryption_key_->key_id) {
54 return Status(error::NOT_FOUND, std::string(
"Key for key ID ") +
55 base::HexEncode(&key_id[0], key_id.size()) +
58 *key = *encryption_key_;
65 *key = *encryption_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()),
79 std::vector<uint8_t> pssh_data(
80 key->key_system_info[0].pssh_data().begin(),
81 key->key_system_info[0].pssh_data().end());
82 std::rotate(pssh_data.begin(),
83 pssh_data.begin() + (crypto_period_index % pssh_data.size()),
89 info.add_key_id(key->key_id);
90 info.set_system_id(kWidevineSystemId, arraysize(kWidevineSystemId));
91 info.set_pssh_box_version(0);
92 info.set_pssh_data(pssh_data);
94 key->key_system_info.clear();
95 key->key_system_info.push_back(info);
101 const std::string& key_id_hex,
102 const std::string& key_hex,
103 const std::string& pssh_data_hex,
104 const std::string& iv_hex) {
105 scoped_ptr<EncryptionKey> encryption_key(
new EncryptionKey());
107 if (!base::HexStringToBytes(key_id_hex, &encryption_key->key_id)) {
108 LOG(ERROR) <<
"Cannot parse key_id_hex " << key_id_hex;
109 return scoped_ptr<KeySource>();
112 if (!base::HexStringToBytes(key_hex, &encryption_key->key)) {
113 LOG(ERROR) <<
"Cannot parse key_hex " << key_hex;
114 return scoped_ptr<KeySource>();
117 std::vector<uint8_t> pssh_data;
118 if (!pssh_data_hex.empty() &&
119 !base::HexStringToBytes(pssh_data_hex, &pssh_data)) {
120 LOG(ERROR) <<
"Cannot parse pssh_hex " << pssh_data_hex;
121 return scoped_ptr<KeySource>();
124 if (!iv_hex.empty()) {
125 if (!base::HexStringToBytes(iv_hex, &encryption_key->iv)) {
126 LOG(ERROR) <<
"Cannot parse iv_hex " << iv_hex;
127 return scoped_ptr<KeySource>();
133 info.add_key_id(encryption_key->key_id);
134 info.set_system_id(kWidevineSystemId, arraysize(kWidevineSystemId));
135 info.set_pssh_box_version(0);
136 info.set_pssh_data(pssh_data);
138 encryption_key->key_system_info.push_back(info);
139 return scoped_ptr<KeySource>(
new KeySource(encryption_key.Pass()));
143 const std::string& track_type_string) {
144 if (track_type_string ==
"SD")
145 return TRACK_TYPE_SD;
146 if (track_type_string ==
"HD")
147 return TRACK_TYPE_HD;
148 if (track_type_string ==
"AUDIO")
149 return TRACK_TYPE_AUDIO;
150 if (track_type_string ==
"UNSPECIFIED")
151 return TRACK_TYPE_UNSPECIFIED;
152 LOG(WARNING) <<
"Unexpected track type: " << track_type_string;
153 return TRACK_TYPE_UNKNOWN;
157 switch (track_type) {
162 case TRACK_TYPE_AUDIO:
165 NOTIMPLEMENTED() <<
"Unknown track type: " << track_type;
170 KeySource::KeySource() {}
171 KeySource::KeySource(scoped_ptr<EncryptionKey> encryption_key)
172 : encryption_key_(encryption_key.Pass()) {
173 DCHECK(encryption_key_);