frame_generator_capturer.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef TEST_FRAME_GENERATOR_CAPTURER_H_
  11. #define TEST_FRAME_GENERATOR_CAPTURER_H_
  12. #include <memory>
  13. #include <string>
  14. #include "api/task_queue/task_queue_factory.h"
  15. #include "api/test/frame_generator_interface.h"
  16. #include "api/video/video_frame.h"
  17. #include "rtc_base/synchronization/mutex.h"
  18. #include "rtc_base/task_queue.h"
  19. #include "rtc_base/task_utils/repeating_task.h"
  20. #include "system_wrappers/include/clock.h"
  21. #include "test/test_video_capturer.h"
  22. namespace webrtc {
  23. namespace test {
  24. namespace frame_gen_cap_impl {
  25. template <typename T>
  26. class AutoOpt : public absl::optional<T> {
  27. public:
  28. using absl::optional<T>::optional;
  29. T* operator->() {
  30. if (!absl::optional<T>::has_value())
  31. this->emplace(T());
  32. return absl::optional<T>::operator->();
  33. }
  34. };
  35. } // namespace frame_gen_cap_impl
  36. struct FrameGeneratorCapturerConfig {
  37. struct SquaresVideo {
  38. int framerate = 30;
  39. FrameGeneratorInterface::OutputType pixel_format =
  40. FrameGeneratorInterface::OutputType::kI420;
  41. int width = 320;
  42. int height = 180;
  43. int num_squares = 10;
  44. };
  45. struct SquareSlides {
  46. int framerate = 30;
  47. TimeDelta change_interval = TimeDelta::Seconds(10);
  48. int width = 1600;
  49. int height = 1200;
  50. };
  51. struct VideoFile {
  52. int framerate = 30;
  53. std::string name;
  54. // Must be set to width and height of the source video file.
  55. int width = 0;
  56. int height = 0;
  57. };
  58. struct ImageSlides {
  59. int framerate = 30;
  60. TimeDelta change_interval = TimeDelta::Seconds(10);
  61. struct Crop {
  62. TimeDelta scroll_duration = TimeDelta::Seconds(0);
  63. absl::optional<int> width;
  64. absl::optional<int> height;
  65. } crop;
  66. int width = 1850;
  67. int height = 1110;
  68. std::vector<std::string> paths = {
  69. "web_screenshot_1850_1110",
  70. "presentation_1850_1110",
  71. "photo_1850_1110",
  72. "difficult_photo_1850_1110",
  73. };
  74. };
  75. frame_gen_cap_impl::AutoOpt<SquaresVideo> squares_video;
  76. frame_gen_cap_impl::AutoOpt<SquareSlides> squares_slides;
  77. frame_gen_cap_impl::AutoOpt<VideoFile> video_file;
  78. frame_gen_cap_impl::AutoOpt<ImageSlides> image_slides;
  79. };
  80. class FrameGeneratorCapturer : public TestVideoCapturer {
  81. public:
  82. class SinkWantsObserver {
  83. public:
  84. // OnSinkWantsChanged is called when FrameGeneratorCapturer::AddOrUpdateSink
  85. // is called.
  86. virtual void OnSinkWantsChanged(rtc::VideoSinkInterface<VideoFrame>* sink,
  87. const rtc::VideoSinkWants& wants) = 0;
  88. protected:
  89. virtual ~SinkWantsObserver() {}
  90. };
  91. FrameGeneratorCapturer(
  92. Clock* clock,
  93. std::unique_ptr<FrameGeneratorInterface> frame_generator,
  94. int target_fps,
  95. TaskQueueFactory& task_queue_factory);
  96. virtual ~FrameGeneratorCapturer();
  97. static std::unique_ptr<FrameGeneratorCapturer> Create(
  98. Clock* clock,
  99. TaskQueueFactory& task_queue_factory,
  100. FrameGeneratorCapturerConfig::SquaresVideo config);
  101. static std::unique_ptr<FrameGeneratorCapturer> Create(
  102. Clock* clock,
  103. TaskQueueFactory& task_queue_factory,
  104. FrameGeneratorCapturerConfig::SquareSlides config);
  105. static std::unique_ptr<FrameGeneratorCapturer> Create(
  106. Clock* clock,
  107. TaskQueueFactory& task_queue_factory,
  108. FrameGeneratorCapturerConfig::VideoFile config);
  109. static std::unique_ptr<FrameGeneratorCapturer> Create(
  110. Clock* clock,
  111. TaskQueueFactory& task_queue_factory,
  112. FrameGeneratorCapturerConfig::ImageSlides config);
  113. static std::unique_ptr<FrameGeneratorCapturer> Create(
  114. Clock* clock,
  115. TaskQueueFactory& task_queue_factory,
  116. const FrameGeneratorCapturerConfig& config);
  117. void Start();
  118. void Stop();
  119. void ChangeResolution(size_t width, size_t height);
  120. void ChangeFramerate(int target_framerate);
  121. void SetSinkWantsObserver(SinkWantsObserver* observer);
  122. void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
  123. const rtc::VideoSinkWants& wants) override;
  124. void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override;
  125. void ForceFrame();
  126. void SetFakeRotation(VideoRotation rotation);
  127. void SetFakeColorSpace(absl::optional<ColorSpace> color_space);
  128. int64_t first_frame_capture_time() const { return first_frame_capture_time_; }
  129. bool Init();
  130. private:
  131. void InsertFrame();
  132. static bool Run(void* obj);
  133. int GetCurrentConfiguredFramerate();
  134. void UpdateFps(int max_fps) RTC_EXCLUSIVE_LOCKS_REQUIRED(&lock_);
  135. Clock* const clock_;
  136. RepeatingTaskHandle frame_task_;
  137. bool sending_;
  138. SinkWantsObserver* sink_wants_observer_ RTC_GUARDED_BY(&lock_);
  139. Mutex lock_;
  140. std::unique_ptr<FrameGeneratorInterface> frame_generator_;
  141. int source_fps_ RTC_GUARDED_BY(&lock_);
  142. int target_capture_fps_ RTC_GUARDED_BY(&lock_);
  143. absl::optional<int> wanted_fps_ RTC_GUARDED_BY(&lock_);
  144. VideoRotation fake_rotation_ = kVideoRotation_0;
  145. absl::optional<ColorSpace> fake_color_space_ RTC_GUARDED_BY(&lock_);
  146. int64_t first_frame_capture_time_;
  147. // Must be the last field, so it will be deconstructed first as tasks
  148. // in the TaskQueue access other fields of the instance of this class.
  149. rtc::TaskQueue task_queue_;
  150. };
  151. } // namespace test
  152. } // namespace webrtc
  153. #endif // TEST_FRAME_GENERATOR_CAPTURER_H_