1 // Copyright 2012 The Chromium Authors
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/strings/string_number_conversions.h"
12 #include "base/task/sequenced_task_runner.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::SequencedTaskRunner>& task_runner,
27 : task_runner_(task_runner), media_log_(media_log) {
28 DETACH_FROM_SEQUENCE(sequence_checker_);
31 VideoDecoderType DecryptingVideoDecoder::GetDecoderType() const {
32 return VideoDecoderType::kDecrypting;
35 void DecryptingVideoDecoder::Initialize(const VideoDecoderConfig& config,
37 CdmContext* cdm_context,
39 const OutputCB& output_cb,
40 const WaitingCB& waiting_cb) {
41 DVLOG(2) << __func__ << ": " << config.AsHumanReadableString();
43 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
44 DCHECK(state_ == kUninitialized || state_ == kIdle ||
45 state_ == kDecodeFinished)
49 DCHECK(config.IsValidConfig());
51 init_cb_ = BindToCurrentLoop(std::move(init_cb));
54 // Once we have a CDM context, one should always be present.
55 DCHECK(!support_clear_content_);
56 std::move(init_cb_).Run(DecoderStatus::Codes::kUnsupportedEncryptionMode);
60 if (!config.is_encrypted() && !support_clear_content_) {
61 std::move(init_cb_).Run(DecoderStatus::Codes::kUnsupportedEncryptionMode);
65 // Once initialized with encryption support, the value is sticky, so we'll use
66 // the decryptor for clear content as well.
67 support_clear_content_ = true;
69 output_cb_ = BindToCurrentLoop(output_cb);
73 waiting_cb_ = waiting_cb;
75 if (state_ == kUninitialized) {
76 if (!cdm_context->GetDecryptor()) {
77 DVLOG(1) << __func__ << ": no decryptor";
78 std::move(init_cb_).Run(DecoderStatus::Codes::kUnsupportedEncryptionMode);
82 decryptor_ = cdm_context->GetDecryptor();
83 event_cb_registration_ = cdm_context->RegisterEventCB(
84 base::BindRepeating(&DecryptingVideoDecoder::OnCdmContextEvent,
85 weak_factory_.GetWeakPtr()));
87 // Reinitialization (i.e. upon a config change). The new config can be
88 // encrypted or clear.
89 decryptor_->DeinitializeDecoder(Decryptor::kVideo);
92 state_ = kPendingDecoderInit;
93 decryptor_->InitializeVideoDecoder(
94 config_, BindToCurrentLoop(
95 base::BindOnce(&DecryptingVideoDecoder::FinishInitialization,
96 weak_factory_.GetWeakPtr())));
99 bool DecryptingVideoDecoder::SupportsDecryption() const {
103 void DecryptingVideoDecoder::Decode(scoped_refptr<DecoderBuffer> buffer,
104 DecodeCB decode_cb) {
105 DVLOG(3) << "Decode()";
106 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
107 DCHECK(state_ == kIdle || state_ == kDecodeFinished || state_ == kError)
110 CHECK(!decode_cb_) << "Overlapping decodes are not supported.";
112 decode_cb_ = BindToCurrentLoop(std::move(decode_cb));
114 if (state_ == kError) {
115 std::move(decode_cb_).Run(DecoderStatus::Codes::kPlatformDecodeFailure);
119 // Return empty frames if decoding has finished.
120 if (state_ == kDecodeFinished) {
121 std::move(decode_cb_).Run(DecoderStatus::Codes::kOk);
125 pending_buffer_to_decode_ = std::move(buffer);
126 state_ = kPendingDecode;
127 DecodePendingBuffer();
130 void DecryptingVideoDecoder::Reset(base::OnceClosure closure) {
131 DVLOG(2) << "Reset() - state: " << state_;
132 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
133 DCHECK(state_ == kIdle || state_ == kPendingDecode ||
134 state_ == kWaitingForKey || state_ == kDecodeFinished ||
137 DCHECK(!init_cb_); // No Reset() during pending initialization.
140 reset_cb_ = BindToCurrentLoop(std::move(closure));
142 decryptor_->ResetDecoder(Decryptor::kVideo);
144 // Reset() cannot complete if the decode callback is still pending.
145 // Defer the resetting process in this case. The |reset_cb_| will be fired
146 // after the decode callback is fired - see DecryptAndDecodeBuffer() and
148 if (state_ == kPendingDecode) {
153 if (state_ == kWaitingForKey) {
154 CompleteWaitingForDecryptionKey();
156 pending_buffer_to_decode_.reset();
157 std::move(decode_cb_).Run(DecoderStatus::Codes::kAborted);
164 DecryptingVideoDecoder::~DecryptingVideoDecoder() {
165 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
167 if (state_ == kUninitialized)
170 if (state_ == kWaitingForKey)
171 CompleteWaitingForDecryptionKey();
172 if (state_ == kPendingDecode)
173 CompletePendingDecode(Decryptor::kError);
176 decryptor_->DeinitializeDecoder(Decryptor::kVideo);
177 decryptor_ = nullptr;
179 pending_buffer_to_decode_.reset();
181 std::move(init_cb_).Run(DecoderStatus::Codes::kInterrupted);
183 std::move(decode_cb_).Run(DecoderStatus::Codes::kAborted);
185 std::move(reset_cb_).Run();
188 void DecryptingVideoDecoder::FinishInitialization(bool success) {
189 DVLOG(2) << "FinishInitialization()";
190 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
191 DCHECK_EQ(state_, kPendingDecoderInit) << state_;
193 DCHECK(!reset_cb_); // No Reset() before initialization finished.
194 DCHECK(!decode_cb_); // No Decode() before initialization finished.
197 DVLOG(1) << __func__ << ": failed to init video decoder on decryptor";
198 // TODO(*) Is there a better reason? Should this method itself take a
200 std::move(init_cb_).Run(DecoderStatus::Codes::kFailed);
201 decryptor_ = nullptr;
202 event_cb_registration_.reset();
209 std::move(init_cb_).Run(DecoderStatus::Codes::kOk);
212 void DecryptingVideoDecoder::DecodePendingBuffer() {
213 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
214 DCHECK_EQ(state_, kPendingDecode) << state_;
216 // Note: Traces require a unique ID per decode, if we ever support multiple
217 // in flight decodes, the trace begin+end macros need the same unique id.
218 DCHECK_EQ(GetMaxDecodeRequests(), 1);
219 TRACE_EVENT_ASYNC_BEGIN1(
220 "media", "DecryptingVideoDecoder::DecodePendingBuffer", this,
222 pending_buffer_to_decode_->end_of_stream()
224 : pending_buffer_to_decode_->timestamp().InMicroseconds());
226 decryptor_->DecryptAndDecodeVideo(
227 pending_buffer_to_decode_,
228 BindToCurrentLoop(base::BindRepeating(
229 &DecryptingVideoDecoder::DeliverFrame, weak_factory_.GetWeakPtr())));
232 void DecryptingVideoDecoder::DeliverFrame(Decryptor::Status status,
233 scoped_refptr<VideoFrame> frame) {
234 DVLOG(3) << "DeliverFrame() - status: " << status;
235 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
236 DCHECK_EQ(state_, kPendingDecode) << state_;
238 DCHECK(pending_buffer_to_decode_.get());
239 CompletePendingDecode(status);
241 bool need_to_try_again_if_nokey_is_returned = key_added_while_decode_pending_;
242 key_added_while_decode_pending_ = false;
244 scoped_refptr<DecoderBuffer> scoped_pending_buffer_to_decode =
245 std::move(pending_buffer_to_decode_);
248 std::move(decode_cb_).Run(DecoderStatus::Codes::kAborted);
253 DCHECK_EQ(status == Decryptor::kSuccess, frame.get() != nullptr);
255 if (status == Decryptor::kError) {
256 DVLOG(2) << "DeliverFrame() - kError";
257 MEDIA_LOG(ERROR, media_log_) << GetDecoderType() << ": decode error";
259 std::move(decode_cb_).Run(DecoderStatus::Codes::kPlatformDecodeFailure);
263 if (status == Decryptor::kNoKey) {
265 scoped_pending_buffer_to_decode->decrypt_config()->key_id();
266 std::string log_message =
267 "no key for key ID " + base::HexEncode(key_id.data(), key_id.size()) +
268 "; will resume decoding after new usable key is available";
269 DVLOG(1) << __func__ << ": " << log_message;
270 MEDIA_LOG(INFO, media_log_) << GetDecoderType() << ": " << log_message;
272 // Set |pending_buffer_to_decode_| back as we need to try decoding the
273 // pending buffer again when new key is added to the decryptor.
274 pending_buffer_to_decode_ = std::move(scoped_pending_buffer_to_decode);
276 if (need_to_try_again_if_nokey_is_returned) {
277 // The |state_| is still kPendingDecode.
278 MEDIA_LOG(INFO, media_log_)
279 << GetDecoderType() << ": key was added, resuming decode";
280 DecodePendingBuffer();
284 TRACE_EVENT_ASYNC_BEGIN0(
285 "media", "DecryptingVideoDecoder::WaitingForDecryptionKey", this);
286 state_ = kWaitingForKey;
287 waiting_cb_.Run(WaitingReason::kNoDecryptionKey);
291 if (status == Decryptor::kNeedMoreData) {
292 DVLOG(2) << "DeliverFrame() - kNeedMoreData";
293 state_ = scoped_pending_buffer_to_decode->end_of_stream() ? kDecodeFinished
295 std::move(decode_cb_).Run(DecoderStatus::Codes::kOk);
299 DCHECK_EQ(status, Decryptor::kSuccess);
302 // Frame returned with kSuccess should not be an end-of-stream frame.
303 DCHECK(!frame->metadata().end_of_stream);
305 // If color space is not set, use the color space in the |config_|.
306 if (!frame->ColorSpace().IsValid()) {
307 DVLOG(3) << "Setting color space using information in the config.";
308 if (config_.color_space_info().IsSpecified())
309 frame->set_color_space(config_.color_space_info().ToGfxColorSpace());
312 output_cb_.Run(std::move(frame));
314 if (scoped_pending_buffer_to_decode->end_of_stream()) {
315 // Set |pending_buffer_to_decode_| back as we need to keep flushing the
317 pending_buffer_to_decode_ = std::move(scoped_pending_buffer_to_decode);
318 DecodePendingBuffer();
323 std::move(decode_cb_).Run(DecoderStatus::Codes::kOk);
326 void DecryptingVideoDecoder::OnCdmContextEvent(CdmContext::Event event) {
327 DVLOG(2) << __func__;
328 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
330 if (event != CdmContext::Event::kHasAdditionalUsableKey)
333 if (state_ == kPendingDecode) {
334 key_added_while_decode_pending_ = true;
338 if (state_ == kWaitingForKey) {
339 CompleteWaitingForDecryptionKey();
340 MEDIA_LOG(INFO, media_log_)
341 << GetDecoderType() << ": key added, resuming decode";
342 state_ = kPendingDecode;
343 DecodePendingBuffer();
347 void DecryptingVideoDecoder::DoReset() {
351 std::move(reset_cb_).Run();
354 void DecryptingVideoDecoder::CompletePendingDecode(Decryptor::Status status) {
355 DCHECK_EQ(state_, kPendingDecode);
356 TRACE_EVENT_ASYNC_END1("media", "DecryptingVideoDecoder::DecodePendingBuffer",
357 this, "status", Decryptor::GetStatusName(status));
360 void DecryptingVideoDecoder::CompleteWaitingForDecryptionKey() {
361 DCHECK_EQ(state_, kWaitingForKey);
362 TRACE_EVENT_ASYNC_END0(
363 "media", "DecryptingVideoDecoder::WaitingForDecryptionKey", this);