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"
14 const uint8_t kWidevineSystemId[] = {0xed, 0xef, 0x8b, 0xa9, 0x79, 0xd6,
15 0x4a, 0xce, 0xa3, 0xc8, 0x27, 0xdc,
16 0xd5, 0x1d, 0x21, 0xed};
18 const char kDefaultUUID[] =
"edef8ba9-79d6-4ace-a3c8-27dcd51d21ed";
19 const char kDefaultSystemName[] =
"";
22 namespace edash_packager {
25 EncryptionKey::EncryptionKey() {}
26 EncryptionKey::~EncryptionKey() {}
28 KeySource::~KeySource() {}
31 const std::string& policy) {
48 DCHECK(encryption_key_);
49 *key = *encryption_key_;
56 DCHECK(encryption_key_);
57 if (key_id != encryption_key_->key_id) {
58 return Status(error::NOT_FOUND, std::string(
"Key for key ID ") +
59 base::HexEncode(&key_id[0], key_id.size()) +
62 *key = *encryption_key_;
69 *key = *encryption_key_;
75 <<
"This naive key rotation algorithm should not be used in production.";
76 std::rotate(key->key_id.begin(),
77 key->key_id.begin() + (crypto_period_index % key->key_id.size()),
79 std::rotate(key->key.begin(),
80 key->key.begin() + (crypto_period_index % key->key.size()),
82 const size_t kPsshHeaderSize = 32u;
83 std::vector<uint8_t> pssh_data(key->pssh.begin() + kPsshHeaderSize,
85 std::rotate(pssh_data.begin(),
86 pssh_data.begin() + (crypto_period_index % pssh_data.size()),
97 return kDefaultSystemName;
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>();
132 return scoped_ptr<KeySource>(
137 const std::string& track_type_string) {
138 if (track_type_string ==
"SD")
139 return TRACK_TYPE_SD;
140 if (track_type_string ==
"HD")
141 return TRACK_TYPE_HD;
142 if (track_type_string ==
"AUDIO")
143 return TRACK_TYPE_AUDIO;
144 if (track_type_string ==
"UNSPECIFIED")
145 return TRACK_TYPE_UNSPECIFIED;
146 LOG(WARNING) <<
"Unexpected track type: " << track_type_string;
147 return TRACK_TYPE_UNKNOWN;
151 switch (track_type) {
156 case TRACK_TYPE_AUDIO:
159 NOTIMPLEMENTED() <<
"Unknown track type: " << track_type;
165 const std::vector<uint8_t>& pssh_data) {
166 const uint8_t kPsshFourCC[] = {
'p',
's',
's',
'h'};
167 const uint32_t kVersionAndFlags = 0;
169 const uint32_t pssh_data_size = pssh_data.size();
170 const uint32_t total_size =
171 sizeof(total_size) +
sizeof(kPsshFourCC) +
sizeof(kVersionAndFlags) +
172 sizeof(kWidevineSystemId) +
sizeof(pssh_data_size) + pssh_data_size;
176 writer.AppendArray(kPsshFourCC,
sizeof(kPsshFourCC));
178 writer.AppendArray(kWidevineSystemId,
sizeof(kWidevineSystemId));
180 writer.AppendVector(pssh_data);
181 return std::vector<uint8_t>(writer.
Buffer(), writer.
Buffer() + writer.Size());
184 KeySource::KeySource() {}
185 KeySource::KeySource(scoped_ptr<EncryptionKey> encryption_key)
186 : encryption_key_(encryption_key.Pass()) {
187 DCHECK(encryption_key_);