Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 5 | #include "content/renderer/media/crypto/proxy_decryptor.h" |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 6 | |
| 7 | #include "base/bind.h" |
| 8 | #include "base/callback_helpers.h" |
| 9 | #include "base/logging.h" |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 10 | #include "content/renderer/media/crypto/content_decryption_module_factory.h" |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 11 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 12 | namespace content { |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 13 | |
| 14 | #if defined(ENABLE_PEPPER_CDMS) |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 15 | void ProxyDecryptor::DestroyHelperPlugin() { |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 16 | ContentDecryptionModuleFactory::DestroyHelperPlugin( |
Torne (Richard Coles) | 3551c9c | 2013-08-23 16:39:15 +0100 | [diff] [blame] | 17 | web_media_player_client_, web_frame_); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 18 | } |
| 19 | #endif // defined(ENABLE_PEPPER_CDMS) |
| 20 | |
| 21 | ProxyDecryptor::ProxyDecryptor( |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 22 | #if defined(ENABLE_PEPPER_CDMS) |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 23 | WebKit::WebMediaPlayerClient* web_media_player_client, |
| 24 | WebKit::WebFrame* web_frame, |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 25 | #elif defined(OS_ANDROID) |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 26 | WebMediaPlayerProxyAndroid* proxy, |
| 27 | int media_keys_id, |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 28 | #endif // defined(ENABLE_PEPPER_CDMS) |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 29 | const media::KeyAddedCB& key_added_cb, |
| 30 | const media::KeyErrorCB& key_error_cb, |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 31 | const media::KeyMessageCB& key_message_cb) |
| 32 | : weak_ptr_factory_(this), |
| 33 | #if defined(ENABLE_PEPPER_CDMS) |
| 34 | web_media_player_client_(web_media_player_client), |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 35 | web_frame_(web_frame), |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 36 | #elif defined(OS_ANDROID) |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 37 | proxy_(proxy), |
| 38 | media_keys_id_(media_keys_id), |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 39 | #endif // defined(ENABLE_PEPPER_CDMS) |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 40 | key_added_cb_(key_added_cb), |
| 41 | key_error_cb_(key_error_cb), |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 42 | key_message_cb_(key_message_cb) { |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | ProxyDecryptor::~ProxyDecryptor() { |
| 46 | // Destroy the decryptor explicitly before destroying the plugin. |
| 47 | { |
| 48 | base::AutoLock auto_lock(lock_); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 49 | media_keys_.reset(); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 50 | } |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | // TODO(xhwang): Support multiple decryptor notification request (e.g. from |
| 54 | // video and audio decoders). The current implementation is okay for the current |
| 55 | // media pipeline since we initialize audio and video decoders in sequence. |
| 56 | // But ProxyDecryptor should not depend on media pipeline's implementation |
| 57 | // detail. |
| 58 | void ProxyDecryptor::SetDecryptorReadyCB( |
| 59 | const media::DecryptorReadyCB& decryptor_ready_cb) { |
| 60 | base::AutoLock auto_lock(lock_); |
| 61 | |
| 62 | // Cancels the previous decryptor request. |
| 63 | if (decryptor_ready_cb.is_null()) { |
| 64 | if (!decryptor_ready_cb_.is_null()) |
| 65 | base::ResetAndReturn(&decryptor_ready_cb_).Run(NULL); |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | // Normal decryptor request. |
| 70 | DCHECK(decryptor_ready_cb_.is_null()); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 71 | if (media_keys_) { |
| 72 | decryptor_ready_cb.Run(media_keys_->GetDecryptor()); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 73 | return; |
| 74 | } |
| 75 | decryptor_ready_cb_ = decryptor_ready_cb; |
| 76 | } |
| 77 | |
Torne (Richard Coles) | 68043e1 | 2013-09-26 13:24:57 +0100 | [diff] [blame] | 78 | bool ProxyDecryptor::InitializeCDM(const std::string& key_system, |
| 79 | const GURL& frame_url) { |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 80 | DVLOG(1) << "InitializeCDM: key_system = " << key_system; |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 81 | |
| 82 | base::AutoLock auto_lock(lock_); |
| 83 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 84 | DCHECK(!media_keys_); |
Torne (Richard Coles) | 68043e1 | 2013-09-26 13:24:57 +0100 | [diff] [blame] | 85 | media_keys_ = CreateMediaKeys(key_system, frame_url); |
Torne (Richard Coles) | 4e180b6 | 2013-10-18 15:46:22 +0100 | [diff] [blame^] | 86 | if (!media_keys_) |
| 87 | return false; |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 88 | |
Torne (Richard Coles) | 4e180b6 | 2013-10-18 15:46:22 +0100 | [diff] [blame^] | 89 | if (!decryptor_ready_cb_.is_null()) |
| 90 | base::ResetAndReturn(&decryptor_ready_cb_).Run(media_keys_->GetDecryptor()); |
| 91 | return true; |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 92 | } |
| 93 | |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 94 | bool ProxyDecryptor::GenerateKeyRequest(const std::string& type, |
| 95 | const uint8* init_data, |
| 96 | int init_data_length) { |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 97 | if (!media_keys_->GenerateKeyRequest(type, init_data, init_data_length)) { |
| 98 | media_keys_.reset(); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 99 | return false; |
| 100 | } |
| 101 | |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 102 | return true; |
| 103 | } |
| 104 | |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 105 | void ProxyDecryptor::AddKey(const uint8* key, |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 106 | int key_length, |
| 107 | const uint8* init_data, |
| 108 | int init_data_length, |
| 109 | const std::string& session_id) { |
| 110 | DVLOG(1) << "AddKey()"; |
| 111 | |
| 112 | // WebMediaPlayerImpl ensures GenerateKeyRequest() has been called. |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 113 | media_keys_->AddKey(key, key_length, init_data, init_data_length, session_id); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 114 | } |
| 115 | |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 116 | void ProxyDecryptor::CancelKeyRequest(const std::string& session_id) { |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 117 | DVLOG(1) << "CancelKeyRequest()"; |
| 118 | |
| 119 | // WebMediaPlayerImpl ensures GenerateKeyRequest() has been called. |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 120 | media_keys_->CancelKeyRequest(session_id); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 121 | } |
| 122 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 123 | scoped_ptr<media::MediaKeys> ProxyDecryptor::CreateMediaKeys( |
Torne (Richard Coles) | 68043e1 | 2013-09-26 13:24:57 +0100 | [diff] [blame] | 124 | const std::string& key_system, |
| 125 | const GURL& frame_url) { |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 126 | return ContentDecryptionModuleFactory::Create( |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 127 | key_system, |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 128 | #if defined(ENABLE_PEPPER_CDMS) |
| 129 | web_media_player_client_, |
| 130 | web_frame_, |
| 131 | base::Bind(&ProxyDecryptor::DestroyHelperPlugin, |
| 132 | weak_ptr_factory_.GetWeakPtr()), |
| 133 | #elif defined(OS_ANDROID) |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 134 | proxy_, |
| 135 | media_keys_id_, |
Torne (Richard Coles) | 68043e1 | 2013-09-26 13:24:57 +0100 | [diff] [blame] | 136 | frame_url, |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 137 | #endif // defined(ENABLE_PEPPER_CDMS) |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 138 | base::Bind(&ProxyDecryptor::KeyAdded, weak_ptr_factory_.GetWeakPtr()), |
| 139 | base::Bind(&ProxyDecryptor::KeyError, weak_ptr_factory_.GetWeakPtr()), |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 140 | base::Bind(&ProxyDecryptor::KeyMessage, weak_ptr_factory_.GetWeakPtr())); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 141 | } |
| 142 | |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 143 | void ProxyDecryptor::KeyAdded(const std::string& session_id) { |
| 144 | key_added_cb_.Run(session_id); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 145 | } |
| 146 | |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 147 | void ProxyDecryptor::KeyError(const std::string& session_id, |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 148 | media::MediaKeys::KeyError error_code, |
| 149 | int system_code) { |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 150 | key_error_cb_.Run(session_id, error_code, system_code); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 151 | } |
| 152 | |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 153 | void ProxyDecryptor::KeyMessage(const std::string& session_id, |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 154 | const std::vector<uint8>& message, |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 155 | const std::string& default_url) { |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 156 | key_message_cb_.Run(session_id, message, default_url); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 157 | } |
| 158 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 159 | } // namespace content |