blob: 858f895f647ffb698cd298afbb0bd197aada443e [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>
ilnikbaded152017-03-17 05:55:25 -070016#include <utility>
17#include <vector>
18
Danil Chapovalovabd42732018-09-10 14:07:45 +020019#include "absl/memory/memory.h"
Yves Gerey3e707812018-11-28 16:47:49 +010020#include "rtc_base/checks.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/criticalsection.h"
22#include "rtc_base/logging.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "rtc_base/task_queue.h"
24#include "rtc_base/timeutils.h"
25#include "system_wrappers/include/clock.h"
pbos@webrtc.org26d12102013-05-29 13:41:03 +000026
27namespace webrtc {
28namespace test {
29
ilnikbaded152017-03-17 05:55:25 -070030class FrameGeneratorCapturer::InsertFrameTask : public rtc::QueuedTask {
31 public:
Danil Chapovalovabd42732018-09-10 14:07:45 +020032 explicit InsertFrameTask(FrameGeneratorCapturer* frame_generator_capturer)
ilnikbaded152017-03-17 05:55:25 -070033 : frame_generator_capturer_(frame_generator_capturer),
Danil Chapovalovabd42732018-09-10 14:07:45 +020034 repeat_interval_ms_(-1),
35 next_run_time_ms_(-1) {}
ilnikbaded152017-03-17 05:55:25 -070036
37 private:
38 bool Run() override {
Danil Chapovalovabd42732018-09-10 14:07:45 +020039 // Check if the frame interval for this
40 // task queue is the same same as the current configured frame rate.
41 int interval_ms =
42 1000 / frame_generator_capturer_->GetCurrentConfiguredFramerate();
43 if (repeat_interval_ms_ != interval_ms) {
44 // Restart the timer if frame rate has changed since task was started.
45 next_run_time_ms_ = rtc::TimeMillis();
46 repeat_interval_ms_ = interval_ms;
ilnikbaded152017-03-17 05:55:25 -070047 }
Danil Chapovalovabd42732018-09-10 14:07:45 +020048 // Schedule the next frame capture event to happen at approximately the
49 // correct absolute time point.
50 next_run_time_ms_ += interval_ms;
51
ilnikbaded152017-03-17 05:55:25 -070052 frame_generator_capturer_->InsertFrame();
Danil Chapovalovabd42732018-09-10 14:07:45 +020053
54 int64_t now_ms = rtc::TimeMillis();
55 if (next_run_time_ms_ < now_ms) {
Danil Chapovalovabd42732018-09-10 14:07:45 +020056 RTC_LOG(LS_ERROR) << "Frame Generator Capturer can't keep up with "
Danil Chapovalovafc3eb12018-09-12 12:53:10 +020057 "requested fps.";
58 rtc::TaskQueue::Current()->PostTask(absl::WrapUnique(this));
59 } else {
60 int64_t delay_ms = next_run_time_ms_ - now_ms;
61 RTC_DCHECK_GE(delay_ms, 0);
62 RTC_DCHECK_LE(delay_ms, interval_ms);
63 rtc::TaskQueue::Current()->PostDelayedTask(absl::WrapUnique(this),
64 delay_ms);
Danil Chapovalovabd42732018-09-10 14:07:45 +020065 }
Danil Chapovalovabd42732018-09-10 14:07:45 +020066 return false;
ilnikbaded152017-03-17 05:55:25 -070067 }
68
69 webrtc::test::FrameGeneratorCapturer* const frame_generator_capturer_;
Danil Chapovalovabd42732018-09-10 14:07:45 +020070 int repeat_interval_ms_;
71 int64_t next_run_time_ms_;
ilnikbaded152017-03-17 05:55:25 -070072};
73
Emircan Uysaler03e6ec92018-03-09 15:03:26 -080074FrameGeneratorCapturer* FrameGeneratorCapturer::Create(
75 int width,
76 int height,
Danil Chapovalov431abd92018-06-18 12:54:17 +020077 absl::optional<FrameGenerator::OutputType> type,
78 absl::optional<int> num_squares,
Emircan Uysaler03e6ec92018-03-09 15:03:26 -080079 int target_fps,
80 Clock* clock) {
Danil Chapovalovabd42732018-09-10 14:07:45 +020081 auto capturer = absl::make_unique<FrameGeneratorCapturer>(
Emircan Uysaler03e6ec92018-03-09 15:03:26 -080082 clock,
83 FrameGenerator::CreateSquareGenerator(width, height, type, num_squares),
Danil Chapovalovabd42732018-09-10 14:07:45 +020084 target_fps);
Taylor Brandstetter081136f2018-03-08 01:54:10 +000085 if (!capturer->Init())
86 return nullptr;
87
88 return capturer.release();
89}
90
andresp@webrtc.orgab654952013-09-19 12:14:03 +000091FrameGeneratorCapturer* FrameGeneratorCapturer::CreateFromYuvFile(
sprang@webrtc.org131bea82015-02-18 12:46:06 +000092 const std::string& file_name,
andresp@webrtc.orgab654952013-09-19 12:14:03 +000093 size_t width,
94 size_t height,
95 int target_fps,
96 Clock* clock) {
Danil Chapovalovabd42732018-09-10 14:07:45 +020097 auto capturer = absl::make_unique<FrameGeneratorCapturer>(
sprangc5d62e22017-04-02 23:53:04 -070098 clock,
99 FrameGenerator::CreateFromYuvFile(std::vector<std::string>(1, file_name),
100 width, height, 1),
Danil Chapovalovabd42732018-09-10 14:07:45 +0200101 target_fps);
sprangc5d62e22017-04-02 23:53:04 -0700102 if (!capturer->Init())
103 return nullptr;
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000104
sprangc5d62e22017-04-02 23:53:04 -0700105 return capturer.release();
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000106}
107
erikvarga579de6f2017-08-29 09:12:57 -0700108FrameGeneratorCapturer* FrameGeneratorCapturer::CreateSlideGenerator(
109 int width,
110 int height,
111 int frame_repeat_count,
112 int target_fps,
113 Clock* clock) {
Danil Chapovalovabd42732018-09-10 14:07:45 +0200114 auto capturer = absl::make_unique<FrameGeneratorCapturer>(
Yves Gerey665174f2018-06-19 15:03:05 +0200115 clock,
116 FrameGenerator::CreateSlideGenerator(width, height, frame_repeat_count),
Danil Chapovalovabd42732018-09-10 14:07:45 +0200117 target_fps);
erikvarga579de6f2017-08-29 09:12:57 -0700118 if (!capturer->Init())
119 return nullptr;
120
121 return capturer.release();
122}
123
perkja8ba1952017-02-27 06:52:10 -0800124FrameGeneratorCapturer::FrameGeneratorCapturer(
125 Clock* clock,
126 std::unique_ptr<FrameGenerator> frame_generator,
127 int target_fps)
perkja49cbd32016-09-16 07:53:41 -0700128 : clock_(clock),
Niels Möller8eeccbe2018-12-14 13:35:32 +0100129 sending_(true),
perkja49cbd32016-09-16 07:53:41 -0700130 sink_(nullptr),
perkj803d97f2016-11-01 11:45:46 -0700131 sink_wants_observer_(nullptr),
perkja8ba1952017-02-27 06:52:10 -0800132 frame_generator_(std::move(frame_generator)),
Sebastian Janssonba3decf2018-08-30 11:19:23 +0200133 source_fps_(target_fps),
134 target_capture_fps_(target_fps),
ilnikbaded152017-03-17 05:55:25 -0700135 first_frame_capture_time_(-1),
Yves Gerey665174f2018-06-19 15:03:05 +0200136 task_queue_("FrameGenCapQ", rtc::TaskQueue::Priority::HIGH) {
perkja8ba1952017-02-27 06:52:10 -0800137 RTC_DCHECK(frame_generator_);
perkja49cbd32016-09-16 07:53:41 -0700138 RTC_DCHECK_GT(target_fps, 0);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000139}
140
141FrameGeneratorCapturer::~FrameGeneratorCapturer() {
142 Stop();
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000143}
144
Perba7dc722016-04-19 15:01:23 +0200145void FrameGeneratorCapturer::SetFakeRotation(VideoRotation rotation) {
146 rtc::CritScope cs(&lock_);
147 fake_rotation_ = rotation;
148}
149
Johannes Kronf7f13e02018-12-12 11:17:43 +0100150void FrameGeneratorCapturer::SetFakeColorSpace(
151 absl::optional<ColorSpace> color_space) {
152 rtc::CritScope cs(&lock_);
153 fake_color_space_ = color_space;
154}
155
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000156bool FrameGeneratorCapturer::Init() {
pbos@webrtc.org94015242013-10-16 11:05:37 +0000157 // This check is added because frame_generator_ might be file based and should
158 // not crash because a file moved.
sprangc5d62e22017-04-02 23:53:04 -0700159 if (frame_generator_.get() == nullptr)
pbos@webrtc.org94015242013-10-16 11:05:37 +0000160 return false;
161
sprangc5d62e22017-04-02 23:53:04 -0700162 int framerate_fps = GetCurrentConfiguredFramerate();
Danil Chapovalovabd42732018-09-10 14:07:45 +0200163 task_queue_.PostDelayedTask(absl::make_unique<InsertFrameTask>(this),
164 1000 / framerate_fps);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000165
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000166 return true;
167}
168
169void FrameGeneratorCapturer::InsertFrame() {
sprangc5d62e22017-04-02 23:53:04 -0700170 rtc::CritScope cs(&lock_);
171 if (sending_) {
172 VideoFrame* frame = frame_generator_->NextFrame();
Sebastian Janssonba3decf2018-08-30 11:19:23 +0200173 // TODO(srte): Use more advanced frame rate control to allow arbritrary
174 // fractions.
175 int decimation =
176 std::round(static_cast<double>(source_fps_) / target_capture_fps_);
177 for (int i = 1; i < decimation; ++i)
178 frame = frame_generator_->NextFrame();
ilnik04f4d122017-06-19 07:18:55 -0700179 frame->set_timestamp_us(clock_->TimeInMicroseconds());
sprangc5d62e22017-04-02 23:53:04 -0700180 frame->set_ntp_time_ms(clock_->CurrentNtpInMilliseconds());
181 frame->set_rotation(fake_rotation_);
Johannes Kronf7f13e02018-12-12 11:17:43 +0100182 if (fake_color_space_) {
183 frame->set_color_space(&fake_color_space_.value());
184 }
sprangc5d62e22017-04-02 23:53:04 -0700185 if (first_frame_capture_time_ == -1) {
186 first_frame_capture_time_ = frame->ntp_time_ms();
187 }
188
189 if (sink_) {
Danil Chapovalov431abd92018-06-18 12:54:17 +0200190 absl::optional<VideoFrame> out_frame = AdaptFrame(*frame);
sprangc5d62e22017-04-02 23:53:04 -0700191 if (out_frame)
192 sink_->OnFrame(*out_frame);
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000193 }
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000194 }
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000195}
196
197void FrameGeneratorCapturer::Start() {
Peter Boströmf2f82832015-05-01 13:00:41 +0200198 rtc::CritScope cs(&lock_);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000199 sending_ = true;
200}
201
202void FrameGeneratorCapturer::Stop() {
Peter Boströmf2f82832015-05-01 13:00:41 +0200203 rtc::CritScope cs(&lock_);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000204 sending_ = false;
205}
sprang867fb522015-08-03 04:38:41 -0700206
perkjfa10b552016-10-02 23:45:26 -0700207void FrameGeneratorCapturer::ChangeResolution(size_t width, size_t height) {
208 rtc::CritScope cs(&lock_);
209 frame_generator_->ChangeResolution(width, height);
210}
211
Sebastian Janssonba3decf2018-08-30 11:19:23 +0200212void FrameGeneratorCapturer::ChangeFramerate(int target_framerate) {
213 rtc::CritScope cs(&lock_);
214 RTC_CHECK(target_capture_fps_ > 0);
215 if (target_framerate > source_fps_)
216 RTC_LOG(LS_WARNING) << "Target framerate clamped from " << target_framerate
217 << " to " << source_fps_;
218 if (source_fps_ % target_capture_fps_ != 0) {
219 int decimation =
220 std::round(static_cast<double>(source_fps_) / target_capture_fps_);
221 int effective_rate = target_capture_fps_ / decimation;
222 RTC_LOG(LS_WARNING) << "Target framerate, " << target_framerate
223 << ", is an uneven fraction of the source rate, "
224 << source_fps_
225 << ". The framerate will be :" << effective_rate;
226 }
227 target_capture_fps_ = std::min(source_fps_, target_framerate);
228}
229
perkj803d97f2016-11-01 11:45:46 -0700230void FrameGeneratorCapturer::SetSinkWantsObserver(SinkWantsObserver* observer) {
231 rtc::CritScope cs(&lock_);
232 RTC_DCHECK(!sink_wants_observer_);
233 sink_wants_observer_ = observer;
234}
235
perkja49cbd32016-09-16 07:53:41 -0700236void FrameGeneratorCapturer::AddOrUpdateSink(
237 rtc::VideoSinkInterface<VideoFrame>* sink,
238 const rtc::VideoSinkWants& wants) {
239 rtc::CritScope cs(&lock_);
perkj803d97f2016-11-01 11:45:46 -0700240 RTC_CHECK(!sink_ || sink_ == sink);
perkja49cbd32016-09-16 07:53:41 -0700241 sink_ = sink;
perkj803d97f2016-11-01 11:45:46 -0700242 if (sink_wants_observer_)
243 sink_wants_observer_->OnSinkWantsChanged(sink, wants);
sprangc5d62e22017-04-02 23:53:04 -0700244
245 // Handle framerate within this class, just pass on resolution for possible
246 // adaptation.
247 rtc::VideoSinkWants resolution_wants = wants;
248 resolution_wants.max_framerate_fps = std::numeric_limits<int>::max();
Sebastian Janssonf1f363f2018-08-13 14:24:58 +0200249 TestVideoCapturer::AddOrUpdateSink(sink, resolution_wants);
sprangc5d62e22017-04-02 23:53:04 -0700250
251 // Ignore any requests for framerate higher than initially configured.
Sebastian Janssonba3decf2018-08-30 11:19:23 +0200252 if (wants.max_framerate_fps < target_capture_fps_) {
sprangc5d62e22017-04-02 23:53:04 -0700253 wanted_fps_.emplace(wants.max_framerate_fps);
254 } else {
255 wanted_fps_.reset();
256 }
perkja49cbd32016-09-16 07:53:41 -0700257}
258
259void FrameGeneratorCapturer::RemoveSink(
260 rtc::VideoSinkInterface<VideoFrame>* sink) {
261 rtc::CritScope cs(&lock_);
262 RTC_CHECK(sink_ == sink);
263 sink_ = nullptr;
264}
265
sprang867fb522015-08-03 04:38:41 -0700266void FrameGeneratorCapturer::ForceFrame() {
ilnikbaded152017-03-17 05:55:25 -0700267 // One-time non-repeating task,
Danil Chapovalovabd42732018-09-10 14:07:45 +0200268 task_queue_.PostTask([this] { InsertFrame(); });
sprang867fb522015-08-03 04:38:41 -0700269}
ilnikbaded152017-03-17 05:55:25 -0700270
sprangc5d62e22017-04-02 23:53:04 -0700271int FrameGeneratorCapturer::GetCurrentConfiguredFramerate() {
272 rtc::CritScope cs(&lock_);
Sebastian Janssonba3decf2018-08-30 11:19:23 +0200273 if (wanted_fps_ && *wanted_fps_ < target_capture_fps_)
sprangc5d62e22017-04-02 23:53:04 -0700274 return *wanted_fps_;
Sebastian Janssonba3decf2018-08-30 11:19:23 +0200275 return target_capture_fps_;
sprangc5d62e22017-04-02 23:53:04 -0700276}
277
ilnikbaded152017-03-17 05:55:25 -0700278} // namespace test
279} // namespace webrtc