| 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 "content/renderer/media/crypto/ppapi_decryptor.h" | 5 #include "content/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/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 14 #include "base/message_loop/message_loop_proxy.h" | 14 #include "base/message_loop/message_loop_proxy.h" |
| 15 #include "content/renderer/media/crypto/key_systems.h" | 15 #include "content/renderer/media/crypto/key_systems.h" |
| 16 #include "content/renderer/pepper/content_decryptor_delegate.h" | 16 #include "content/renderer/pepper/content_decryptor_delegate.h" |
| 17 #include "content/renderer/pepper/pepper_plugin_instance_impl.h" | 17 #include "content/renderer/pepper/pepper_plugin_instance_impl.h" |
| 18 #include "media/base/audio_decoder_config.h" | 18 #include "media/base/audio_decoder_config.h" |
| 19 #include "media/base/cdm_promise.h" |
| 19 #include "media/base/data_buffer.h" | 20 #include "media/base/data_buffer.h" |
| 20 #include "media/base/decoder_buffer.h" | 21 #include "media/base/decoder_buffer.h" |
| 21 #include "media/base/video_decoder_config.h" | 22 #include "media/base/video_decoder_config.h" |
| 22 #include "media/base/video_frame.h" | 23 #include "media/base/video_frame.h" |
| 23 | 24 |
| 24 namespace content { | 25 namespace content { |
| 25 | 26 |
| 27 // This class is need so that resolving an Update() promise triggers playback |
| 28 // of the stream. |
| 29 class SessionUpdatedPromise : public media::CdmChangeSessionPromise { |
| 30 public: |
| 31 SessionUpdatedPromise( |
| 32 scoped_ptr<media::CdmChangeSessionPromise> caller_promise, |
| 33 base::Callback<void()> resolve_cb) |
| 34 : CdmPromise(resolve_cb, |
| 35 base::Bind(&media::CdmChangeSessionPromise::reject, |
| 36 base::Unretained(caller_promise.get()))), |
| 37 caller_promise_(caller_promise.Pass()) {} |
| 38 |
| 39 virtual void resolve() OVERRIDE { |
| 40 media::CdmChangeSessionPromise::resolve(); |
| 41 caller_promise_->resolve(); |
| 42 } |
| 43 |
| 44 protected: |
| 45 scoped_ptr<media::CdmChangeSessionPromise> caller_promise_; |
| 46 }; |
| 47 |
| 26 scoped_ptr<PpapiDecryptor> PpapiDecryptor::Create( | 48 scoped_ptr<PpapiDecryptor> PpapiDecryptor::Create( |
| 27 const std::string& key_system, | 49 const std::string& key_system, |
| 28 const GURL& security_origin, | 50 const GURL& security_origin, |
| 29 const CreatePepperCdmCB& create_pepper_cdm_cb, | 51 const CreatePepperCdmCB& create_pepper_cdm_cb, |
| 30 const media::SessionCreatedCB& session_created_cb, | |
| 31 const media::SessionMessageCB& session_message_cb, | 52 const media::SessionMessageCB& session_message_cb, |
| 32 const media::SessionReadyCB& session_ready_cb, | 53 const media::SessionReadyCB& session_ready_cb, |
| 33 const media::SessionClosedCB& session_closed_cb, | 54 const media::SessionClosedCB& session_closed_cb, |
| 34 const media::SessionErrorCB& session_error_cb) { | 55 const media::SessionErrorCB& session_error_cb) { |
| 35 std::string plugin_type = GetPepperType(key_system); | 56 std::string plugin_type = GetPepperType(key_system); |
| 36 DCHECK(!plugin_type.empty()); | 57 DCHECK(!plugin_type.empty()); |
| 37 scoped_ptr<PepperCdmWrapper> pepper_cdm_wrapper = | 58 scoped_ptr<PepperCdmWrapper> pepper_cdm_wrapper = |
| 38 create_pepper_cdm_cb.Run(plugin_type, security_origin); | 59 create_pepper_cdm_cb.Run(plugin_type, security_origin); |
| 39 if (!pepper_cdm_wrapper) { | 60 if (!pepper_cdm_wrapper) { |
| 40 DLOG(ERROR) << "Plugin instance creation failed."; | 61 DLOG(ERROR) << "Plugin instance creation failed."; |
| 41 return scoped_ptr<PpapiDecryptor>(); | 62 return scoped_ptr<PpapiDecryptor>(); |
| 42 } | 63 } |
| 43 | 64 |
| 44 return scoped_ptr<PpapiDecryptor>( | 65 return scoped_ptr<PpapiDecryptor>( |
| 45 new PpapiDecryptor(key_system, | 66 new PpapiDecryptor(key_system, |
| 46 pepper_cdm_wrapper.Pass(), | 67 pepper_cdm_wrapper.Pass(), |
| 47 session_created_cb, | |
| 48 session_message_cb, | 68 session_message_cb, |
| 49 session_ready_cb, | 69 session_ready_cb, |
| 50 session_closed_cb, | 70 session_closed_cb, |
| 51 session_error_cb)); | 71 session_error_cb)); |
| 52 } | 72 } |
| 53 | 73 |
| 54 PpapiDecryptor::PpapiDecryptor( | 74 PpapiDecryptor::PpapiDecryptor( |
| 55 const std::string& key_system, | 75 const std::string& key_system, |
| 56 scoped_ptr<PepperCdmWrapper> pepper_cdm_wrapper, | 76 scoped_ptr<PepperCdmWrapper> pepper_cdm_wrapper, |
| 57 const media::SessionCreatedCB& session_created_cb, | |
| 58 const media::SessionMessageCB& session_message_cb, | 77 const media::SessionMessageCB& session_message_cb, |
| 59 const media::SessionReadyCB& session_ready_cb, | 78 const media::SessionReadyCB& session_ready_cb, |
| 60 const media::SessionClosedCB& session_closed_cb, | 79 const media::SessionClosedCB& session_closed_cb, |
| 61 const media::SessionErrorCB& session_error_cb) | 80 const media::SessionErrorCB& session_error_cb) |
| 62 : pepper_cdm_wrapper_(pepper_cdm_wrapper.Pass()), | 81 : pepper_cdm_wrapper_(pepper_cdm_wrapper.Pass()), |
| 63 session_created_cb_(session_created_cb), | |
| 64 session_message_cb_(session_message_cb), | 82 session_message_cb_(session_message_cb), |
| 65 session_ready_cb_(session_ready_cb), | 83 session_ready_cb_(session_ready_cb), |
| 66 session_closed_cb_(session_closed_cb), | 84 session_closed_cb_(session_closed_cb), |
| 67 session_error_cb_(session_error_cb), | 85 session_error_cb_(session_error_cb), |
| 68 render_loop_proxy_(base::MessageLoopProxy::current()), | 86 render_loop_proxy_(base::MessageLoopProxy::current()), |
| 69 weak_ptr_factory_(this) { | 87 weak_ptr_factory_(this) { |
| 70 DCHECK(pepper_cdm_wrapper_.get()); | 88 DCHECK(pepper_cdm_wrapper_.get()); |
| 71 DCHECK(!session_created_cb_.is_null()); | |
| 72 DCHECK(!session_message_cb_.is_null()); | 89 DCHECK(!session_message_cb_.is_null()); |
| 73 DCHECK(!session_ready_cb_.is_null()); | 90 DCHECK(!session_ready_cb_.is_null()); |
| 74 DCHECK(!session_closed_cb_.is_null()); | 91 DCHECK(!session_closed_cb_.is_null()); |
| 75 DCHECK(!session_error_cb_.is_null()); | 92 DCHECK(!session_error_cb_.is_null()); |
| 76 | 93 |
| 77 base::WeakPtr<PpapiDecryptor> weak_this = weak_ptr_factory_.GetWeakPtr(); | 94 base::WeakPtr<PpapiDecryptor> weak_this = weak_ptr_factory_.GetWeakPtr(); |
| 78 CdmDelegate()->Initialize( | 95 CdmDelegate()->Initialize( |
| 79 key_system, | 96 key_system, |
| 80 base::Bind(&PpapiDecryptor::OnSessionCreated, weak_this), | |
| 81 base::Bind(&PpapiDecryptor::OnSessionMessage, weak_this), | 97 base::Bind(&PpapiDecryptor::OnSessionMessage, weak_this), |
| 82 base::Bind(&PpapiDecryptor::OnSessionReady, weak_this), | 98 base::Bind(&PpapiDecryptor::OnSessionReady, weak_this), |
| 83 base::Bind(&PpapiDecryptor::OnSessionClosed, weak_this), | 99 base::Bind(&PpapiDecryptor::OnSessionClosed, weak_this), |
| 84 base::Bind(&PpapiDecryptor::OnSessionError, weak_this), | 100 base::Bind(&PpapiDecryptor::OnSessionError, weak_this), |
| 85 base::Bind(&PpapiDecryptor::OnFatalPluginError, weak_this)); | 101 base::Bind(&PpapiDecryptor::OnFatalPluginError, weak_this)); |
| 86 } | 102 } |
| 87 | 103 |
| 88 PpapiDecryptor::~PpapiDecryptor() { | 104 PpapiDecryptor::~PpapiDecryptor() { |
| 89 pepper_cdm_wrapper_.reset(); | 105 pepper_cdm_wrapper_.reset(); |
| 90 } | 106 } |
| 91 | 107 |
| 92 bool PpapiDecryptor::CreateSession(uint32 session_id, | 108 void PpapiDecryptor::CreateSession( |
| 93 const std::string& content_type, | 109 const std::string& init_data_type, |
| 94 const uint8* init_data, | 110 const uint8* init_data, |
| 95 int init_data_length) { | 111 int init_data_length, |
| 96 DVLOG(2) << __FUNCTION__; | 112 SessionType session_type, |
| 97 DCHECK(render_loop_proxy_->BelongsToCurrentThread()); | 113 scoped_ptr<media::CdmNewSessionPromise> promise) { |
| 98 | |
| 99 if (!CdmDelegate() || | |
| 100 !CdmDelegate()->CreateSession( | |
| 101 session_id, content_type, init_data, init_data_length)) { | |
| 102 ReportFailureToCallPlugin(session_id); | |
| 103 return false; | |
| 104 } | |
| 105 | |
| 106 return true; | |
| 107 } | |
| 108 | |
| 109 void PpapiDecryptor::LoadSession(uint32 session_id, | |
| 110 const std::string& web_session_id) { | |
| 111 DVLOG(2) << __FUNCTION__; | 114 DVLOG(2) << __FUNCTION__; |
| 112 DCHECK(render_loop_proxy_->BelongsToCurrentThread()); | 115 DCHECK(render_loop_proxy_->BelongsToCurrentThread()); |
| 113 | 116 |
| 114 if (!CdmDelegate()) { | 117 if (!CdmDelegate()) { |
| 115 ReportFailureToCallPlugin(session_id); | 118 promise->reject(MEDIA_KEYS_EXCEPTION_INVALID_STATE_ERROR, |
| 119 0, |
| 120 "CdmDelegate() does not exist."); |
| 121 promise.reset(); |
| 116 return; | 122 return; |
| 117 } | 123 } |
| 118 | 124 |
| 119 CdmDelegate()->LoadSession(session_id, web_session_id); | 125 CdmDelegate()->CreateSession(init_data_type, |
| 126 init_data, |
| 127 init_data_length, |
| 128 session_type, |
| 129 promise.Pass()); |
| 120 } | 130 } |
| 121 | 131 |
| 122 void PpapiDecryptor::UpdateSession(uint32 session_id, | 132 void PpapiDecryptor::LoadSession( |
| 123 const uint8* response, | 133 const std::string& web_session_id, |
| 124 int response_length) { | 134 scoped_ptr<media::CdmNewSessionPromise> promise) { |
| 125 DVLOG(2) << __FUNCTION__; | 135 DVLOG(2) << __FUNCTION__; |
| 126 DCHECK(render_loop_proxy_->BelongsToCurrentThread()); | 136 DCHECK(render_loop_proxy_->BelongsToCurrentThread()); |
| 127 | 137 |
| 128 if (!CdmDelegate() || | 138 if (!CdmDelegate()) { |
| 129 !CdmDelegate()->UpdateSession(session_id, response, response_length)) { | 139 promise->reject(MEDIA_KEYS_EXCEPTION_INVALID_STATE_ERROR, |
| 130 ReportFailureToCallPlugin(session_id); | 140 0, |
| 141 "CdmDelegate() does not exist."); |
| 142 promise.reset(); |
| 131 return; | 143 return; |
| 132 } | 144 } |
| 145 |
| 146 CdmDelegate()->LoadSession(web_session_id, promise.Pass()); |
| 133 } | 147 } |
| 134 | 148 |
| 135 void PpapiDecryptor::ReleaseSession(uint32 session_id) { | 149 void PpapiDecryptor::UpdateSession( |
| 136 DVLOG(2) << __FUNCTION__; | 150 const std::string& web_session_id, |
| 151 const uint8* response, |
| 152 int response_length, |
| 153 scoped_ptr<media::CdmChangeSessionPromise> promise) { |
| 137 DCHECK(render_loop_proxy_->BelongsToCurrentThread()); | 154 DCHECK(render_loop_proxy_->BelongsToCurrentThread()); |
| 138 | 155 |
| 139 if (!CdmDelegate() || !CdmDelegate()->ReleaseSession(session_id)) { | 156 if (!CdmDelegate()) { |
| 140 ReportFailureToCallPlugin(session_id); | 157 promise->reject(MEDIA_KEYS_EXCEPTION_INVALID_STATE_ERROR, |
| 158 0, |
| 159 "CdmDelegate() does not exist."); |
| 160 promise.reset(); |
| 141 return; | 161 return; |
| 142 } | 162 } |
| 163 |
| 164 scoped_ptr<SessionUpdatedPromise> update_promise( |
| 165 new SessionUpdatedPromise(promise.Pass(), |
| 166 base::Bind(&PpapiDecryptor::ResumePlayback, |
| 167 weak_ptr_factory_.GetWeakPtr()))); |
| 168 CdmDelegate()->UpdateSession( |
| 169 web_session_id, |
| 170 response, |
| 171 response_length, |
| 172 update_promise.PassAs<media::CdmChangeSessionPromise>()); |
| 173 } |
| 174 |
| 175 void PpapiDecryptor::ReleaseSession( |
| 176 const std::string& web_session_id, |
| 177 scoped_ptr<media::CdmChangeSessionPromise> promise) { |
| 178 DCHECK(render_loop_proxy_->BelongsToCurrentThread()); |
| 179 |
| 180 if (!CdmDelegate()) { |
| 181 promise->reject(MEDIA_KEYS_EXCEPTION_INVALID_STATE_ERROR, |
| 182 0, |
| 183 "CdmDelegate() does not exist."); |
| 184 promise.reset(); |
| 185 return; |
| 186 } |
| 187 |
| 188 CdmDelegate()->ReleaseSession(web_session_id, promise.Pass()); |
| 143 } | 189 } |
| 144 | 190 |
| 145 media::Decryptor* PpapiDecryptor::GetDecryptor() { | 191 media::Decryptor* PpapiDecryptor::GetDecryptor() { |
| 146 return this; | 192 return this; |
| 147 } | 193 } |
| 148 | 194 |
| 149 void PpapiDecryptor::RegisterNewKeyCB(StreamType stream_type, | 195 void PpapiDecryptor::RegisterNewKeyCB(StreamType stream_type, |
| 150 const NewKeyCB& new_key_cb) { | 196 const NewKeyCB& new_key_cb) { |
| 151 if (!render_loop_proxy_->BelongsToCurrentThread()) { | 197 if (!render_loop_proxy_->BelongsToCurrentThread()) { |
| 152 render_loop_proxy_->PostTask(FROM_HERE, | 198 render_loop_proxy_->PostTask(FROM_HERE, |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 weak_ptr_factory_.GetWeakPtr(), | 369 weak_ptr_factory_.GetWeakPtr(), |
| 324 stream_type)); | 370 stream_type)); |
| 325 return; | 371 return; |
| 326 } | 372 } |
| 327 | 373 |
| 328 DVLOG(2) << __FUNCTION__ << " - stream_type: " << stream_type; | 374 DVLOG(2) << __FUNCTION__ << " - stream_type: " << stream_type; |
| 329 if (CdmDelegate()) | 375 if (CdmDelegate()) |
| 330 CdmDelegate()->DeinitializeDecoder(stream_type); | 376 CdmDelegate()->DeinitializeDecoder(stream_type); |
| 331 } | 377 } |
| 332 | 378 |
| 333 void PpapiDecryptor::ReportFailureToCallPlugin(uint32 session_id) { | |
| 334 DCHECK(render_loop_proxy_->BelongsToCurrentThread()); | |
| 335 DVLOG(1) << "Failed to call plugin."; | |
| 336 session_error_cb_.Run(session_id, kUnknownError, 0); | |
| 337 } | |
| 338 | |
| 339 void PpapiDecryptor::OnDecoderInitialized(StreamType stream_type, | 379 void PpapiDecryptor::OnDecoderInitialized(StreamType stream_type, |
| 340 bool success) { | 380 bool success) { |
| 341 DCHECK(render_loop_proxy_->BelongsToCurrentThread()); | 381 DCHECK(render_loop_proxy_->BelongsToCurrentThread()); |
| 342 switch (stream_type) { | 382 switch (stream_type) { |
| 343 case kAudio: | 383 case kAudio: |
| 344 DCHECK(!audio_decoder_init_cb_.is_null()); | 384 DCHECK(!audio_decoder_init_cb_.is_null()); |
| 345 base::ResetAndReturn(&audio_decoder_init_cb_).Run(success); | 385 base::ResetAndReturn(&audio_decoder_init_cb_).Run(success); |
| 346 break; | 386 break; |
| 347 case kVideo: | 387 case kVideo: |
| 348 DCHECK(!video_decoder_init_cb_.is_null()); | 388 DCHECK(!video_decoder_init_cb_.is_null()); |
| 349 base::ResetAndReturn(&video_decoder_init_cb_).Run(success); | 389 base::ResetAndReturn(&video_decoder_init_cb_).Run(success); |
| 350 break; | 390 break; |
| 351 default: | 391 default: |
| 352 NOTREACHED(); | 392 NOTREACHED(); |
| 353 } | 393 } |
| 354 } | 394 } |
| 355 | 395 |
| 356 void PpapiDecryptor::OnSessionCreated(uint32 session_id, | 396 void PpapiDecryptor::OnSessionMessage(const std::string& web_session_id, |
| 357 const std::string& web_session_id) { | |
| 358 DCHECK(render_loop_proxy_->BelongsToCurrentThread()); | |
| 359 session_created_cb_.Run(session_id, web_session_id); | |
| 360 } | |
| 361 | |
| 362 void PpapiDecryptor::OnSessionMessage(uint32 session_id, | |
| 363 const std::vector<uint8>& message, | 397 const std::vector<uint8>& message, |
| 364 const std::string& destination_url) { | 398 const std::string& destination_url) { |
| 365 DCHECK(render_loop_proxy_->BelongsToCurrentThread()); | 399 DCHECK(render_loop_proxy_->BelongsToCurrentThread()); |
| 366 session_message_cb_.Run(session_id, message, destination_url); | 400 session_message_cb_.Run(web_session_id, message, destination_url); |
| 367 } | 401 } |
| 368 | 402 |
| 369 void PpapiDecryptor::OnSessionReady(uint32 session_id) { | 403 void PpapiDecryptor::OnSessionReady(const std::string& web_session_id) { |
| 370 DCHECK(render_loop_proxy_->BelongsToCurrentThread()); | 404 DCHECK(render_loop_proxy_->BelongsToCurrentThread()); |
| 371 | 405 |
| 406 ResumePlayback(); |
| 407 session_ready_cb_.Run(web_session_id); |
| 408 } |
| 409 |
| 410 void PpapiDecryptor::OnSessionClosed(const std::string& web_session_id) { |
| 411 DCHECK(render_loop_proxy_->BelongsToCurrentThread()); |
| 412 session_closed_cb_.Run(web_session_id); |
| 413 } |
| 414 |
| 415 void PpapiDecryptor::OnSessionError( |
| 416 const std::string& web_session_id, |
| 417 MediaKeys::MediaKeysException exception_code, |
| 418 uint32 system_code, |
| 419 const std::string& error_description) { |
| 420 DCHECK(render_loop_proxy_->BelongsToCurrentThread()); |
| 421 session_error_cb_.Run( |
| 422 web_session_id, exception_code, system_code, error_description); |
| 423 } |
| 424 |
| 425 void PpapiDecryptor::ResumePlayback() { |
| 372 // Based on the spec, we need to resume playback when update() completes | 426 // Based on the spec, we need to resume playback when update() completes |
| 373 // successfully, or when a session is successfully loaded. In both cases, | 427 // successfully, or when a session is successfully loaded (triggered by |
| 374 // the CDM fires OnSessionReady() event. So we choose to call the NewKeyCBs | 428 // OnSessionReady()). So we choose to call the NewKeyCBs here. |
| 375 // here. | |
| 376 // TODO(xhwang): Rename OnSessionReady to indicate that the playback may | |
| 377 // resume successfully (e.g. a new key is available or available again). | |
| 378 if (!new_audio_key_cb_.is_null()) | 429 if (!new_audio_key_cb_.is_null()) |
| 379 new_audio_key_cb_.Run(); | 430 new_audio_key_cb_.Run(); |
| 380 | 431 |
| 381 if (!new_video_key_cb_.is_null()) | 432 if (!new_video_key_cb_.is_null()) |
| 382 new_video_key_cb_.Run(); | 433 new_video_key_cb_.Run(); |
| 383 | |
| 384 session_ready_cb_.Run(session_id); | |
| 385 } | |
| 386 | |
| 387 void PpapiDecryptor::OnSessionClosed(uint32 session_id) { | |
| 388 DCHECK(render_loop_proxy_->BelongsToCurrentThread()); | |
| 389 session_closed_cb_.Run(session_id); | |
| 390 } | |
| 391 | |
| 392 void PpapiDecryptor::OnSessionError(uint32 session_id, | |
| 393 media::MediaKeys::KeyError error_code, | |
| 394 uint32 system_code) { | |
| 395 DCHECK(render_loop_proxy_->BelongsToCurrentThread()); | |
| 396 session_error_cb_.Run(session_id, error_code, system_code); | |
| 397 } | 434 } |
| 398 | 435 |
| 399 void PpapiDecryptor::OnFatalPluginError() { | 436 void PpapiDecryptor::OnFatalPluginError() { |
| 400 DCHECK(render_loop_proxy_->BelongsToCurrentThread()); | 437 DCHECK(render_loop_proxy_->BelongsToCurrentThread()); |
| 401 pepper_cdm_wrapper_.reset(); | 438 pepper_cdm_wrapper_.reset(); |
| 402 } | 439 } |
| 403 | 440 |
| 404 ContentDecryptorDelegate* PpapiDecryptor::CdmDelegate() { | 441 ContentDecryptorDelegate* PpapiDecryptor::CdmDelegate() { |
| 405 DCHECK(render_loop_proxy_->BelongsToCurrentThread()); | 442 DCHECK(render_loop_proxy_->BelongsToCurrentThread()); |
| 406 return (pepper_cdm_wrapper_) ? pepper_cdm_wrapper_->GetCdmDelegate() : NULL; | 443 return (pepper_cdm_wrapper_) ? pepper_cdm_wrapper_->GetCdmDelegate() : NULL; |
| 407 } | 444 } |
| 408 | 445 |
| 409 } // namespace content | 446 } // namespace content |
| OLD | NEW |