Upload upstream chromium 108.0.5359.1
[platform/framework/web/chromium-efl.git] / media / filters / decrypting_video_decoder.h
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.
4
5 #ifndef MEDIA_FILTERS_DECRYPTING_VIDEO_DECODER_H_
6 #define MEDIA_FILTERS_DECRYPTING_VIDEO_DECODER_H_
7
8 #include <stdint.h>
9
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"
19
20 namespace base {
21 class SequencedTaskRunner;
22 }
23
24 namespace media {
25
26 class DecoderBuffer;
27 class Decryptor;
28 class MediaLog;
29
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 {
35  public:
36   DecryptingVideoDecoder(
37       const scoped_refptr<base::SequencedTaskRunner>& task_runner,
38       MediaLog* media_log);
39
40   DecryptingVideoDecoder(const DecryptingVideoDecoder&) = delete;
41   DecryptingVideoDecoder& operator=(const DecryptingVideoDecoder&) = delete;
42
43   ~DecryptingVideoDecoder() override;
44
45   bool SupportsDecryption() const override;
46
47   // VideoDecoder implementation.
48   VideoDecoderType GetDecoderType() const override;
49   void Initialize(const VideoDecoderConfig& config,
50                   bool low_delay,
51                   CdmContext* cdm_context,
52                   InitCB init_cb,
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;
57
58   static const char kDecoderName[];
59
60  private:
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
63   // stabilizes.
64   enum State {
65     kUninitialized = 0,
66     kPendingDecoderInit,
67     kIdle,
68     kPendingDecode,
69     kWaitingForKey,
70     kDecodeFinished,
71     kError
72   };
73
74   // Callback for Decryptor::InitializeVideoDecoder() during initialization.
75   void FinishInitialization(bool success);
76
77   void DecodePendingBuffer();
78
79   // Callback for Decryptor::DecryptAndDecodeVideo().
80   void DeliverFrame(Decryptor::Status status, scoped_refptr<VideoFrame> frame);
81
82   // Callback for the CDM to notify |this|.
83   void OnCdmContextEvent(CdmContext::Event event);
84
85   // Reset decoder and call |reset_cb_|.
86   void DoReset();
87
88   // Completes traces for various pending states.
89   void CompletePendingDecode(Decryptor::Status status);
90   void CompleteWaitingForDecryptionKey();
91
92   // Set in constructor.
93   scoped_refptr<base::SequencedTaskRunner> const task_runner_;
94   const raw_ptr<MediaLog> media_log_;
95
96   SEQUENCE_CHECKER(sequence_checker_);
97
98   State state_ = kUninitialized;
99
100   InitCB init_cb_;
101   OutputCB output_cb_;
102   DecodeCB decode_cb_;
103   base::OnceClosure reset_cb_;
104   WaitingCB waiting_cb_;
105
106   VideoDecoderConfig config_;
107
108   raw_ptr<Decryptor> decryptor_ = nullptr;
109
110   // The buffer that needs decrypting/decoding.
111   scoped_refptr<media::DecoderBuffer> pending_buffer_to_decode_;
112
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
117   // decryption key.
118   bool key_added_while_decode_pending_ = false;
119
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;
123
124   // To keep the CdmContext event callback registered.
125   std::unique_ptr<CallbackRegistration> event_cb_registration_;
126
127   base::WeakPtrFactory<DecryptingVideoDecoder> weak_factory_{this};
128 };
129
130 }  // namespace media
131
132 #endif  // MEDIA_FILTERS_DECRYPTING_VIDEO_DECODER_H_