OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/pepper/content_decryptor_delegate.h" | 5 #include "content/renderer/pepper/content_decryptor_delegate.h" |
6 | 6 |
7 #include "base/callback_helpers.h" | 7 #include "base/callback_helpers.h" |
8 #include "base/debug/trace_event.h" | 8 #include "base/debug/trace_event.h" |
9 #include "base/message_loop/message_loop_proxy.h" | 9 #include "base/message_loop/message_loop_proxy.h" |
10 #include "base/numerics/safe_conversions.h" | 10 #include "base/numerics/safe_conversions.h" |
11 #include "content/renderer/pepper/ppb_buffer_impl.h" | 11 #include "content/renderer/pepper/ppb_buffer_impl.h" |
12 #include "media/base/audio_buffer.h" | 12 #include "media/base/audio_buffer.h" |
13 #include "media/base/audio_decoder_config.h" | 13 #include "media/base/audio_decoder_config.h" |
14 #include "media/base/bind_to_current_loop.h" | 14 #include "media/base/bind_to_current_loop.h" |
15 #include "media/base/channel_layout.h" | 15 #include "media/base/channel_layout.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/decrypt_config.h" | 18 #include "media/base/decrypt_config.h" |
| 19 #include "media/base/media_keys_session_promise.h" |
19 #include "media/base/video_decoder_config.h" | 20 #include "media/base/video_decoder_config.h" |
20 #include "media/base/video_frame.h" | 21 #include "media/base/video_frame.h" |
21 #include "media/base/video_util.h" | 22 #include "media/base/video_util.h" |
22 #include "ppapi/shared_impl/scoped_pp_resource.h" | 23 #include "ppapi/shared_impl/scoped_pp_resource.h" |
23 #include "ppapi/shared_impl/var.h" | 24 #include "ppapi/shared_impl/var.h" |
24 #include "ppapi/shared_impl/var_tracker.h" | 25 #include "ppapi/shared_impl/var_tracker.h" |
25 #include "ppapi/thunk/enter.h" | 26 #include "ppapi/thunk/enter.h" |
26 #include "ppapi/thunk/ppb_buffer_api.h" | 27 #include "ppapi/thunk/ppb_buffer_api.h" |
27 #include "ui/gfx/rect.h" | 28 #include "ui/gfx/rect.h" |
28 | 29 |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 case PP_DECRYPTEDSAMPLEFORMAT_PLANAR_S16: | 239 case PP_DECRYPTEDSAMPLEFORMAT_PLANAR_S16: |
239 return media::kSampleFormatPlanarS16; | 240 return media::kSampleFormatPlanarS16; |
240 case PP_DECRYPTEDSAMPLEFORMAT_PLANAR_F32: | 241 case PP_DECRYPTEDSAMPLEFORMAT_PLANAR_F32: |
241 return media::kSampleFormatPlanarF32; | 242 return media::kSampleFormatPlanarF32; |
242 default: | 243 default: |
243 NOTREACHED(); | 244 NOTREACHED(); |
244 return media::kUnknownSampleFormat; | 245 return media::kUnknownSampleFormat; |
245 } | 246 } |
246 } | 247 } |
247 | 248 |
| 249 PP_SessionType MediaSessionTypeToPpSessionType( |
| 250 media::MediaKeys::SessionType session_type) { |
| 251 switch (session_type) { |
| 252 case media::MediaKeys::SessionType::kTemporary: |
| 253 return PP_SESSIONTYPE_TEMPORARY; |
| 254 case media::MediaKeys::SessionType::kPersistent: |
| 255 return PP_SESSIONTYPE_PERSISTENT; |
| 256 default: |
| 257 NOTREACHED(); |
| 258 return PP_SESSIONTYPE_TEMPORARY; |
| 259 } |
| 260 } |
| 261 |
248 } // namespace | 262 } // namespace |
249 | 263 |
250 ContentDecryptorDelegate::ContentDecryptorDelegate( | 264 ContentDecryptorDelegate::ContentDecryptorDelegate( |
251 PP_Instance pp_instance, | 265 PP_Instance pp_instance, |
252 const PPP_ContentDecryptor_Private* plugin_decryption_interface) | 266 const PPP_ContentDecryptor_Private* plugin_decryption_interface) |
253 : pp_instance_(pp_instance), | 267 : pp_instance_(pp_instance), |
254 plugin_decryption_interface_(plugin_decryption_interface), | 268 plugin_decryption_interface_(plugin_decryption_interface), |
255 next_decryption_request_id_(1), | 269 next_decryption_request_id_(1), |
256 audio_samples_per_second_(0), | 270 audio_samples_per_second_(0), |
257 audio_channel_count_(0), | 271 audio_channel_count_(0), |
258 audio_channel_layout_(media::CHANNEL_LAYOUT_NONE), | 272 audio_channel_layout_(media::CHANNEL_LAYOUT_NONE), |
| 273 next_promise_id_(0), |
259 weak_ptr_factory_(this) { | 274 weak_ptr_factory_(this) { |
260 weak_this_ = weak_ptr_factory_.GetWeakPtr(); | 275 weak_this_ = weak_ptr_factory_.GetWeakPtr(); |
261 } | 276 } |
262 | 277 |
263 ContentDecryptorDelegate::~ContentDecryptorDelegate() { | 278 ContentDecryptorDelegate::~ContentDecryptorDelegate() { |
264 SatisfyAllPendingCallbacksOnError(); | 279 SatisfyAllPendingCallbacksOnError(); |
265 } | 280 } |
266 | 281 |
267 void ContentDecryptorDelegate::Initialize( | 282 void ContentDecryptorDelegate::Initialize( |
268 const std::string& key_system, | 283 const std::string& key_system, |
269 const media::SessionCreatedCB& session_created_cb, | |
270 const media::SessionMessageCB& session_message_cb, | 284 const media::SessionMessageCB& session_message_cb, |
271 const media::SessionReadyCB& session_ready_cb, | 285 const media::SessionReadyCB& session_ready_cb, |
272 const media::SessionClosedCB& session_closed_cb, | 286 const media::SessionClosedCB& session_closed_cb, |
273 const media::SessionErrorCB& session_error_cb, | 287 const media::SessionErrorCB& session_error_cb, |
274 const base::Closure& fatal_plugin_error_cb) { | 288 const base::Closure& fatal_plugin_error_cb) { |
275 DCHECK(!key_system.empty()); | 289 DCHECK(!key_system.empty()); |
276 DCHECK(key_system_.empty()); | 290 DCHECK(key_system_.empty()); |
277 key_system_ = key_system; | 291 key_system_ = key_system; |
278 | 292 |
279 session_created_cb_ = session_created_cb; | |
280 session_message_cb_ = session_message_cb; | 293 session_message_cb_ = session_message_cb; |
281 session_ready_cb_ = session_ready_cb; | 294 session_ready_cb_ = session_ready_cb; |
282 session_closed_cb_ = session_closed_cb; | 295 session_closed_cb_ = session_closed_cb; |
283 session_error_cb_ = session_error_cb; | 296 session_error_cb_ = session_error_cb; |
284 fatal_plugin_error_cb_ = fatal_plugin_error_cb; | 297 fatal_plugin_error_cb_ = fatal_plugin_error_cb; |
285 | 298 |
286 plugin_decryption_interface_->Initialize( | 299 plugin_decryption_interface_->Initialize( |
287 pp_instance_, StringVar::StringToPPVar(key_system_)); | 300 pp_instance_, StringVar::StringToPPVar(key_system_)); |
288 } | 301 } |
289 | 302 |
290 void ContentDecryptorDelegate::InstanceCrashed() { | 303 void ContentDecryptorDelegate::InstanceCrashed() { |
291 fatal_plugin_error_cb_.Run(); | 304 fatal_plugin_error_cb_.Run(); |
292 SatisfyAllPendingCallbacksOnError(); | 305 SatisfyAllPendingCallbacksOnError(); |
293 } | 306 } |
294 | 307 |
295 bool ContentDecryptorDelegate::CreateSession(uint32 session_id, | 308 void ContentDecryptorDelegate::CreateSession( |
296 const std::string& content_type, | 309 const std::string& init_data_type, |
297 const uint8* init_data, | 310 const uint8* init_data, |
298 int init_data_length) { | 311 int init_data_length, |
| 312 media::MediaKeys::SessionType session_type, |
| 313 scoped_ptr<media::MediaKeysSessionPromise> promise) { |
| 314 uint32_t promise_id = SavePromise(promise.Pass()); |
299 PP_Var init_data_array = | 315 PP_Var init_data_array = |
300 PpapiGlobals::Get()->GetVarTracker()->MakeArrayBufferPPVar( | 316 PpapiGlobals::Get()->GetVarTracker()->MakeArrayBufferPPVar( |
301 init_data_length, init_data); | 317 init_data_length, init_data); |
302 | |
303 plugin_decryption_interface_->CreateSession( | 318 plugin_decryption_interface_->CreateSession( |
304 pp_instance_, | 319 pp_instance_, |
305 session_id, | 320 promise_id, |
306 StringVar::StringToPPVar(content_type), | 321 StringVar::StringToPPVar(init_data_type), |
307 init_data_array); | 322 init_data_array, |
308 return true; | 323 MediaSessionTypeToPpSessionType(session_type)); |
309 } | 324 } |
310 | 325 |
311 void ContentDecryptorDelegate::LoadSession(uint32 session_id, | 326 void ContentDecryptorDelegate::LoadSession( |
312 const std::string& web_session_id) { | 327 const std::string& web_session_id, |
| 328 scoped_ptr<media::MediaKeysSessionPromise> promise) { |
| 329 uint32_t promise_id = SavePromise(promise.Pass()); |
313 plugin_decryption_interface_->LoadSession( | 330 plugin_decryption_interface_->LoadSession( |
314 pp_instance_, session_id, StringVar::StringToPPVar(web_session_id)); | 331 pp_instance_, promise_id, StringVar::StringToPPVar(web_session_id)); |
315 } | 332 } |
316 | 333 |
317 bool ContentDecryptorDelegate::UpdateSession(uint32 session_id, | 334 void ContentDecryptorDelegate::UpdateSession( |
318 const uint8* response, | 335 const std::string& web_session_id, |
319 int response_length) { | 336 const uint8* response, |
| 337 int response_length, |
| 338 scoped_ptr<media::MediaKeysSessionPromise> promise) { |
| 339 uint32_t promise_id = SavePromise(promise.Pass()); |
320 PP_Var response_array = | 340 PP_Var response_array = |
321 PpapiGlobals::Get()->GetVarTracker()->MakeArrayBufferPPVar( | 341 PpapiGlobals::Get()->GetVarTracker()->MakeArrayBufferPPVar( |
322 response_length, response); | 342 response_length, response); |
323 plugin_decryption_interface_->UpdateSession( | 343 plugin_decryption_interface_->UpdateSession( |
324 pp_instance_, session_id, response_array); | 344 pp_instance_, |
325 return true; | 345 promise_id, |
| 346 StringVar::StringToPPVar(web_session_id), |
| 347 response_array); |
326 } | 348 } |
327 | 349 |
328 bool ContentDecryptorDelegate::ReleaseSession(uint32 session_id) { | 350 void ContentDecryptorDelegate::ReleaseSession( |
329 plugin_decryption_interface_->ReleaseSession(pp_instance_, session_id); | 351 const std::string& web_session_id, |
330 return true; | 352 scoped_ptr<media::MediaKeysSessionPromise> promise) { |
| 353 uint32_t promise_id = SavePromise(promise.Pass()); |
| 354 plugin_decryption_interface_->ReleaseSession( |
| 355 pp_instance_, promise_id, StringVar::StringToPPVar(web_session_id)); |
331 } | 356 } |
332 | 357 |
333 // TODO(xhwang): Remove duplication of code in Decrypt(), | 358 // TODO(xhwang): Remove duplication of code in Decrypt(), |
334 // DecryptAndDecodeAudio() and DecryptAndDecodeVideo(). | 359 // DecryptAndDecodeAudio() and DecryptAndDecodeVideo(). |
335 bool ContentDecryptorDelegate::Decrypt( | 360 bool ContentDecryptorDelegate::Decrypt( |
336 Decryptor::StreamType stream_type, | 361 Decryptor::StreamType stream_type, |
337 const scoped_refptr<media::DecoderBuffer>& encrypted_buffer, | 362 const scoped_refptr<media::DecoderBuffer>& encrypted_buffer, |
338 const Decryptor::DecryptCB& decrypt_cb) { | 363 const Decryptor::DecryptCB& decrypt_cb) { |
339 DVLOG(3) << "Decrypt() - stream_type: " << stream_type; | 364 DVLOG(3) << "Decrypt() - stream_type: " << stream_type; |
340 | 365 |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
566 // buffer. | 591 // buffer. |
567 video_decode_cb_.Set(request_id, video_decode_cb); | 592 video_decode_cb_.Set(request_id, video_decode_cb); |
568 | 593 |
569 // TODO(tomfinegan): Need to get stream type from media stack. | 594 // TODO(tomfinegan): Need to get stream type from media stack. |
570 ScopedPPResource pp_resource(encrypted_resource.get()); | 595 ScopedPPResource pp_resource(encrypted_resource.get()); |
571 plugin_decryption_interface_->DecryptAndDecode( | 596 plugin_decryption_interface_->DecryptAndDecode( |
572 pp_instance_, PP_DECRYPTORSTREAMTYPE_VIDEO, pp_resource, &block_info); | 597 pp_instance_, PP_DECRYPTORSTREAMTYPE_VIDEO, pp_resource, &block_info); |
573 return true; | 598 return true; |
574 } | 599 } |
575 | 600 |
576 void ContentDecryptorDelegate::OnSessionCreated(uint32 session_id, | 601 void ContentDecryptorDelegate::OnPromiseResolved(uint32 promise_id) { |
577 PP_Var web_session_id_var) { | 602 scoped_ptr<media::MediaKeysSessionPromise> promise = FindPromise(promise_id); |
578 if (session_created_cb_.is_null()) | 603 promise->resolve(); |
579 return; | 604 promise.reset(); |
580 | |
581 StringVar* session_id_string = StringVar::FromPPVar(web_session_id_var); | |
582 | |
583 if (!session_id_string) { | |
584 OnSessionError(session_id, media::MediaKeys::kUnknownError, 0); | |
585 return; | |
586 } | |
587 | |
588 session_created_cb_.Run(session_id, session_id_string->value()); | |
589 } | 605 } |
590 | 606 |
591 void ContentDecryptorDelegate::OnSessionMessage(uint32 session_id, | 607 void ContentDecryptorDelegate::OnPromiseResolvedWithSession( |
| 608 uint32 promise_id, |
| 609 PP_Var web_session_id_var) { |
| 610 scoped_ptr<media::MediaKeysSessionPromise> promise = FindPromise(promise_id); |
| 611 |
| 612 StringVar* web_session_id = StringVar::FromPPVar(web_session_id_var); |
| 613 DCHECK(web_session_id); |
| 614 |
| 615 promise->resolve(web_session_id->value()); |
| 616 promise.reset(); |
| 617 } |
| 618 |
| 619 void ContentDecryptorDelegate::OnPromiseRejected(uint32 promise_id, |
| 620 PP_Var error_name_var, |
| 621 uint32 system_code, |
| 622 PP_Var error_description_var) { |
| 623 scoped_ptr<media::MediaKeysSessionPromise> promise = FindPromise(promise_id); |
| 624 |
| 625 StringVar* error_name = StringVar::FromPPVar(error_name_var); |
| 626 DCHECK(error_name); |
| 627 |
| 628 StringVar* error_description = StringVar::FromPPVar(error_description_var); |
| 629 DCHECK(error_description); |
| 630 |
| 631 promise->reject(error_name->value(), system_code, error_description->value()); |
| 632 promise.reset(); |
| 633 } |
| 634 |
| 635 void ContentDecryptorDelegate::OnSessionMessage(PP_Var web_session_id_var, |
592 PP_Var message_var, | 636 PP_Var message_var, |
593 PP_Var default_url_var) { | 637 PP_Var destination_url_var) { |
594 if (session_message_cb_.is_null()) | 638 if (session_message_cb_.is_null()) |
595 return; | 639 return; |
596 | 640 |
| 641 StringVar* web_session_id = StringVar::FromPPVar(web_session_id_var); |
| 642 DCHECK(web_session_id); |
| 643 |
597 ArrayBufferVar* message_array_buffer = ArrayBufferVar::FromPPVar(message_var); | 644 ArrayBufferVar* message_array_buffer = ArrayBufferVar::FromPPVar(message_var); |
598 | |
599 std::vector<uint8> message; | 645 std::vector<uint8> message; |
600 if (message_array_buffer) { | 646 if (message_array_buffer) { |
601 const uint8* data = static_cast<const uint8*>(message_array_buffer->Map()); | 647 const uint8* data = static_cast<const uint8*>(message_array_buffer->Map()); |
602 message.assign(data, data + message_array_buffer->ByteLength()); | 648 message.assign(data, data + message_array_buffer->ByteLength()); |
603 } | 649 } |
604 | 650 |
605 StringVar* default_url_string = StringVar::FromPPVar(default_url_var); | 651 StringVar* destination_url_string = StringVar::FromPPVar(destination_url_var); |
| 652 DCHECK(destination_url_string); |
606 | 653 |
607 if (!default_url_string) { | 654 session_message_cb_.Run( |
608 OnSessionError(session_id, media::MediaKeys::kUnknownError, 0); | 655 web_session_id->value(), message, destination_url_string->value()); |
609 return; | |
610 } | |
611 | |
612 session_message_cb_.Run(session_id, message, default_url_string->value()); | |
613 } | 656 } |
614 | 657 |
615 void ContentDecryptorDelegate::OnSessionReady(uint32 session_id) { | 658 void ContentDecryptorDelegate::OnSessionReady(PP_Var web_session_id_var) { |
616 if (session_ready_cb_.is_null()) | 659 if (session_ready_cb_.is_null()) |
617 return; | 660 return; |
618 | 661 |
619 session_ready_cb_.Run(session_id); | 662 StringVar* web_session_id = StringVar::FromPPVar(web_session_id_var); |
| 663 DCHECK(web_session_id); |
| 664 |
| 665 session_ready_cb_.Run(web_session_id->value()); |
620 } | 666 } |
621 | 667 |
622 void ContentDecryptorDelegate::OnSessionClosed(uint32 session_id) { | 668 void ContentDecryptorDelegate::OnSessionClosed(PP_Var web_session_id_var) { |
623 if (session_closed_cb_.is_null()) | 669 if (session_closed_cb_.is_null()) |
624 return; | 670 return; |
625 | 671 |
626 session_closed_cb_.Run(session_id); | 672 StringVar* web_session_id = StringVar::FromPPVar(web_session_id_var); |
| 673 DCHECK(web_session_id); |
| 674 |
| 675 session_closed_cb_.Run(web_session_id->value()); |
627 } | 676 } |
628 | 677 |
629 void ContentDecryptorDelegate::OnSessionError(uint32 session_id, | 678 void ContentDecryptorDelegate::OnSessionError(PP_Var web_session_id_var, |
630 int32_t media_error, | 679 PP_Var error_name_var, |
631 uint32_t system_code) { | 680 uint32 system_code, |
| 681 PP_Var error_description_var) { |
632 if (session_error_cb_.is_null()) | 682 if (session_error_cb_.is_null()) |
633 return; | 683 return; |
634 | 684 |
635 session_error_cb_.Run(session_id, | 685 StringVar* web_session_id = StringVar::FromPPVar(web_session_id_var); |
636 static_cast<media::MediaKeys::KeyError>(media_error), | 686 DCHECK(web_session_id); |
637 system_code); | 687 |
| 688 StringVar* error_name = StringVar::FromPPVar(error_name_var); |
| 689 DCHECK(error_name); |
| 690 |
| 691 StringVar* error_description = StringVar::FromPPVar(error_description_var); |
| 692 DCHECK(error_description); |
| 693 |
| 694 session_error_cb_.Run(web_session_id->value(), |
| 695 error_name->value(), |
| 696 system_code, |
| 697 error_description->value()); |
638 } | 698 } |
639 | 699 |
640 void ContentDecryptorDelegate::DecoderInitializeDone( | 700 void ContentDecryptorDelegate::DecoderInitializeDone( |
641 PP_DecryptorStreamType decoder_type, | 701 PP_DecryptorStreamType decoder_type, |
642 uint32_t request_id, | 702 uint32_t request_id, |
643 PP_Bool success) { | 703 PP_Bool success) { |
644 if (decoder_type == PP_DECRYPTORSTREAMTYPE_AUDIO) { | 704 if (decoder_type == PP_DECRYPTORSTREAMTYPE_AUDIO) { |
645 // If the request ID is not valid or does not match what's saved, do | 705 // If the request ID is not valid or does not match what's saved, do |
646 // nothing. | 706 // nothing. |
647 if (request_id == 0 || !audio_decoder_init_cb_.Matches(request_id)) | 707 if (request_id == 0 || !audio_decoder_init_cb_.Matches(request_id)) |
(...skipping 409 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1057 if (!audio_decode_cb_.is_null()) { | 1117 if (!audio_decode_cb_.is_null()) { |
1058 const media::Decryptor::AudioBuffers empty_frames; | 1118 const media::Decryptor::AudioBuffers empty_frames; |
1059 audio_decode_cb_.ResetAndReturn().Run(media::Decryptor::kError, | 1119 audio_decode_cb_.ResetAndReturn().Run(media::Decryptor::kError, |
1060 empty_frames); | 1120 empty_frames); |
1061 } | 1121 } |
1062 | 1122 |
1063 if (!video_decode_cb_.is_null()) | 1123 if (!video_decode_cb_.is_null()) |
1064 video_decode_cb_.ResetAndReturn().Run(media::Decryptor::kError, NULL); | 1124 video_decode_cb_.ResetAndReturn().Run(media::Decryptor::kError, NULL); |
1065 } | 1125 } |
1066 | 1126 |
| 1127 uint32_t ContentDecryptorDelegate::SavePromise( |
| 1128 scoped_ptr<media::MediaKeysSessionPromise> promise) { |
| 1129 uint32_t promise_id = ++next_promise_id_; |
| 1130 promises_.insert(std::make_pair(promise_id, promise.release())); |
| 1131 return promise_id; |
| 1132 } |
| 1133 |
| 1134 scoped_ptr<media::MediaKeysSessionPromise> |
| 1135 ContentDecryptorDelegate::FindPromise(uint32_t promise_id) { |
| 1136 std::map<uint32_t, media::MediaKeysSessionPromise*>::iterator it = |
| 1137 promises_.find(promise_id); |
| 1138 DCHECK(it != promises_.end()); |
| 1139 scoped_ptr<media::MediaKeysSessionPromise> result(it->second); |
| 1140 promises_.erase(it); |
| 1141 return result.Pass(); |
| 1142 } |
| 1143 |
1067 } // namespace content | 1144 } // namespace content |
OLD | NEW |