blob: 872918727d41f6b36674d244c439bd1c2df78f20 [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),
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000129 sending_(false),
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
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000150bool FrameGeneratorCapturer::Init() {
pbos@webrtc.org94015242013-10-16 11:05:37 +0000151 // This check is added because frame_generator_ might be file based and should
152 // not crash because a file moved.
sprangc5d62e22017-04-02 23:53:04 -0700153 if (frame_generator_.get() == nullptr)
pbos@webrtc.org94015242013-10-16 11:05:37 +0000154 return false;
155
sprangc5d62e22017-04-02 23:53:04 -0700156 int framerate_fps = GetCurrentConfiguredFramerate();
Danil Chapovalovabd42732018-09-10 14:07:45 +0200157 task_queue_.PostDelayedTask(absl::make_unique<InsertFrameTask>(this),
158 1000 / framerate_fps);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000159
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000160 return true;
161}
162
163void FrameGeneratorCapturer::InsertFrame() {
sprangc5d62e22017-04-02 23:53:04 -0700164 rtc::CritScope cs(&lock_);
165 if (sending_) {
166 VideoFrame* frame = frame_generator_->NextFrame();
Sebastian Janssonba3decf2018-08-30 11:19:23 +0200167 // TODO(srte): Use more advanced frame rate control to allow arbritrary
168 // fractions.
169 int decimation =
170 std::round(static_cast<double>(source_fps_) / target_capture_fps_);
171 for (int i = 1; i < decimation; ++i)
172 frame = frame_generator_->NextFrame();
ilnik04f4d122017-06-19 07:18:55 -0700173 frame->set_timestamp_us(clock_->TimeInMicroseconds());
sprangc5d62e22017-04-02 23:53:04 -0700174 frame->set_ntp_time_ms(clock_->CurrentNtpInMilliseconds());
175 frame->set_rotation(fake_rotation_);
176 if (first_frame_capture_time_ == -1) {
177 first_frame_capture_time_ = frame->ntp_time_ms();
178 }
179
180 if (sink_) {
Danil Chapovalov431abd92018-06-18 12:54:17 +0200181 absl::optional<VideoFrame> out_frame = AdaptFrame(*frame);
sprangc5d62e22017-04-02 23:53:04 -0700182 if (out_frame)
183 sink_->OnFrame(*out_frame);
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000184 }
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000185 }
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000186}
187
188void FrameGeneratorCapturer::Start() {
Peter Boströmf2f82832015-05-01 13:00:41 +0200189 rtc::CritScope cs(&lock_);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000190 sending_ = true;
191}
192
193void FrameGeneratorCapturer::Stop() {
Peter Boströmf2f82832015-05-01 13:00:41 +0200194 rtc::CritScope cs(&lock_);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000195 sending_ = false;
196}
sprang867fb522015-08-03 04:38:41 -0700197
perkjfa10b552016-10-02 23:45:26 -0700198void FrameGeneratorCapturer::ChangeResolution(size_t width, size_t height) {
199 rtc::CritScope cs(&lock_);
200 frame_generator_->ChangeResolution(width, height);
201}
202
Sebastian Janssonba3decf2018-08-30 11:19:23 +0200203void FrameGeneratorCapturer::ChangeFramerate(int target_framerate) {
204 rtc::CritScope cs(&lock_);
205 RTC_CHECK(target_capture_fps_ > 0);
206 if (target_framerate > source_fps_)
207 RTC_LOG(LS_WARNING) << "Target framerate clamped from " << target_framerate
208 << " to " << source_fps_;
209 if (source_fps_ % target_capture_fps_ != 0) {
210 int decimation =
211 std::round(static_cast<double>(source_fps_) / target_capture_fps_);
212 int effective_rate = target_capture_fps_ / decimation;
213 RTC_LOG(LS_WARNING) << "Target framerate, " << target_framerate
214 << ", is an uneven fraction of the source rate, "
215 << source_fps_
216 << ". The framerate will be :" << effective_rate;
217 }
218 target_capture_fps_ = std::min(source_fps_, target_framerate);
219}
220
perkj803d97f2016-11-01 11:45:46 -0700221void FrameGeneratorCapturer::SetSinkWantsObserver(SinkWantsObserver* observer) {
222 rtc::CritScope cs(&lock_);
223 RTC_DCHECK(!sink_wants_observer_);
224 sink_wants_observer_ = observer;
225}
226
perkja49cbd32016-09-16 07:53:41 -0700227void FrameGeneratorCapturer::AddOrUpdateSink(
228 rtc::VideoSinkInterface<VideoFrame>* sink,
229 const rtc::VideoSinkWants& wants) {
230 rtc::CritScope cs(&lock_);
perkj803d97f2016-11-01 11:45:46 -0700231 RTC_CHECK(!sink_ || sink_ == sink);
perkja49cbd32016-09-16 07:53:41 -0700232 sink_ = sink;
perkj803d97f2016-11-01 11:45:46 -0700233 if (sink_wants_observer_)
234 sink_wants_observer_->OnSinkWantsChanged(sink, wants);
sprangc5d62e22017-04-02 23:53:04 -0700235
236 // Handle framerate within this class, just pass on resolution for possible
237 // adaptation.
238 rtc::VideoSinkWants resolution_wants = wants;
239 resolution_wants.max_framerate_fps = std::numeric_limits<int>::max();
Sebastian Janssonf1f363f2018-08-13 14:24:58 +0200240 TestVideoCapturer::AddOrUpdateSink(sink, resolution_wants);
sprangc5d62e22017-04-02 23:53:04 -0700241
242 // Ignore any requests for framerate higher than initially configured.
Sebastian Janssonba3decf2018-08-30 11:19:23 +0200243 if (wants.max_framerate_fps < target_capture_fps_) {
sprangc5d62e22017-04-02 23:53:04 -0700244 wanted_fps_.emplace(wants.max_framerate_fps);
245 } else {
246 wanted_fps_.reset();
247 }
perkja49cbd32016-09-16 07:53:41 -0700248}
249
250void FrameGeneratorCapturer::RemoveSink(
251 rtc::VideoSinkInterface<VideoFrame>* sink) {
252 rtc::CritScope cs(&lock_);
253 RTC_CHECK(sink_ == sink);
254 sink_ = nullptr;
255}
256
sprang867fb522015-08-03 04:38:41 -0700257void FrameGeneratorCapturer::ForceFrame() {
ilnikbaded152017-03-17 05:55:25 -0700258 // One-time non-repeating task,
Danil Chapovalovabd42732018-09-10 14:07:45 +0200259 task_queue_.PostTask([this] { InsertFrame(); });
sprang867fb522015-08-03 04:38:41 -0700260}
ilnikbaded152017-03-17 05:55:25 -0700261
sprangc5d62e22017-04-02 23:53:04 -0700262int FrameGeneratorCapturer::GetCurrentConfiguredFramerate() {
263 rtc::CritScope cs(&lock_);
Sebastian Janssonba3decf2018-08-30 11:19:23 +0200264 if (wanted_fps_ && *wanted_fps_ < target_capture_fps_)
sprangc5d62e22017-04-02 23:53:04 -0700265 return *wanted_fps_;
Sebastian Janssonba3decf2018-08-30 11:19:23 +0200266 return target_capture_fps_;
sprangc5d62e22017-04-02 23:53:04 -0700267}
268
ilnikbaded152017-03-17 05:55:25 -0700269} // namespace test
270} // namespace webrtc