decrypting_video_decoder.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_VIDEO_DECODER_H_
  5. #define MEDIA_FILTERS_DECRYPTING_VIDEO_DECODER_H_
  6. #include <stdint.h>
  7. #include "base/callback.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "base/sequence_checker.h"
  11. #include "media/base/callback_registry.h"
  12. #include "media/base/cdm_context.h"
  13. #include "media/base/decryptor.h"
  14. #include "media/base/video_decoder.h"
  15. #include "media/base/video_decoder_config.h"
  16. namespace base {
  17. class SequencedTaskRunner;
  18. }
  19. namespace media {
  20. class DecoderBuffer;
  21. class Decryptor;
  22. class MediaLog;
  23. // Decryptor-based VideoDecoder implementation that can decrypt and decode
  24. // encrypted video buffers and return decrypted and decompressed video frames.
  25. // All public APIs and callbacks are trampolined to the |task_runner_| so
  26. // that no locks are required for thread safety.
  27. class MEDIA_EXPORT DecryptingVideoDecoder : public VideoDecoder {
  28. public:
  29. DecryptingVideoDecoder(
  30. const scoped_refptr<base::SequencedTaskRunner>& task_runner,
  31. MediaLog* media_log);
  32. DecryptingVideoDecoder(const DecryptingVideoDecoder&) = delete;
  33. DecryptingVideoDecoder& operator=(const DecryptingVideoDecoder&) = delete;
  34. ~DecryptingVideoDecoder() override;
  35. bool SupportsDecryption() const override;
  36. // VideoDecoder implementation.
  37. VideoDecoderType GetDecoderType() const override;
  38. void Initialize(const VideoDecoderConfig& config,
  39. bool low_delay,
  40. CdmContext* cdm_context,
  41. InitCB init_cb,
  42. const OutputCB& output_cb,
  43. const WaitingCB& waiting_cb) override;
  44. void Decode(scoped_refptr<DecoderBuffer> buffer, DecodeCB decode_cb) override;
  45. void Reset(base::OnceClosure closure) override;
  46. static const char kDecoderName[];
  47. private:
  48. // For a detailed state diagram please see this link: http://goo.gl/8jAok
  49. // TODO(xhwang): Add a ASCII state diagram in this file after this class
  50. // stabilizes.
  51. enum State {
  52. kUninitialized = 0,
  53. kPendingDecoderInit,
  54. kIdle,
  55. kPendingDecode,
  56. kWaitingForKey,
  57. kDecodeFinished,
  58. kError
  59. };
  60. // Callback for Decryptor::InitializeVideoDecoder() during initialization.
  61. void FinishInitialization(bool success);
  62. void DecodePendingBuffer();
  63. // Callback for Decryptor::DecryptAndDecodeVideo().
  64. void DeliverFrame(Decryptor::Status status, scoped_refptr<VideoFrame> frame);
  65. // Callback for the CDM to notify |this|.
  66. void OnCdmContextEvent(CdmContext::Event event);
  67. // Reset decoder and call |reset_cb_|.
  68. void DoReset();
  69. // Completes traces for various pending states.
  70. void CompletePendingDecode(Decryptor::Status status);
  71. void CompleteWaitingForDecryptionKey();
  72. // Set in constructor.
  73. scoped_refptr<base::SequencedTaskRunner> const task_runner_;
  74. const raw_ptr<MediaLog> media_log_;
  75. SEQUENCE_CHECKER(sequence_checker_);
  76. State state_ = kUninitialized;
  77. InitCB init_cb_;
  78. OutputCB output_cb_;
  79. DecodeCB decode_cb_;
  80. base::OnceClosure reset_cb_;
  81. WaitingCB waiting_cb_;
  82. VideoDecoderConfig config_;
  83. raw_ptr<Decryptor> decryptor_ = nullptr;
  84. // The buffer that needs decrypting/decoding.
  85. scoped_refptr<media::DecoderBuffer> pending_buffer_to_decode_;
  86. // Indicates the situation where new key is added during pending decode
  87. // (in other words, this variable can only be set in state kPendingDecode).
  88. // If this variable is true and kNoKey is returned then we need to try
  89. // decrypting/decoding again in case the newly added key is the correct
  90. // decryption key.
  91. bool key_added_while_decode_pending_ = false;
  92. // Once Initialized() with encrypted content support, if the stream changes to
  93. // clear content, we want to ensure this decoder remains used.
  94. bool support_clear_content_ = false;
  95. // To keep the CdmContext event callback registered.
  96. std::unique_ptr<CallbackRegistration> event_cb_registration_;
  97. base::WeakPtrFactory<DecryptingVideoDecoder> weak_factory_{this};
  98. };
  99. } // namespace media
  100. #endif // MEDIA_FILTERS_DECRYPTING_VIDEO_DECODER_H_