fake_video_decoder.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright 2013 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_FAKE_VIDEO_DECODER_H_
  5. #define MEDIA_FILTERS_FAKE_VIDEO_DECODER_H_
  6. #include <stddef.h>
  7. #include <list>
  8. #include "base/bind.h"
  9. #include "base/callback.h"
  10. #include "base/callback_helpers.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "base/sequence_checker.h"
  13. #include "media/base/callback_holder.h"
  14. #include "media/base/decoder_buffer.h"
  15. #include "media/base/pipeline_status.h"
  16. #include "media/base/video_decoder.h"
  17. #include "media/base/video_decoder_config.h"
  18. #include "media/base/video_frame.h"
  19. #include "ui/gfx/geometry/size.h"
  20. namespace media {
  21. using BytesDecodedCB = base::RepeatingCallback<void(int)>;
  22. class FakeVideoDecoder : public VideoDecoder {
  23. public:
  24. // Constructs an object with a decoding delay of |decoding_delay| frames.
  25. // |bytes_decoded_cb| is called after each decode. The sum of the byte
  26. // count over all calls will be equal to total_bytes_decoded().
  27. // Allows setting a fake ID so that tests for wrapper decoders can check
  28. // that underlying decoders change successfully.
  29. FakeVideoDecoder(int decoder_id,
  30. int decoding_delay,
  31. int max_parallel_decoding_requests,
  32. const BytesDecodedCB& bytes_decoded_cb);
  33. FakeVideoDecoder(const FakeVideoDecoder&) = delete;
  34. FakeVideoDecoder& operator=(const FakeVideoDecoder&) = delete;
  35. ~FakeVideoDecoder() override;
  36. // Enables encrypted config supported. Must be called before Initialize().
  37. void EnableEncryptedConfigSupport();
  38. // Sets whether this decoder is a platform decoder. Must be called before
  39. // Initialize().
  40. void SetIsPlatformDecoder(bool value);
  41. // Decoder implementation.
  42. bool SupportsDecryption() const override;
  43. bool IsPlatformDecoder() const override;
  44. VideoDecoderType GetDecoderType() const override;
  45. int GetDecoderId() { return decoder_id_; }
  46. // VideoDecoder implementation
  47. void Initialize(const VideoDecoderConfig& config,
  48. bool low_delay,
  49. CdmContext* cdm_context,
  50. InitCB init_cb,
  51. const OutputCB& output_cb,
  52. const WaitingCB& waiting_cb) override;
  53. void Decode(scoped_refptr<DecoderBuffer> buffer, DecodeCB decode_cb) override;
  54. void Reset(base::OnceClosure closure) override;
  55. int GetMaxDecodeRequests() const override;
  56. base::WeakPtr<FakeVideoDecoder> GetWeakPtr();
  57. // Holds the next init/decode/reset callback from firing.
  58. void HoldNextInit();
  59. void HoldDecode();
  60. void HoldNextReset();
  61. // Satisfies the pending init/decode/reset callback, which must be ready to
  62. // fire when these methods are called.
  63. void SatisfyInit();
  64. void SatisfyDecode();
  65. void SatisfyReset();
  66. // Satisfies single decode request.
  67. void SatisfySingleDecode();
  68. void SimulateError();
  69. // Fail with status DECODER_ERROR_NOT_SUPPORTED when Initialize() is called.
  70. void SimulateFailureToInit();
  71. int total_bytes_decoded() const { return total_bytes_decoded_; }
  72. protected:
  73. enum State {
  74. STATE_UNINITIALIZED,
  75. STATE_NORMAL,
  76. STATE_END_OF_STREAM,
  77. STATE_ERROR,
  78. };
  79. // Derived classes may override to customize the VideoFrame.
  80. virtual scoped_refptr<VideoFrame> MakeVideoFrame(const DecoderBuffer& buffer);
  81. // Callback for updating |total_bytes_decoded_|.
  82. void OnFrameDecoded(int buffer_size,
  83. DecodeCB decode_cb,
  84. DecoderStatus status);
  85. // Runs |decode_cb| or puts it to |held_decode_callbacks_| depending on
  86. // current value of |hold_decode_|.
  87. void RunOrHoldDecode(DecodeCB decode_cb);
  88. // Runs |decode_cb| with a frame from |decoded_frames_|.
  89. void RunDecodeCallback(DecodeCB decode_cb);
  90. void DoReset();
  91. SEQUENCE_CHECKER(sequence_checker_);
  92. const int decoder_id_;
  93. const size_t decoding_delay_;
  94. const int max_parallel_decoding_requests_;
  95. BytesDecodedCB bytes_decoded_cb_;
  96. bool is_platform_decoder_ = false;
  97. bool supports_encrypted_config_ = false;
  98. State state_;
  99. CallbackHolder<InitCB> init_cb_;
  100. CallbackHolder<base::OnceClosure> reset_cb_;
  101. OutputCB output_cb_;
  102. bool hold_decode_;
  103. std::list<DecodeCB> held_decode_callbacks_;
  104. VideoDecoderConfig current_config_;
  105. std::list<scoped_refptr<VideoFrame> > decoded_frames_;
  106. int total_bytes_decoded_;
  107. bool fail_to_initialize_;
  108. // NOTE: Weak pointers must be invalidated before all other member variables.
  109. base::WeakPtrFactory<FakeVideoDecoder> weak_factory_{this};
  110. };
  111. } // namespace media
  112. #endif // MEDIA_FILTERS_FAKE_VIDEO_DECODER_H_