VideoTrackSourceTest.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "pch.h"
  2. #include "Context.h"
  3. #include "GpuMemoryBuffer.h"
  4. #include "GraphicsDevice/IGraphicsDevice.h"
  5. #include "GraphicsDevice/ITexture2D.h"
  6. #include "GraphicsDeviceTestBase.h"
  7. #include "UnityVideoTrackSource.h"
  8. #include "VideoFrameUtil.h"
  9. #include <api/task_queue/default_task_queue_factory.h>
  10. using testing::_;
  11. using testing::Invoke;
  12. using testing::Mock;
  13. namespace unity
  14. {
  15. namespace webrtc
  16. {
  17. constexpr TimeDelta kTimeout = TimeDelta::Millis(1000);
  18. class MockVideoSink : public rtc::VideoSinkInterface<::webrtc::VideoFrame>
  19. {
  20. public:
  21. ~MockVideoSink() override = default;
  22. MOCK_METHOD(void, OnFrame, (const ::webrtc::VideoFrame&), (override));
  23. };
  24. const int kWidth = 1280;
  25. const int kHeight = 720;
  26. class VideoTrackSourceTest : public GraphicsDeviceTestBase
  27. {
  28. public:
  29. VideoTrackSourceTest()
  30. : m_texture(nullptr)
  31. , m_taskQueueFactory(CreateDefaultTaskQueueFactory())
  32. {
  33. m_trackSource = UnityVideoTrackSource::Create(false, absl::nullopt, m_taskQueueFactory.get());
  34. m_trackSource->AddOrUpdateSink(&sink_, rtc::VideoSinkWants());
  35. }
  36. ~VideoTrackSourceTest() override { m_trackSource->RemoveSink(&sink_); }
  37. protected:
  38. void SetUp() override
  39. {
  40. if (!device())
  41. GTEST_SKIP() << "The graphics driver is not installed on the device.";
  42. m_texture.reset(device()->CreateDefaultTextureV(kWidth, kHeight, format()));
  43. if (!m_texture)
  44. GTEST_SKIP() << "The graphics driver cannot create a texture resource.";
  45. ContextDependencies dependencies;
  46. dependencies.device = device();
  47. context = std::make_unique<Context>(dependencies);
  48. }
  49. std::unique_ptr<Context> context;
  50. std::unique_ptr<ITexture2D> m_texture;
  51. std::unique_ptr<TaskQueueFactory> m_taskQueueFactory;
  52. MockVideoSink sink_;
  53. rtc::scoped_refptr<UnityVideoTrackSource> m_trackSource;
  54. ::webrtc::VideoFrame::Builder CreateBlackFrameBuilder(int width, int height)
  55. {
  56. rtc::scoped_refptr<webrtc::I420Buffer> buffer = webrtc::I420Buffer::Create(width, height);
  57. webrtc::I420Buffer::SetBlack(buffer);
  58. return ::webrtc::VideoFrame::Builder().set_video_frame_buffer(buffer);
  59. }
  60. void SendTestFrame()
  61. {
  62. auto frame = CreateTestFrame(device(), m_texture.get(), format());
  63. m_trackSource->OnFrameCaptured(std::move(frame));
  64. }
  65. };
  66. TEST_P(VideoTrackSourceTest, OnFrameCaptured)
  67. {
  68. rtc::Event done;
  69. SendTestFrame();
  70. EXPECT_CALL(sink_, OnFrame(_)).WillOnce(Invoke([&done](const ::webrtc::VideoFrame& frame) { done.Set(); }));
  71. EXPECT_TRUE(done.Wait(kTimeout.ms()));
  72. }
  73. INSTANTIATE_TEST_SUITE_P(GfxDeviceAndColorSpece, VideoTrackSourceTest, testing::ValuesIn(VALUES_TEST_ENV));
  74. } // end namespace webrtc
  75. } // end namespace unity