decrypting_demuxer_stream.h 6.7 KB

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