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"
15 const char kDefaultUUID[] =
"edef8ba9-79d6-4ace-a3c8-27dcd51d21ed";
16 const char kDefaultSystemName[] =
"";
19 namespace edash_packager {
22 EncryptionKey::EncryptionKey() {}
23 EncryptionKey::~EncryptionKey() {}
25 KeySource::~KeySource() {}
28 const std::string& policy) {
50 DCHECK(encryption_key_);
51 *key = *encryption_key_;
58 DCHECK(encryption_key_);
59 if (key_id != encryption_key_->key_id) {
60 return Status(error::NOT_FOUND, std::string(
"Key for key ID ") +
61 base::HexEncode(&key_id[0], key_id.size()) +
64 *key = *encryption_key_;
71 *key = *encryption_key_;
77 <<
"This naive key rotation algorithm should not be used in production.";
78 std::rotate(key->key_id.begin(),
79 key->key_id.begin() + (crypto_period_index % key->key_id.size()),
81 std::rotate(key->key.begin(),
82 key->key.begin() + (crypto_period_index % key->key.size()),
84 const size_t kPsshHeaderSize = 32u;
85 std::vector<uint8_t> pssh_data(key->pssh.begin() + kPsshHeaderSize,
87 std::rotate(pssh_data.begin(),
88 pssh_data.begin() + (crypto_period_index % pssh_data.size()),
99 return kDefaultSystemName;
103 const std::string& key_id_hex,
104 const std::string& key_hex,
105 const std::string& pssh_data_hex,
106 const std::string& iv_hex) {
107 scoped_ptr<EncryptionKey> encryption_key(
new EncryptionKey());
109 if (!base::HexStringToBytes(key_id_hex, &encryption_key->key_id)) {
110 LOG(ERROR) <<
"Cannot parse key_id_hex " << key_id_hex;
111 return scoped_ptr<KeySource>();
114 if (!base::HexStringToBytes(key_hex, &encryption_key->key)) {
115 LOG(ERROR) <<
"Cannot parse key_hex " << key_hex;
116 return scoped_ptr<KeySource>();
119 std::vector<uint8_t> pssh_data;
120 if (!pssh_data_hex.empty() &&
121 !base::HexStringToBytes(pssh_data_hex, &pssh_data)) {
122 LOG(ERROR) <<
"Cannot parse pssh_hex " << pssh_data_hex;
123 return scoped_ptr<KeySource>();
126 if (!iv_hex.empty()) {
127 if (!base::HexStringToBytes(iv_hex, &encryption_key->iv)) {
128 LOG(ERROR) <<
"Cannot parse iv_hex " << iv_hex;
129 return scoped_ptr<KeySource>();
134 return scoped_ptr<KeySource>(
139 const std::string& track_type_string) {
140 if (track_type_string ==
"SD")
141 return TRACK_TYPE_SD;
142 if (track_type_string ==
"HD")
143 return TRACK_TYPE_HD;
144 if (track_type_string ==
"AUDIO")
145 return TRACK_TYPE_AUDIO;
146 if (track_type_string ==
"UNSPECIFIED")
147 return TRACK_TYPE_UNSPECIFIED;
148 LOG(WARNING) <<
"Unexpected track type: " << track_type_string;
149 return TRACK_TYPE_UNKNOWN;
153 switch (track_type) {
158 case TRACK_TYPE_AUDIO:
161 NOTIMPLEMENTED() <<
"Unknown track type: " << track_type;
167 const std::vector<uint8_t>& pssh_data) {
168 const uint8_t kPsshFourCC[] = {
'p',
's',
's',
'h'};
169 const uint32_t kVersionAndFlags = 0;
171 const uint32_t pssh_data_size = pssh_data.size();
172 const uint32_t total_size =
173 sizeof(total_size) +
sizeof(kPsshFourCC) +
sizeof(kVersionAndFlags) +
174 sizeof(kWidevineSystemId) +
sizeof(pssh_data_size) + pssh_data_size;
178 writer.AppendArray(kPsshFourCC,
sizeof(kPsshFourCC));
180 writer.AppendArray(kWidevineSystemId,
sizeof(kWidevineSystemId));
182 writer.AppendVector(pssh_data);
183 return std::vector<uint8_t>(writer.
Buffer(), writer.
Buffer() + writer.Size());
186 KeySource::KeySource() {}
187 KeySource::KeySource(scoped_ptr<EncryptionKey> encryption_key)
188 : encryption_key_(encryption_key.Pass()) {
189 DCHECK(encryption_key_);