Upload upstream chromium 108.0.5359.1
[platform/framework/web/chromium-efl.git] / media / filters / decrypting_demuxer_stream.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_DEMUXER_STREAM_H_
6 #define MEDIA_FILTERS_DECRYPTING_DEMUXER_STREAM_H_
7
8 #include "base/callback.h"
9 #include "base/memory/raw_ptr.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/sequence_checker.h"
13 #include "media/base/audio_decoder_config.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/demuxer_stream.h"
18 #include "media/base/pipeline_status.h"
19 #include "media/base/video_decoder_config.h"
20 #include "media/base/waiting.h"
21 #include "third_party/abseil-cpp/absl/types/optional.h"
22
23 namespace base {
24 class SequencedTaskRunner;
25 }
26
27 namespace media {
28
29 class CdmContext;
30 class DecoderBuffer;
31 class MediaLog;
32
33 // Decryptor-based DemuxerStream implementation that converts a potentially
34 // encrypted demuxer stream to a clear demuxer stream.
35 // All public APIs and callbacks are trampolined to the |task_runner_| so
36 // that no locks are required for thread safety.
37 class MEDIA_EXPORT DecryptingDemuxerStream : public DemuxerStream {
38  public:
39   DecryptingDemuxerStream(
40       const scoped_refptr<base::SequencedTaskRunner>& task_runner,
41       MediaLog* media_log,
42       const WaitingCB& waiting_cb);
43
44   DecryptingDemuxerStream(const DecryptingDemuxerStream&) = delete;
45   DecryptingDemuxerStream& operator=(const DecryptingDemuxerStream&) = delete;
46
47   // Cancels all pending operations immediately and fires all pending callbacks.
48   ~DecryptingDemuxerStream() override;
49
50   // |stream| must be encrypted and |cdm_context| must be non-null.
51   void Initialize(DemuxerStream* stream,
52                   CdmContext* cdm_context,
53                   PipelineStatusCallback status_cb);
54
55   // Cancels all pending operations and fires all pending callbacks. If in
56   // kPendingDemuxerRead or kPendingDecrypt state, waits for the pending
57   // operation to finish before satisfying |closure|. Sets the state to
58   // kUninitialized if |this| hasn't been initialized, or to kIdle otherwise.
59   void Reset(base::OnceClosure closure);
60
61   // Returns the name of this class for logging purpose.
62   std::string GetDisplayName() const;
63
64   // DemuxerStream implementation.
65   void Read(ReadCB read_cb) override;
66   AudioDecoderConfig audio_decoder_config() override;
67   VideoDecoderConfig video_decoder_config() override;
68   Type type() const override;
69   StreamLiveness liveness() const override;
70   void EnableBitstreamConverter() override;
71   bool SupportsConfigChanges() override;
72
73   // Returns whether the stream has clear lead.
74   bool HasClearLead() const;
75
76  private:
77   // See this link for a detailed state diagram: http://shortn/_1nXgoVIrps
78   // Each line has a number that corresponds to an action, status or function
79   // that results in a state change. These actions, etc are all listed below.
80   // NOTE: invoking Reset() will cause a transition from any state except
81   //       kUninitialized to the kIdle state.
82   //
83   //    +----------------+         +---------------------------------+
84   //    | kUninitialized |         | Any State Except kUninitialized |
85   //    +----------------+         +---------------------------------+
86   //             |                                  |
87   //             0                                  7
88   //             v                                  v
89   //         +-------+                          +-------+
90   //         | kIdle |<-------+-+               | kIdle |
91   //         +-------+        | |               +-------+
92   //             |            | |
93   //             1            4 5
94   //             v            | |
95   //  +---------------------+ | |
96   //  | kPendingDemuxerRead |-+ |
97   //  +---------------------+   |
98   //             |              |
99   //             2              |
100   //             v              |
101   //    +-----------------+     |
102   // +->| kPendingDecrypt |-----+
103   // |  +-----------------+
104   // |           |
105   // 6           3
106   // |           v
107   // |   +----------------+
108   // +---| kWaitingForKey |
109   //     +----------------+
110   //
111   // 1) Read()
112   // 2) Has encrypted buffer
113   // 3) kNoKey
114   // 4) kConfigChanged, kAborted, has clear buffer or end of stream
115   // 5) kSuccess or kAborted
116   // 6) OnKeyAdded()
117   // 7) Reset()
118
119   enum State {
120     kUninitialized = 0,
121     kIdle,
122     kPendingDemuxerRead,
123     kPendingDecrypt,
124     kWaitingForKey
125   };
126
127   // Callback for DemuxerStream::Read().
128   void OnBufferReadFromDemuxerStream(DemuxerStream::Status status,
129                                      scoped_refptr<DecoderBuffer> buffer);
130
131   void DecryptPendingBuffer();
132
133   // Callback for Decryptor::Decrypt().
134   void OnBufferDecrypted(Decryptor::Status status,
135                          scoped_refptr<DecoderBuffer> decrypted_buffer);
136
137   // Callback for the CDM to notify |this|.
138   void OnCdmContextEvent(CdmContext::Event event);
139
140   // Resets decoder and calls |reset_cb_|.
141   void DoReset();
142
143   // Returns Decryptor::StreamType converted from |stream_type_|.
144   Decryptor::StreamType GetDecryptorStreamType() const;
145
146   // Creates and initializes either |audio_config_| or |video_config_| based on
147   // |demuxer_stream_|.
148   void InitializeDecoderConfig();
149
150   // Completes traces for various pending states.
151   void CompletePendingDecrypt(Decryptor::Status status);
152   void CompleteWaitingForDecryptionKey();
153
154   void LogMetadata();
155
156   scoped_refptr<base::SequencedTaskRunner> task_runner_;
157   SEQUENCE_CHECKER(sequence_checker_);
158   const raw_ptr<MediaLog> media_log_;
159   WaitingCB waiting_cb_;
160
161   State state_ = kUninitialized;
162
163   PipelineStatusCallback init_cb_;
164   ReadCB read_cb_;
165   base::OnceClosure reset_cb_;
166
167   // Pointer to the input demuxer stream that will feed us encrypted buffers.
168   raw_ptr<DemuxerStream> demuxer_stream_ = nullptr;
169
170   AudioDecoderConfig audio_config_;
171   VideoDecoderConfig video_config_;
172
173   raw_ptr<Decryptor> decryptor_ = nullptr;
174
175   absl::optional<bool> has_clear_lead_;
176
177   // The buffer returned by the demuxer that needs to be decrypted.
178   scoped_refptr<media::DecoderBuffer> pending_buffer_to_decrypt_;
179
180   // Indicates the situation where new key is added during pending decryption
181   // (in other words, this variable can only be set in state kPendingDecrypt).
182   // If this variable is true and kNoKey is returned then we need to try
183   // decrypting again in case the newly added key is the correct decryption key.
184   bool key_added_while_decrypt_pending_ = false;
185
186   // To keep the CdmContext event callback registered.
187   std::unique_ptr<CallbackRegistration> event_cb_registration_;
188
189   base::WeakPtrFactory<DecryptingDemuxerStream> weak_factory_{this};
190 };
191
192 }  // namespace media
193
194 #endif  // MEDIA_FILTERS_DECRYPTING_DEMUXER_STREAM_H_