VideoCodecTest.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #pragma once
  2. #include <IUnityGraphics.h>
  3. #include "api/test/frame_generator_interface.h"
  4. #include <api/video_codecs/h264_profile_level_id.h>
  5. #include <api/video_codecs/video_codec.h>
  6. #include <api/video_codecs/video_decoder.h>
  7. #include <api/video_codecs/video_encoder.h>
  8. #include <media/base/codec.h>
  9. #include <modules/video_coding/include/video_codec_interface.h>
  10. #include "rtc_base/checks.h"
  11. #include "rtc_base/event.h"
  12. #include "rtc_base/synchronization/mutex.h"
  13. #include "gtest/gtest.h"
  14. namespace unity
  15. {
  16. namespace webrtc
  17. {
  18. using namespace ::webrtc;
  19. using namespace ::webrtc::test;
  20. // todo(kazuki):: move another header for CUDA platform
  21. constexpr int kNumCores = 1;
  22. constexpr size_t kMaxPayloadSize = 1440;
  23. const H264ProfileLevelId kProfileLevelId(H264Profile::kProfileBaseline, H264Level::kLevel3_1);
  24. VideoEncoder::Capabilities kCapabilities();
  25. std::string kProfileLevelIdString();
  26. VideoEncoder::Settings kSettings();
  27. // todo(kazuki):: fix workaround
  28. #pragma clang diagnostic push
  29. #pragma clang diagnostic ignored "-Wunused-function"
  30. static void SetDefaultSettings(VideoCodec* codec_settings)
  31. {
  32. codec_settings->codecType = kVideoCodecH264;
  33. codec_settings->maxFramerate = 60;
  34. codec_settings->width = 1280;
  35. codec_settings->height = 720;
  36. // If frame dropping is false, we get a warning that bitrate can't
  37. // be controlled for RC_QUALITY_MODE; RC_BITRATE_MODE and RC_TIMESTAMP_MODE
  38. codec_settings->H264()->frameDroppingOn = true;
  39. codec_settings->startBitrate = 2000;
  40. codec_settings->maxBitrate = 4000;
  41. }
  42. #pragma clang diagnostic pop
  43. class VideoCodecTest : public testing::TestWithParam<UnityGfxRenderer>
  44. {
  45. public:
  46. VideoCodecTest()
  47. : encodedImageCallback_(this)
  48. , decodedImageCallback_(this)
  49. , lastInputFrameTimestamp_(0)
  50. {
  51. }
  52. virtual ~VideoCodecTest() override { }
  53. virtual std::unique_ptr<VideoEncoder> CreateEncoder() = 0;
  54. virtual std::unique_ptr<VideoDecoder> CreateDecoder() = 0;
  55. virtual std::unique_ptr<FrameGeneratorInterface> CreateFrameGenerator(
  56. int width,
  57. int height,
  58. absl::optional<FrameGeneratorInterface::OutputType> type,
  59. absl::optional<int> num_squares) = 0;
  60. virtual void ModifyCodecSettings(VideoCodec* codec_settings) = 0;
  61. void SetUp() override;
  62. void TearDown() override;
  63. VideoFrame NextInputFrame();
  64. void ChangeFrameResolution(size_t width, size_t height);
  65. // Helper method for waiting a single encoded frame.
  66. bool WaitForEncodedFrame(EncodedImage* frame, CodecSpecificInfo* codec_specific_info);
  67. bool
  68. WaitForEncodedFrames(std::vector<EncodedImage>* frames, std::vector<CodecSpecificInfo>* codec_specific_info);
  69. // Helper method for waiting a single decoded frame.
  70. bool WaitForDecodedFrame(std::unique_ptr<VideoFrame>* frame, absl::optional<uint8_t>* qp);
  71. protected:
  72. class FakeEncodedImageCallback : public EncodedImageCallback
  73. {
  74. public:
  75. explicit FakeEncodedImageCallback(VideoCodecTest* test)
  76. : _test(test)
  77. {
  78. }
  79. Result OnEncodedImage(const EncodedImage& frame, const CodecSpecificInfo* codec_specific_info) override;
  80. private:
  81. VideoCodecTest* _test;
  82. };
  83. class FakeDecodedImageCallback : public DecodedImageCallback
  84. {
  85. public:
  86. explicit FakeDecodedImageCallback(VideoCodecTest* test)
  87. : _test(test)
  88. {
  89. }
  90. int32_t Decoded(VideoFrame& decodedImage) override
  91. {
  92. RTC_CHECK_NOTREACHED();
  93. return -1;
  94. }
  95. void
  96. Decoded(VideoFrame& frame, absl::optional<int32_t> decode_time_ms, absl::optional<uint8_t> qp) override;
  97. private:
  98. VideoCodecTest* _test;
  99. };
  100. VideoCodec codecSettings_;
  101. std::unique_ptr<VideoEncoder> encoder_;
  102. std::unique_ptr<VideoDecoder> decoder_;
  103. std::unique_ptr<test::FrameGeneratorInterface> inputFrameGenerator_;
  104. private:
  105. rtc::Event encodedFrameEvent_;
  106. rtc::Event decodedFrameEvent_;
  107. Mutex encodedFrameSection_;
  108. Mutex decodedFrameSection_;
  109. std::vector<EncodedImage> encodedFrames_;
  110. absl::optional<VideoFrame> decodedFrame_;
  111. std::vector<CodecSpecificInfo> codecSpecificInfos_;
  112. absl::optional<uint8_t> decodedQp_;
  113. FakeEncodedImageCallback encodedImageCallback_;
  114. FakeDecodedImageCallback decodedImageCallback_;
  115. uint32_t lastInputFrameTimestamp_;
  116. };
  117. }
  118. }