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 "webkit/media/crypto/proxy_decryptor.h" | 5 #include "webkit/media/crypto/proxy_decryptor.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "media/base/decoder_buffer.h" | 8 #include "media/base/decoder_buffer.h" |
9 #include "media/base/decryptor_client.h" | 9 #include "media/base/decryptor_client.h" |
10 #include "media/crypto/aes_decryptor.h" | 10 #include "media/crypto/aes_decryptor.h" |
| 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 12 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" |
11 #include "webkit/media/crypto/key_systems.h" | 13 #include "webkit/media/crypto/key_systems.h" |
| 14 #include "webkit/media/crypto/ppapi_decryptor.h" |
| 15 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
| 16 #include "webkit/plugins/ppapi/ppapi_webplugin_impl.h" |
| 17 // TODO(xhwang): Put this include after "ppapi_plugin_instance.h" for definition |
| 18 // of "uint8_t", which WebMediaPlayer.h uses without including a header for it. |
| 19 // See: https://bugs.webkit.org/show_bug.cgi?id=92031 |
| 20 // Fix include order here when the bug is fixed. |
| 21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebMediaPlayerClient.
h" |
12 | 22 |
13 namespace webkit_media { | 23 namespace webkit_media { |
14 | 24 |
15 ProxyDecryptor::ProxyDecryptor(media::DecryptorClient* client) | 25 static scoped_refptr<webkit::ppapi::PluginInstance> CreatePluginInstance( |
16 : client_(client) { | 26 const std::string& plugin_type, |
| 27 WebKit::WebMediaPlayerClient* web_media_player_client, |
| 28 WebKit::WebFrame* web_frame) { |
| 29 DCHECK(web_media_player_client); |
| 30 DCHECK(web_frame); |
| 31 |
| 32 WebKit::WebPlugin* web_plugin = web_media_player_client->createHelperPlugin( |
| 33 WebKit::WebString::fromUTF8(plugin_type), web_frame); |
| 34 if (!web_plugin) |
| 35 return NULL; |
| 36 |
| 37 DCHECK(!web_plugin->isPlaceholder()); // Prevented by WebKit. |
| 38 // Only Pepper plugins are supported, so it must be a ppapi object. |
| 39 webkit::ppapi::WebPluginImpl* ppapi_plugin = |
| 40 static_cast<webkit::ppapi::WebPluginImpl*>(web_plugin); |
| 41 return ppapi_plugin->instance(); |
| 42 } |
| 43 |
| 44 ProxyDecryptor::ProxyDecryptor( |
| 45 media::DecryptorClient* decryptor_client, |
| 46 WebKit::WebMediaPlayerClient* web_media_player_client, |
| 47 WebKit::WebFrame* web_frame) |
| 48 : client_(decryptor_client), |
| 49 web_media_player_client_(web_media_player_client), |
| 50 web_frame_(web_frame) { |
17 } | 51 } |
18 | 52 |
19 ProxyDecryptor::~ProxyDecryptor() { | 53 ProxyDecryptor::~ProxyDecryptor() { |
20 } | 54 } |
21 | 55 |
22 void ProxyDecryptor::GenerateKeyRequest(const std::string& key_system, | 56 void ProxyDecryptor::GenerateKeyRequest(const std::string& key_system, |
23 const uint8* init_data, | 57 const uint8* init_data, |
24 int init_data_length) { | 58 int init_data_length) { |
25 // We do not support run-time switching of decryptors. GenerateKeyRequest() | 59 // We do not support run-time switching of decryptors. GenerateKeyRequest() |
26 // only creates a new decryptor when |decryptor_| is not initialized. | 60 // only creates a new decryptor when |decryptor_| is not initialized. |
27 if (!decryptor_.get()) { | 61 if (!decryptor_.get()) { |
28 base::AutoLock auto_lock(lock_); | 62 base::AutoLock auto_lock(lock_); |
29 decryptor_ = CreateDecryptor(key_system, client_); | 63 decryptor_ = CreateDecryptor(key_system); |
30 } | 64 } |
31 | 65 |
32 DCHECK(decryptor_.get()); | 66 DCHECK(client_); |
| 67 if (!decryptor_.get()) { |
| 68 client_->KeyError(key_system, "", media::Decryptor::kUnknownError, 0); |
| 69 return; |
| 70 } |
| 71 |
33 decryptor_->GenerateKeyRequest(key_system, init_data, init_data_length); | 72 decryptor_->GenerateKeyRequest(key_system, init_data, init_data_length); |
34 } | 73 } |
35 | 74 |
36 void ProxyDecryptor::AddKey(const std::string& key_system, | 75 void ProxyDecryptor::AddKey(const std::string& key_system, |
37 const uint8* key, | 76 const uint8* key, |
38 int key_length, | 77 int key_length, |
39 const uint8* init_data, | 78 const uint8* init_data, |
40 int init_data_length, | 79 int init_data_length, |
41 const std::string& session_id) { | 80 const std::string& session_id) { |
42 // WebMediaPlayerImpl ensures GenerateKeyRequest() has been called. | 81 // WebMediaPlayerImpl ensures GenerateKeyRequest() has been called. |
43 DCHECK(decryptor_.get()); | 82 DCHECK(decryptor_.get()); |
44 decryptor_->AddKey(key_system, key, key_length, init_data, init_data_length, | 83 decryptor_->AddKey(key_system, key, key_length, init_data, init_data_length, |
45 session_id); | 84 session_id); |
46 } | 85 } |
47 | 86 |
48 void ProxyDecryptor::CancelKeyRequest(const std::string& key_system, | 87 void ProxyDecryptor::CancelKeyRequest(const std::string& key_system, |
49 const std::string& session_id) { | 88 const std::string& session_id) { |
50 // WebMediaPlayerImpl ensures GenerateKeyRequest() has been called. | 89 // WebMediaPlayerImpl ensures GenerateKeyRequest() has been called. |
51 DCHECK(decryptor_.get()); | 90 DCHECK(decryptor_.get()); |
52 decryptor_->CancelKeyRequest(key_system, session_id); | 91 decryptor_->CancelKeyRequest(key_system, session_id); |
53 } | 92 } |
54 | 93 |
55 void ProxyDecryptor::Decrypt( | 94 void ProxyDecryptor::Decrypt( |
56 const scoped_refptr<media::DecoderBuffer>& encrypted, | 95 const scoped_refptr<media::DecoderBuffer>& encrypted, |
57 const DecryptCB& decrypt_cb) { | 96 const DecryptCB& decrypt_cb) { |
58 // This is safe as we do not replace/delete an existing decryptor at run-time. | 97 // This is safe as we do not replace/delete an existing decryptor at run-time. |
59 Decryptor* decryptor = NULL; | 98 media::Decryptor* decryptor = NULL; |
60 { | 99 { |
61 base::AutoLock auto_lock(lock_); | 100 base::AutoLock auto_lock(lock_); |
62 decryptor = decryptor_.get(); | 101 decryptor = decryptor_.get(); |
63 } | 102 } |
64 if (!decryptor) { | 103 if (!decryptor) { |
65 DVLOG(1) << "ProxyDecryptor::Decrypt(): decryptor not initialized."; | 104 DVLOG(1) << "ProxyDecryptor::Decrypt(): decryptor not initialized."; |
66 decrypt_cb.Run(kError, NULL); | 105 decrypt_cb.Run(kError, NULL); |
67 return; | 106 return; |
68 } | 107 } |
69 | 108 |
70 decryptor->Decrypt(encrypted, decrypt_cb); | 109 decryptor->Decrypt(encrypted, decrypt_cb); |
71 } | 110 } |
72 | 111 |
| 112 scoped_ptr<media::Decryptor> ProxyDecryptor::CreatePpapiDecryptor( |
| 113 const std::string& key_system) { |
| 114 DCHECK(client_); |
| 115 DCHECK(web_media_player_client_); |
| 116 DCHECK(web_frame_); |
| 117 |
| 118 std::string plugin_type = GetPluginType(key_system); |
| 119 DCHECK(!plugin_type.empty()); |
| 120 const scoped_refptr<webkit::ppapi::PluginInstance>& plugin_instance = |
| 121 CreatePluginInstance(plugin_type, web_media_player_client_, web_frame_); |
| 122 if (!plugin_instance) { |
| 123 DVLOG(1) << "PpapiDecryptor: plugin instance creation failed."; |
| 124 return scoped_ptr<media::Decryptor>(); |
| 125 } |
| 126 |
| 127 return scoped_ptr<media::Decryptor>(new PpapiDecryptor(client_, |
| 128 plugin_instance)); |
| 129 } |
| 130 |
| 131 scoped_ptr<media::Decryptor> ProxyDecryptor::CreateDecryptor( |
| 132 const std::string& key_system) { |
| 133 DCHECK(client_); |
| 134 |
| 135 if (CanUseAesDecryptor(key_system)) |
| 136 return scoped_ptr<media::Decryptor>(new media::AesDecryptor(client_)); |
| 137 |
| 138 // We only support AesDecryptor and PpapiDecryptor. So if we cannot |
| 139 // use the AesDecryptor, then we'll try to create a PpapiDecryptor for given |
| 140 // |key_system|. |
| 141 return CreatePpapiDecryptor(key_system); |
| 142 } |
| 143 |
73 } // namespace webkit_media | 144 } // namespace webkit_media |
OLD | NEW |