FrameGenerator.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include "pch.h"
  2. #include "FrameGenerator.h"
  3. #include "GraphicsDevice/ITexture2D.h"
  4. #include "UnityVideoTrackSource.h"
  5. #include "VideoFrameAdapter.h"
  6. #include "VideoFrameUtil.h"
  7. namespace unity
  8. {
  9. namespace webrtc
  10. {
  11. std::unique_ptr<FrameGeneratorInterface> CreateVideoFrameGenerator(
  12. IGraphicsDevice* device,
  13. int width,
  14. int height,
  15. absl::optional<FrameGeneratorInterface::OutputType> type,
  16. absl::optional<int> num_squares)
  17. {
  18. return std::make_unique<VideoFrameGenerator>(
  19. device, width, height, type.value_or(FrameGeneratorInterface::OutputType::kI420), num_squares.value_or(10));
  20. }
  21. VideoFrameGenerator::VideoFrameGenerator(
  22. IGraphicsDevice* device, int width, int height, OutputType type, int num_squares)
  23. : device_(device)
  24. , width_(width)
  25. , height_(height)
  26. {
  27. }
  28. void VideoFrameGenerator::ChangeResolution(size_t width, size_t height)
  29. {
  30. MutexLock lock(&mutex_);
  31. width_ = static_cast<int>(width);
  32. height_ = static_cast<int>(height);
  33. RTC_CHECK(width_ > 0);
  34. RTC_CHECK(height_ > 0);
  35. }
  36. FrameGeneratorInterface::VideoFrameData VideoFrameGenerator::NextFrame()
  37. {
  38. MutexLock lock(&mutex_);
  39. const UnityRenderingExtTextureFormat kFormat = kUnityRenderingExtFormatR8G8B8A8_SRGB;
  40. ITexture2D* texture = device_->CreateCPUReadTextureV(
  41. static_cast<uint32_t>(width_), static_cast<uint32_t>(height_), kFormat);
  42. queue_.push(std::unique_ptr<ITexture2D>(texture));
  43. rtc::scoped_refptr<VideoFrame> frame = CreateTestFrame(device_, texture, kFormat);
  44. EXPECT_TRUE(device_->WaitIdleForTest());
  45. ::webrtc::VideoFrame videoFrame = VideoFrameAdapter::CreateVideoFrame(frame);
  46. rtc::scoped_refptr<VideoFrameBuffer> buffer = videoFrame.video_frame_buffer();
  47. return VideoFrameData(buffer, absl::nullopt);
  48. }
  49. }
  50. }