VideoFrameSchedulerTest.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include "pch.h"
  2. #include "VideoFrameScheduler.h"
  3. #include <api/task_queue/default_task_queue_factory.h>
  4. namespace unity
  5. {
  6. namespace webrtc
  7. {
  8. class FakeTaskQueue : public TaskQueueBase
  9. {
  10. public:
  11. explicit FakeTaskQueue(SimulatedClock* clock)
  12. : task_queue_setter_(this)
  13. , clock_(clock)
  14. {
  15. }
  16. void Delete() override {}
  17. void PostTask(std::unique_ptr<QueuedTask> task) override
  18. {
  19. last_task_ = std::move(task);
  20. last_delay_ = 0;
  21. }
  22. void PostDelayedTask(std::unique_ptr<QueuedTask> task, uint32_t milliseconds) override
  23. {
  24. last_task_ = std::move(task);
  25. last_delay_ = milliseconds;
  26. }
  27. bool AdvanceTimeAndRunLastTask()
  28. {
  29. EXPECT_TRUE(last_task_);
  30. EXPECT_TRUE(last_delay_);
  31. clock_->AdvanceTimeMilliseconds(last_delay_.value_or(0));
  32. last_delay_.reset();
  33. auto task = std::move(last_task_);
  34. bool delete_task = task->Run();
  35. if (!delete_task)
  36. {
  37. // If the task should not be deleted then just release it.
  38. task.release();
  39. }
  40. return delete_task;
  41. }
  42. bool IsTaskQueued() { return !!last_task_; }
  43. uint32_t last_delay() const
  44. {
  45. EXPECT_TRUE(last_delay_.has_value());
  46. return last_delay_.value_or(-1);
  47. }
  48. private:
  49. CurrentTaskQueueSetter task_queue_setter_;
  50. SimulatedClock* clock_;
  51. std::unique_ptr<QueuedTask> last_task_;
  52. absl::optional<uint32_t> last_delay_;
  53. };
  54. class VideoFrameSchedulerTest : public ::testing::Test
  55. {
  56. public:
  57. VideoFrameSchedulerTest()
  58. : count_(0)
  59. , clock_(0)
  60. {
  61. }
  62. ~VideoFrameSchedulerTest() override = default;
  63. void InitScheduler(FakeTaskQueue& queue)
  64. {
  65. scheduler_ = std::make_unique<VideoFrameScheduler>(&queue, &clock_);
  66. scheduler_->SetMaxFramerateFps(kMaxFramerate);
  67. scheduler_->Start(std::bind(&VideoFrameSchedulerTest::CaptureCallback, this));
  68. }
  69. void CaptureCallback()
  70. {
  71. count_++;
  72. }
  73. protected:
  74. const int kMaxFramerate = 30;
  75. const TimeDelta kTimeDelta = TimeDelta::Seconds(1) / kMaxFramerate;
  76. int count_ = 0;
  77. SimulatedClock clock_;
  78. std::unique_ptr<VideoFrameScheduler> scheduler_;
  79. };
  80. TEST_F(VideoFrameSchedulerTest, Run)
  81. {
  82. FakeTaskQueue queue(&clock_);
  83. InitScheduler(queue);
  84. EXPECT_EQ(0, count_);
  85. EXPECT_FALSE(queue.AdvanceTimeAndRunLastTask());
  86. EXPECT_EQ(1, count_);
  87. scheduler_ = nullptr;
  88. }
  89. TEST_F(VideoFrameSchedulerTest, Pause)
  90. {
  91. FakeTaskQueue queue(&clock_);
  92. InitScheduler(queue);
  93. EXPECT_EQ(0, count_);
  94. scheduler_->Pause(true);
  95. EXPECT_TRUE(queue.AdvanceTimeAndRunLastTask());
  96. EXPECT_EQ(0, count_);
  97. scheduler_->Pause(false);
  98. EXPECT_FALSE(queue.AdvanceTimeAndRunLastTask());
  99. EXPECT_EQ(1, count_);
  100. scheduler_ = nullptr;
  101. }
  102. TEST_F(VideoFrameSchedulerTest, SetMaxFramerateFps)
  103. {
  104. FakeTaskQueue queue(&clock_);
  105. InitScheduler(queue);
  106. EXPECT_EQ(0, count_);
  107. const int maxFramerate = 5;
  108. scheduler_->SetMaxFramerateFps(maxFramerate);
  109. uint32_t delay = queue.last_delay();
  110. EXPECT_GT(delay, 0u);
  111. EXPECT_FALSE(queue.AdvanceTimeAndRunLastTask());
  112. EXPECT_EQ(1, count_);
  113. EXPECT_GT(queue.last_delay(), delay);
  114. EXPECT_FALSE(queue.AdvanceTimeAndRunLastTask());
  115. EXPECT_EQ(2, count_);
  116. scheduler_ = nullptr;
  117. }
  118. }
  119. }