| 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 "webkit/renderer/media/crypto/ppapi_decryptor.h" | 5 #include "webkit/renderer/media/crypto/ppapi_decryptor.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
| 11 #include "base/location.h" | 11 #include "base/location.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
| 14 #include "base/message_loop/message_loop_proxy.h" | 14 #include "base/message_loop/message_loop_proxy.h" |
| 15 #include "media/base/audio_decoder_config.h" | 15 #include "media/base/audio_decoder_config.h" |
| 16 #include "media/base/data_buffer.h" | 16 #include "media/base/data_buffer.h" |
| 17 #include "media/base/decoder_buffer.h" | 17 #include "media/base/decoder_buffer.h" |
| 18 #include "media/base/video_decoder_config.h" | 18 #include "media/base/video_decoder_config.h" |
| 19 #include "media/base/video_frame.h" | 19 #include "media/base/video_frame.h" |
| 20 #include "webkit/plugins/ppapi/content_decryptor_delegate.h" | 20 #include "webkit/plugins/ppapi/content_decryptor_delegate.h" |
| 21 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | 21 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
| 22 #include "webkit/renderer/media/crypto/key_systems.h" | |
| 23 | 22 |
| 24 namespace webkit_media { | 23 namespace webkit_media { |
| 25 | 24 |
| 25 PpapiDecryptor* PpapiDecryptor::Create( |
| 26 const std::string& key_system, |
| 27 const scoped_refptr<webkit::ppapi::PluginInstance>& plugin_instance, |
| 28 const media::KeyAddedCB& key_added_cb, |
| 29 const media::KeyErrorCB& key_error_cb, |
| 30 const media::KeyMessageCB& key_message_cb, |
| 31 const media::NeedKeyCB& need_key_cb, |
| 32 const base::Closure& destroy_plugin_cb) { |
| 33 webkit::ppapi::ContentDecryptorDelegate* plugin_cdm_delegate = |
| 34 plugin_instance->GetContentDecryptorDelegate(); |
| 35 if (!plugin_cdm_delegate) { |
| 36 DVLOG(1) << "PpapiDecryptor: plugin cdm delegate creation failed."; |
| 37 return NULL; |
| 38 } |
| 39 |
| 40 plugin_cdm_delegate->Initialize(key_system); |
| 41 |
| 42 return new PpapiDecryptor(plugin_instance, |
| 43 plugin_cdm_delegate, |
| 44 key_added_cb, |
| 45 key_error_cb, |
| 46 key_message_cb, |
| 47 need_key_cb, |
| 48 destroy_plugin_cb); |
| 49 } |
| 50 |
| 26 PpapiDecryptor::PpapiDecryptor( | 51 PpapiDecryptor::PpapiDecryptor( |
| 27 const scoped_refptr<webkit::ppapi::PluginInstance>& plugin_instance, | 52 const scoped_refptr<webkit::ppapi::PluginInstance>& plugin_instance, |
| 53 webkit::ppapi::ContentDecryptorDelegate* plugin_cdm_delegate, |
| 28 const media::KeyAddedCB& key_added_cb, | 54 const media::KeyAddedCB& key_added_cb, |
| 29 const media::KeyErrorCB& key_error_cb, | 55 const media::KeyErrorCB& key_error_cb, |
| 30 const media::KeyMessageCB& key_message_cb, | 56 const media::KeyMessageCB& key_message_cb, |
| 31 const media::NeedKeyCB& need_key_cb, | 57 const media::NeedKeyCB& need_key_cb, |
| 32 const base::Closure& destroy_plugin_cb) | 58 const base::Closure& destroy_plugin_cb) |
| 33 : plugin_instance_(plugin_instance), | 59 : plugin_instance_(plugin_instance), |
| 60 plugin_cdm_delegate_(plugin_cdm_delegate), |
| 34 key_added_cb_(key_added_cb), | 61 key_added_cb_(key_added_cb), |
| 35 key_error_cb_(key_error_cb), | 62 key_error_cb_(key_error_cb), |
| 36 key_message_cb_(key_message_cb), | 63 key_message_cb_(key_message_cb), |
| 37 need_key_cb_(need_key_cb), | 64 need_key_cb_(need_key_cb), |
| 38 destroy_plugin_cb_(destroy_plugin_cb), | 65 destroy_plugin_cb_(destroy_plugin_cb), |
| 39 plugin_cdm_delegate_(NULL), | |
| 40 render_loop_proxy_(base::MessageLoopProxy::current()), | 66 render_loop_proxy_(base::MessageLoopProxy::current()), |
| 41 weak_ptr_factory_(this), | 67 weak_ptr_factory_(this), |
| 42 weak_this_(weak_ptr_factory_.GetWeakPtr()) { | 68 weak_this_(weak_ptr_factory_.GetWeakPtr()) { |
| 43 DCHECK(plugin_instance_.get()); | 69 DCHECK(plugin_instance_.get()); |
| 70 |
| 71 plugin_cdm_delegate_->SetKeyEventCallbacks( |
| 72 base::Bind(&PpapiDecryptor::KeyAdded, weak_this_), |
| 73 base::Bind(&PpapiDecryptor::KeyError, weak_this_), |
| 74 base::Bind(&PpapiDecryptor::KeyMessage, weak_this_), |
| 75 base::Bind(&PpapiDecryptor::NeedKey, weak_this_)); |
| 44 } | 76 } |
| 45 | 77 |
| 46 PpapiDecryptor::~PpapiDecryptor() { | 78 PpapiDecryptor::~PpapiDecryptor() { |
| 47 plugin_cdm_delegate_ = NULL; | 79 plugin_cdm_delegate_ = NULL; |
| 48 plugin_instance_ = NULL; | 80 plugin_instance_ = NULL; |
| 49 destroy_plugin_cb_.Run(); | 81 destroy_plugin_cb_.Run(); |
| 50 } | 82 } |
| 51 | 83 |
| 52 bool PpapiDecryptor::GenerateKeyRequest(const std::string& key_system, | 84 bool PpapiDecryptor::GenerateKeyRequest(const std::string& type, |
| 53 const std::string& type, | |
| 54 const uint8* init_data, | 85 const uint8* init_data, |
| 55 int init_data_length) { | 86 int init_data_length) { |
| 56 DVLOG(2) << "GenerateKeyRequest()"; | 87 DVLOG(2) << "GenerateKeyRequest()"; |
| 57 DCHECK(render_loop_proxy_->BelongsToCurrentThread()); | 88 DCHECK(render_loop_proxy_->BelongsToCurrentThread()); |
| 58 | 89 DCHECK(plugin_cdm_delegate_); |
| 59 if (!plugin_cdm_delegate_) { | |
| 60 plugin_cdm_delegate_ = plugin_instance_->GetContentDecryptorDelegate(); | |
| 61 if (!plugin_cdm_delegate_) { | |
| 62 DVLOG(1) << "PpapiDecryptor: plugin cdm delegate creation failed."; | |
| 63 return false; | |
| 64 } | |
| 65 plugin_cdm_delegate_->SetKeyEventCallbacks( | |
| 66 base::Bind(&PpapiDecryptor::KeyAdded, weak_this_), | |
| 67 base::Bind(&PpapiDecryptor::KeyError, weak_this_), | |
| 68 base::Bind(&PpapiDecryptor::KeyMessage, weak_this_), | |
| 69 base::Bind(&PpapiDecryptor::NeedKey, weak_this_)); | |
| 70 } | |
| 71 | 90 |
| 72 if (!plugin_cdm_delegate_->GenerateKeyRequest( | 91 if (!plugin_cdm_delegate_->GenerateKeyRequest( |
| 73 key_system, type, init_data, init_data_length)) { | 92 type, init_data, init_data_length)) { |
| 74 ReportFailureToCallPlugin(key_system, std::string()); | 93 ReportFailureToCallPlugin(std::string()); |
| 75 return false; | 94 return false; |
| 76 } | 95 } |
| 77 | 96 |
| 78 return true; | 97 return true; |
| 79 } | 98 } |
| 80 | 99 |
| 81 void PpapiDecryptor::AddKey(const std::string& key_system, | 100 void PpapiDecryptor::AddKey(const uint8* key, |
| 82 const uint8* key, | |
| 83 int key_length, | 101 int key_length, |
| 84 const uint8* init_data, | 102 const uint8* init_data, |
| 85 int init_data_length, | 103 int init_data_length, |
| 86 const std::string& session_id) { | 104 const std::string& session_id) { |
| 87 DVLOG(2) << "AddKey()"; | 105 DVLOG(2) << "AddKey()"; |
| 88 DCHECK(render_loop_proxy_->BelongsToCurrentThread()); | 106 DCHECK(render_loop_proxy_->BelongsToCurrentThread()); |
| 89 | 107 |
| 90 if (!plugin_cdm_delegate_->AddKey( | 108 if (!plugin_cdm_delegate_->AddKey( |
| 91 session_id, key, key_length, init_data, init_data_length)) { | 109 session_id, key, key_length, init_data, init_data_length)) { |
| 92 ReportFailureToCallPlugin(key_system, session_id); | 110 ReportFailureToCallPlugin(session_id); |
| 93 } | 111 } |
| 94 | 112 |
| 95 if (!new_audio_key_cb_.is_null()) | 113 if (!new_audio_key_cb_.is_null()) |
| 96 new_audio_key_cb_.Run(); | 114 new_audio_key_cb_.Run(); |
| 97 | 115 |
| 98 if (!new_video_key_cb_.is_null()) | 116 if (!new_video_key_cb_.is_null()) |
| 99 new_video_key_cb_.Run(); | 117 new_video_key_cb_.Run(); |
| 100 } | 118 } |
| 101 | 119 |
| 102 void PpapiDecryptor::CancelKeyRequest(const std::string& key_system, | 120 void PpapiDecryptor::CancelKeyRequest(const std::string& session_id) { |
| 103 const std::string& session_id) { | |
| 104 DVLOG(2) << "CancelKeyRequest()"; | 121 DVLOG(2) << "CancelKeyRequest()"; |
| 105 DCHECK(render_loop_proxy_->BelongsToCurrentThread()); | 122 DCHECK(render_loop_proxy_->BelongsToCurrentThread()); |
| 106 | 123 |
| 107 if (!plugin_cdm_delegate_->CancelKeyRequest(session_id)) | 124 if (!plugin_cdm_delegate_->CancelKeyRequest(session_id)) |
| 108 ReportFailureToCallPlugin(key_system, session_id); | 125 ReportFailureToCallPlugin(session_id); |
| 109 } | 126 } |
| 110 | 127 |
| 111 media::MediaKeys* PpapiDecryptor::GetMediaKeys() { | 128 media::MediaKeys* PpapiDecryptor::GetMediaKeys() { |
| 112 return this; | 129 return this; |
| 113 } | 130 } |
| 114 | 131 |
| 115 void PpapiDecryptor::RegisterNewKeyCB(StreamType stream_type, | 132 void PpapiDecryptor::RegisterNewKeyCB(StreamType stream_type, |
| 116 const NewKeyCB& new_key_cb) { | 133 const NewKeyCB& new_key_cb) { |
| 117 switch (stream_type) { | 134 switch (stream_type) { |
| 118 case kAudio: | 135 case kAudio: |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 if (!render_loop_proxy_->BelongsToCurrentThread()) { | 251 if (!render_loop_proxy_->BelongsToCurrentThread()) { |
| 235 render_loop_proxy_->PostTask(FROM_HERE, base::Bind( | 252 render_loop_proxy_->PostTask(FROM_HERE, base::Bind( |
| 236 &PpapiDecryptor::DeinitializeDecoder, weak_this_, stream_type)); | 253 &PpapiDecryptor::DeinitializeDecoder, weak_this_, stream_type)); |
| 237 return; | 254 return; |
| 238 } | 255 } |
| 239 | 256 |
| 240 DVLOG(2) << "DeinitializeDecoder() - stream_type: " << stream_type; | 257 DVLOG(2) << "DeinitializeDecoder() - stream_type: " << stream_type; |
| 241 plugin_cdm_delegate_->DeinitializeDecoder(stream_type); | 258 plugin_cdm_delegate_->DeinitializeDecoder(stream_type); |
| 242 } | 259 } |
| 243 | 260 |
| 244 void PpapiDecryptor::ReportFailureToCallPlugin(const std::string& key_system, | 261 void PpapiDecryptor::ReportFailureToCallPlugin(const std::string& session_id) { |
| 245 const std::string& session_id) { | |
| 246 DVLOG(1) << "Failed to call plugin."; | 262 DVLOG(1) << "Failed to call plugin."; |
| 247 key_error_cb_.Run(key_system, session_id, kUnknownError, 0); | 263 key_error_cb_.Run(session_id, kUnknownError, 0); |
| 248 } | 264 } |
| 249 | 265 |
| 250 void PpapiDecryptor::OnDecoderInitialized(StreamType stream_type, | 266 void PpapiDecryptor::OnDecoderInitialized(StreamType stream_type, |
| 251 bool success) { | 267 bool success) { |
| 252 switch (stream_type) { | 268 switch (stream_type) { |
| 253 case kAudio: | 269 case kAudio: |
| 254 DCHECK(!audio_decoder_init_cb_.is_null()); | 270 DCHECK(!audio_decoder_init_cb_.is_null()); |
| 255 base::ResetAndReturn(&audio_decoder_init_cb_).Run(success); | 271 base::ResetAndReturn(&audio_decoder_init_cb_).Run(success); |
| 256 break; | 272 break; |
| 257 case kVideo: | 273 case kVideo: |
| 258 DCHECK(!video_decoder_init_cb_.is_null()); | 274 DCHECK(!video_decoder_init_cb_.is_null()); |
| 259 base::ResetAndReturn(&video_decoder_init_cb_).Run(success); | 275 base::ResetAndReturn(&video_decoder_init_cb_).Run(success); |
| 260 break; | 276 break; |
| 261 default: | 277 default: |
| 262 NOTREACHED(); | 278 NOTREACHED(); |
| 263 } | 279 } |
| 264 } | 280 } |
| 265 | 281 |
| 266 void PpapiDecryptor::KeyAdded(const std::string& key_system, | 282 void PpapiDecryptor::KeyAdded(const std::string& session_id) { |
| 267 const std::string& session_id) { | |
| 268 DCHECK(render_loop_proxy_->BelongsToCurrentThread()); | 283 DCHECK(render_loop_proxy_->BelongsToCurrentThread()); |
| 269 key_added_cb_.Run(key_system, session_id); | 284 key_added_cb_.Run(session_id); |
| 270 } | 285 } |
| 271 | 286 |
| 272 void PpapiDecryptor::KeyError(const std::string& key_system, | 287 void PpapiDecryptor::KeyError(const std::string& session_id, |
| 273 const std::string& session_id, | |
| 274 media::MediaKeys::KeyError error_code, | 288 media::MediaKeys::KeyError error_code, |
| 275 int system_code) { | 289 int system_code) { |
| 276 DCHECK(render_loop_proxy_->BelongsToCurrentThread()); | 290 DCHECK(render_loop_proxy_->BelongsToCurrentThread()); |
| 277 key_error_cb_.Run(key_system, session_id, error_code, system_code); | 291 key_error_cb_.Run(session_id, error_code, system_code); |
| 278 } | 292 } |
| 279 | 293 |
| 280 void PpapiDecryptor::KeyMessage(const std::string& key_system, | 294 void PpapiDecryptor::KeyMessage(const std::string& session_id, |
| 281 const std::string& session_id, | |
| 282 const std::string& message, | 295 const std::string& message, |
| 283 const std::string& default_url) { | 296 const std::string& default_url) { |
| 284 DCHECK(render_loop_proxy_->BelongsToCurrentThread()); | 297 DCHECK(render_loop_proxy_->BelongsToCurrentThread()); |
| 285 key_message_cb_.Run(key_system, session_id, message, default_url); | 298 key_message_cb_.Run(session_id, message, default_url); |
| 286 } | 299 } |
| 287 | 300 |
| 288 void PpapiDecryptor::NeedKey(const std::string& key_system, | 301 void PpapiDecryptor::NeedKey(const std::string& session_id, |
| 289 const std::string& session_id, | |
| 290 const std::string& type, | 302 const std::string& type, |
| 291 scoped_ptr<uint8[]> init_data, | 303 scoped_ptr<uint8[]> init_data, |
| 292 int init_data_size) { | 304 int init_data_size) { |
| 293 DCHECK(render_loop_proxy_->BelongsToCurrentThread()); | 305 DCHECK(render_loop_proxy_->BelongsToCurrentThread()); |
| 294 need_key_cb_.Run(key_system, session_id, type, | 306 need_key_cb_.Run(session_id, type, init_data.Pass(), init_data_size); |
| 295 init_data.Pass(), init_data_size); | |
| 296 } | 307 } |
| 297 | 308 |
| 298 } // namespace webkit_media | 309 } // namespace webkit_media |
| OLD | NEW |