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
|
// 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 "media/filters/decrypting_demuxer_stream.h"
#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/single_thread_task_runner.h"
#include "media/base/bind_to_current_loop.h"
#include "media/base/decoder_buffer.h"
#include "media/base/media_log.h"
namespace media {
static bool IsStreamValidAndEncrypted(DemuxerStream* stream) {
return ((stream->type() == DemuxerStream::AUDIO &&
stream->audio_decoder_config().IsValidConfig() &&
stream->audio_decoder_config().is_encrypted()) ||
(stream->type() == DemuxerStream::VIDEO &&
stream->video_decoder_config().IsValidConfig() &&
stream->video_decoder_config().is_encrypted()));
}
DecryptingDemuxerStream::DecryptingDemuxerStream(
const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
const scoped_refptr<MediaLog>& media_log,
const base::Closure& waiting_for_decryption_key_cb)
: task_runner_(task_runner),
media_log_(media_log),
state_(kUninitialized),
waiting_for_decryption_key_cb_(waiting_for_decryption_key_cb),
demuxer_stream_(NULL),
decryptor_(NULL),
key_added_while_decrypt_pending_(false),
weak_factory_(this) {}
std::string DecryptingDemuxerStream::GetDisplayName() const {
return "DecryptingDemuxerStream";
}
void DecryptingDemuxerStream::Initialize(DemuxerStream* stream,
CdmContext* cdm_context,
const PipelineStatusCB& status_cb) {
DVLOG(2) << __FUNCTION__;
DCHECK(task_runner_->BelongsToCurrentThread());
DCHECK_EQ(state_, kUninitialized) << state_;
DCHECK(stream);
DCHECK(cdm_context);
DCHECK(!demuxer_stream_);
weak_this_ = weak_factory_.GetWeakPtr();
demuxer_stream_ = stream;
init_cb_ = BindToCurrentLoop(status_cb);
InitializeDecoderConfig();
if (!cdm_context->GetDecryptor()) {
MEDIA_LOG(DEBUG, media_log_) << GetDisplayName() << ": no decryptor";
state_ = kUninitialized;
base::ResetAndReturn(&init_cb_).Run(DECODER_ERROR_NOT_SUPPORTED);
return;
}
decryptor_ = cdm_context->GetDecryptor();
decryptor_->RegisterNewKeyCB(
GetDecryptorStreamType(),
BindToCurrentLoop(
base::Bind(&DecryptingDemuxerStream::OnKeyAdded, weak_this_)));
state_ = kIdle;
base::ResetAndReturn(&init_cb_).Run(PIPELINE_OK);
}
void DecryptingDemuxerStream::Read(const ReadCB& read_cb) {
DVLOG(3) << __FUNCTION__;
DCHECK(task_runner_->BelongsToCurrentThread());
DCHECK_EQ(state_, kIdle) << state_;
DCHECK(!read_cb.is_null());
CHECK(read_cb_.is_null()) << "Overlapping reads are not supported.";
read_cb_ = BindToCurrentLoop(read_cb);
state_ = kPendingDemuxerRead;
demuxer_stream_->Read(
base::Bind(&DecryptingDemuxerStream::DecryptBuffer, weak_this_));
}
void DecryptingDemuxerStream::Reset(const base::Closure& closure) {
DVLOG(2) << __FUNCTION__ << " - state: " << state_;
DCHECK(task_runner_->BelongsToCurrentThread());
DCHECK(state_ != kUninitialized) << state_;
DCHECK(reset_cb_.is_null());
reset_cb_ = BindToCurrentLoop(closure);
decryptor_->CancelDecrypt(GetDecryptorStreamType());
// Reset() cannot complete if the read callback is still pending.
// Defer the resetting process in this case. The |reset_cb_| will be fired
// after the read callback is fired - see DoDecryptBuffer() and
// DoDeliverBuffer().
if (state_ == kPendingDemuxerRead || state_ == kPendingDecrypt) {
DCHECK(!read_cb_.is_null());
return;
}
if (state_ == kWaitingForKey) {
DCHECK(!read_cb_.is_null());
pending_buffer_to_decrypt_ = NULL;
base::ResetAndReturn(&read_cb_).Run(kAborted, NULL);
}
DCHECK(read_cb_.is_null());
DoReset();
}
AudioDecoderConfig DecryptingDemuxerStream::audio_decoder_config() {
DCHECK(state_ != kUninitialized) << state_;
CHECK_EQ(demuxer_stream_->type(), AUDIO);
return audio_config_;
}
VideoDecoderConfig DecryptingDemuxerStream::video_decoder_config() {
DCHECK(state_ != kUninitialized) << state_;
CHECK_EQ(demuxer_stream_->type(), VIDEO);
return video_config_;
}
DemuxerStream::Type DecryptingDemuxerStream::type() const {
DCHECK(state_ != kUninitialized) << state_;
return demuxer_stream_->type();
}
DemuxerStream::Liveness DecryptingDemuxerStream::liveness() const {
DCHECK(state_ != kUninitialized) << state_;
return demuxer_stream_->liveness();
}
void DecryptingDemuxerStream::EnableBitstreamConverter() {
demuxer_stream_->EnableBitstreamConverter();
}
bool DecryptingDemuxerStream::SupportsConfigChanges() {
return demuxer_stream_->SupportsConfigChanges();
}
VideoRotation DecryptingDemuxerStream::video_rotation() {
return demuxer_stream_->video_rotation();
}
DecryptingDemuxerStream::~DecryptingDemuxerStream() {
DVLOG(2) << __FUNCTION__ << " : state_ = " << state_;
DCHECK(task_runner_->BelongsToCurrentThread());
if (state_ == kUninitialized)
return;
if (decryptor_) {
decryptor_->CancelDecrypt(GetDecryptorStreamType());
decryptor_ = NULL;
}
if (!init_cb_.is_null())
base::ResetAndReturn(&init_cb_).Run(PIPELINE_ERROR_ABORT);
if (!read_cb_.is_null())
base::ResetAndReturn(&read_cb_).Run(kAborted, NULL);
if (!reset_cb_.is_null())
base::ResetAndReturn(&reset_cb_).Run();
pending_buffer_to_decrypt_ = NULL;
}
void DecryptingDemuxerStream::DecryptBuffer(
DemuxerStream::Status status,
const scoped_refptr<DecoderBuffer>& buffer) {
DVLOG(3) << __FUNCTION__;
DCHECK(task_runner_->BelongsToCurrentThread());
DCHECK_EQ(state_, kPendingDemuxerRead) << state_;
DCHECK(!read_cb_.is_null());
DCHECK_EQ(buffer.get() != NULL, status == kOk) << status;
// Even when |!reset_cb_.is_null()|, we need to pass |kConfigChanged| back to
// the caller so that the downstream decoder can be properly reinitialized.
if (status == kConfigChanged) {
DVLOG(2) << "DoDecryptBuffer() - kConfigChanged.";
DCHECK_EQ(demuxer_stream_->type() == AUDIO, audio_config_.IsValidConfig());
DCHECK_EQ(demuxer_stream_->type() == VIDEO, video_config_.IsValidConfig());
// Update the decoder config, which the decoder will use when it is notified
// of kConfigChanged.
InitializeDecoderConfig();
state_ = kIdle;
base::ResetAndReturn(&read_cb_).Run(kConfigChanged, NULL);
if (!reset_cb_.is_null())
DoReset();
return;
}
if (!reset_cb_.is_null()) {
base::ResetAndReturn(&read_cb_).Run(kAborted, NULL);
DoReset();
return;
}
if (status == kAborted) {
DVLOG(2) << "DoDecryptBuffer() - kAborted.";
state_ = kIdle;
base::ResetAndReturn(&read_cb_).Run(kAborted, NULL);
return;
}
if (buffer->end_of_stream()) {
DVLOG(2) << "DoDecryptBuffer() - EOS buffer.";
state_ = kIdle;
base::ResetAndReturn(&read_cb_).Run(status, buffer);
return;
}
DCHECK(buffer->decrypt_config());
// An empty iv string signals that the frame is unencrypted.
if (buffer->decrypt_config()->iv().empty()) {
DVLOG(2) << "DoDecryptBuffer() - clear buffer.";
scoped_refptr<DecoderBuffer> decrypted = DecoderBuffer::CopyFrom(
buffer->data(), buffer->data_size());
decrypted->set_timestamp(buffer->timestamp());
decrypted->set_duration(buffer->duration());
if (buffer->is_key_frame())
decrypted->set_is_key_frame(true);
state_ = kIdle;
base::ResetAndReturn(&read_cb_).Run(kOk, decrypted);
return;
}
pending_buffer_to_decrypt_ = buffer;
state_ = kPendingDecrypt;
DecryptPendingBuffer();
}
void DecryptingDemuxerStream::DecryptPendingBuffer() {
DCHECK(task_runner_->BelongsToCurrentThread());
DCHECK_EQ(state_, kPendingDecrypt) << state_;
decryptor_->Decrypt(
GetDecryptorStreamType(),
pending_buffer_to_decrypt_,
BindToCurrentLoop(
base::Bind(&DecryptingDemuxerStream::DeliverBuffer, weak_this_)));
}
void DecryptingDemuxerStream::DeliverBuffer(
Decryptor::Status status,
const scoped_refptr<DecoderBuffer>& decrypted_buffer) {
DVLOG(3) << __FUNCTION__ << " - status: " << status;
DCHECK(task_runner_->BelongsToCurrentThread());
DCHECK_EQ(state_, kPendingDecrypt) << state_;
DCHECK_NE(status, Decryptor::kNeedMoreData);
DCHECK(!read_cb_.is_null());
DCHECK(pending_buffer_to_decrypt_.get());
bool need_to_try_again_if_nokey = key_added_while_decrypt_pending_;
key_added_while_decrypt_pending_ = false;
if (!reset_cb_.is_null()) {
pending_buffer_to_decrypt_ = NULL;
base::ResetAndReturn(&read_cb_).Run(kAborted, NULL);
DoReset();
return;
}
DCHECK_EQ(status == Decryptor::kSuccess, decrypted_buffer.get() != NULL);
if (status == Decryptor::kError) {
DVLOG(2) << "DoDeliverBuffer() - kError";
MEDIA_LOG(ERROR, media_log_) << GetDisplayName() << ": decrypt error";
pending_buffer_to_decrypt_ = NULL;
state_ = kIdle;
base::ResetAndReturn(&read_cb_).Run(kAborted, NULL);
return;
}
if (status == Decryptor::kNoKey) {
DVLOG(2) << "DoDeliverBuffer() - kNoKey";
MEDIA_LOG(DEBUG, media_log_) << GetDisplayName() << ": no key";
if (need_to_try_again_if_nokey) {
// The |state_| is still kPendingDecrypt.
DecryptPendingBuffer();
return;
}
state_ = kWaitingForKey;
waiting_for_decryption_key_cb_.Run();
return;
}
DCHECK_EQ(status, Decryptor::kSuccess);
// Copy the key frame flag from the encrypted to decrypted buffer, assuming
// that the decryptor initialized the flag to false.
if (pending_buffer_to_decrypt_->is_key_frame())
decrypted_buffer->set_is_key_frame(true);
pending_buffer_to_decrypt_ = NULL;
state_ = kIdle;
base::ResetAndReturn(&read_cb_).Run(kOk, decrypted_buffer);
}
void DecryptingDemuxerStream::OnKeyAdded() {
DCHECK(task_runner_->BelongsToCurrentThread());
if (state_ == kPendingDecrypt) {
key_added_while_decrypt_pending_ = true;
return;
}
if (state_ == kWaitingForKey) {
state_ = kPendingDecrypt;
DecryptPendingBuffer();
}
}
void DecryptingDemuxerStream::DoReset() {
DCHECK(state_ != kUninitialized);
DCHECK(init_cb_.is_null());
DCHECK(read_cb_.is_null());
state_ = kIdle;
base::ResetAndReturn(&reset_cb_).Run();
}
Decryptor::StreamType DecryptingDemuxerStream::GetDecryptorStreamType() const {
if (demuxer_stream_->type() == AUDIO)
return Decryptor::kAudio;
DCHECK_EQ(demuxer_stream_->type(), VIDEO);
return Decryptor::kVideo;
}
void DecryptingDemuxerStream::InitializeDecoderConfig() {
// The decoder selector or upstream demuxer make sure the stream is valid and
// potentially encrypted.
DCHECK(IsStreamValidAndEncrypted(demuxer_stream_));
switch (demuxer_stream_->type()) {
case AUDIO: {
AudioDecoderConfig input_audio_config =
demuxer_stream_->audio_decoder_config();
audio_config_.Initialize(input_audio_config.codec(),
input_audio_config.sample_format(),
input_audio_config.channel_layout(),
input_audio_config.samples_per_second(),
input_audio_config.extra_data(),
false, // Output audio is not encrypted.
input_audio_config.seek_preroll(),
input_audio_config.codec_delay());
break;
}
case VIDEO: {
VideoDecoderConfig input_video_config =
demuxer_stream_->video_decoder_config();
video_config_.Initialize(
input_video_config.codec(), input_video_config.profile(),
input_video_config.format(), input_video_config.color_space(),
input_video_config.coded_size(), input_video_config.visible_rect(),
input_video_config.natural_size(), input_video_config.extra_data(),
false); // Output video is not encrypted.
break;
}
default:
NOTREACHED();
return;
}
}
} // namespace media
|