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 "media/filters/decrypting_video_decoder.h"
8 #include "base/callback_helpers.h"
9 #include "base/location.h"
10 #include "base/logging.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/trace_event/trace_event.h"
14 #include "media/base/bind_to_current_loop.h"
15 #include "media/base/cdm_context.h"
16 #include "media/base/decoder_buffer.h"
17 #include "media/base/media_log.h"
18 #include "media/base/video_frame.h"
22 const char DecryptingVideoDecoder::kDecoderName[] = "DecryptingVideoDecoder";
24 DecryptingVideoDecoder::DecryptingVideoDecoder(
25 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
27 : task_runner_(task_runner), media_log_(media_log), weak_factory_(this) {}
29 std::string DecryptingVideoDecoder::GetDisplayName() const {
33 void DecryptingVideoDecoder::Initialize(const VideoDecoderConfig& config,
35 CdmContext* cdm_context,
36 const InitCB& init_cb,
37 const OutputCB& output_cb,
38 const WaitingCB& waiting_cb) {
39 DVLOG(2) << __func__ << ": " << config.AsHumanReadableString();
41 DCHECK(task_runner_->BelongsToCurrentThread());
42 DCHECK(state_ == kUninitialized || state_ == kIdle ||
43 state_ == kDecodeFinished)
47 DCHECK(config.IsValidConfig());
49 init_cb_ = BindToCurrentLoop(init_cb);
51 // Once we have a CDM context, one should always be present.
52 DCHECK(!support_clear_content_);
53 std::move(init_cb_).Run(false);
57 if (!config.is_encrypted() && !support_clear_content_) {
58 std::move(init_cb_).Run(false);
62 // Once initialized with encryption support, the value is sticky, so we'll use
63 // the decryptor for clear content as well.
64 support_clear_content_ = true;
66 output_cb_ = BindToCurrentLoop(output_cb);
67 weak_this_ = weak_factory_.GetWeakPtr();
71 waiting_cb_ = waiting_cb;
73 if (state_ == kUninitialized) {
74 if (!cdm_context->GetDecryptor()) {
75 DVLOG(1) << __func__ << ": no decryptor";
76 std::move(init_cb_).Run(false);
80 decryptor_ = cdm_context->GetDecryptor();
82 // Reinitialization (i.e. upon a config change). The new config can be
83 // encrypted or clear.
84 decryptor_->DeinitializeDecoder(Decryptor::kVideo);
87 state_ = kPendingDecoderInit;
88 decryptor_->InitializeVideoDecoder(
89 config_, BindToCurrentLoop(base::Bind(
90 &DecryptingVideoDecoder::FinishInitialization, weak_this_)));
93 void DecryptingVideoDecoder::Decode(scoped_refptr<DecoderBuffer> buffer,
94 const DecodeCB& decode_cb) {
95 DVLOG(3) << "Decode()";
96 DCHECK(task_runner_->BelongsToCurrentThread());
97 DCHECK(state_ == kIdle || state_ == kDecodeFinished || state_ == kError)
100 CHECK(!decode_cb_) << "Overlapping decodes are not supported.";
102 decode_cb_ = BindToCurrentLoop(decode_cb);
104 if (state_ == kError) {
105 std::move(decode_cb_).Run(DecodeStatus::DECODE_ERROR);
109 // Return empty frames if decoding has finished.
110 if (state_ == kDecodeFinished) {
111 std::move(decode_cb_).Run(DecodeStatus::OK);
115 pending_buffer_to_decode_ = std::move(buffer);
116 state_ = kPendingDecode;
117 DecodePendingBuffer();
120 void DecryptingVideoDecoder::Reset(const base::Closure& closure) {
121 DVLOG(2) << "Reset() - state: " << state_;
122 DCHECK(task_runner_->BelongsToCurrentThread());
123 DCHECK(state_ == kIdle || state_ == kPendingDecode ||
124 state_ == kWaitingForKey || state_ == kDecodeFinished ||
127 DCHECK(!init_cb_); // No Reset() during pending initialization.
130 reset_cb_ = BindToCurrentLoop(closure);
132 decryptor_->ResetDecoder(Decryptor::kVideo);
134 // Reset() cannot complete if the decode callback is still pending.
135 // Defer the resetting process in this case. The |reset_cb_| will be fired
136 // after the decode callback is fired - see DecryptAndDecodeBuffer() and
138 if (state_ == kPendingDecode) {
143 if (state_ == kWaitingForKey) {
144 CompleteWaitingForDecryptionKey();
146 pending_buffer_to_decode_ = NULL;
147 std::move(decode_cb_).Run(DecodeStatus::ABORTED);
154 DecryptingVideoDecoder::~DecryptingVideoDecoder() {
155 DCHECK(task_runner_->BelongsToCurrentThread());
157 if (state_ == kUninitialized)
160 if (state_ == kWaitingForKey)
161 CompleteWaitingForDecryptionKey();
162 if (state_ == kPendingDecode)
163 CompletePendingDecode(Decryptor::kError);
166 decryptor_->DeinitializeDecoder(Decryptor::kVideo);
169 pending_buffer_to_decode_ = NULL;
171 std::move(init_cb_).Run(false);
173 std::move(decode_cb_).Run(DecodeStatus::ABORTED);
175 std::move(reset_cb_).Run();
178 void DecryptingVideoDecoder::FinishInitialization(bool success) {
179 DVLOG(2) << "FinishInitialization()";
180 DCHECK(task_runner_->BelongsToCurrentThread());
181 DCHECK_EQ(state_, kPendingDecoderInit) << state_;
183 DCHECK(!reset_cb_); // No Reset() before initialization finished.
184 DCHECK(!decode_cb_); // No Decode() before initialization finished.
187 DVLOG(1) << __func__ << ": failed to init video decoder on decryptor";
188 std::move(init_cb_).Run(false);
194 decryptor_->RegisterNewKeyCB(
195 Decryptor::kVideo, BindToCurrentLoop(base::Bind(
196 &DecryptingVideoDecoder::OnKeyAdded, weak_this_)));
200 std::move(init_cb_).Run(true);
203 void DecryptingVideoDecoder::DecodePendingBuffer() {
204 DCHECK(task_runner_->BelongsToCurrentThread());
205 DCHECK_EQ(state_, kPendingDecode) << state_;
207 // Note: Traces require a unique ID per decode, if we ever support multiple
208 // in flight decodes, the trace begin+end macros need the same unique id.
209 DCHECK_EQ(GetMaxDecodeRequests(), 1);
210 TRACE_EVENT_ASYNC_BEGIN1(
211 "media", "DecryptingVideoDecoder::DecodePendingBuffer", this,
213 pending_buffer_to_decode_->end_of_stream()
215 : pending_buffer_to_decode_->timestamp().InMicroseconds());
217 decryptor_->DecryptAndDecodeVideo(
218 pending_buffer_to_decode_,
219 BindToCurrentLoop(base::BindRepeating(
220 &DecryptingVideoDecoder::DeliverFrame, weak_this_)));
223 void DecryptingVideoDecoder::DeliverFrame(
224 Decryptor::Status status,
225 const scoped_refptr<VideoFrame>& frame) {
226 DVLOG(3) << "DeliverFrame() - status: " << status;
227 DCHECK(task_runner_->BelongsToCurrentThread());
228 DCHECK_EQ(state_, kPendingDecode) << state_;
230 DCHECK(pending_buffer_to_decode_.get());
231 CompletePendingDecode(status);
233 bool need_to_try_again_if_nokey_is_returned = key_added_while_decode_pending_;
234 key_added_while_decode_pending_ = false;
236 scoped_refptr<DecoderBuffer> scoped_pending_buffer_to_decode =
237 std::move(pending_buffer_to_decode_);
240 std::move(decode_cb_).Run(DecodeStatus::ABORTED);
245 DCHECK_EQ(status == Decryptor::kSuccess, frame.get() != NULL);
247 if (status == Decryptor::kError) {
248 DVLOG(2) << "DeliverFrame() - kError";
249 MEDIA_LOG(ERROR, media_log_) << GetDisplayName() << ": decode error";
251 std::move(decode_cb_).Run(DecodeStatus::DECODE_ERROR);
255 if (status == Decryptor::kNoKey) {
257 scoped_pending_buffer_to_decode->decrypt_config()->key_id();
258 std::string log_message =
259 "no key for key ID " + base::HexEncode(key_id.data(), key_id.size()) +
260 "; will resume decoding after new usable key is available";
261 DVLOG(1) << __func__ << ": " << log_message;
262 MEDIA_LOG(INFO, media_log_) << GetDisplayName() << ": " << log_message;
264 // Set |pending_buffer_to_decode_| back as we need to try decoding the
265 // pending buffer again when new key is added to the decryptor.
266 pending_buffer_to_decode_ = std::move(scoped_pending_buffer_to_decode);
268 if (need_to_try_again_if_nokey_is_returned) {
269 // The |state_| is still kPendingDecode.
270 MEDIA_LOG(INFO, media_log_)
271 << GetDisplayName() << ": key was added, resuming decode";
272 DecodePendingBuffer();
276 TRACE_EVENT_ASYNC_BEGIN0(
277 "media", "DecryptingVideoDecoder::WaitingForDecryptionKey", this);
278 state_ = kWaitingForKey;
279 waiting_cb_.Run(WaitingReason::kNoDecryptionKey);
283 if (status == Decryptor::kNeedMoreData) {
284 DVLOG(2) << "DeliverFrame() - kNeedMoreData";
285 state_ = scoped_pending_buffer_to_decode->end_of_stream() ? kDecodeFinished
287 std::move(decode_cb_).Run(DecodeStatus::OK);
291 DCHECK_EQ(status, Decryptor::kSuccess);
294 // Frame returned with kSuccess should not be an end-of-stream frame.
295 DCHECK(!frame->metadata()->IsTrue(VideoFrameMetadata::END_OF_STREAM));
297 // If color space is not set, use the color space in the |config_|.
298 if (!frame->ColorSpace().IsValid()) {
299 DVLOG(3) << "Setting color space using information in the config.";
300 if (config_.color_space_info().IsSpecified())
301 frame->set_color_space(config_.color_space_info().ToGfxColorSpace());
304 output_cb_.Run(frame);
306 if (scoped_pending_buffer_to_decode->end_of_stream()) {
307 // Set |pending_buffer_to_decode_| back as we need to keep flushing the
309 pending_buffer_to_decode_ = std::move(scoped_pending_buffer_to_decode);
310 DecodePendingBuffer();
315 std::move(decode_cb_).Run(DecodeStatus::OK);
318 void DecryptingVideoDecoder::OnKeyAdded() {
319 DVLOG(2) << "OnKeyAdded()";
320 DCHECK(task_runner_->BelongsToCurrentThread());
322 if (state_ == kPendingDecode) {
323 key_added_while_decode_pending_ = true;
327 if (state_ == kWaitingForKey) {
328 CompleteWaitingForDecryptionKey();
329 MEDIA_LOG(INFO, media_log_)
330 << GetDisplayName() << ": key added, resuming decode";
331 state_ = kPendingDecode;
332 DecodePendingBuffer();
336 void DecryptingVideoDecoder::DoReset() {
340 std::move(reset_cb_).Run();
343 void DecryptingVideoDecoder::CompletePendingDecode(Decryptor::Status status) {
344 DCHECK_EQ(state_, kPendingDecode);
345 TRACE_EVENT_ASYNC_END1("media", "DecryptingVideoDecoder::DecodePendingBuffer",
346 this, "status", Decryptor::GetStatusName(status));
349 void DecryptingVideoDecoder::CompleteWaitingForDecryptionKey() {
350 DCHECK_EQ(state_, kWaitingForKey);
351 TRACE_EVENT_ASYNC_END0(
352 "media", "DecryptingVideoDecoder::WaitingForDecryptionKey", this);