webrtc_video_track_source_unittest.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "remoting/protocol/webrtc_video_track_source.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/callback_helpers.h"
  8. #include "base/test/mock_callback.h"
  9. #include "base/test/task_environment.h"
  10. #include "testing/gmock/include/gmock/gmock.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h"
  13. #include "third_party/webrtc/modules/desktop_capture/desktop_geometry.h"
  14. #include "third_party/webrtc/rtc_base/ref_counted_object.h"
  15. using testing::Field;
  16. using testing::InSequence;
  17. using testing::Property;
  18. using webrtc::BasicDesktopFrame;
  19. using webrtc::DesktopSize;
  20. using webrtc::VideoFrame;
  21. namespace remoting {
  22. namespace protocol {
  23. namespace {
  24. class MockVideoSink : public rtc::VideoSinkInterface<VideoFrame> {
  25. public:
  26. ~MockVideoSink() override = default;
  27. MOCK_METHOD(void, OnFrame, (const VideoFrame& frame), (override));
  28. };
  29. } // namespace
  30. class WebrtcVideoTrackSourceTest : public testing::Test {
  31. public:
  32. WebrtcVideoTrackSourceTest() = default;
  33. protected:
  34. base::test::SingleThreadTaskEnvironment task_environment_{
  35. base::test::TaskEnvironment::TimeSource::MOCK_TIME};
  36. MockVideoSink video_sink_;
  37. base::MockCallback<WebrtcVideoTrackSource::AddSinkCallback>
  38. add_sink_callback_;
  39. };
  40. TEST_F(WebrtcVideoTrackSourceTest, AddSinkTriggersCallback) {
  41. EXPECT_CALL(add_sink_callback_,
  42. Run(Field(&rtc::VideoSinkWants::max_framerate_fps, 123)));
  43. rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source(
  44. new rtc::RefCountedObject<WebrtcVideoTrackSource>(
  45. add_sink_callback_.Get()));
  46. rtc::VideoSinkWants wants;
  47. wants.max_framerate_fps = 123;
  48. source->AddOrUpdateSink(&video_sink_, wants);
  49. task_environment_.FastForwardUntilNoTasksRemain();
  50. }
  51. TEST_F(WebrtcVideoTrackSourceTest, CapturedFrameSentToAddedSink) {
  52. auto frame = std::make_unique<BasicDesktopFrame>(DesktopSize(123, 234));
  53. EXPECT_CALL(video_sink_, OnFrame(Property(&VideoFrame::width, 123)));
  54. rtc::scoped_refptr<WebrtcVideoTrackSource> source(
  55. new rtc::RefCountedObject<WebrtcVideoTrackSource>(
  56. add_sink_callback_.Get()));
  57. source->AddOrUpdateSink(&video_sink_, rtc::VideoSinkWants());
  58. source->SendCapturedFrame(std::move(frame), nullptr);
  59. task_environment_.FastForwardUntilNoTasksRemain();
  60. }
  61. TEST_F(WebrtcVideoTrackSourceTest, FramesHaveIncrementingIds) {
  62. {
  63. InSequence s;
  64. EXPECT_CALL(video_sink_, OnFrame(Property(&VideoFrame::id, 0)));
  65. EXPECT_CALL(video_sink_, OnFrame(Property(&VideoFrame::id, 1)));
  66. EXPECT_CALL(video_sink_, OnFrame(Property(&VideoFrame::id, 2)));
  67. }
  68. rtc::scoped_refptr<WebrtcVideoTrackSource> source(
  69. new rtc::RefCountedObject<WebrtcVideoTrackSource>(
  70. add_sink_callback_.Get()));
  71. source->AddOrUpdateSink(&video_sink_, rtc::VideoSinkWants());
  72. source->SendCapturedFrame(
  73. std::make_unique<BasicDesktopFrame>(DesktopSize(100, 100)), nullptr);
  74. source->SendCapturedFrame(
  75. std::make_unique<BasicDesktopFrame>(DesktopSize(100, 100)), nullptr);
  76. source->SendCapturedFrame(
  77. std::make_unique<BasicDesktopFrame>(DesktopSize(100, 100)), nullptr);
  78. task_environment_.FastForwardUntilNoTasksRemain();
  79. }
  80. } // namespace protocol
  81. } // namespace remoting