summaryrefslogtreecommitdiffstats
path: root/ppapi/cpp/private/content_decryptor_private.cc
blob: 76dc2f33c04b4f533ebf6aeb44d629c48c81314a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ppapi/cpp/private/content_decryptor_private.h"

#include <cstring>  // memcpy

#include "ppapi/c/ppb_var.h"
#include "ppapi/c/private/ppb_content_decryptor_private.h"
#include "ppapi/c/private/ppp_content_decryptor_private.h"
#include "ppapi/cpp/instance.h"
#include "ppapi/cpp/instance_handle.h"
#include "ppapi/cpp/logging.h"
#include "ppapi/cpp/module.h"
#include "ppapi/cpp/module_impl.h"
#include "ppapi/cpp/var.h"
#include "ppapi/cpp/var_array.h"

namespace pp {

namespace {

static const char kPPPContentDecryptorInterface[] =
    PPP_CONTENTDECRYPTOR_PRIVATE_INTERFACE;

void Initialize(PP_Instance instance,
                PP_Var key_system_arg) {
  void* object =
      Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
  if (!object)
    return;

  pp::Var key_system_var(pp::PASS_REF, key_system_arg);
  if (!key_system_var.is_string())
    return;

  static_cast<ContentDecryptor_Private*>(object)->Initialize(
      key_system_var.AsString());
}

void SetServerCertificate(PP_Instance instance,
                          uint32_t promise_id,
                          PP_Var server_certificate_arg) {
  void* object =
      Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
  if (!object)
    return;

  pp::Var server_certificate_var(server_certificate_arg);
  if (!server_certificate_var.is_array_buffer())
    return;
  pp::VarArrayBuffer server_certificate(server_certificate_var);

  static_cast<ContentDecryptor_Private*>(object)
      ->SetServerCertificate(promise_id, server_certificate);
}

void CreateSessionAndGenerateRequest(PP_Instance instance,
                                     uint32_t promise_id,
                                     PP_SessionType session_type,
                                     PP_Var init_data_type_arg,
                                     PP_Var init_data_arg) {
  void* object =
      Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
  if (!object)
    return;

  pp::Var init_data_type_var(pp::PASS_REF, init_data_type_arg);
  if (!init_data_type_var.is_string())
    return;

  pp::Var init_data_var(pp::PASS_REF, init_data_arg);
  if (!init_data_var.is_array_buffer())
    return;
  pp::VarArrayBuffer init_data_array_buffer(init_data_var);

  static_cast<ContentDecryptor_Private*>(object)
      ->CreateSessionAndGenerateRequest(promise_id, session_type,
                                        init_data_type_var.AsString(),
                                        init_data_array_buffer);
}

void LoadSession(PP_Instance instance,
                 uint32_t promise_id,
                 PP_SessionType session_type,
                 PP_Var session_id_arg) {
  void* object =
      Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
  if (!object)
    return;

  pp::Var session_id_var(session_id_arg);
  if (!session_id_var.is_string())
    return;

  static_cast<ContentDecryptor_Private*>(object)
      ->LoadSession(promise_id, session_type, session_id_var.AsString());
}

void UpdateSession(PP_Instance instance,
                   uint32_t promise_id,
                   PP_Var session_id_arg,
                   PP_Var response_arg) {
  void* object =
      Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
  if (!object)
    return;

  pp::Var session_id_var(session_id_arg);
  if (!session_id_var.is_string())
    return;

  pp::Var response_var(response_arg);
  if (!response_var.is_array_buffer())
    return;
  pp::VarArrayBuffer response(response_var);

  static_cast<ContentDecryptor_Private*>(object)
      ->UpdateSession(promise_id, session_id_var.AsString(), response);
}

void CloseSession(PP_Instance instance,
                  uint32_t promise_id,
                  PP_Var session_id_arg) {
  void* object =
      Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
  if (!object)
    return;

  pp::Var session_id_var(session_id_arg);
  if (!session_id_var.is_string())
    return;

  static_cast<ContentDecryptor_Private*>(object)
      ->CloseSession(promise_id, session_id_var.AsString());
}

void RemoveSession(PP_Instance instance,
                   uint32_t promise_id,
                   PP_Var session_id_arg) {
  void* object =
      Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
  if (!object)
    return;

  pp::Var session_id_var(session_id_arg);
  if (!session_id_var.is_string())
    return;

  static_cast<ContentDecryptor_Private*>(object)
      ->RemoveSession(promise_id, session_id_var.AsString());
}

void Decrypt(PP_Instance instance,
             PP_Resource encrypted_resource,
             const PP_EncryptedBlockInfo* encrypted_block_info) {
  pp::Buffer_Dev encrypted_block(encrypted_resource);

  void* object =
      Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
  if (!object)
    return;

  static_cast<ContentDecryptor_Private*>(object)->Decrypt(
      encrypted_block,
      *encrypted_block_info);
}

void InitializeAudioDecoder(
    PP_Instance instance,
    const PP_AudioDecoderConfig* decoder_config,
    PP_Resource extra_data_resource) {
  pp::Buffer_Dev extra_data_buffer(extra_data_resource);

  void* object =
      Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
  if (!object)
    return;

  static_cast<ContentDecryptor_Private*>(object)->InitializeAudioDecoder(
      *decoder_config,
      extra_data_buffer);
}

void InitializeVideoDecoder(
    PP_Instance instance,
    const PP_VideoDecoderConfig* decoder_config,
    PP_Resource extra_data_resource) {
  pp::Buffer_Dev extra_data_buffer(extra_data_resource);

  void* object =
      Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
  if (!object)
    return;

  static_cast<ContentDecryptor_Private*>(object)->InitializeVideoDecoder(
      *decoder_config,
      extra_data_buffer);
}

void DeinitializeDecoder(PP_Instance instance,
                         PP_DecryptorStreamType decoder_type,
                         uint32_t request_id) {
  void* object =
      Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
  if (!object)
    return;
  static_cast<ContentDecryptor_Private*>(object)->DeinitializeDecoder(
      decoder_type,
      request_id);
}

void ResetDecoder(PP_Instance instance,
                  PP_DecryptorStreamType decoder_type,
                  uint32_t request_id) {
  void* object =
      Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
  if (!object)
    return;
  static_cast<ContentDecryptor_Private*>(object)->ResetDecoder(decoder_type,
                                                               request_id);
}

void DecryptAndDecode(PP_Instance instance,
                      PP_DecryptorStreamType decoder_type,
                      PP_Resource encrypted_resource,
                      const PP_EncryptedBlockInfo* encrypted_block_info) {
  pp::Buffer_Dev encrypted_buffer(encrypted_resource);

  void* object =
      Instance::GetPerInstanceObject(instance, kPPPContentDecryptorInterface);
  if (!object)
    return;

  static_cast<ContentDecryptor_Private*>(object)->DecryptAndDecode(
      decoder_type,
      encrypted_buffer,
      *encrypted_block_info);
}

const PPP_ContentDecryptor_Private ppp_content_decryptor = {
    &Initialize,
    &SetServerCertificate,
    &CreateSessionAndGenerateRequest,
    &LoadSession,
    &UpdateSession,
    &CloseSession,
    &RemoveSession,
    &Decrypt,
    &InitializeAudioDecoder,
    &InitializeVideoDecoder,
    &DeinitializeDecoder,
    &ResetDecoder,
    &DecryptAndDecode};

template <> const char* interface_name<PPB_ContentDecryptor_Private>() {
  return PPB_CONTENTDECRYPTOR_PRIVATE_INTERFACE;
}

}  // namespace

ContentDecryptor_Private::ContentDecryptor_Private(Instance* instance)
    : associated_instance_(instance) {
  Module::Get()->AddPluginInterface(kPPPContentDecryptorInterface,
                                    &ppp_content_decryptor);
  instance->AddPerInstanceObject(kPPPContentDecryptorInterface, this);
}

ContentDecryptor_Private::~ContentDecryptor_Private() {
  Instance::RemovePerInstanceObject(associated_instance_,
                                    kPPPContentDecryptorInterface,
                                    this);
}

void ContentDecryptor_Private::PromiseResolved(uint32_t promise_id) {
  if (has_interface<PPB_ContentDecryptor_Private>()) {
    get_interface<PPB_ContentDecryptor_Private>()->PromiseResolved(
        associated_instance_.pp_instance(), promise_id);
  }
}

void ContentDecryptor_Private::PromiseResolvedWithSession(
    uint32_t promise_id,
    const std::string& session_id) {
  if (has_interface<PPB_ContentDecryptor_Private>()) {
    pp::Var session_id_var(session_id);
    get_interface<PPB_ContentDecryptor_Private>()->PromiseResolvedWithSession(
        associated_instance_.pp_instance(), promise_id,
        session_id_var.pp_var());
  }
}

void ContentDecryptor_Private::PromiseRejected(
    uint32_t promise_id,
    PP_CdmExceptionCode exception_code,
    uint32_t system_code,
    const std::string& error_description) {
  if (has_interface<PPB_ContentDecryptor_Private>()) {
    pp::Var error_description_var(error_description);
    get_interface<PPB_ContentDecryptor_Private>()->PromiseRejected(
        associated_instance_.pp_instance(),
        promise_id,
        exception_code,
        system_code,
        error_description_var.pp_var());
  }
}

void ContentDecryptor_Private::SessionMessage(
    const std::string& session_id,
    PP_CdmMessageType message_type,
    pp::VarArrayBuffer message,
    const std::string& legacy_destination_url) {
  if (has_interface<PPB_ContentDecryptor_Private>()) {
    pp::Var session_id_var(session_id);
    pp::Var legacy_destination_url_var(legacy_destination_url);
    get_interface<PPB_ContentDecryptor_Private>()->SessionMessage(
        associated_instance_.pp_instance(), session_id_var.pp_var(),
        message_type, message.pp_var(), legacy_destination_url_var.pp_var());
  }
}

void ContentDecryptor_Private::SessionKeysChange(
    const std::string& session_id,
    bool has_additional_usable_key,
    const std::vector<PP_KeyInformation>& key_information) {
  if (has_interface<PPB_ContentDecryptor_Private>()) {
    pp::Var session_id_var(session_id);
    get_interface<PPB_ContentDecryptor_Private>()->SessionKeysChange(
        associated_instance_.pp_instance(), session_id_var.pp_var(),
        PP_FromBool(has_additional_usable_key), key_information.size(),
        key_information.empty() ? NULL : &key_information[0]);
  }
}

void ContentDecryptor_Private::SessionExpirationChange(
    const std::string& session_id,
    PP_Time new_expiry_time) {
  if (has_interface<PPB_ContentDecryptor_Private>()) {
    pp::Var session_id_var(session_id);
    get_interface<PPB_ContentDecryptor_Private>()->SessionExpirationChange(
        associated_instance_.pp_instance(), session_id_var.pp_var(),
        new_expiry_time);
  }
}

void ContentDecryptor_Private::SessionClosed(const std::string& session_id) {
  if (has_interface<PPB_ContentDecryptor_Private>()) {
    pp::Var session_id_var(session_id);
    get_interface<PPB_ContentDecryptor_Private>()->SessionClosed(
        associated_instance_.pp_instance(), session_id_var.pp_var());
  }
}

void ContentDecryptor_Private::SessionError(
    const std::string& session_id,
    PP_CdmExceptionCode exception_code,
    uint32_t system_code,
    const std::string& error_description) {
  if (has_interface<PPB_ContentDecryptor_Private>()) {
    pp::Var session_id_var(session_id);
    pp::Var error_description_var(error_description);
    get_interface<PPB_ContentDecryptor_Private>()->SessionError(
        associated_instance_.pp_instance(), session_id_var.pp_var(),
        exception_code, system_code, error_description_var.pp_var());
  }
}

void ContentDecryptor_Private::DeliverBlock(
    pp::Buffer_Dev decrypted_block,
    const PP_DecryptedBlockInfo& decrypted_block_info) {
  if (has_interface<PPB_ContentDecryptor_Private>()) {
    get_interface<PPB_ContentDecryptor_Private>()->DeliverBlock(
        associated_instance_.pp_instance(),
        decrypted_block.pp_resource(),
        &decrypted_block_info);
  }
}

void ContentDecryptor_Private::DecoderInitializeDone(
    PP_DecryptorStreamType decoder_type,
    uint32_t request_id,
    bool success) {
  if (has_interface<PPB_ContentDecryptor_Private>()) {
    get_interface<PPB_ContentDecryptor_Private>()->DecoderInitializeDone(
        associated_instance_.pp_instance(),
        decoder_type,
        request_id,
        PP_FromBool(success));
  }
}

void ContentDecryptor_Private::DecoderDeinitializeDone(
    PP_DecryptorStreamType decoder_type,
    uint32_t request_id) {
  if (has_interface<PPB_ContentDecryptor_Private>()) {
    get_interface<PPB_ContentDecryptor_Private>()->DecoderDeinitializeDone(
        associated_instance_.pp_instance(),
        decoder_type,
        request_id);
  }
}

void ContentDecryptor_Private::DecoderResetDone(
    PP_DecryptorStreamType decoder_type,
    uint32_t request_id) {
  if (has_interface<PPB_ContentDecryptor_Private>()) {
    get_interface<PPB_ContentDecryptor_Private>()->DecoderResetDone(
        associated_instance_.pp_instance(),
        decoder_type,
        request_id);
  }
}

void ContentDecryptor_Private::DeliverFrame(
    pp::Buffer_Dev decrypted_frame,
    const PP_DecryptedFrameInfo& decrypted_frame_info) {
  if (has_interface<PPB_ContentDecryptor_Private>()) {
    get_interface<PPB_ContentDecryptor_Private>()->DeliverFrame(
        associated_instance_.pp_instance(),
        decrypted_frame.pp_resource(),
        &decrypted_frame_info);
  }
}

void ContentDecryptor_Private::DeliverSamples(
    pp::Buffer_Dev audio_frames,
    const PP_DecryptedSampleInfo& decrypted_sample_info) {
  if (has_interface<PPB_ContentDecryptor_Private>()) {
    get_interface<PPB_ContentDecryptor_Private>()->DeliverSamples(
        associated_instance_.pp_instance(),
        audio_frames.pp_resource(),
        &decrypted_sample_info);
  }
}

}  // namespace pp