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/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 // Set in constructor.
93 scoped_refptr<base::SequencedTaskRunner> const task_runner_;
94 const raw_ptr<MediaLog> media_log_;
96 SEQUENCE_CHECKER(sequence_checker_);
98 State state_ = kUninitialized;
103 base::OnceClosure reset_cb_;
104 WaitingCB waiting_cb_;
106 VideoDecoderConfig config_;
108 raw_ptr<Decryptor> decryptor_ = nullptr;
110 // The buffer that needs decrypting/decoding.
111 scoped_refptr<media::DecoderBuffer> pending_buffer_to_decode_;
113 // Indicates the situation where new key is added during pending decode
114 // (in other words, this variable can only be set in state kPendingDecode).
115 // If this variable is true and kNoKey is returned then we need to try
116 // decrypting/decoding again in case the newly added key is the correct
118 bool key_added_while_decode_pending_ = false;
120 // Once Initialized() with encrypted content support, if the stream changes to
121 // clear content, we want to ensure this decoder remains used.
122 bool support_clear_content_ = false;
124 // To keep the CdmContext event callback registered.
125 std::unique_ptr<CallbackRegistration> event_cb_registration_;
127 base::WeakPtrFactory<DecryptingVideoDecoder> weak_factory_{this};
132 #endif // MEDIA_FILTERS_DECRYPTING_VIDEO_DECODER_H_