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
3 // found in the LICENSE file.
5 #include "ppapi/cpp/private/content_decryptor_private.h"
7 #include <cstring> // memcpy
9 #include "ppapi/c/ppb_var.h"
10 #include "ppapi/c/private/ppb_content_decryptor_private.h"
11 #include "ppapi/c/private/ppp_content_decryptor_private.h"
12 #include "ppapi/cpp/instance.h"
13 #include "ppapi/cpp/instance_handle.h"
14 #include "ppapi/cpp/logging.h"
15 #include "ppapi/cpp/module.h"
16 #include "ppapi/cpp/module_impl.h"
17 #include "ppapi/cpp/var.h"
23 static const char kPPPContentDecryptorInterface
[] =
24 PPP_CONTENTDECRYPTOR_PRIVATE_INTERFACE
;
26 void GenerateKeyRequest(PP_Instance instance
,
27 PP_Var key_system_arg
,
29 PP_Var init_data_arg
) {
31 Instance::GetPerInstanceObject(instance
, kPPPContentDecryptorInterface
);
35 pp::Var
key_system_var(pp::PASS_REF
, key_system_arg
);
36 if (!key_system_var
.is_string())
39 pp::Var
type_var(pp::PASS_REF
, type_arg
);
40 if (!type_var
.is_string())
43 pp::Var
init_data_var(pp::PASS_REF
, init_data_arg
);
44 if (!init_data_var
.is_array_buffer())
46 pp::VarArrayBuffer
init_data_array_buffer(init_data_var
);
48 static_cast<ContentDecryptor_Private
*>(object
)->GenerateKeyRequest(
49 key_system_var
.AsString(),
51 init_data_array_buffer
);
54 void AddKey(PP_Instance instance
,
55 PP_Var session_id_arg
,
57 PP_Var init_data_arg
) {
59 Instance::GetPerInstanceObject(instance
, kPPPContentDecryptorInterface
);
63 pp::Var
session_id_var(pp::PASS_REF
, session_id_arg
);
64 if (!session_id_var
.is_string())
67 pp::Var
key_var(pp::PASS_REF
, key_arg
);
68 if (!key_var
.is_array_buffer())
70 pp::VarArrayBuffer
key(key_var
);
72 pp::Var
init_data_var(pp::PASS_REF
, init_data_arg
);
73 if (!init_data_var
.is_array_buffer())
75 pp::VarArrayBuffer
init_data(init_data_var
);
78 static_cast<ContentDecryptor_Private
*>(object
)->AddKey(
79 session_id_var
.AsString(),
84 void CancelKeyRequest(PP_Instance instance
, PP_Var session_id_arg
) {
86 Instance::GetPerInstanceObject(instance
, kPPPContentDecryptorInterface
);
90 pp::Var
session_id_var(pp::PASS_REF
, session_id_arg
);
91 if (!session_id_var
.is_string())
94 static_cast<ContentDecryptor_Private
*>(object
)->CancelKeyRequest(
95 session_id_var
.AsString());
99 void Decrypt(PP_Instance instance
,
100 PP_Resource encrypted_resource
,
101 const PP_EncryptedBlockInfo
* encrypted_block_info
) {
102 pp::Buffer_Dev
encrypted_block(encrypted_resource
);
105 Instance::GetPerInstanceObject(instance
, kPPPContentDecryptorInterface
);
109 static_cast<ContentDecryptor_Private
*>(object
)->Decrypt(
111 *encrypted_block_info
);
114 void InitializeAudioDecoder(
115 PP_Instance instance
,
116 const PP_AudioDecoderConfig
* decoder_config
,
117 PP_Resource extra_data_resource
) {
118 pp::Buffer_Dev
extra_data_buffer(extra_data_resource
);
121 Instance::GetPerInstanceObject(instance
, kPPPContentDecryptorInterface
);
125 static_cast<ContentDecryptor_Private
*>(object
)->InitializeAudioDecoder(
130 void InitializeVideoDecoder(
131 PP_Instance instance
,
132 const PP_VideoDecoderConfig
* decoder_config
,
133 PP_Resource extra_data_resource
) {
134 pp::Buffer_Dev
extra_data_buffer(extra_data_resource
);
137 Instance::GetPerInstanceObject(instance
, kPPPContentDecryptorInterface
);
141 static_cast<ContentDecryptor_Private
*>(object
)->InitializeVideoDecoder(
146 void DeinitializeDecoder(PP_Instance instance
,
147 PP_DecryptorStreamType decoder_type
,
148 uint32_t request_id
) {
150 Instance::GetPerInstanceObject(instance
, kPPPContentDecryptorInterface
);
153 static_cast<ContentDecryptor_Private
*>(object
)->DeinitializeDecoder(
158 void ResetDecoder(PP_Instance instance
,
159 PP_DecryptorStreamType decoder_type
,
160 uint32_t request_id
) {
162 Instance::GetPerInstanceObject(instance
, kPPPContentDecryptorInterface
);
165 static_cast<ContentDecryptor_Private
*>(object
)->ResetDecoder(decoder_type
,
169 void DecryptAndDecode(PP_Instance instance
,
170 PP_DecryptorStreamType decoder_type
,
171 PP_Resource encrypted_resource
,
172 const PP_EncryptedBlockInfo
* encrypted_block_info
) {
173 pp::Buffer_Dev
encrypted_buffer(encrypted_resource
);
176 Instance::GetPerInstanceObject(instance
, kPPPContentDecryptorInterface
);
180 static_cast<ContentDecryptor_Private
*>(object
)->DecryptAndDecode(
183 *encrypted_block_info
);
186 const PPP_ContentDecryptor_Private ppp_content_decryptor
= {
191 &InitializeAudioDecoder
,
192 &InitializeVideoDecoder
,
193 &DeinitializeDecoder
,
198 template <> const char* interface_name
<PPB_ContentDecryptor_Private
>() {
199 return PPB_CONTENTDECRYPTOR_PRIVATE_INTERFACE
;
204 ContentDecryptor_Private::ContentDecryptor_Private(Instance
* instance
)
205 : associated_instance_(instance
) {
206 Module::Get()->AddPluginInterface(kPPPContentDecryptorInterface
,
207 &ppp_content_decryptor
);
208 instance
->AddPerInstanceObject(kPPPContentDecryptorInterface
, this);
211 ContentDecryptor_Private::~ContentDecryptor_Private() {
212 Instance::RemovePerInstanceObject(associated_instance_
,
213 kPPPContentDecryptorInterface
,
217 void ContentDecryptor_Private::NeedKey(const std::string
& key_system
,
218 const std::string
& session_id
,
219 pp::VarArrayBuffer init_data
) {
220 // session_id can be empty here.
221 if (has_interface
<PPB_ContentDecryptor_Private
>()) {
222 pp::Var
key_system_var(key_system
);
223 pp::Var
session_id_var(session_id
);
225 get_interface
<PPB_ContentDecryptor_Private
>()->NeedKey(
226 associated_instance_
.pp_instance(),
227 key_system_var
.pp_var(),
228 session_id_var
.pp_var(),
233 void ContentDecryptor_Private::KeyAdded(const std::string
& key_system
,
234 const std::string
& session_id
) {
235 if (has_interface
<PPB_ContentDecryptor_Private
>()) {
236 pp::Var
key_system_var(key_system
);
237 pp::Var
session_id_var(session_id
);
238 get_interface
<PPB_ContentDecryptor_Private
>()->KeyAdded(
239 associated_instance_
.pp_instance(),
240 key_system_var
.pp_var(),
241 session_id_var
.pp_var());
245 void ContentDecryptor_Private::KeyMessage(const std::string
& key_system
,
246 const std::string
& session_id
,
247 pp::VarArrayBuffer message
,
248 const std::string
& default_url
) {
249 if (has_interface
<PPB_ContentDecryptor_Private
>()) {
250 pp::Var
key_system_var(key_system
);
251 pp::Var
session_id_var(session_id
);
252 pp::Var
default_url_var(default_url
);
253 get_interface
<PPB_ContentDecryptor_Private
>()->KeyMessage(
254 associated_instance_
.pp_instance(),
255 key_system_var
.pp_var(),
256 session_id_var
.pp_var(),
258 default_url_var
.pp_var());
262 void ContentDecryptor_Private::KeyError(const std::string
& key_system
,
263 const std::string
& session_id
,
265 int32_t system_code
) {
266 if (has_interface
<PPB_ContentDecryptor_Private
>()) {
267 pp::Var
key_system_var(key_system
);
268 pp::Var
session_id_var(session_id
);
269 get_interface
<PPB_ContentDecryptor_Private
>()->KeyError(
270 associated_instance_
.pp_instance(),
271 key_system_var
.pp_var(),
272 session_id_var
.pp_var(),
278 void ContentDecryptor_Private::DeliverBlock(
279 pp::Buffer_Dev decrypted_block
,
280 const PP_DecryptedBlockInfo
& decrypted_block_info
) {
281 if (has_interface
<PPB_ContentDecryptor_Private
>()) {
282 get_interface
<PPB_ContentDecryptor_Private
>()->DeliverBlock(
283 associated_instance_
.pp_instance(),
284 decrypted_block
.pp_resource(),
285 &decrypted_block_info
);
289 void ContentDecryptor_Private::DecoderInitializeDone(
290 PP_DecryptorStreamType decoder_type
,
293 if (has_interface
<PPB_ContentDecryptor_Private
>()) {
294 get_interface
<PPB_ContentDecryptor_Private
>()->DecoderInitializeDone(
295 associated_instance_
.pp_instance(),
298 PP_FromBool(success
));
302 void ContentDecryptor_Private::DecoderDeinitializeDone(
303 PP_DecryptorStreamType decoder_type
,
304 uint32_t request_id
) {
305 if (has_interface
<PPB_ContentDecryptor_Private
>()) {
306 get_interface
<PPB_ContentDecryptor_Private
>()->DecoderDeinitializeDone(
307 associated_instance_
.pp_instance(),
313 void ContentDecryptor_Private::DecoderResetDone(
314 PP_DecryptorStreamType decoder_type
,
315 uint32_t request_id
) {
316 if (has_interface
<PPB_ContentDecryptor_Private
>()) {
317 get_interface
<PPB_ContentDecryptor_Private
>()->DecoderResetDone(
318 associated_instance_
.pp_instance(),
324 void ContentDecryptor_Private::DeliverFrame(
325 pp::Buffer_Dev decrypted_frame
,
326 const PP_DecryptedFrameInfo
& decrypted_frame_info
) {
327 if (has_interface
<PPB_ContentDecryptor_Private
>()) {
328 get_interface
<PPB_ContentDecryptor_Private
>()->DeliverFrame(
329 associated_instance_
.pp_instance(),
330 decrypted_frame
.pp_resource(),
331 &decrypted_frame_info
);
335 void ContentDecryptor_Private::DeliverSamples(
336 pp::Buffer_Dev audio_frames
,
337 const PP_DecryptedBlockInfo
& decrypted_block_info
) {
338 if (has_interface
<PPB_ContentDecryptor_Private
>()) {
339 get_interface
<PPB_ContentDecryptor_Private
>()->DeliverSamples(
340 associated_instance_
.pp_instance(),
341 audio_frames
.pp_resource(),
342 &decrypted_block_info
);