blob: faf049843d128bbb7a7f855b6fd919ff7d4a9372 [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"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "api/video/video_frame.h"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "rtc_base/critical_section.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/task_queue.h"
Sebastian Jansson53571c72019-07-31 17:30:03 +020020#include "rtc_base/task_utils/repeating_task.h"
Emircan Uysaler03e6ec92018-03-09 15:03:26 -080021#include "test/frame_generator.h"
Sebastian Janssonf1f363f2018-08-13 14:24:58 +020022#include "test/test_video_capturer.h"
pbos@webrtc.org26d12102013-05-29 13:41:03 +000023
24namespace webrtc {
25
pbos@webrtc.org26d12102013-05-29 13:41:03 +000026namespace test {
Sebastian Jansson53571c72019-07-31 17:30:03 +020027namespace frame_gen_cap_impl {
28template <typename T>
29class AutoOpt : public absl::optional<T> {
30 public:
31 T* operator->() {
32 if (!absl::optional<T>::has_value())
33 this->emplace(T());
34 return absl::optional<T>::operator->();
35 }
36};
37} // namespace frame_gen_cap_impl
38struct FrameGeneratorCapturerConfig {
39 struct SquaresVideo {
40 int framerate = 30;
Sebastian Jansson79f32872019-10-04 09:25:06 +020041 FrameGenerator::OutputType pixel_format = FrameGenerator::OutputType::kI420;
Sebastian Jansson53571c72019-07-31 17:30:03 +020042 int width = 320;
43 int height = 180;
44 int num_squares = 10;
45 };
pbos@webrtc.org26d12102013-05-29 13:41:03 +000046
Sebastian Jansson53571c72019-07-31 17:30:03 +020047 struct SquareSlides {
48 int framerate = 30;
49 TimeDelta change_interval = TimeDelta::seconds(10);
50 int width = 1600;
51 int height = 1200;
52 };
53
54 struct VideoFile {
55 int framerate = 30;
56 std::string name;
57 // Must be set to width and height of the source video file.
58 int width = 0;
59 int height = 0;
60 };
61
62 struct ImageSlides {
63 int framerate = 30;
64 TimeDelta change_interval = TimeDelta::seconds(10);
65 struct Crop {
66 TimeDelta scroll_duration = TimeDelta::seconds(0);
67 absl::optional<int> width;
68 absl::optional<int> height;
69 } crop;
70 int width = 1850;
71 int height = 1110;
72 std::vector<std::string> paths = {
73 "web_screenshot_1850_1110",
74 "presentation_1850_1110",
75 "photo_1850_1110",
76 "difficult_photo_1850_1110",
77 };
78 };
79
80 frame_gen_cap_impl::AutoOpt<SquaresVideo> squares_video;
81 frame_gen_cap_impl::AutoOpt<SquareSlides> squares_slides;
82 frame_gen_cap_impl::AutoOpt<VideoFile> video_file;
83 frame_gen_cap_impl::AutoOpt<ImageSlides> image_slides;
84};
pbos@webrtc.org26d12102013-05-29 13:41:03 +000085
Sebastian Janssonf1f363f2018-08-13 14:24:58 +020086class FrameGeneratorCapturer : public TestVideoCapturer {
pbos@webrtc.org26d12102013-05-29 13:41:03 +000087 public:
perkj803d97f2016-11-01 11:45:46 -070088 class SinkWantsObserver {
89 public:
90 // OnSinkWantsChanged is called when FrameGeneratorCapturer::AddOrUpdateSink
91 // is called.
92 virtual void OnSinkWantsChanged(rtc::VideoSinkInterface<VideoFrame>* sink,
93 const rtc::VideoSinkWants& wants) = 0;
94
95 protected:
96 virtual ~SinkWantsObserver() {}
97 };
98
Danil Chapovalov59b64d32019-04-23 13:45:12 +020099 FrameGeneratorCapturer(Clock* clock,
100 std::unique_ptr<FrameGenerator> frame_generator,
101 int target_fps,
102 TaskQueueFactory& task_queue_factory);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000103 virtual ~FrameGeneratorCapturer();
104
Sebastian Jansson53571c72019-07-31 17:30:03 +0200105 static std::unique_ptr<FrameGeneratorCapturer> Create(
106 Clock* clock,
107 TaskQueueFactory& task_queue_factory,
108 FrameGeneratorCapturerConfig::SquaresVideo config);
109 static std::unique_ptr<FrameGeneratorCapturer> Create(
110 Clock* clock,
111 TaskQueueFactory& task_queue_factory,
112 FrameGeneratorCapturerConfig::SquareSlides config);
113 static std::unique_ptr<FrameGeneratorCapturer> Create(
114 Clock* clock,
115 TaskQueueFactory& task_queue_factory,
116 FrameGeneratorCapturerConfig::VideoFile config);
117 static std::unique_ptr<FrameGeneratorCapturer> Create(
118 Clock* clock,
119 TaskQueueFactory& task_queue_factory,
120 FrameGeneratorCapturerConfig::ImageSlides config);
121 static std::unique_ptr<FrameGeneratorCapturer> Create(
122 Clock* clock,
123 TaskQueueFactory& task_queue_factory,
124 const FrameGeneratorCapturerConfig& config);
125
Niels Möller8eeccbe2018-12-14 13:35:32 +0100126 void Start();
127 void Stop();
perkjfa10b552016-10-02 23:45:26 -0700128 void ChangeResolution(size_t width, size_t height);
Sebastian Janssonba3decf2018-08-30 11:19:23 +0200129 void ChangeFramerate(int target_framerate);
perkja49cbd32016-09-16 07:53:41 -0700130
perkj803d97f2016-11-01 11:45:46 -0700131 void SetSinkWantsObserver(SinkWantsObserver* observer);
132
perkja49cbd32016-09-16 07:53:41 -0700133 void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
134 const rtc::VideoSinkWants& wants) override;
135 void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override;
136
sprang867fb522015-08-03 04:38:41 -0700137 void ForceFrame();
Perba7dc722016-04-19 15:01:23 +0200138 void SetFakeRotation(VideoRotation rotation);
Johannes Kronf7f13e02018-12-12 11:17:43 +0100139 void SetFakeColorSpace(absl::optional<ColorSpace> color_space);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000140
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000141 int64_t first_frame_capture_time() const { return first_frame_capture_time_; }
142
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000143 bool Init();
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000144
145 private:
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000146 void InsertFrame();
147 static bool Run(void* obj);
sprangc5d62e22017-04-02 23:53:04 -0700148 int GetCurrentConfiguredFramerate();
Niels Möller3793bb42018-12-20 13:46:06 +0100149 void UpdateFps(int max_fps) RTC_EXCLUSIVE_LOCKS_REQUIRED(&lock_);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000150
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000151 Clock* const clock_;
Sebastian Jansson53571c72019-07-31 17:30:03 +0200152 RepeatingTaskHandle frame_task_;
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000153 bool sending_;
danilchapa37de392017-09-09 04:17:22 -0700154 SinkWantsObserver* sink_wants_observer_ RTC_GUARDED_BY(&lock_);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000155
Peter Boströmf2f82832015-05-01 13:00:41 +0200156 rtc::CriticalSection lock_;
kwibergbfefb032016-05-01 14:53:46 -0700157 std::unique_ptr<FrameGenerator> frame_generator_;
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000158
Sebastian Janssonba3decf2018-08-30 11:19:23 +0200159 int source_fps_ RTC_GUARDED_BY(&lock_);
160 int target_capture_fps_ RTC_GUARDED_BY(&lock_);
Danil Chapovalov431abd92018-06-18 12:54:17 +0200161 absl::optional<int> wanted_fps_ RTC_GUARDED_BY(&lock_);
Perba7dc722016-04-19 15:01:23 +0200162 VideoRotation fake_rotation_ = kVideoRotation_0;
Johannes Kronf7f13e02018-12-12 11:17:43 +0100163 absl::optional<ColorSpace> fake_color_space_ RTC_GUARDED_BY(&lock_);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000164
165 int64_t first_frame_capture_time_;
ilnikbaded152017-03-17 05:55:25 -0700166 // Must be the last field, so it will be deconstructed first as tasks
167 // in the TaskQueue access other fields of the instance of this class.
168 rtc::TaskQueue task_queue_;
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000169};
ilnikbaded152017-03-17 05:55:25 -0700170} // namespace test
171} // namespace webrtc
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000172
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200173#endif // TEST_FRAME_GENERATOR_CAPTURER_H_