VideoCodecTest.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include "pch.h"
  2. #include "VideoCodecTest.h"
  3. #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
  4. static const int kEncodeTimeoutMs = 100;
  5. static const int kDecodeTimeoutMs = 100;
  6. namespace unity
  7. {
  8. namespace webrtc
  9. {
  10. VideoEncoder::Capabilities kCapabilities() { return VideoEncoder::Capabilities(false); }
  11. std::string kProfileLevelIdString() { return *H264ProfileLevelIdToString(kProfileLevelId); }
  12. VideoEncoder::Settings kSettings() { return VideoEncoder::Settings(kCapabilities(), kNumCores, kMaxPayloadSize); }
  13. EncodedImageCallback::Result VideoCodecTest::FakeEncodedImageCallback::OnEncodedImage(
  14. const EncodedImage& frame, const CodecSpecificInfo* codec_specific_info)
  15. {
  16. MutexLock lock(&_test->encodedFrameSection_);
  17. _test->encodedFrames_.push_back(frame);
  18. RTC_DCHECK(codec_specific_info);
  19. _test->codecSpecificInfos_.push_back(*codec_specific_info);
  20. // if (!_test->wait_for_encoded_frames_threshold_)
  21. {
  22. _test->encodedFrameEvent_.Set();
  23. return Result(Result::OK);
  24. }
  25. // if (_test->encodedFrames_.size() == _test->wait_for_encoded_frames_threshold_)
  26. //{
  27. // _test->wait_for_encoded_frames_threshold_ = 1;
  28. // _test->encodedFrameEvent_.Set();
  29. //}
  30. // return Result(Result::OK);
  31. }
  32. void VideoCodecTest::FakeDecodedImageCallback::Decoded(
  33. VideoFrame& frame, absl::optional<int32_t> decode_time_ms, absl::optional<uint8_t> qp)
  34. {
  35. MutexLock lock(&_test->decodedFrameSection_);
  36. _test->decodedFrame_.emplace(frame);
  37. _test->decodedQp_ = qp;
  38. _test->decodedFrameEvent_.Set();
  39. }
  40. VideoFrame VideoCodecTest::NextInputFrame()
  41. {
  42. test::FrameGeneratorInterface::VideoFrameData frame_data = inputFrameGenerator_->NextFrame();
  43. VideoFrame input_frame = VideoFrame::Builder()
  44. .set_video_frame_buffer(frame_data.buffer)
  45. .set_update_rect(frame_data.update_rect)
  46. .build();
  47. // I420Buffer::SetBlack(frame_data.buffer);
  48. const uint32_t timestamp = lastInputFrameTimestamp_ + kVideoPayloadTypeFrequency / codecSettings_.maxFramerate;
  49. input_frame.set_timestamp(timestamp);
  50. lastInputFrameTimestamp_ = timestamp;
  51. return input_frame;
  52. }
  53. void VideoCodecTest::ChangeFrameResolution(size_t width, size_t height)
  54. {
  55. inputFrameGenerator_->ChangeResolution(width, height);
  56. }
  57. bool VideoCodecTest::WaitForEncodedFrame(EncodedImage* frame, CodecSpecificInfo* codec_specific_info)
  58. {
  59. std::vector<EncodedImage> frames;
  60. std::vector<CodecSpecificInfo> codec_specific_infos;
  61. if (!WaitForEncodedFrames(&frames, &codec_specific_infos))
  62. return false;
  63. EXPECT_EQ(frames.size(), static_cast<size_t>(1));
  64. EXPECT_EQ(frames.size(), codec_specific_infos.size());
  65. *frame = frames[0];
  66. *codec_specific_info = codec_specific_infos[0];
  67. return true;
  68. }
  69. bool VideoCodecTest::WaitForEncodedFrames(
  70. std::vector<EncodedImage>* frames, std::vector<CodecSpecificInfo>* codec_specific_info)
  71. {
  72. EXPECT_TRUE(encodedFrameEvent_.Wait(kEncodeTimeoutMs)) << "Timed out while waiting for encoded frame.";
  73. // This becomes unsafe if there are multiple threads waiting for frames.
  74. MutexLock lock(&encodedFrameSection_);
  75. EXPECT_FALSE(encodedFrames_.empty());
  76. EXPECT_FALSE(codecSpecificInfos_.empty());
  77. EXPECT_EQ(encodedFrames_.size(), codecSpecificInfos_.size());
  78. if (!encodedFrames_.empty())
  79. {
  80. *frames = encodedFrames_;
  81. encodedFrames_.clear();
  82. RTC_DCHECK(!codecSpecificInfos_.empty());
  83. *codec_specific_info = codecSpecificInfos_;
  84. codecSpecificInfos_.clear();
  85. return true;
  86. }
  87. return false;
  88. }
  89. bool VideoCodecTest::WaitForDecodedFrame(std::unique_ptr<VideoFrame>* frame, absl::optional<uint8_t>* qp)
  90. {
  91. EXPECT_TRUE(decodedFrameEvent_.Wait(kDecodeTimeoutMs)) << "Timed out while waiting for a decoded frame.";
  92. // This becomes unsafe if there are multiple threads waiting for frames.
  93. MutexLock lock(&decodedFrameSection_);
  94. EXPECT_TRUE(decodedFrame_);
  95. if (decodedFrame_)
  96. {
  97. frame->reset(new VideoFrame(std::move(*decodedFrame_)));
  98. *qp = decodedQp_;
  99. decodedFrame_.reset();
  100. return true;
  101. }
  102. else
  103. {
  104. return false;
  105. }
  106. }
  107. void VideoCodecTest::SetUp()
  108. {
  109. ModifyCodecSettings(&codecSettings_);
  110. inputFrameGenerator_ = CreateFrameGenerator(
  111. codecSettings_.width,
  112. codecSettings_.height,
  113. test::FrameGeneratorInterface::OutputType::kI420,
  114. absl::optional<int>());
  115. encoder_ = CreateEncoder();
  116. decoder_ = CreateDecoder();
  117. encoder_->RegisterEncodeCompleteCallback(&encodedImageCallback_);
  118. decoder_->RegisterDecodeCompleteCallback(&decodedImageCallback_);
  119. }
  120. void VideoCodecTest::TearDown()
  121. {
  122. // call destructor
  123. inputFrameGenerator_ = nullptr;
  124. }
  125. }
  126. }