pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 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 | */ |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 10 | #ifndef TEST_FRAME_GENERATOR_CAPTURER_H_ |
| 11 | #define TEST_FRAME_GENERATOR_CAPTURER_H_ |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 12 | |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 13 | #include <memory> |
sprang@webrtc.org | 131bea8 | 2015-02-18 12:46:06 +0000 | [diff] [blame] | 14 | #include <string> |
| 15 | |
Sebastian Jansson | fb14c5d | 2019-02-28 13:30:04 +0100 | [diff] [blame] | 16 | #include "api/task_queue/task_queue_factory.h" |
Artem Titov | 33f9d2b | 2019-12-05 15:59:00 +0100 | [diff] [blame] | 17 | #include "api/test/frame_generator_interface.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "api/video/video_frame.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 19 | #include "rtc_base/critical_section.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "rtc_base/task_queue.h" |
Sebastian Jansson | 53571c7 | 2019-07-31 17:30:03 +0200 | [diff] [blame] | 21 | #include "rtc_base/task_utils/repeating_task.h" |
Artem Titov | 33f9d2b | 2019-12-05 15:59:00 +0100 | [diff] [blame] | 22 | #include "system_wrappers/include/clock.h" |
Sebastian Jansson | f1f363f | 2018-08-13 14:24:58 +0200 | [diff] [blame] | 23 | #include "test/test_video_capturer.h" |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 24 | |
| 25 | namespace webrtc { |
| 26 | |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 27 | namespace test { |
Sebastian Jansson | 53571c7 | 2019-07-31 17:30:03 +0200 | [diff] [blame] | 28 | namespace frame_gen_cap_impl { |
| 29 | template <typename T> |
| 30 | class AutoOpt : public absl::optional<T> { |
| 31 | public: |
| 32 | T* operator->() { |
| 33 | if (!absl::optional<T>::has_value()) |
| 34 | this->emplace(T()); |
| 35 | return absl::optional<T>::operator->(); |
| 36 | } |
| 37 | }; |
| 38 | } // namespace frame_gen_cap_impl |
| 39 | struct FrameGeneratorCapturerConfig { |
| 40 | struct SquaresVideo { |
| 41 | int framerate = 30; |
Artem Titov | 33f9d2b | 2019-12-05 15:59:00 +0100 | [diff] [blame] | 42 | FrameGeneratorInterface::OutputType pixel_format = |
| 43 | FrameGeneratorInterface::OutputType::kI420; |
Sebastian Jansson | 53571c7 | 2019-07-31 17:30:03 +0200 | [diff] [blame] | 44 | int width = 320; |
| 45 | int height = 180; |
| 46 | int num_squares = 10; |
| 47 | }; |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 48 | |
Sebastian Jansson | 53571c7 | 2019-07-31 17:30:03 +0200 | [diff] [blame] | 49 | struct SquareSlides { |
| 50 | int framerate = 30; |
| 51 | TimeDelta change_interval = TimeDelta::seconds(10); |
| 52 | int width = 1600; |
| 53 | int height = 1200; |
| 54 | }; |
| 55 | |
| 56 | struct VideoFile { |
| 57 | int framerate = 30; |
| 58 | std::string name; |
| 59 | // Must be set to width and height of the source video file. |
| 60 | int width = 0; |
| 61 | int height = 0; |
| 62 | }; |
| 63 | |
| 64 | struct ImageSlides { |
| 65 | int framerate = 30; |
| 66 | TimeDelta change_interval = TimeDelta::seconds(10); |
| 67 | struct Crop { |
| 68 | TimeDelta scroll_duration = TimeDelta::seconds(0); |
| 69 | absl::optional<int> width; |
| 70 | absl::optional<int> height; |
| 71 | } crop; |
| 72 | int width = 1850; |
| 73 | int height = 1110; |
| 74 | std::vector<std::string> paths = { |
| 75 | "web_screenshot_1850_1110", |
| 76 | "presentation_1850_1110", |
| 77 | "photo_1850_1110", |
| 78 | "difficult_photo_1850_1110", |
| 79 | }; |
| 80 | }; |
| 81 | |
| 82 | frame_gen_cap_impl::AutoOpt<SquaresVideo> squares_video; |
| 83 | frame_gen_cap_impl::AutoOpt<SquareSlides> squares_slides; |
| 84 | frame_gen_cap_impl::AutoOpt<VideoFile> video_file; |
| 85 | frame_gen_cap_impl::AutoOpt<ImageSlides> image_slides; |
| 86 | }; |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 87 | |
Sebastian Jansson | f1f363f | 2018-08-13 14:24:58 +0200 | [diff] [blame] | 88 | class FrameGeneratorCapturer : public TestVideoCapturer { |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 89 | public: |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 90 | class SinkWantsObserver { |
| 91 | public: |
| 92 | // OnSinkWantsChanged is called when FrameGeneratorCapturer::AddOrUpdateSink |
| 93 | // is called. |
| 94 | virtual void OnSinkWantsChanged(rtc::VideoSinkInterface<VideoFrame>* sink, |
| 95 | const rtc::VideoSinkWants& wants) = 0; |
| 96 | |
| 97 | protected: |
| 98 | virtual ~SinkWantsObserver() {} |
| 99 | }; |
| 100 | |
Artem Titov | 503d723 | 2019-12-04 12:37:13 +0100 | [diff] [blame] | 101 | FrameGeneratorCapturer( |
| 102 | Clock* clock, |
| 103 | std::unique_ptr<FrameGeneratorInterface> frame_generator, |
| 104 | int target_fps, |
| 105 | TaskQueueFactory& task_queue_factory); |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 106 | virtual ~FrameGeneratorCapturer(); |
| 107 | |
Sebastian Jansson | 53571c7 | 2019-07-31 17:30:03 +0200 | [diff] [blame] | 108 | static std::unique_ptr<FrameGeneratorCapturer> Create( |
| 109 | Clock* clock, |
| 110 | TaskQueueFactory& task_queue_factory, |
| 111 | FrameGeneratorCapturerConfig::SquaresVideo config); |
| 112 | static std::unique_ptr<FrameGeneratorCapturer> Create( |
| 113 | Clock* clock, |
| 114 | TaskQueueFactory& task_queue_factory, |
| 115 | FrameGeneratorCapturerConfig::SquareSlides config); |
| 116 | static std::unique_ptr<FrameGeneratorCapturer> Create( |
| 117 | Clock* clock, |
| 118 | TaskQueueFactory& task_queue_factory, |
| 119 | FrameGeneratorCapturerConfig::VideoFile config); |
| 120 | static std::unique_ptr<FrameGeneratorCapturer> Create( |
| 121 | Clock* clock, |
| 122 | TaskQueueFactory& task_queue_factory, |
| 123 | FrameGeneratorCapturerConfig::ImageSlides config); |
| 124 | static std::unique_ptr<FrameGeneratorCapturer> Create( |
| 125 | Clock* clock, |
| 126 | TaskQueueFactory& task_queue_factory, |
| 127 | const FrameGeneratorCapturerConfig& config); |
| 128 | |
Niels Möller | 8eeccbe | 2018-12-14 13:35:32 +0100 | [diff] [blame] | 129 | void Start(); |
| 130 | void Stop(); |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 131 | void ChangeResolution(size_t width, size_t height); |
Sebastian Jansson | ba3decf | 2018-08-30 11:19:23 +0200 | [diff] [blame] | 132 | void ChangeFramerate(int target_framerate); |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 133 | |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 134 | void SetSinkWantsObserver(SinkWantsObserver* observer); |
| 135 | |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 136 | void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink, |
| 137 | const rtc::VideoSinkWants& wants) override; |
| 138 | void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override; |
| 139 | |
sprang | 867fb52 | 2015-08-03 04:38:41 -0700 | [diff] [blame] | 140 | void ForceFrame(); |
Per | ba7dc72 | 2016-04-19 15:01:23 +0200 | [diff] [blame] | 141 | void SetFakeRotation(VideoRotation rotation); |
Johannes Kron | f7f13e0 | 2018-12-12 11:17:43 +0100 | [diff] [blame] | 142 | void SetFakeColorSpace(absl::optional<ColorSpace> color_space); |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 143 | |
wu@webrtc.org | cd70119 | 2014-04-24 22:10:24 +0000 | [diff] [blame] | 144 | int64_t first_frame_capture_time() const { return first_frame_capture_time_; } |
| 145 | |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 146 | bool Init(); |
sprang@webrtc.org | 131bea8 | 2015-02-18 12:46:06 +0000 | [diff] [blame] | 147 | |
| 148 | private: |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 149 | void InsertFrame(); |
| 150 | static bool Run(void* obj); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 151 | int GetCurrentConfiguredFramerate(); |
Niels Möller | 3793bb4 | 2018-12-20 13:46:06 +0100 | [diff] [blame] | 152 | void UpdateFps(int max_fps) RTC_EXCLUSIVE_LOCKS_REQUIRED(&lock_); |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 153 | |
pbos@webrtc.org | de1429e | 2014-04-28 13:00:21 +0000 | [diff] [blame] | 154 | Clock* const clock_; |
Sebastian Jansson | 53571c7 | 2019-07-31 17:30:03 +0200 | [diff] [blame] | 155 | RepeatingTaskHandle frame_task_; |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 156 | bool sending_; |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 157 | SinkWantsObserver* sink_wants_observer_ RTC_GUARDED_BY(&lock_); |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 158 | |
Peter Boström | f2f8283 | 2015-05-01 13:00:41 +0200 | [diff] [blame] | 159 | rtc::CriticalSection lock_; |
Artem Titov | 503d723 | 2019-12-04 12:37:13 +0100 | [diff] [blame] | 160 | std::unique_ptr<FrameGeneratorInterface> frame_generator_; |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 161 | |
Sebastian Jansson | ba3decf | 2018-08-30 11:19:23 +0200 | [diff] [blame] | 162 | int source_fps_ RTC_GUARDED_BY(&lock_); |
| 163 | int target_capture_fps_ RTC_GUARDED_BY(&lock_); |
Danil Chapovalov | 431abd9 | 2018-06-18 12:54:17 +0200 | [diff] [blame] | 164 | absl::optional<int> wanted_fps_ RTC_GUARDED_BY(&lock_); |
Per | ba7dc72 | 2016-04-19 15:01:23 +0200 | [diff] [blame] | 165 | VideoRotation fake_rotation_ = kVideoRotation_0; |
Johannes Kron | f7f13e0 | 2018-12-12 11:17:43 +0100 | [diff] [blame] | 166 | absl::optional<ColorSpace> fake_color_space_ RTC_GUARDED_BY(&lock_); |
wu@webrtc.org | cd70119 | 2014-04-24 22:10:24 +0000 | [diff] [blame] | 167 | |
| 168 | int64_t first_frame_capture_time_; |
ilnik | baded15 | 2017-03-17 05:55:25 -0700 | [diff] [blame] | 169 | // Must be the last field, so it will be deconstructed first as tasks |
| 170 | // in the TaskQueue access other fields of the instance of this class. |
| 171 | rtc::TaskQueue task_queue_; |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 172 | }; |
ilnik | baded15 | 2017-03-17 05:55:25 -0700 | [diff] [blame] | 173 | } // namespace test |
| 174 | } // namespace webrtc |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 175 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 176 | #endif // TEST_FRAME_GENERATOR_CAPTURER_H_ |