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
,
43 PP_Var init_data_type_arg
,
45 PP_SessionType session_type
) {
47 Instance::GetPerInstanceObject(instance
, kPPPContentDecryptorInterface
);
51 pp::Var
init_data_type_var(pp::PASS_REF
, init_data_type_arg
);
52 if (!init_data_type_var
.is_string())
55 pp::Var
init_data_var(pp::PASS_REF
, init_data_arg
);
56 if (!init_data_var
.is_array_buffer())
58 pp::VarArrayBuffer
init_data_array_buffer(init_data_var
);
60 static_cast<ContentDecryptor_Private
*>(object
)
61 ->CreateSession(promise_id
,
62 init_data_type_var
.AsString(),
63 init_data_array_buffer
,
67 void LoadSession(PP_Instance instance
,
69 PP_Var web_session_id_arg
) {
71 Instance::GetPerInstanceObject(instance
, kPPPContentDecryptorInterface
);
75 pp::Var
web_session_id_var(web_session_id_arg
);
76 if (!web_session_id_var
.is_string())
79 static_cast<ContentDecryptor_Private
*>(object
)
80 ->LoadSession(promise_id
, web_session_id_var
.AsString());
83 void UpdateSession(PP_Instance instance
,
85 PP_Var web_session_id_arg
,
86 PP_Var response_arg
) {
88 Instance::GetPerInstanceObject(instance
, kPPPContentDecryptorInterface
);
92 pp::Var
web_session_id_var(web_session_id_arg
);
93 if (!web_session_id_var
.is_string())
96 pp::Var
response_var(response_arg
);
97 if (!response_var
.is_array_buffer())
99 pp::VarArrayBuffer
response(response_var
);
101 static_cast<ContentDecryptor_Private
*>(object
)
102 ->UpdateSession(promise_id
, web_session_id_var
.AsString(), response
);
105 void ReleaseSession(PP_Instance instance
,
107 PP_Var web_session_id_arg
) {
109 Instance::GetPerInstanceObject(instance
, kPPPContentDecryptorInterface
);
113 pp::Var
web_session_id_var(web_session_id_arg
);
114 if (!web_session_id_var
.is_string())
117 static_cast<ContentDecryptor_Private
*>(object
)
118 ->ReleaseSession(promise_id
, web_session_id_var
.AsString());
121 void Decrypt(PP_Instance instance
,
122 PP_Resource encrypted_resource
,
123 const PP_EncryptedBlockInfo
* encrypted_block_info
) {
124 pp::Buffer_Dev
encrypted_block(encrypted_resource
);
127 Instance::GetPerInstanceObject(instance
, kPPPContentDecryptorInterface
);
131 static_cast<ContentDecryptor_Private
*>(object
)->Decrypt(
133 *encrypted_block_info
);
136 void InitializeAudioDecoder(
137 PP_Instance instance
,
138 const PP_AudioDecoderConfig
* decoder_config
,
139 PP_Resource extra_data_resource
) {
140 pp::Buffer_Dev
extra_data_buffer(extra_data_resource
);
143 Instance::GetPerInstanceObject(instance
, kPPPContentDecryptorInterface
);
147 static_cast<ContentDecryptor_Private
*>(object
)->InitializeAudioDecoder(
152 void InitializeVideoDecoder(
153 PP_Instance instance
,
154 const PP_VideoDecoderConfig
* decoder_config
,
155 PP_Resource extra_data_resource
) {
156 pp::Buffer_Dev
extra_data_buffer(extra_data_resource
);
159 Instance::GetPerInstanceObject(instance
, kPPPContentDecryptorInterface
);
163 static_cast<ContentDecryptor_Private
*>(object
)->InitializeVideoDecoder(
168 void DeinitializeDecoder(PP_Instance instance
,
169 PP_DecryptorStreamType decoder_type
,
170 uint32_t request_id
) {
172 Instance::GetPerInstanceObject(instance
, kPPPContentDecryptorInterface
);
175 static_cast<ContentDecryptor_Private
*>(object
)->DeinitializeDecoder(
180 void ResetDecoder(PP_Instance instance
,
181 PP_DecryptorStreamType decoder_type
,
182 uint32_t request_id
) {
184 Instance::GetPerInstanceObject(instance
, kPPPContentDecryptorInterface
);
187 static_cast<ContentDecryptor_Private
*>(object
)->ResetDecoder(decoder_type
,
191 void DecryptAndDecode(PP_Instance instance
,
192 PP_DecryptorStreamType decoder_type
,
193 PP_Resource encrypted_resource
,
194 const PP_EncryptedBlockInfo
* encrypted_block_info
) {
195 pp::Buffer_Dev
encrypted_buffer(encrypted_resource
);
198 Instance::GetPerInstanceObject(instance
, kPPPContentDecryptorInterface
);
202 static_cast<ContentDecryptor_Private
*>(object
)->DecryptAndDecode(
205 *encrypted_block_info
);
208 const PPP_ContentDecryptor_Private ppp_content_decryptor
= {
215 &InitializeAudioDecoder
,
216 &InitializeVideoDecoder
,
217 &DeinitializeDecoder
,
222 template <> const char* interface_name
<PPB_ContentDecryptor_Private
>() {
223 return PPB_CONTENTDECRYPTOR_PRIVATE_INTERFACE
;
228 ContentDecryptor_Private::ContentDecryptor_Private(Instance
* instance
)
229 : associated_instance_(instance
) {
230 Module::Get()->AddPluginInterface(kPPPContentDecryptorInterface
,
231 &ppp_content_decryptor
);
232 instance
->AddPerInstanceObject(kPPPContentDecryptorInterface
, this);
235 ContentDecryptor_Private::~ContentDecryptor_Private() {
236 Instance::RemovePerInstanceObject(associated_instance_
,
237 kPPPContentDecryptorInterface
,
241 void ContentDecryptor_Private::PromiseResolved(uint32_t promise_id
) {
242 if (has_interface
<PPB_ContentDecryptor_Private
>()) {
243 get_interface
<PPB_ContentDecryptor_Private
>()->PromiseResolved(
244 associated_instance_
.pp_instance(), promise_id
);
248 void ContentDecryptor_Private::PromiseResolvedWithSession(
250 const std::string
& web_session_id
) {
251 if (has_interface
<PPB_ContentDecryptor_Private
>()) {
252 pp::Var
web_session_id_var(web_session_id
);
253 get_interface
<PPB_ContentDecryptor_Private
>()->PromiseResolvedWithSession(
254 associated_instance_
.pp_instance(),
256 web_session_id_var
.pp_var());
260 void ContentDecryptor_Private::PromiseRejected(
262 PP_CdmExceptionCode exception_code
,
263 uint32_t system_code
,
264 const std::string
& error_description
) {
265 if (has_interface
<PPB_ContentDecryptor_Private
>()) {
266 pp::Var
error_description_var(error_description
);
267 get_interface
<PPB_ContentDecryptor_Private
>()->PromiseRejected(
268 associated_instance_
.pp_instance(),
272 error_description_var
.pp_var());
276 void ContentDecryptor_Private::SessionMessage(
277 const std::string
& web_session_id
,
278 pp::VarArrayBuffer message
,
279 const std::string
& destination_url
) {
280 if (has_interface
<PPB_ContentDecryptor_Private
>()) {
281 pp::Var
web_session_id_var(web_session_id
);
282 pp::Var
destination_url_var(destination_url
);
283 get_interface
<PPB_ContentDecryptor_Private
>()->SessionMessage(
284 associated_instance_
.pp_instance(),
285 web_session_id_var
.pp_var(),
287 destination_url_var
.pp_var());
291 void ContentDecryptor_Private::SessionReady(const std::string
& web_session_id
) {
292 if (has_interface
<PPB_ContentDecryptor_Private
>()) {
293 pp::Var
web_session_id_var(web_session_id
);
294 get_interface
<PPB_ContentDecryptor_Private
>()->SessionReady(
295 associated_instance_
.pp_instance(), web_session_id_var
.pp_var());
299 void ContentDecryptor_Private::SessionClosed(
300 const std::string
& web_session_id
) {
301 if (has_interface
<PPB_ContentDecryptor_Private
>()) {
302 pp::Var
web_session_id_var(web_session_id
);
303 get_interface
<PPB_ContentDecryptor_Private
>()->SessionClosed(
304 associated_instance_
.pp_instance(), web_session_id_var
.pp_var());
308 void ContentDecryptor_Private::SessionError(
309 const std::string
& web_session_id
,
310 PP_CdmExceptionCode exception_code
,
311 uint32_t system_code
,
312 const std::string
& error_description
) {
313 if (has_interface
<PPB_ContentDecryptor_Private
>()) {
314 pp::Var
web_session_id_var(web_session_id
);
315 pp::Var
error_description_var(error_description
);
316 get_interface
<PPB_ContentDecryptor_Private
>()->SessionError(
317 associated_instance_
.pp_instance(),
318 web_session_id_var
.pp_var(),
321 error_description_var
.pp_var());
325 void ContentDecryptor_Private::DeliverBlock(
326 pp::Buffer_Dev decrypted_block
,
327 const PP_DecryptedBlockInfo
& decrypted_block_info
) {
328 if (has_interface
<PPB_ContentDecryptor_Private
>()) {
329 get_interface
<PPB_ContentDecryptor_Private
>()->DeliverBlock(
330 associated_instance_
.pp_instance(),
331 decrypted_block
.pp_resource(),
332 &decrypted_block_info
);
336 void ContentDecryptor_Private::DecoderInitializeDone(
337 PP_DecryptorStreamType decoder_type
,
340 if (has_interface
<PPB_ContentDecryptor_Private
>()) {
341 get_interface
<PPB_ContentDecryptor_Private
>()->DecoderInitializeDone(
342 associated_instance_
.pp_instance(),
345 PP_FromBool(success
));
349 void ContentDecryptor_Private::DecoderDeinitializeDone(
350 PP_DecryptorStreamType decoder_type
,
351 uint32_t request_id
) {
352 if (has_interface
<PPB_ContentDecryptor_Private
>()) {
353 get_interface
<PPB_ContentDecryptor_Private
>()->DecoderDeinitializeDone(
354 associated_instance_
.pp_instance(),
360 void ContentDecryptor_Private::DecoderResetDone(
361 PP_DecryptorStreamType decoder_type
,
362 uint32_t request_id
) {
363 if (has_interface
<PPB_ContentDecryptor_Private
>()) {
364 get_interface
<PPB_ContentDecryptor_Private
>()->DecoderResetDone(
365 associated_instance_
.pp_instance(),
371 void ContentDecryptor_Private::DeliverFrame(
372 pp::Buffer_Dev decrypted_frame
,
373 const PP_DecryptedFrameInfo
& decrypted_frame_info
) {
374 if (has_interface
<PPB_ContentDecryptor_Private
>()) {
375 get_interface
<PPB_ContentDecryptor_Private
>()->DeliverFrame(
376 associated_instance_
.pp_instance(),
377 decrypted_frame
.pp_resource(),
378 &decrypted_frame_info
);
382 void ContentDecryptor_Private::DeliverSamples(
383 pp::Buffer_Dev audio_frames
,
384 const PP_DecryptedSampleInfo
& decrypted_sample_info
) {
385 if (has_interface
<PPB_ContentDecryptor_Private
>()) {
386 get_interface
<PPB_ContentDecryptor_Private
>()->DeliverSamples(
387 associated_instance_
.pp_instance(),
388 audio_frames
.pp_resource(),
389 &decrypted_sample_info
);