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 Initialize(PP_Instance instance
,
27 PP_Var key_system_arg
) {
29 Instance::GetPerInstanceObject(instance
, kPPPContentDecryptorInterface
);
33 pp::Var
key_system_var(pp::PASS_REF
, key_system_arg
);
34 if (!key_system_var
.is_string())
37 static_cast<ContentDecryptor_Private
*>(object
)->Initialize(
38 key_system_var
.AsString());
41 void CreateSession(PP_Instance instance
,
44 PP_Var init_data_arg
) {
46 Instance::GetPerInstanceObject(instance
, kPPPContentDecryptorInterface
);
50 pp::Var
type_var(pp::PASS_REF
, type_arg
);
51 if (!type_var
.is_string())
54 pp::Var
init_data_var(pp::PASS_REF
, init_data_arg
);
55 if (!init_data_var
.is_array_buffer())
57 pp::VarArrayBuffer
init_data_array_buffer(init_data_var
);
59 static_cast<ContentDecryptor_Private
*>(object
)
60 ->CreateSession(session_id
, type_var
.AsString(), init_data_array_buffer
);
63 void LoadSession(PP_Instance instance
,
65 PP_Var web_session_id_arg
) {
67 Instance::GetPerInstanceObject(instance
, kPPPContentDecryptorInterface
);
71 pp::Var
web_session_id_var(pp::PASS_REF
, web_session_id_arg
);
72 if (!web_session_id_var
.is_string())
75 static_cast<ContentDecryptor_Private
*>(object
)
76 ->LoadSession(session_id
, web_session_id_var
.AsString());
79 void UpdateSession(PP_Instance instance
,
81 PP_Var response_arg
) {
83 Instance::GetPerInstanceObject(instance
, kPPPContentDecryptorInterface
);
87 pp::Var
response_var(pp::PASS_REF
, response_arg
);
88 if (!response_var
.is_array_buffer())
90 pp::VarArrayBuffer
response(response_var
);
92 static_cast<ContentDecryptor_Private
*>(object
)
93 ->UpdateSession(session_id
, response
);
96 void ReleaseSession(PP_Instance instance
, uint32_t session_id
) {
98 Instance::GetPerInstanceObject(instance
, kPPPContentDecryptorInterface
);
102 static_cast<ContentDecryptor_Private
*>(object
)->ReleaseSession(session_id
);
106 void Decrypt(PP_Instance instance
,
107 PP_Resource encrypted_resource
,
108 const PP_EncryptedBlockInfo
* encrypted_block_info
) {
109 pp::Buffer_Dev
encrypted_block(encrypted_resource
);
112 Instance::GetPerInstanceObject(instance
, kPPPContentDecryptorInterface
);
116 static_cast<ContentDecryptor_Private
*>(object
)->Decrypt(
118 *encrypted_block_info
);
121 void InitializeAudioDecoder(
122 PP_Instance instance
,
123 const PP_AudioDecoderConfig
* decoder_config
,
124 PP_Resource extra_data_resource
) {
125 pp::Buffer_Dev
extra_data_buffer(extra_data_resource
);
128 Instance::GetPerInstanceObject(instance
, kPPPContentDecryptorInterface
);
132 static_cast<ContentDecryptor_Private
*>(object
)->InitializeAudioDecoder(
137 void InitializeVideoDecoder(
138 PP_Instance instance
,
139 const PP_VideoDecoderConfig
* decoder_config
,
140 PP_Resource extra_data_resource
) {
141 pp::Buffer_Dev
extra_data_buffer(extra_data_resource
);
144 Instance::GetPerInstanceObject(instance
, kPPPContentDecryptorInterface
);
148 static_cast<ContentDecryptor_Private
*>(object
)->InitializeVideoDecoder(
153 void DeinitializeDecoder(PP_Instance instance
,
154 PP_DecryptorStreamType decoder_type
,
155 uint32_t request_id
) {
157 Instance::GetPerInstanceObject(instance
, kPPPContentDecryptorInterface
);
160 static_cast<ContentDecryptor_Private
*>(object
)->DeinitializeDecoder(
165 void ResetDecoder(PP_Instance instance
,
166 PP_DecryptorStreamType decoder_type
,
167 uint32_t request_id
) {
169 Instance::GetPerInstanceObject(instance
, kPPPContentDecryptorInterface
);
172 static_cast<ContentDecryptor_Private
*>(object
)->ResetDecoder(decoder_type
,
176 void DecryptAndDecode(PP_Instance instance
,
177 PP_DecryptorStreamType decoder_type
,
178 PP_Resource encrypted_resource
,
179 const PP_EncryptedBlockInfo
* encrypted_block_info
) {
180 pp::Buffer_Dev
encrypted_buffer(encrypted_resource
);
183 Instance::GetPerInstanceObject(instance
, kPPPContentDecryptorInterface
);
187 static_cast<ContentDecryptor_Private
*>(object
)->DecryptAndDecode(
190 *encrypted_block_info
);
193 const PPP_ContentDecryptor_Private ppp_content_decryptor
= {
200 &InitializeAudioDecoder
,
201 &InitializeVideoDecoder
,
202 &DeinitializeDecoder
,
207 template <> const char* interface_name
<PPB_ContentDecryptor_Private
>() {
208 return PPB_CONTENTDECRYPTOR_PRIVATE_INTERFACE
;
213 ContentDecryptor_Private::ContentDecryptor_Private(Instance
* instance
)
214 : associated_instance_(instance
) {
215 Module::Get()->AddPluginInterface(kPPPContentDecryptorInterface
,
216 &ppp_content_decryptor
);
217 instance
->AddPerInstanceObject(kPPPContentDecryptorInterface
, this);
220 ContentDecryptor_Private::~ContentDecryptor_Private() {
221 Instance::RemovePerInstanceObject(associated_instance_
,
222 kPPPContentDecryptorInterface
,
226 void ContentDecryptor_Private::SessionCreated(
228 const std::string
& web_session_id
) {
229 if (has_interface
<PPB_ContentDecryptor_Private
>()) {
230 pp::Var
web_session_id_var(web_session_id
);
231 get_interface
<PPB_ContentDecryptor_Private
>()->SessionCreated(
232 associated_instance_
.pp_instance(),
234 web_session_id_var
.pp_var());
238 void ContentDecryptor_Private::SessionMessage(uint32_t session_id
,
239 pp::VarArrayBuffer message
,
240 const std::string
& default_url
) {
241 if (has_interface
<PPB_ContentDecryptor_Private
>()) {
242 pp::Var
default_url_var(default_url
);
243 get_interface
<PPB_ContentDecryptor_Private
>()->SessionMessage(
244 associated_instance_
.pp_instance(),
247 default_url_var
.pp_var());
251 void ContentDecryptor_Private::SessionReady(uint32_t session_id
) {
252 if (has_interface
<PPB_ContentDecryptor_Private
>()) {
253 get_interface
<PPB_ContentDecryptor_Private
>()->SessionReady(
254 associated_instance_
.pp_instance(), session_id
);
258 void ContentDecryptor_Private::SessionClosed(uint32_t session_id
) {
259 if (has_interface
<PPB_ContentDecryptor_Private
>()) {
260 get_interface
<PPB_ContentDecryptor_Private
>()->SessionClosed(
261 associated_instance_
.pp_instance(), session_id
);
265 void ContentDecryptor_Private::SessionError(uint32_t session_id
,
267 uint32_t system_code
) {
268 if (has_interface
<PPB_ContentDecryptor_Private
>()) {
269 get_interface
<PPB_ContentDecryptor_Private
>()->SessionError(
270 associated_instance_
.pp_instance(),
277 void ContentDecryptor_Private::DeliverBlock(
278 pp::Buffer_Dev decrypted_block
,
279 const PP_DecryptedBlockInfo
& decrypted_block_info
) {
280 if (has_interface
<PPB_ContentDecryptor_Private
>()) {
281 get_interface
<PPB_ContentDecryptor_Private
>()->DeliverBlock(
282 associated_instance_
.pp_instance(),
283 decrypted_block
.pp_resource(),
284 &decrypted_block_info
);
288 void ContentDecryptor_Private::DecoderInitializeDone(
289 PP_DecryptorStreamType decoder_type
,
292 if (has_interface
<PPB_ContentDecryptor_Private
>()) {
293 get_interface
<PPB_ContentDecryptor_Private
>()->DecoderInitializeDone(
294 associated_instance_
.pp_instance(),
297 PP_FromBool(success
));
301 void ContentDecryptor_Private::DecoderDeinitializeDone(
302 PP_DecryptorStreamType decoder_type
,
303 uint32_t request_id
) {
304 if (has_interface
<PPB_ContentDecryptor_Private
>()) {
305 get_interface
<PPB_ContentDecryptor_Private
>()->DecoderDeinitializeDone(
306 associated_instance_
.pp_instance(),
312 void ContentDecryptor_Private::DecoderResetDone(
313 PP_DecryptorStreamType decoder_type
,
314 uint32_t request_id
) {
315 if (has_interface
<PPB_ContentDecryptor_Private
>()) {
316 get_interface
<PPB_ContentDecryptor_Private
>()->DecoderResetDone(
317 associated_instance_
.pp_instance(),
323 void ContentDecryptor_Private::DeliverFrame(
324 pp::Buffer_Dev decrypted_frame
,
325 const PP_DecryptedFrameInfo
& decrypted_frame_info
) {
326 if (has_interface
<PPB_ContentDecryptor_Private
>()) {
327 get_interface
<PPB_ContentDecryptor_Private
>()->DeliverFrame(
328 associated_instance_
.pp_instance(),
329 decrypted_frame
.pp_resource(),
330 &decrypted_frame_info
);
334 void ContentDecryptor_Private::DeliverSamples(
335 pp::Buffer_Dev audio_frames
,
336 const PP_DecryptedSampleInfo
& decrypted_sample_info
) {
337 if (has_interface
<PPB_ContentDecryptor_Private
>()) {
338 get_interface
<PPB_ContentDecryptor_Private
>()->DeliverSamples(
339 associated_instance_
.pp_instance(),
340 audio_frames
.pp_resource(),
341 &decrypted_sample_info
);