blob: aaed205423b2433e19eec7b04a0d456cf5a0fbe9 [file] [log] [blame]
pbos@webrtc.org26d12102013-05-29 13:41:03 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020010#ifndef TEST_FRAME_GENERATOR_CAPTURER_H_
11#define TEST_FRAME_GENERATOR_CAPTURER_H_
pbos@webrtc.org26d12102013-05-29 13:41:03 +000012
kwibergbfefb032016-05-01 14:53:46 -070013#include <memory>
sprang@webrtc.org131bea82015-02-18 12:46:06 +000014#include <string>
15
Sebastian Janssonfb14c5d2019-02-28 13:30:04 +010016#include "api/task_queue/task_queue_factory.h"
Artem Titov33f9d2b2019-12-05 15:59:00 +010017#include "api/test/frame_generator_interface.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "api/video/video_frame.h"
Steve Anton10542f22019-01-11 09:11:00 -080019#include "rtc_base/critical_section.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/task_queue.h"
Sebastian Jansson53571c72019-07-31 17:30:03 +020021#include "rtc_base/task_utils/repeating_task.h"
Artem Titov33f9d2b2019-12-05 15:59:00 +010022#include "system_wrappers/include/clock.h"
Sebastian Janssonf1f363f2018-08-13 14:24:58 +020023#include "test/test_video_capturer.h"
pbos@webrtc.org26d12102013-05-29 13:41:03 +000024
25namespace webrtc {
26
pbos@webrtc.org26d12102013-05-29 13:41:03 +000027namespace test {
Sebastian Jansson53571c72019-07-31 17:30:03 +020028namespace frame_gen_cap_impl {
29template <typename T>
30class 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
39struct FrameGeneratorCapturerConfig {
40 struct SquaresVideo {
41 int framerate = 30;
Artem Titov33f9d2b2019-12-05 15:59:00 +010042 FrameGeneratorInterface::OutputType pixel_format =
43 FrameGeneratorInterface::OutputType::kI420;
Sebastian Jansson53571c72019-07-31 17:30:03 +020044 int width = 320;
45 int height = 180;
46 int num_squares = 10;
47 };
pbos@webrtc.org26d12102013-05-29 13:41:03 +000048
Sebastian Jansson53571c72019-07-31 17:30:03 +020049 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.org26d12102013-05-29 13:41:03 +000087
Sebastian Janssonf1f363f2018-08-13 14:24:58 +020088class FrameGeneratorCapturer : public TestVideoCapturer {
pbos@webrtc.org26d12102013-05-29 13:41:03 +000089 public:
perkj803d97f2016-11-01 11:45:46 -070090 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 Titov503d7232019-12-04 12:37:13 +0100101 FrameGeneratorCapturer(
102 Clock* clock,
103 std::unique_ptr<FrameGeneratorInterface> frame_generator,
104 int target_fps,
105 TaskQueueFactory& task_queue_factory);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000106 virtual ~FrameGeneratorCapturer();
107
Sebastian Jansson53571c72019-07-31 17:30:03 +0200108 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öller8eeccbe2018-12-14 13:35:32 +0100129 void Start();
130 void Stop();
perkjfa10b552016-10-02 23:45:26 -0700131 void ChangeResolution(size_t width, size_t height);
Sebastian Janssonba3decf2018-08-30 11:19:23 +0200132 void ChangeFramerate(int target_framerate);
perkja49cbd32016-09-16 07:53:41 -0700133
perkj803d97f2016-11-01 11:45:46 -0700134 void SetSinkWantsObserver(SinkWantsObserver* observer);
135
perkja49cbd32016-09-16 07:53:41 -0700136 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
137 const rtc::VideoSinkWants& wants) override;
138 void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override;
139
sprang867fb522015-08-03 04:38:41 -0700140 void ForceFrame();
Perba7dc722016-04-19 15:01:23 +0200141 void SetFakeRotation(VideoRotation rotation);
Johannes Kronf7f13e02018-12-12 11:17:43 +0100142 void SetFakeColorSpace(absl::optional<ColorSpace> color_space);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000143
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000144 int64_t first_frame_capture_time() const { return first_frame_capture_time_; }
145
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000146 bool Init();
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000147
148 private:
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000149 void InsertFrame();
150 static bool Run(void* obj);
sprangc5d62e22017-04-02 23:53:04 -0700151 int GetCurrentConfiguredFramerate();
Niels Möller3793bb42018-12-20 13:46:06 +0100152 void UpdateFps(int max_fps) RTC_EXCLUSIVE_LOCKS_REQUIRED(&lock_);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000153
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000154 Clock* const clock_;
Sebastian Jansson53571c72019-07-31 17:30:03 +0200155 RepeatingTaskHandle frame_task_;
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000156 bool sending_;
danilchapa37de392017-09-09 04:17:22 -0700157 SinkWantsObserver* sink_wants_observer_ RTC_GUARDED_BY(&lock_);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000158
Peter Boströmf2f82832015-05-01 13:00:41 +0200159 rtc::CriticalSection lock_;
Artem Titov503d7232019-12-04 12:37:13 +0100160 std::unique_ptr<FrameGeneratorInterface> frame_generator_;
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000161
Sebastian Janssonba3decf2018-08-30 11:19:23 +0200162 int source_fps_ RTC_GUARDED_BY(&lock_);
163 int target_capture_fps_ RTC_GUARDED_BY(&lock_);
Danil Chapovalov431abd92018-06-18 12:54:17 +0200164 absl::optional<int> wanted_fps_ RTC_GUARDED_BY(&lock_);
Perba7dc722016-04-19 15:01:23 +0200165 VideoRotation fake_rotation_ = kVideoRotation_0;
Johannes Kronf7f13e02018-12-12 11:17:43 +0100166 absl::optional<ColorSpace> fake_color_space_ RTC_GUARDED_BY(&lock_);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000167
168 int64_t first_frame_capture_time_;
ilnikbaded152017-03-17 05:55:25 -0700169 // 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.org26d12102013-05-29 13:41:03 +0000172};
ilnikbaded152017-03-17 05:55:25 -0700173} // namespace test
174} // namespace webrtc
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000175
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200176#endif // TEST_FRAME_GENERATOR_CAPTURER_H_