blob: 36249d672d6d1f901ad3bb5369f07b3041d08579 [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 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "test/frame_generator_capturer.h"
pbos@webrtc.org26d12102013-05-29 13:41:03 +000012
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <algorithm>
14#include <cmath>
15#include <limits>
Mirko Bonadei317a1f02019-09-17 17:06:18 +020016#include <memory>
ilnikbaded152017-03-17 05:55:25 -070017#include <utility>
18#include <vector>
19
Artem Titov33f9d2b2019-12-05 15:59:00 +010020#include "api/test/create_frame_generator.h"
Yves Gerey3e707812018-11-28 16:47:49 +010021#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "rtc_base/critical_section.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "rtc_base/logging.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/task_queue.h"
Steve Anton10542f22019-01-11 09:11:00 -080025#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "system_wrappers/include/clock.h"
Sebastian Jansson53571c72019-07-31 17:30:03 +020027#include "test/testsupport/file_utils.h"
pbos@webrtc.org26d12102013-05-29 13:41:03 +000028
29namespace webrtc {
30namespace test {
Sebastian Jansson53571c72019-07-31 17:30:03 +020031namespace {
32std::string TransformFilePath(std::string path) {
33 static const std::string resource_prefix = "res://";
34 int ext_pos = path.rfind(".");
35 if (ext_pos < 0) {
36 return test::ResourcePath(path, "yuv");
37 } else if (path.find(resource_prefix) == 0) {
38 std::string name = path.substr(resource_prefix.length(), ext_pos);
39 std::string ext = path.substr(ext_pos, path.size());
40 return test::ResourcePath(name, ext);
41 }
42 return path;
43}
44} // namespace
pbos@webrtc.org26d12102013-05-29 13:41:03 +000045
Sebastian Janssonfb14c5d2019-02-28 13:30:04 +010046FrameGeneratorCapturer::FrameGeneratorCapturer(
47 Clock* clock,
Artem Titov503d7232019-12-04 12:37:13 +010048 std::unique_ptr<FrameGeneratorInterface> frame_generator,
Sebastian Janssonfb14c5d2019-02-28 13:30:04 +010049 int target_fps,
50 TaskQueueFactory& task_queue_factory)
perkja49cbd32016-09-16 07:53:41 -070051 : clock_(clock),
Niels Möller8eeccbe2018-12-14 13:35:32 +010052 sending_(true),
perkj803d97f2016-11-01 11:45:46 -070053 sink_wants_observer_(nullptr),
perkja8ba1952017-02-27 06:52:10 -080054 frame_generator_(std::move(frame_generator)),
Sebastian Janssonba3decf2018-08-30 11:19:23 +020055 source_fps_(target_fps),
56 target_capture_fps_(target_fps),
ilnikbaded152017-03-17 05:55:25 -070057 first_frame_capture_time_(-1),
Sebastian Janssonfb14c5d2019-02-28 13:30:04 +010058 task_queue_(task_queue_factory.CreateTaskQueue(
59 "FrameGenCapQ",
60 TaskQueueFactory::Priority::HIGH)) {
perkja8ba1952017-02-27 06:52:10 -080061 RTC_DCHECK(frame_generator_);
perkja49cbd32016-09-16 07:53:41 -070062 RTC_DCHECK_GT(target_fps, 0);
pbos@webrtc.org26d12102013-05-29 13:41:03 +000063}
64
65FrameGeneratorCapturer::~FrameGeneratorCapturer() {
66 Stop();
pbos@webrtc.org26d12102013-05-29 13:41:03 +000067}
68
Sebastian Jansson53571c72019-07-31 17:30:03 +020069std::unique_ptr<FrameGeneratorCapturer> FrameGeneratorCapturer::Create(
70 Clock* clock,
71 TaskQueueFactory& task_queue_factory,
72 FrameGeneratorCapturerConfig::SquaresVideo config) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +020073 return std::make_unique<FrameGeneratorCapturer>(
Sebastian Jansson53571c72019-07-31 17:30:03 +020074 clock,
Artem Titov33f9d2b2019-12-05 15:59:00 +010075 CreateSquareFrameGenerator(config.width, config.height,
76 config.pixel_format, config.num_squares),
Sebastian Jansson53571c72019-07-31 17:30:03 +020077 config.framerate, task_queue_factory);
78}
79std::unique_ptr<FrameGeneratorCapturer> FrameGeneratorCapturer::Create(
80 Clock* clock,
81 TaskQueueFactory& task_queue_factory,
82 FrameGeneratorCapturerConfig::SquareSlides config) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +020083 return std::make_unique<FrameGeneratorCapturer>(
Sebastian Jansson53571c72019-07-31 17:30:03 +020084 clock,
Artem Titov33f9d2b2019-12-05 15:59:00 +010085 CreateSlideFrameGenerator(
Sebastian Jansson53571c72019-07-31 17:30:03 +020086 config.width, config.height,
87 /*frame_repeat_count*/ config.change_interval.seconds<double>() *
88 config.framerate),
89 config.framerate, task_queue_factory);
90}
91std::unique_ptr<FrameGeneratorCapturer> FrameGeneratorCapturer::Create(
92 Clock* clock,
93 TaskQueueFactory& task_queue_factory,
94 FrameGeneratorCapturerConfig::VideoFile config) {
95 RTC_CHECK(config.width && config.height);
Mirko Bonadei317a1f02019-09-17 17:06:18 +020096 return std::make_unique<FrameGeneratorCapturer>(
Sebastian Jansson53571c72019-07-31 17:30:03 +020097 clock,
Artem Titov33f9d2b2019-12-05 15:59:00 +010098 CreateFromYuvFileFrameGenerator({TransformFilePath(config.name)},
99 config.width, config.height,
100 /*frame_repeat_count*/ 1),
Sebastian Jansson53571c72019-07-31 17:30:03 +0200101 config.framerate, task_queue_factory);
102}
103
104std::unique_ptr<FrameGeneratorCapturer> FrameGeneratorCapturer::Create(
105 Clock* clock,
106 TaskQueueFactory& task_queue_factory,
107 FrameGeneratorCapturerConfig::ImageSlides config) {
Artem Titov33f9d2b2019-12-05 15:59:00 +0100108 std::unique_ptr<FrameGeneratorInterface> slides_generator;
Sebastian Jansson53571c72019-07-31 17:30:03 +0200109 std::vector<std::string> paths = config.paths;
110 for (std::string& path : paths)
111 path = TransformFilePath(path);
112
113 if (config.crop.width || config.crop.height) {
114 TimeDelta pause_duration =
115 config.change_interval - config.crop.scroll_duration;
116 RTC_CHECK_GE(pause_duration, TimeDelta::Zero());
117 int crop_width = config.crop.width.value_or(config.width);
118 int crop_height = config.crop.height.value_or(config.height);
119 RTC_CHECK_LE(crop_width, config.width);
120 RTC_CHECK_LE(crop_height, config.height);
Artem Titov33f9d2b2019-12-05 15:59:00 +0100121 slides_generator = CreateScrollingInputFromYuvFilesFrameGenerator(
Sebastian Jansson53571c72019-07-31 17:30:03 +0200122 clock, paths, config.width, config.height, crop_width, crop_height,
123 config.crop.scroll_duration.ms(), pause_duration.ms());
124 } else {
Artem Titov33f9d2b2019-12-05 15:59:00 +0100125 slides_generator = CreateFromYuvFileFrameGenerator(
Sebastian Jansson53571c72019-07-31 17:30:03 +0200126 paths, config.width, config.height,
127 /*frame_repeat_count*/ config.change_interval.seconds<double>() *
128 config.framerate);
129 }
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200130 return std::make_unique<FrameGeneratorCapturer>(
Sebastian Jansson53571c72019-07-31 17:30:03 +0200131 clock, std::move(slides_generator), config.framerate, task_queue_factory);
132}
133
134std::unique_ptr<FrameGeneratorCapturer> FrameGeneratorCapturer::Create(
135 Clock* clock,
136 TaskQueueFactory& task_queue_factory,
137 const FrameGeneratorCapturerConfig& config) {
138 if (config.video_file) {
139 return Create(clock, task_queue_factory, *config.video_file);
140 } else if (config.image_slides) {
141 return Create(clock, task_queue_factory, *config.image_slides);
142 } else if (config.squares_slides) {
143 return Create(clock, task_queue_factory, *config.squares_slides);
144 } else {
145 return Create(clock, task_queue_factory,
146 config.squares_video.value_or(
147 FrameGeneratorCapturerConfig::SquaresVideo()));
148 }
149}
150
Perba7dc722016-04-19 15:01:23 +0200151void FrameGeneratorCapturer::SetFakeRotation(VideoRotation rotation) {
152 rtc::CritScope cs(&lock_);
153 fake_rotation_ = rotation;
154}
155
Johannes Kronf7f13e02018-12-12 11:17:43 +0100156void FrameGeneratorCapturer::SetFakeColorSpace(
157 absl::optional<ColorSpace> color_space) {
158 rtc::CritScope cs(&lock_);
159 fake_color_space_ = color_space;
160}
161
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000162bool FrameGeneratorCapturer::Init() {
pbos@webrtc.org94015242013-10-16 11:05:37 +0000163 // This check is added because frame_generator_ might be file based and should
164 // not crash because a file moved.
sprangc5d62e22017-04-02 23:53:04 -0700165 if (frame_generator_.get() == nullptr)
pbos@webrtc.org94015242013-10-16 11:05:37 +0000166 return false;
167
Sebastian Jansson53571c72019-07-31 17:30:03 +0200168 frame_task_ = RepeatingTaskHandle::DelayedStart(
Danil Chapovalov4423c362019-03-06 18:41:39 +0100169 task_queue_.Get(),
170 TimeDelta::seconds(1) / GetCurrentConfiguredFramerate(), [this] {
Sebastian Janssonecb68972019-01-18 10:30:54 +0100171 InsertFrame();
172 return TimeDelta::seconds(1) / GetCurrentConfiguredFramerate();
173 });
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000174 return true;
175}
176
177void FrameGeneratorCapturer::InsertFrame() {
sprangc5d62e22017-04-02 23:53:04 -0700178 rtc::CritScope cs(&lock_);
179 if (sending_) {
Artem Titov33f9d2b2019-12-05 15:59:00 +0100180 FrameGeneratorInterface::VideoFrameData frame_data =
181 frame_generator_->NextFrame();
Sebastian Janssonba3decf2018-08-30 11:19:23 +0200182 // TODO(srte): Use more advanced frame rate control to allow arbritrary
183 // fractions.
184 int decimation =
185 std::round(static_cast<double>(source_fps_) / target_capture_fps_);
186 for (int i = 1; i < decimation; ++i)
Artem Titov5256d8b2019-12-02 10:34:12 +0100187 frame_data = frame_generator_->NextFrame();
188
189 VideoFrame frame = VideoFrame::Builder()
190 .set_video_frame_buffer(frame_data.buffer)
191 .set_rotation(fake_rotation_)
192 .set_timestamp_us(clock_->TimeInMicroseconds())
193 .set_ntp_time_ms(clock_->CurrentNtpInMilliseconds())
194 .set_update_rect(frame_data.update_rect)
195 .set_color_space(fake_color_space_)
196 .build();
sprangc5d62e22017-04-02 23:53:04 -0700197 if (first_frame_capture_time_ == -1) {
Artem Titov5256d8b2019-12-02 10:34:12 +0100198 first_frame_capture_time_ = frame.ntp_time_ms();
sprangc5d62e22017-04-02 23:53:04 -0700199 }
200
Artem Titov5256d8b2019-12-02 10:34:12 +0100201 TestVideoCapturer::OnFrame(frame);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000202 }
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000203}
204
205void FrameGeneratorCapturer::Start() {
Sebastian Jansson53571c72019-07-31 17:30:03 +0200206 {
207 rtc::CritScope cs(&lock_);
208 sending_ = true;
209 }
210 if (!frame_task_.Running()) {
211 frame_task_ = RepeatingTaskHandle::Start(task_queue_.Get(), [this] {
212 InsertFrame();
213 return TimeDelta::seconds(1) / GetCurrentConfiguredFramerate();
214 });
215 }
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000216}
217
218void FrameGeneratorCapturer::Stop() {
Peter Boströmf2f82832015-05-01 13:00:41 +0200219 rtc::CritScope cs(&lock_);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000220 sending_ = false;
221}
sprang867fb522015-08-03 04:38:41 -0700222
perkjfa10b552016-10-02 23:45:26 -0700223void FrameGeneratorCapturer::ChangeResolution(size_t width, size_t height) {
224 rtc::CritScope cs(&lock_);
225 frame_generator_->ChangeResolution(width, height);
226}
227
Sebastian Janssonba3decf2018-08-30 11:19:23 +0200228void FrameGeneratorCapturer::ChangeFramerate(int target_framerate) {
229 rtc::CritScope cs(&lock_);
230 RTC_CHECK(target_capture_fps_ > 0);
231 if (target_framerate > source_fps_)
232 RTC_LOG(LS_WARNING) << "Target framerate clamped from " << target_framerate
233 << " to " << source_fps_;
234 if (source_fps_ % target_capture_fps_ != 0) {
235 int decimation =
236 std::round(static_cast<double>(source_fps_) / target_capture_fps_);
237 int effective_rate = target_capture_fps_ / decimation;
238 RTC_LOG(LS_WARNING) << "Target framerate, " << target_framerate
239 << ", is an uneven fraction of the source rate, "
240 << source_fps_
241 << ". The framerate will be :" << effective_rate;
242 }
243 target_capture_fps_ = std::min(source_fps_, target_framerate);
244}
245
perkj803d97f2016-11-01 11:45:46 -0700246void FrameGeneratorCapturer::SetSinkWantsObserver(SinkWantsObserver* observer) {
247 rtc::CritScope cs(&lock_);
248 RTC_DCHECK(!sink_wants_observer_);
249 sink_wants_observer_ = observer;
250}
251
perkja49cbd32016-09-16 07:53:41 -0700252void FrameGeneratorCapturer::AddOrUpdateSink(
253 rtc::VideoSinkInterface<VideoFrame>* sink,
254 const rtc::VideoSinkWants& wants) {
Niels Möller3793bb42018-12-20 13:46:06 +0100255 TestVideoCapturer::AddOrUpdateSink(sink, wants);
perkja49cbd32016-09-16 07:53:41 -0700256 rtc::CritScope cs(&lock_);
Niels Möller3793bb42018-12-20 13:46:06 +0100257 if (sink_wants_observer_) {
258 // Tests need to observe unmodified sink wants.
perkj803d97f2016-11-01 11:45:46 -0700259 sink_wants_observer_->OnSinkWantsChanged(sink, wants);
sprangc5d62e22017-04-02 23:53:04 -0700260 }
Niels Möller3793bb42018-12-20 13:46:06 +0100261 UpdateFps(GetSinkWants().max_framerate_fps);
perkja49cbd32016-09-16 07:53:41 -0700262}
263
264void FrameGeneratorCapturer::RemoveSink(
265 rtc::VideoSinkInterface<VideoFrame>* sink) {
Niels Möller3793bb42018-12-20 13:46:06 +0100266 TestVideoCapturer::RemoveSink(sink);
267
perkja49cbd32016-09-16 07:53:41 -0700268 rtc::CritScope cs(&lock_);
Niels Möller3793bb42018-12-20 13:46:06 +0100269 UpdateFps(GetSinkWants().max_framerate_fps);
270}
271
272void FrameGeneratorCapturer::UpdateFps(int max_fps) {
273 if (max_fps < target_capture_fps_) {
274 wanted_fps_.emplace(max_fps);
275 } else {
276 wanted_fps_.reset();
277 }
perkja49cbd32016-09-16 07:53:41 -0700278}
279
sprang867fb522015-08-03 04:38:41 -0700280void FrameGeneratorCapturer::ForceFrame() {
ilnikbaded152017-03-17 05:55:25 -0700281 // One-time non-repeating task,
Danil Chapovalovabd42732018-09-10 14:07:45 +0200282 task_queue_.PostTask([this] { InsertFrame(); });
sprang867fb522015-08-03 04:38:41 -0700283}
ilnikbaded152017-03-17 05:55:25 -0700284
sprangc5d62e22017-04-02 23:53:04 -0700285int FrameGeneratorCapturer::GetCurrentConfiguredFramerate() {
286 rtc::CritScope cs(&lock_);
Sebastian Janssonba3decf2018-08-30 11:19:23 +0200287 if (wanted_fps_ && *wanted_fps_ < target_capture_fps_)
sprangc5d62e22017-04-02 23:53:04 -0700288 return *wanted_fps_;
Sebastian Janssonba3decf2018-08-30 11:19:23 +0200289 return target_capture_fps_;
sprangc5d62e22017-04-02 23:53:04 -0700290}
291
ilnikbaded152017-03-17 05:55:25 -0700292} // namespace test
293} // namespace webrtc