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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
|
// Copyright 2013 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 "content/renderer/media/crypto/ppapi_decryptor.h"
#include <string>
#include "base/bind.h"
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "base/message_loop/message_loop_proxy.h"
#include "content/renderer/media/crypto/cdm_initialized_promise.h"
#include "content/renderer/pepper/content_decryptor_delegate.h"
#include "content/renderer/pepper/pepper_plugin_instance_impl.h"
#include "media/base/audio_decoder_config.h"
#include "media/base/cdm_key_information.h"
#include "media/base/data_buffer.h"
#include "media/base/decoder_buffer.h"
#include "media/base/key_systems.h"
#include "media/base/video_decoder_config.h"
#include "media/base/video_frame.h"
namespace content {
void PpapiDecryptor::Create(
const std::string& key_system,
bool allow_distinctive_identifier,
bool allow_persistent_state,
const GURL& security_origin,
const CreatePepperCdmCB& create_pepper_cdm_cb,
const media::SessionMessageCB& session_message_cb,
const media::SessionClosedCB& session_closed_cb,
const media::LegacySessionErrorCB& legacy_session_error_cb,
const media::SessionKeysChangeCB& session_keys_change_cb,
const media::SessionExpirationUpdateCB& session_expiration_update_cb,
const media::CdmCreatedCB& cdm_created_cb) {
std::string plugin_type = media::GetPepperType(key_system);
DCHECK(!plugin_type.empty());
scoped_ptr<PepperCdmWrapper> pepper_cdm_wrapper =
create_pepper_cdm_cb.Run(plugin_type, security_origin);
if (!pepper_cdm_wrapper) {
DLOG(ERROR) << "Plugin instance creation failed.";
base::MessageLoopProxy::current()->PostTask(
FROM_HERE, base::Bind(cdm_created_cb, nullptr,
"Plugin instance creation failed."));
}
scoped_ptr<PpapiDecryptor> ppapi_decryptor(
new PpapiDecryptor(pepper_cdm_wrapper.Pass(), session_message_cb,
session_closed_cb, legacy_session_error_cb,
session_keys_change_cb, session_expiration_update_cb));
// PpapiDecryptor ownership passed to the promise, but keep a copy in order
// to call InitializeCdm().
PpapiDecryptor* ppapi_decryptor_copy = ppapi_decryptor.get();
scoped_ptr<CdmInitializedPromise> promise(
new CdmInitializedPromise(cdm_created_cb, ppapi_decryptor.Pass()));
ppapi_decryptor_copy->InitializeCdm(key_system, allow_distinctive_identifier,
allow_persistent_state, promise.Pass());
}
PpapiDecryptor::PpapiDecryptor(
scoped_ptr<PepperCdmWrapper> pepper_cdm_wrapper,
const media::SessionMessageCB& session_message_cb,
const media::SessionClosedCB& session_closed_cb,
const media::LegacySessionErrorCB& legacy_session_error_cb,
const media::SessionKeysChangeCB& session_keys_change_cb,
const media::SessionExpirationUpdateCB& session_expiration_update_cb)
: pepper_cdm_wrapper_(pepper_cdm_wrapper.Pass()),
session_message_cb_(session_message_cb),
session_closed_cb_(session_closed_cb),
legacy_session_error_cb_(legacy_session_error_cb),
session_keys_change_cb_(session_keys_change_cb),
session_expiration_update_cb_(session_expiration_update_cb),
render_loop_proxy_(base::MessageLoopProxy::current()),
weak_ptr_factory_(this) {
DCHECK(pepper_cdm_wrapper_.get());
DCHECK(!session_message_cb_.is_null());
DCHECK(!session_closed_cb_.is_null());
DCHECK(!legacy_session_error_cb_.is_null());
DCHECK(!session_keys_change_cb.is_null());
DCHECK(!session_expiration_update_cb.is_null());
}
PpapiDecryptor::~PpapiDecryptor() {
pepper_cdm_wrapper_.reset();
}
void PpapiDecryptor::InitializeCdm(
const std::string& key_system,
bool allow_distinctive_identifier,
bool allow_persistent_state,
scoped_ptr<media::SimpleCdmPromise> promise) {
base::WeakPtr<PpapiDecryptor> weak_this = weak_ptr_factory_.GetWeakPtr();
CdmDelegate()->Initialize(
key_system, allow_distinctive_identifier, allow_persistent_state,
base::Bind(&PpapiDecryptor::OnSessionMessage, weak_this),
base::Bind(&PpapiDecryptor::OnSessionClosed, weak_this),
base::Bind(&PpapiDecryptor::OnLegacySessionError, weak_this),
base::Bind(&PpapiDecryptor::OnSessionKeysChange, weak_this),
base::Bind(&PpapiDecryptor::OnSessionExpirationUpdate, weak_this),
base::Bind(&PpapiDecryptor::OnFatalPluginError, weak_this),
promise.Pass());
}
void PpapiDecryptor::SetServerCertificate(
const std::vector<uint8_t>& certificate,
scoped_ptr<media::SimpleCdmPromise> promise) {
DVLOG(2) << __FUNCTION__;
DCHECK(render_loop_proxy_->BelongsToCurrentThread());
if (!CdmDelegate()) {
promise->reject(INVALID_STATE_ERROR, 0, "CdmDelegate() does not exist.");
return;
}
CdmDelegate()->SetServerCertificate(certificate, promise.Pass());
}
void PpapiDecryptor::CreateSessionAndGenerateRequest(
SessionType session_type,
media::EmeInitDataType init_data_type,
const std::vector<uint8_t>& init_data,
scoped_ptr<media::NewSessionCdmPromise> promise) {
DVLOG(2) << __FUNCTION__;
DCHECK(render_loop_proxy_->BelongsToCurrentThread());
if (!CdmDelegate()) {
promise->reject(INVALID_STATE_ERROR, 0, "CdmDelegate() does not exist.");
return;
}
CdmDelegate()->CreateSessionAndGenerateRequest(session_type, init_data_type,
init_data, promise.Pass());
}
void PpapiDecryptor::LoadSession(
SessionType session_type,
const std::string& session_id,
scoped_ptr<media::NewSessionCdmPromise> promise) {
DVLOG(2) << __FUNCTION__;
DCHECK(render_loop_proxy_->BelongsToCurrentThread());
if (!CdmDelegate()) {
promise->reject(INVALID_STATE_ERROR, 0, "CdmDelegate() does not exist.");
return;
}
CdmDelegate()->LoadSession(session_type, session_id, promise.Pass());
}
void PpapiDecryptor::UpdateSession(
const std::string& session_id,
const std::vector<uint8_t>& response,
scoped_ptr<media::SimpleCdmPromise> promise) {
DCHECK(render_loop_proxy_->BelongsToCurrentThread());
if (!CdmDelegate()) {
promise->reject(INVALID_STATE_ERROR, 0, "CdmDelegate() does not exist.");
return;
}
CdmDelegate()->UpdateSession(session_id, response, promise.Pass());
}
void PpapiDecryptor::CloseSession(const std::string& session_id,
scoped_ptr<media::SimpleCdmPromise> promise) {
DCHECK(render_loop_proxy_->BelongsToCurrentThread());
if (!CdmDelegate()) {
promise->reject(INVALID_STATE_ERROR, 0, "CdmDelegate() does not exist.");
return;
}
CdmDelegate()->CloseSession(session_id, promise.Pass());
}
void PpapiDecryptor::RemoveSession(
const std::string& session_id,
scoped_ptr<media::SimpleCdmPromise> promise) {
DCHECK(render_loop_proxy_->BelongsToCurrentThread());
if (!CdmDelegate()) {
promise->reject(INVALID_STATE_ERROR, 0, "CdmDelegate() does not exist.");
return;
}
CdmDelegate()->RemoveSession(session_id, promise.Pass());
}
media::CdmContext* PpapiDecryptor::GetCdmContext() {
return this;
}
media::Decryptor* PpapiDecryptor::GetDecryptor() {
return this;
}
int PpapiDecryptor::GetCdmId() const {
return kInvalidCdmId;
}
void PpapiDecryptor::RegisterNewKeyCB(StreamType stream_type,
const NewKeyCB& new_key_cb) {
if (!render_loop_proxy_->BelongsToCurrentThread()) {
render_loop_proxy_->PostTask(FROM_HERE,
base::Bind(&PpapiDecryptor::RegisterNewKeyCB,
weak_ptr_factory_.GetWeakPtr(),
stream_type,
new_key_cb));
return;
}
DVLOG(3) << __FUNCTION__ << " - stream_type: " << stream_type;
switch (stream_type) {
case kAudio:
new_audio_key_cb_ = new_key_cb;
break;
case kVideo:
new_video_key_cb_ = new_key_cb;
break;
default:
NOTREACHED();
}
}
void PpapiDecryptor::Decrypt(
StreamType stream_type,
const scoped_refptr<media::DecoderBuffer>& encrypted,
const DecryptCB& decrypt_cb) {
if (!render_loop_proxy_->BelongsToCurrentThread()) {
render_loop_proxy_->PostTask(FROM_HERE,
base::Bind(&PpapiDecryptor::Decrypt,
weak_ptr_factory_.GetWeakPtr(),
stream_type,
encrypted,
decrypt_cb));
return;
}
DVLOG(3) << __FUNCTION__ << " - stream_type: " << stream_type;
if (!CdmDelegate() ||
!CdmDelegate()->Decrypt(stream_type, encrypted, decrypt_cb)) {
decrypt_cb.Run(kError, NULL);
}
}
void PpapiDecryptor::CancelDecrypt(StreamType stream_type) {
if (!render_loop_proxy_->BelongsToCurrentThread()) {
render_loop_proxy_->PostTask(FROM_HERE,
base::Bind(&PpapiDecryptor::CancelDecrypt,
weak_ptr_factory_.GetWeakPtr(),
stream_type));
return;
}
DVLOG(1) << __FUNCTION__ << " - stream_type: " << stream_type;
if (CdmDelegate())
CdmDelegate()->CancelDecrypt(stream_type);
}
void PpapiDecryptor::InitializeAudioDecoder(
const media::AudioDecoderConfig& config,
const DecoderInitCB& init_cb) {
if (!render_loop_proxy_->BelongsToCurrentThread()) {
render_loop_proxy_->PostTask(
FROM_HERE,
base::Bind(&PpapiDecryptor::InitializeAudioDecoder,
weak_ptr_factory_.GetWeakPtr(),
config,
init_cb));
return;
}
DVLOG(2) << __FUNCTION__;
DCHECK(config.is_encrypted());
DCHECK(config.IsValidConfig());
audio_decoder_init_cb_ = init_cb;
if (!CdmDelegate() || !CdmDelegate()->InitializeAudioDecoder(
config,
base::Bind(&PpapiDecryptor::OnDecoderInitialized,
weak_ptr_factory_.GetWeakPtr(),
kAudio))) {
base::ResetAndReturn(&audio_decoder_init_cb_).Run(false);
return;
}
}
void PpapiDecryptor::InitializeVideoDecoder(
const media::VideoDecoderConfig& config,
const DecoderInitCB& init_cb) {
if (!render_loop_proxy_->BelongsToCurrentThread()) {
render_loop_proxy_->PostTask(
FROM_HERE,
base::Bind(&PpapiDecryptor::InitializeVideoDecoder,
weak_ptr_factory_.GetWeakPtr(),
config,
init_cb));
return;
}
DVLOG(2) << __FUNCTION__;
DCHECK(config.is_encrypted());
DCHECK(config.IsValidConfig());
video_decoder_init_cb_ = init_cb;
if (!CdmDelegate() || !CdmDelegate()->InitializeVideoDecoder(
config,
base::Bind(&PpapiDecryptor::OnDecoderInitialized,
weak_ptr_factory_.GetWeakPtr(),
kVideo))) {
base::ResetAndReturn(&video_decoder_init_cb_).Run(false);
return;
}
}
void PpapiDecryptor::DecryptAndDecodeAudio(
const scoped_refptr<media::DecoderBuffer>& encrypted,
const AudioDecodeCB& audio_decode_cb) {
if (!render_loop_proxy_->BelongsToCurrentThread()) {
render_loop_proxy_->PostTask(
FROM_HERE,
base::Bind(&PpapiDecryptor::DecryptAndDecodeAudio,
weak_ptr_factory_.GetWeakPtr(),
encrypted,
audio_decode_cb));
return;
}
DVLOG(3) << __FUNCTION__;
if (!CdmDelegate() ||
!CdmDelegate()->DecryptAndDecodeAudio(encrypted, audio_decode_cb)) {
audio_decode_cb.Run(kError, AudioFrames());
}
}
void PpapiDecryptor::DecryptAndDecodeVideo(
const scoped_refptr<media::DecoderBuffer>& encrypted,
const VideoDecodeCB& video_decode_cb) {
if (!render_loop_proxy_->BelongsToCurrentThread()) {
render_loop_proxy_->PostTask(
FROM_HERE,
base::Bind(&PpapiDecryptor::DecryptAndDecodeVideo,
weak_ptr_factory_.GetWeakPtr(),
encrypted,
video_decode_cb));
return;
}
DVLOG(3) << __FUNCTION__;
if (!CdmDelegate() ||
!CdmDelegate()->DecryptAndDecodeVideo(encrypted, video_decode_cb)) {
video_decode_cb.Run(kError, NULL);
}
}
void PpapiDecryptor::ResetDecoder(StreamType stream_type) {
if (!render_loop_proxy_->BelongsToCurrentThread()) {
render_loop_proxy_->PostTask(FROM_HERE,
base::Bind(&PpapiDecryptor::ResetDecoder,
weak_ptr_factory_.GetWeakPtr(),
stream_type));
return;
}
DVLOG(2) << __FUNCTION__ << " - stream_type: " << stream_type;
if (CdmDelegate())
CdmDelegate()->ResetDecoder(stream_type);
}
void PpapiDecryptor::DeinitializeDecoder(StreamType stream_type) {
if (!render_loop_proxy_->BelongsToCurrentThread()) {
render_loop_proxy_->PostTask(
FROM_HERE,
base::Bind(&PpapiDecryptor::DeinitializeDecoder,
weak_ptr_factory_.GetWeakPtr(),
stream_type));
return;
}
DVLOG(2) << __FUNCTION__ << " - stream_type: " << stream_type;
if (CdmDelegate())
CdmDelegate()->DeinitializeDecoder(stream_type);
}
void PpapiDecryptor::OnDecoderInitialized(StreamType stream_type,
bool success) {
DCHECK(render_loop_proxy_->BelongsToCurrentThread());
switch (stream_type) {
case kAudio:
DCHECK(!audio_decoder_init_cb_.is_null());
base::ResetAndReturn(&audio_decoder_init_cb_).Run(success);
break;
case kVideo:
DCHECK(!video_decoder_init_cb_.is_null());
base::ResetAndReturn(&video_decoder_init_cb_).Run(success);
break;
default:
NOTREACHED();
}
}
void PpapiDecryptor::OnSessionMessage(const std::string& session_id,
MessageType message_type,
const std::vector<uint8_t>& message,
const GURL& legacy_destination_url) {
DCHECK(render_loop_proxy_->BelongsToCurrentThread());
session_message_cb_.Run(session_id, message_type, message,
legacy_destination_url);
}
void PpapiDecryptor::OnSessionKeysChange(const std::string& session_id,
bool has_additional_usable_key,
media::CdmKeysInfo keys_info) {
DCHECK(render_loop_proxy_->BelongsToCurrentThread());
// TODO(jrummell): Handling resume playback should be done in the media
// player, not in the Decryptors. http://crbug.com/413413.
if (has_additional_usable_key)
AttemptToResumePlayback();
session_keys_change_cb_.Run(session_id, has_additional_usable_key,
keys_info.Pass());
}
void PpapiDecryptor::OnSessionExpirationUpdate(
const std::string& session_id,
const base::Time& new_expiry_time) {
DCHECK(render_loop_proxy_->BelongsToCurrentThread());
session_expiration_update_cb_.Run(session_id, new_expiry_time);
}
void PpapiDecryptor::OnSessionClosed(const std::string& session_id) {
DCHECK(render_loop_proxy_->BelongsToCurrentThread());
session_closed_cb_.Run(session_id);
}
void PpapiDecryptor::OnLegacySessionError(
const std::string& session_id,
MediaKeys::Exception exception_code,
uint32_t system_code,
const std::string& error_description) {
DCHECK(render_loop_proxy_->BelongsToCurrentThread());
legacy_session_error_cb_.Run(session_id, exception_code, system_code,
error_description);
}
void PpapiDecryptor::AttemptToResumePlayback() {
if (!new_audio_key_cb_.is_null())
new_audio_key_cb_.Run();
if (!new_video_key_cb_.is_null())
new_video_key_cb_.Run();
}
void PpapiDecryptor::OnFatalPluginError() {
DCHECK(render_loop_proxy_->BelongsToCurrentThread());
pepper_cdm_wrapper_.reset();
}
ContentDecryptorDelegate* PpapiDecryptor::CdmDelegate() {
DCHECK(render_loop_proxy_->BelongsToCurrentThread());
return (pepper_cdm_wrapper_) ? pepper_cdm_wrapper_->GetCdmDelegate() : NULL;
}
} // namespace content
|