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_DEMUXER_STREAM_H_
6 #define MEDIA_FILTERS_DECRYPTING_DEMUXER_STREAM_H_
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"
24 class SequencedTaskRunner;
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 {
39 DecryptingDemuxerStream(
40 const scoped_refptr<base::SequencedTaskRunner>& task_runner,
42 const WaitingCB& waiting_cb);
44 DecryptingDemuxerStream(const DecryptingDemuxerStream&) = delete;
45 DecryptingDemuxerStream& operator=(const DecryptingDemuxerStream&) = delete;
47 // Cancels all pending operations immediately and fires all pending callbacks.
48 ~DecryptingDemuxerStream() override;
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);
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);
61 // Returns the name of this class for logging purpose.
62 std::string GetDisplayName() const;
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;
73 // Returns whether the stream has clear lead.
74 bool HasClearLead() const;
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.
83 // +----------------+ +---------------------------------+
84 // | kUninitialized | | Any State Except kUninitialized |
85 // +----------------+ +---------------------------------+
89 // +-------+ +-------+
90 // | kIdle |<-------+-+ | kIdle |
91 // +-------+ | | +-------+
95 // +---------------------+ | |
96 // | kPendingDemuxerRead |-+ |
97 // +---------------------+ |
101 // +-----------------+ |
102 // +->| kPendingDecrypt |-----+
103 // | +-----------------+
107 // | +----------------+
108 // +---| kWaitingForKey |
109 // +----------------+
112 // 2) Has encrypted buffer
114 // 4) kConfigChanged, kAborted, has clear buffer or end of stream
115 // 5) kSuccess or kAborted
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);
134 void DecryptPendingBuffer();
136 // Callback for Decryptor::Decrypt().
137 void OnBufferDecrypted(Decryptor::Status status,
138 scoped_refptr<DecoderBuffer> decrypted_buffer);
140 // Callback for the CDM to notify |this|.
141 void OnCdmContextEvent(CdmContext::Event event);
143 // Resets decoder and calls |reset_cb_|.
146 // Returns Decryptor::StreamType converted from |stream_type_|.
147 Decryptor::StreamType GetDecryptorStreamType() const;
149 // Creates and initializes either |audio_config_| or |video_config_| based on
150 // |demuxer_stream_|.
151 void InitializeDecoderConfig();
153 // Completes traces for various pending states.
154 void CompletePendingDecrypt(Decryptor::Status status);
155 void CompleteWaitingForDecryptionKey();
159 scoped_refptr<base::SequencedTaskRunner> task_runner_;
160 SEQUENCE_CHECKER(sequence_checker_);
161 const raw_ptr<MediaLog> media_log_;
162 WaitingCB waiting_cb_;
164 State state_ = kUninitialized;
166 PipelineStatusCallback init_cb_;
168 base::OnceClosure reset_cb_;
170 // Pointer to the input demuxer stream that will feed us encrypted buffers.
171 raw_ptr<DemuxerStream> demuxer_stream_ = nullptr;
173 AudioDecoderConfig audio_config_;
174 VideoDecoderConfig video_config_;
176 raw_ptr<Decryptor> decryptor_ = nullptr;
178 absl::optional<bool> has_clear_lead_;
180 bool switched_clear_to_encrypted_ = false;
182 // The buffer returned by the demuxer that needs to be decrypted.
183 scoped_refptr<media::DecoderBuffer> pending_buffer_to_decrypt_;
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;
191 // To keep the CdmContext event callback registered.
192 std::unique_ptr<CallbackRegistration> event_cb_registration_;
194 base::WeakPtrFactory<DecryptingDemuxerStream> weak_factory_{this};
199 #endif // MEDIA_FILTERS_DECRYPTING_DEMUXER_STREAM_H_