| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/cdm/aes_decryptor.h" | 5 #include "media/cdm/aes_decryptor.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "crypto/encryptor.h" | 12 #include "crypto/encryptor.h" |
| 13 #include "crypto/symmetric_key.h" | 13 #include "crypto/symmetric_key.h" |
| 14 #include "media/base/audio_decoder_config.h" | 14 #include "media/base/audio_decoder_config.h" |
| 15 #include "media/base/decoder_buffer.h" | 15 #include "media/base/decoder_buffer.h" |
| 16 #include "media/base/decrypt_config.h" | 16 #include "media/base/decrypt_config.h" |
| 17 #include "media/base/video_decoder_config.h" | 17 #include "media/base/video_decoder_config.h" |
| 18 #include "media/base/video_frame.h" | 18 #include "media/base/video_frame.h" |
| 19 #include "media/cdm/json_web_key.h" | 19 #include "media/cdm/json_web_key.h" |
| 20 | 20 |
| 21 namespace media { | 21 namespace media { |
| 22 | 22 |
| 23 uint32 AesDecryptor::next_session_id_ = 1; | 23 uint32 AesDecryptor::next_web_session_id_ = 1; |
| 24 | 24 |
| 25 enum ClearBytesBufferSel { | 25 enum ClearBytesBufferSel { |
| 26 kSrcContainsClearBytes, | 26 kSrcContainsClearBytes, |
| 27 kDstContainsClearBytes | 27 kDstContainsClearBytes |
| 28 }; | 28 }; |
| 29 | 29 |
| 30 static void CopySubsamples(const std::vector<SubsampleEntry>& subsamples, | 30 static void CopySubsamples(const std::vector<SubsampleEntry>& subsamples, |
| 31 const ClearBytesBufferSel sel, | 31 const ClearBytesBufferSel sel, |
| 32 const uint8* src, | 32 const uint8* src, |
| 33 uint8* dst) { | 33 uint8* dst) { |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 : session_created_cb_(session_created_cb), | 150 : session_created_cb_(session_created_cb), |
| 151 session_message_cb_(session_message_cb), | 151 session_message_cb_(session_message_cb), |
| 152 session_ready_cb_(session_ready_cb), | 152 session_ready_cb_(session_ready_cb), |
| 153 session_closed_cb_(session_closed_cb), | 153 session_closed_cb_(session_closed_cb), |
| 154 session_error_cb_(session_error_cb) {} | 154 session_error_cb_(session_error_cb) {} |
| 155 | 155 |
| 156 AesDecryptor::~AesDecryptor() { | 156 AesDecryptor::~AesDecryptor() { |
| 157 STLDeleteValues(&key_map_); | 157 STLDeleteValues(&key_map_); |
| 158 } | 158 } |
| 159 | 159 |
| 160 bool AesDecryptor::CreateSession(uint32 reference_id, | 160 bool AesDecryptor::CreateSession(uint32 session_id, |
| 161 const std::string& type, | 161 const std::string& type, |
| 162 const uint8* init_data, | 162 const uint8* init_data, |
| 163 int init_data_length) { | 163 int init_data_length) { |
| 164 std::string session_id_string(base::UintToString(next_session_id_++)); | 164 std::string web_session_id_string(base::UintToString(next_web_session_id_++)); |
| 165 | 165 |
| 166 // For now, the AesDecryptor does not care about |type|; | 166 // For now, the AesDecryptor does not care about |type|; |
| 167 // just fire the event with the |init_data| as the request. | 167 // just fire the event with the |init_data| as the request. |
| 168 std::vector<uint8> message; | 168 std::vector<uint8> message; |
| 169 if (init_data && init_data_length) | 169 if (init_data && init_data_length) |
| 170 message.assign(init_data, init_data + init_data_length); | 170 message.assign(init_data, init_data + init_data_length); |
| 171 | 171 |
| 172 session_created_cb_.Run(reference_id, session_id_string); | 172 session_created_cb_.Run(session_id, web_session_id_string); |
| 173 session_message_cb_.Run(reference_id, message, std::string()); | 173 session_message_cb_.Run(session_id, message, std::string()); |
| 174 return true; | 174 return true; |
| 175 } | 175 } |
| 176 | 176 |
| 177 void AesDecryptor::UpdateSession(uint32 reference_id, | 177 void AesDecryptor::UpdateSession(uint32 session_id, |
| 178 const uint8* response, | 178 const uint8* response, |
| 179 int response_length) { | 179 int response_length) { |
| 180 CHECK(response); | 180 CHECK(response); |
| 181 CHECK_GT(response_length, 0); | 181 CHECK_GT(response_length, 0); |
| 182 | 182 |
| 183 std::string key_string(reinterpret_cast<const char*>(response), | 183 std::string key_string(reinterpret_cast<const char*>(response), |
| 184 response_length); | 184 response_length); |
| 185 KeyIdAndKeyPairs keys; | 185 KeyIdAndKeyPairs keys; |
| 186 if (!ExtractKeysFromJWKSet(key_string, &keys)) { | 186 if (!ExtractKeysFromJWKSet(key_string, &keys)) { |
| 187 session_error_cb_.Run(reference_id, MediaKeys::kUnknownError, 0); | 187 session_error_cb_.Run(session_id, MediaKeys::kUnknownError, 0); |
| 188 return; | 188 return; |
| 189 } | 189 } |
| 190 | 190 |
| 191 // Make sure that at least one key was extracted. | 191 // Make sure that at least one key was extracted. |
| 192 if (keys.empty()) { | 192 if (keys.empty()) { |
| 193 session_error_cb_.Run(reference_id, MediaKeys::kUnknownError, 0); | 193 session_error_cb_.Run(session_id, MediaKeys::kUnknownError, 0); |
| 194 return; | 194 return; |
| 195 } | 195 } |
| 196 | 196 |
| 197 for (KeyIdAndKeyPairs::iterator it = keys.begin(); it != keys.end(); ++it) { | 197 for (KeyIdAndKeyPairs::iterator it = keys.begin(); it != keys.end(); ++it) { |
| 198 if (it->second.length() != | 198 if (it->second.length() != |
| 199 static_cast<size_t>(DecryptConfig::kDecryptionKeySize)) { | 199 static_cast<size_t>(DecryptConfig::kDecryptionKeySize)) { |
| 200 DVLOG(1) << "Invalid key length: " << key_string.length(); | 200 DVLOG(1) << "Invalid key length: " << key_string.length(); |
| 201 session_error_cb_.Run(reference_id, MediaKeys::kUnknownError, 0); | 201 session_error_cb_.Run(session_id, MediaKeys::kUnknownError, 0); |
| 202 return; | 202 return; |
| 203 } | 203 } |
| 204 if (!AddDecryptionKey(it->first, it->second)) { | 204 if (!AddDecryptionKey(it->first, it->second)) { |
| 205 session_error_cb_.Run(reference_id, MediaKeys::kUnknownError, 0); | 205 session_error_cb_.Run(session_id, MediaKeys::kUnknownError, 0); |
| 206 return; | 206 return; |
| 207 } | 207 } |
| 208 } | 208 } |
| 209 | 209 |
| 210 if (!new_audio_key_cb_.is_null()) | 210 if (!new_audio_key_cb_.is_null()) |
| 211 new_audio_key_cb_.Run(); | 211 new_audio_key_cb_.Run(); |
| 212 | 212 |
| 213 if (!new_video_key_cb_.is_null()) | 213 if (!new_video_key_cb_.is_null()) |
| 214 new_video_key_cb_.Run(); | 214 new_video_key_cb_.Run(); |
| 215 | 215 |
| 216 session_ready_cb_.Run(reference_id); | 216 session_ready_cb_.Run(session_id); |
| 217 } | 217 } |
| 218 | 218 |
| 219 void AesDecryptor::ReleaseSession(uint32 reference_id) { | 219 void AesDecryptor::ReleaseSession(uint32 session_id) { |
| 220 // TODO: Implement: http://crbug.com/313412. | 220 // TODO: Implement: http://crbug.com/313412. |
| 221 } | 221 } |
| 222 | 222 |
| 223 Decryptor* AesDecryptor::GetDecryptor() { | 223 Decryptor* AesDecryptor::GetDecryptor() { |
| 224 return this; | 224 return this; |
| 225 } | 225 } |
| 226 | 226 |
| 227 void AesDecryptor::RegisterNewKeyCB(StreamType stream_type, | 227 void AesDecryptor::RegisterNewKeyCB(StreamType stream_type, |
| 228 const NewKeyCB& new_key_cb) { | 228 const NewKeyCB& new_key_cb) { |
| 229 switch (stream_type) { | 229 switch (stream_type) { |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 350 bool AesDecryptor::DecryptionKey::Init() { | 350 bool AesDecryptor::DecryptionKey::Init() { |
| 351 CHECK(!secret_.empty()); | 351 CHECK(!secret_.empty()); |
| 352 decryption_key_.reset(crypto::SymmetricKey::Import( | 352 decryption_key_.reset(crypto::SymmetricKey::Import( |
| 353 crypto::SymmetricKey::AES, secret_)); | 353 crypto::SymmetricKey::AES, secret_)); |
| 354 if (!decryption_key_) | 354 if (!decryption_key_) |
| 355 return false; | 355 return false; |
| 356 return true; | 356 return true; |
| 357 } | 357 } |
| 358 | 358 |
| 359 } // namespace media | 359 } // namespace media |
| OLD | NEW |