Revert "[M120 Migration]Fix for crash during chrome exit"
[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/functional/callback.h"
9 #include "base/memory/raw_ptr.h"
10 #include "base/memory/scoped_refptr.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(uint32_t count, 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   void OnBuffersReadFromDemuxerStream(
128       DemuxerStream::Status status,
129       DemuxerStream::DecoderBufferVector buffers);
130   // Callback for DemuxerStream::Read().
131   void OnBufferReadFromDemuxerStream(DemuxerStream::Status status,
132                                      scoped_refptr<DecoderBuffer> buffer);
133
134   void DecryptPendingBuffer();
135
136   // Callback for Decryptor::Decrypt().
137   void OnBufferDecrypted(Decryptor::Status status,
138                          scoped_refptr<DecoderBuffer> decrypted_buffer);
139
140   // Callback for the CDM to notify |this|.
141   void OnCdmContextEvent(CdmContext::Event event);
142
143   // Resets decoder and calls |reset_cb_|.
144   void DoReset();
145
146   // Returns Decryptor::StreamType converted from |stream_type_|.
147   Decryptor::StreamType GetDecryptorStreamType() const;
148
149   // Creates and initializes either |audio_config_| or |video_config_| based on
150   // |demuxer_stream_|.
151   void InitializeDecoderConfig();
152
153   // Completes traces for various pending states.
154   void CompletePendingDecrypt(Decryptor::Status status);
155   void CompleteWaitingForDecryptionKey();
156
157   void LogMetadata();
158
159   scoped_refptr<base::SequencedTaskRunner> task_runner_;
160   SEQUENCE_CHECKER(sequence_checker_);
161   const raw_ptr<MediaLog> media_log_;
162   WaitingCB waiting_cb_;
163
164   State state_ = kUninitialized;
165
166   PipelineStatusCallback init_cb_;
167   ReadCB read_cb_;
168   base::OnceClosure reset_cb_;
169
170   // Pointer to the input demuxer stream that will feed us encrypted buffers.
171   raw_ptr<DemuxerStream> demuxer_stream_ = nullptr;
172
173   AudioDecoderConfig audio_config_;
174   VideoDecoderConfig video_config_;
175
176   raw_ptr<Decryptor> decryptor_ = nullptr;
177
178   absl::optional<bool> has_clear_lead_;
179
180   bool switched_clear_to_encrypted_ = false;
181
182   // The buffer returned by the demuxer that needs to be decrypted.
183   scoped_refptr<media::DecoderBuffer> pending_buffer_to_decrypt_;
184
185   // Indicates the situation where new key is added during pending decryption
186   // (in other words, this variable can only be set in state kPendingDecrypt).
187   // If this variable is true and kNoKey is returned then we need to try
188   // decrypting again in case the newly added key is the correct decryption key.
189   bool key_added_while_decrypt_pending_ = false;
190
191   // To keep the CdmContext event callback registered.
192   std::unique_ptr<CallbackRegistration> event_cb_registration_;
193
194   base::WeakPtrFactory<DecryptingDemuxerStream> weak_factory_{this};
195 };
196
197 }  // namespace media
198
199 #endif  // MEDIA_FILTERS_DECRYPTING_DEMUXER_STREAM_H_