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 #ifndef MEDIA_FILTERS_DECRYPTING_VIDEO_DECODER_H_
6 #define MEDIA_FILTERS_DECRYPTING_VIDEO_DECODER_H_
10 #include "base/functional/callback.h"
11 #include "base/memory/raw_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/sequence_checker.h"
14 #include "media/base/callback_registry.h"
15 #include "media/base/cdm_context.h"
16 #include "media/base/decryptor.h"
17 #include "media/base/video_decoder.h"
18 #include "media/base/video_decoder_config.h"
21 class SequencedTaskRunner;
30 // Decryptor-based VideoDecoder implementation that can decrypt and decode
31 // encrypted video buffers and return decrypted and decompressed video frames.
32 // All public APIs and callbacks are trampolined to the |task_runner_| so
33 // that no locks are required for thread safety.
34 class MEDIA_EXPORT DecryptingVideoDecoder : public VideoDecoder {
36 DecryptingVideoDecoder(
37 const scoped_refptr<base::SequencedTaskRunner>& task_runner,
40 DecryptingVideoDecoder(const DecryptingVideoDecoder&) = delete;
41 DecryptingVideoDecoder& operator=(const DecryptingVideoDecoder&) = delete;
43 ~DecryptingVideoDecoder() override;
45 bool SupportsDecryption() const override;
47 // VideoDecoder implementation.
48 VideoDecoderType GetDecoderType() const override;
49 void Initialize(const VideoDecoderConfig& config,
51 CdmContext* cdm_context,
53 const OutputCB& output_cb,
54 const WaitingCB& waiting_cb) override;
55 void Decode(scoped_refptr<DecoderBuffer> buffer, DecodeCB decode_cb) override;
56 void Reset(base::OnceClosure closure) override;
58 static const char kDecoderName[];
61 // For a detailed state diagram please see this link: http://goo.gl/8jAok
62 // TODO(xhwang): Add a ASCII state diagram in this file after this class
74 // Callback for Decryptor::InitializeVideoDecoder() during initialization.
75 void FinishInitialization(bool success);
77 void DecodePendingBuffer();
79 // Callback for Decryptor::DecryptAndDecodeVideo().
80 void DeliverFrame(Decryptor::Status status, scoped_refptr<VideoFrame> frame);
82 // Callback for the CDM to notify |this|.
83 void OnCdmContextEvent(CdmContext::Event event);
85 // Reset decoder and call |reset_cb_|.
88 // Completes traces for various pending states.
89 void CompletePendingDecode(Decryptor::Status status);
90 void CompleteWaitingForDecryptionKey();
92 bool HasClearLead() const { return has_clear_lead_.value_or(false); }
94 // Set in constructor.
95 scoped_refptr<base::SequencedTaskRunner> const task_runner_;
96 const raw_ptr<MediaLog> media_log_;
98 SEQUENCE_CHECKER(sequence_checker_);
100 State state_ = kUninitialized;
105 base::OnceClosure reset_cb_;
106 WaitingCB waiting_cb_;
108 VideoDecoderConfig config_;
110 raw_ptr<Decryptor> decryptor_ = nullptr;
112 // The buffer that needs decrypting/decoding.
113 scoped_refptr<media::DecoderBuffer> pending_buffer_to_decode_;
115 // Indicates the situation where new key is added during pending decode
116 // (in other words, this variable can only be set in state kPendingDecode).
117 // If this variable is true and kNoKey is returned then we need to try
118 // decrypting/decoding again in case the newly added key is the correct
120 bool key_added_while_decode_pending_ = false;
122 // Once Initialized() with encrypted content support, if the stream changes to
123 // clear content, we want to ensure this decoder remains used.
124 bool support_clear_content_ = false;
126 absl::optional<bool> has_clear_lead_;
128 bool switched_clear_to_encrypted_ = false;
130 // To keep the CdmContext event callback registered.
131 std::unique_ptr<CallbackRegistration> event_cb_registration_;
133 base::WeakPtrFactory<DecryptingVideoDecoder> weak_factory_{this};
138 #endif // MEDIA_FILTERS_DECRYPTING_VIDEO_DECODER_H_