InternalCodecsTest.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include "pch.h"
  2. #include "FrameGenerator.h"
  3. #include "GraphicsDevice/IGraphicsDevice.h"
  4. #include "GraphicsDevice/ITexture2D.h"
  5. #include "GraphicsDeviceContainer.h"
  6. #include "VideoCodecTest.h"
  7. #include "media/engine/internal_decoder_factory.h"
  8. #include "media/engine/internal_encoder_factory.h"
  9. #include "modules/video_coding/utility/vp8_header_parser.h"
  10. #include "test/video_codec_settings.h"
  11. namespace unity
  12. {
  13. namespace webrtc
  14. {
  15. constexpr int kWidth = 172;
  16. constexpr int kHeight = 144;
  17. constexpr UnityRenderingExtTextureFormat kFormat = kUnityRenderingExtFormatR8G8B8A8_SRGB;
  18. using testing::Values;
  19. class InternalCodecsTest : public VideoCodecTest
  20. {
  21. public:
  22. InternalCodecsTest()
  23. : container_(CreateGraphicsDeviceContainer(GetParam()))
  24. , device_(container_->device())
  25. {
  26. }
  27. ~InternalCodecsTest() override
  28. {
  29. if (encoder_)
  30. encoder_ = nullptr;
  31. }
  32. protected:
  33. void SetUp() override
  34. {
  35. if (!device_)
  36. GTEST_SKIP() << "The graphics driver is not installed on the device.";
  37. std::unique_ptr<ITexture2D> texture(device_->CreateDefaultTextureV(kWidth, kHeight, kFormat));
  38. if (!texture)
  39. GTEST_SKIP() << "The graphics driver cannot create a texture resource.";
  40. VideoCodecTest::SetUp();
  41. }
  42. SdpVideoFormat FindFormat(std::string name, const std::vector<SdpVideoFormat>& formats)
  43. {
  44. auto result =
  45. std::find_if(formats.begin(), formats.end(), [name](SdpVideoFormat x) { return x.name == name; });
  46. return *result;
  47. }
  48. std::unique_ptr<VideoEncoder> CreateEncoder() override
  49. {
  50. SdpVideoFormat format = FindFormat(codecName, encoderFactory.SupportedFormats());
  51. return encoderFactory.CreateVideoEncoder(format);
  52. }
  53. std::unique_ptr<VideoDecoder> CreateDecoder() override
  54. {
  55. SdpVideoFormat format = FindFormat(codecName, decoderFactory.GetSupportedFormats());
  56. return decoderFactory.CreateVideoDecoder(format);
  57. }
  58. std::unique_ptr<FrameGeneratorInterface> CreateFrameGenerator(
  59. int width,
  60. int height,
  61. absl::optional<FrameGeneratorInterface::OutputType> type,
  62. absl::optional<int> num_squares) override
  63. {
  64. return CreateVideoFrameGenerator(container_->device(), width, height, type, num_squares);
  65. }
  66. void ModifyCodecSettings(VideoCodec* codec_settings) override
  67. {
  68. webrtc::test::CodecSettings(kVideoCodecVP8, codec_settings);
  69. codec_settings->width = kWidth;
  70. codec_settings->height = kHeight;
  71. codec_settings->VP8()->complexity = VideoCodecComplexity::kComplexityNormal;
  72. }
  73. void VerifyQpParser(const EncodedImage& encoded_frame) const
  74. {
  75. int qp;
  76. EXPECT_GT(encoded_frame.size(), 0u);
  77. ASSERT_TRUE(vp8::GetQp(encoded_frame.data(), encoded_frame.size(), &qp));
  78. EXPECT_EQ(encoded_frame.qp_, qp) << "Encoder QP != parsed bitstream QP.";
  79. }
  80. void EncodeAndWaitForFrame(
  81. const VideoFrame& inputFrame,
  82. EncodedImage* encodedFrame,
  83. CodecSpecificInfo* codec_specific_info,
  84. bool keyframe = false)
  85. {
  86. std::vector<VideoFrameType> frame_types;
  87. if (keyframe)
  88. {
  89. frame_types.emplace_back(VideoFrameType::kVideoFrameKey);
  90. }
  91. else
  92. {
  93. frame_types.emplace_back(VideoFrameType::kVideoFrameDelta);
  94. }
  95. EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->Encode(inputFrame, &frame_types));
  96. ASSERT_TRUE(WaitForEncodedFrame(encodedFrame, codec_specific_info));
  97. VerifyQpParser(*encodedFrame);
  98. EXPECT_EQ(kVideoCodecVP8, codec_specific_info->codecType);
  99. EXPECT_EQ(0, encodedFrame->SpatialIndex());
  100. }
  101. std::string codecName = "VP8";
  102. InternalEncoderFactory encoderFactory;
  103. InternalDecoderFactory decoderFactory;
  104. std::unique_ptr<GraphicsDeviceContainer> container_;
  105. IGraphicsDevice* device_;
  106. };
  107. TEST_P(InternalCodecsTest, EncodeFrameAndRelease)
  108. {
  109. EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->Release());
  110. EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->InitEncode(&codecSettings_, kSettings()));
  111. EncodedImage encoded_frame;
  112. CodecSpecificInfo codec_specific_info;
  113. EncodeAndWaitForFrame(NextInputFrame(), &encoded_frame, &codec_specific_info);
  114. EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->Release());
  115. EXPECT_EQ(WEBRTC_VIDEO_CODEC_UNINITIALIZED, encoder_->Encode(NextInputFrame(), nullptr));
  116. }
  117. INSTANTIATE_TEST_SUITE_P(GfxDevice, InternalCodecsTest, testing::ValuesIn(supportedGfxDevices));
  118. }
  119. }