blob: 2e4280f58a504000661622d67fc5fcf8616613ba [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
ilnikbaded152017-03-17 05:55:25 -070013#include <utility>
14#include <vector>
15
Danil Chapovalovabd42732018-09-10 14:07:45 +020016#include "absl/memory/memory.h"
Yves Gerey665174f2018-06-19 15:03:05 +020017#include "call/video_send_stream.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/criticalsection.h"
19#include "rtc_base/logging.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/task_queue.h"
21#include "rtc_base/timeutils.h"
22#include "system_wrappers/include/clock.h"
pbos@webrtc.org26d12102013-05-29 13:41:03 +000023
24namespace webrtc {
25namespace test {
26
ilnikbaded152017-03-17 05:55:25 -070027class FrameGeneratorCapturer::InsertFrameTask : public rtc::QueuedTask {
28 public:
Danil Chapovalovabd42732018-09-10 14:07:45 +020029 explicit InsertFrameTask(FrameGeneratorCapturer* frame_generator_capturer)
ilnikbaded152017-03-17 05:55:25 -070030 : frame_generator_capturer_(frame_generator_capturer),
Danil Chapovalovabd42732018-09-10 14:07:45 +020031 repeat_interval_ms_(-1),
32 next_run_time_ms_(-1) {}
ilnikbaded152017-03-17 05:55:25 -070033
34 private:
35 bool Run() override {
Danil Chapovalovabd42732018-09-10 14:07:45 +020036 // Check if the frame interval for this
37 // task queue is the same same as the current configured frame rate.
38 int interval_ms =
39 1000 / frame_generator_capturer_->GetCurrentConfiguredFramerate();
40 if (repeat_interval_ms_ != interval_ms) {
41 // Restart the timer if frame rate has changed since task was started.
42 next_run_time_ms_ = rtc::TimeMillis();
43 repeat_interval_ms_ = interval_ms;
ilnikbaded152017-03-17 05:55:25 -070044 }
Danil Chapovalovabd42732018-09-10 14:07:45 +020045 // Schedule the next frame capture event to happen at approximately the
46 // correct absolute time point.
47 next_run_time_ms_ += interval_ms;
48
ilnikbaded152017-03-17 05:55:25 -070049 frame_generator_capturer_->InsertFrame();
Danil Chapovalovabd42732018-09-10 14:07:45 +020050
51 int64_t now_ms = rtc::TimeMillis();
52 if (next_run_time_ms_ < now_ms) {
53 int skipped_frames = 1 + (now_ms - next_run_time_ms_) / interval_ms;
54 next_run_time_ms_ += skipped_frames * interval_ms;
55 RTC_LOG(LS_ERROR) << "Frame Generator Capturer can't keep up with "
56 "requested fps, skipped "
57 << skipped_frames << " frame(s).";
58 }
59 int64_t delay_ms = next_run_time_ms_ - now_ms;
60 RTC_DCHECK_GE(delay_ms, 0);
61 RTC_DCHECK_LE(delay_ms, interval_ms);
62 rtc::TaskQueue::Current()->PostDelayedTask(absl::WrapUnique(this),
63 delay_ms);
64 return false;
ilnikbaded152017-03-17 05:55:25 -070065 }
66
67 webrtc::test::FrameGeneratorCapturer* const frame_generator_capturer_;
Danil Chapovalovabd42732018-09-10 14:07:45 +020068 int repeat_interval_ms_;
69 int64_t next_run_time_ms_;
ilnikbaded152017-03-17 05:55:25 -070070};
71
Emircan Uysaler03e6ec92018-03-09 15:03:26 -080072FrameGeneratorCapturer* FrameGeneratorCapturer::Create(
73 int width,
74 int height,
Danil Chapovalov431abd92018-06-18 12:54:17 +020075 absl::optional<FrameGenerator::OutputType> type,
76 absl::optional<int> num_squares,
Emircan Uysaler03e6ec92018-03-09 15:03:26 -080077 int target_fps,
78 Clock* clock) {
Danil Chapovalovabd42732018-09-10 14:07:45 +020079 auto capturer = absl::make_unique<FrameGeneratorCapturer>(
Emircan Uysaler03e6ec92018-03-09 15:03:26 -080080 clock,
81 FrameGenerator::CreateSquareGenerator(width, height, type, num_squares),
Danil Chapovalovabd42732018-09-10 14:07:45 +020082 target_fps);
Taylor Brandstetter081136f2018-03-08 01:54:10 +000083 if (!capturer->Init())
84 return nullptr;
85
86 return capturer.release();
87}
88
andresp@webrtc.orgab654952013-09-19 12:14:03 +000089FrameGeneratorCapturer* FrameGeneratorCapturer::CreateFromYuvFile(
sprang@webrtc.org131bea82015-02-18 12:46:06 +000090 const std::string& file_name,
andresp@webrtc.orgab654952013-09-19 12:14:03 +000091 size_t width,
92 size_t height,
93 int target_fps,
94 Clock* clock) {
Danil Chapovalovabd42732018-09-10 14:07:45 +020095 auto capturer = absl::make_unique<FrameGeneratorCapturer>(
sprangc5d62e22017-04-02 23:53:04 -070096 clock,
97 FrameGenerator::CreateFromYuvFile(std::vector<std::string>(1, file_name),
98 width, height, 1),
Danil Chapovalovabd42732018-09-10 14:07:45 +020099 target_fps);
sprangc5d62e22017-04-02 23:53:04 -0700100 if (!capturer->Init())
101 return nullptr;
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000102
sprangc5d62e22017-04-02 23:53:04 -0700103 return capturer.release();
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000104}
105
erikvarga579de6f2017-08-29 09:12:57 -0700106FrameGeneratorCapturer* FrameGeneratorCapturer::CreateSlideGenerator(
107 int width,
108 int height,
109 int frame_repeat_count,
110 int target_fps,
111 Clock* clock) {
Danil Chapovalovabd42732018-09-10 14:07:45 +0200112 auto capturer = absl::make_unique<FrameGeneratorCapturer>(
Yves Gerey665174f2018-06-19 15:03:05 +0200113 clock,
114 FrameGenerator::CreateSlideGenerator(width, height, frame_repeat_count),
Danil Chapovalovabd42732018-09-10 14:07:45 +0200115 target_fps);
erikvarga579de6f2017-08-29 09:12:57 -0700116 if (!capturer->Init())
117 return nullptr;
118
119 return capturer.release();
120}
121
perkja8ba1952017-02-27 06:52:10 -0800122FrameGeneratorCapturer::FrameGeneratorCapturer(
123 Clock* clock,
124 std::unique_ptr<FrameGenerator> frame_generator,
125 int target_fps)
perkja49cbd32016-09-16 07:53:41 -0700126 : clock_(clock),
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000127 sending_(false),
perkja49cbd32016-09-16 07:53:41 -0700128 sink_(nullptr),
perkj803d97f2016-11-01 11:45:46 -0700129 sink_wants_observer_(nullptr),
perkja8ba1952017-02-27 06:52:10 -0800130 frame_generator_(std::move(frame_generator)),
Sebastian Janssonba3decf2018-08-30 11:19:23 +0200131 source_fps_(target_fps),
132 target_capture_fps_(target_fps),
ilnikbaded152017-03-17 05:55:25 -0700133 first_frame_capture_time_(-1),
Yves Gerey665174f2018-06-19 15:03:05 +0200134 task_queue_("FrameGenCapQ", rtc::TaskQueue::Priority::HIGH) {
perkja8ba1952017-02-27 06:52:10 -0800135 RTC_DCHECK(frame_generator_);
perkja49cbd32016-09-16 07:53:41 -0700136 RTC_DCHECK_GT(target_fps, 0);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000137}
138
139FrameGeneratorCapturer::~FrameGeneratorCapturer() {
140 Stop();
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000141}
142
Perba7dc722016-04-19 15:01:23 +0200143void FrameGeneratorCapturer::SetFakeRotation(VideoRotation rotation) {
144 rtc::CritScope cs(&lock_);
145 fake_rotation_ = rotation;
146}
147
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000148bool FrameGeneratorCapturer::Init() {
pbos@webrtc.org94015242013-10-16 11:05:37 +0000149 // This check is added because frame_generator_ might be file based and should
150 // not crash because a file moved.
sprangc5d62e22017-04-02 23:53:04 -0700151 if (frame_generator_.get() == nullptr)
pbos@webrtc.org94015242013-10-16 11:05:37 +0000152 return false;
153
sprangc5d62e22017-04-02 23:53:04 -0700154 int framerate_fps = GetCurrentConfiguredFramerate();
Danil Chapovalovabd42732018-09-10 14:07:45 +0200155 task_queue_.PostDelayedTask(absl::make_unique<InsertFrameTask>(this),
156 1000 / framerate_fps);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000157
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000158 return true;
159}
160
161void FrameGeneratorCapturer::InsertFrame() {
sprangc5d62e22017-04-02 23:53:04 -0700162 rtc::CritScope cs(&lock_);
163 if (sending_) {
164 VideoFrame* frame = frame_generator_->NextFrame();
Sebastian Janssonba3decf2018-08-30 11:19:23 +0200165 // TODO(srte): Use more advanced frame rate control to allow arbritrary
166 // fractions.
167 int decimation =
168 std::round(static_cast<double>(source_fps_) / target_capture_fps_);
169 for (int i = 1; i < decimation; ++i)
170 frame = frame_generator_->NextFrame();
ilnik04f4d122017-06-19 07:18:55 -0700171 frame->set_timestamp_us(clock_->TimeInMicroseconds());
sprangc5d62e22017-04-02 23:53:04 -0700172 frame->set_ntp_time_ms(clock_->CurrentNtpInMilliseconds());
173 frame->set_rotation(fake_rotation_);
174 if (first_frame_capture_time_ == -1) {
175 first_frame_capture_time_ = frame->ntp_time_ms();
176 }
177
178 if (sink_) {
Danil Chapovalov431abd92018-06-18 12:54:17 +0200179 absl::optional<VideoFrame> out_frame = AdaptFrame(*frame);
sprangc5d62e22017-04-02 23:53:04 -0700180 if (out_frame)
181 sink_->OnFrame(*out_frame);
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000182 }
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000183 }
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000184}
185
186void FrameGeneratorCapturer::Start() {
Peter Boströmf2f82832015-05-01 13:00:41 +0200187 rtc::CritScope cs(&lock_);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000188 sending_ = true;
189}
190
191void FrameGeneratorCapturer::Stop() {
Peter Boströmf2f82832015-05-01 13:00:41 +0200192 rtc::CritScope cs(&lock_);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000193 sending_ = false;
194}
sprang867fb522015-08-03 04:38:41 -0700195
perkjfa10b552016-10-02 23:45:26 -0700196void FrameGeneratorCapturer::ChangeResolution(size_t width, size_t height) {
197 rtc::CritScope cs(&lock_);
198 frame_generator_->ChangeResolution(width, height);
199}
200
Sebastian Janssonba3decf2018-08-30 11:19:23 +0200201void FrameGeneratorCapturer::ChangeFramerate(int target_framerate) {
202 rtc::CritScope cs(&lock_);
203 RTC_CHECK(target_capture_fps_ > 0);
204 if (target_framerate > source_fps_)
205 RTC_LOG(LS_WARNING) << "Target framerate clamped from " << target_framerate
206 << " to " << source_fps_;
207 if (source_fps_ % target_capture_fps_ != 0) {
208 int decimation =
209 std::round(static_cast<double>(source_fps_) / target_capture_fps_);
210 int effective_rate = target_capture_fps_ / decimation;
211 RTC_LOG(LS_WARNING) << "Target framerate, " << target_framerate
212 << ", is an uneven fraction of the source rate, "
213 << source_fps_
214 << ". The framerate will be :" << effective_rate;
215 }
216 target_capture_fps_ = std::min(source_fps_, target_framerate);
217}
218
perkj803d97f2016-11-01 11:45:46 -0700219void FrameGeneratorCapturer::SetSinkWantsObserver(SinkWantsObserver* observer) {
220 rtc::CritScope cs(&lock_);
221 RTC_DCHECK(!sink_wants_observer_);
222 sink_wants_observer_ = observer;
223}
224
perkja49cbd32016-09-16 07:53:41 -0700225void FrameGeneratorCapturer::AddOrUpdateSink(
226 rtc::VideoSinkInterface<VideoFrame>* sink,
227 const rtc::VideoSinkWants& wants) {
228 rtc::CritScope cs(&lock_);
perkj803d97f2016-11-01 11:45:46 -0700229 RTC_CHECK(!sink_ || sink_ == sink);
perkja49cbd32016-09-16 07:53:41 -0700230 sink_ = sink;
perkj803d97f2016-11-01 11:45:46 -0700231 if (sink_wants_observer_)
232 sink_wants_observer_->OnSinkWantsChanged(sink, wants);
sprangc5d62e22017-04-02 23:53:04 -0700233
234 // Handle framerate within this class, just pass on resolution for possible
235 // adaptation.
236 rtc::VideoSinkWants resolution_wants = wants;
237 resolution_wants.max_framerate_fps = std::numeric_limits<int>::max();
Sebastian Janssonf1f363f2018-08-13 14:24:58 +0200238 TestVideoCapturer::AddOrUpdateSink(sink, resolution_wants);
sprangc5d62e22017-04-02 23:53:04 -0700239
240 // Ignore any requests for framerate higher than initially configured.
Sebastian Janssonba3decf2018-08-30 11:19:23 +0200241 if (wants.max_framerate_fps < target_capture_fps_) {
sprangc5d62e22017-04-02 23:53:04 -0700242 wanted_fps_.emplace(wants.max_framerate_fps);
243 } else {
244 wanted_fps_.reset();
245 }
perkja49cbd32016-09-16 07:53:41 -0700246}
247
248void FrameGeneratorCapturer::RemoveSink(
249 rtc::VideoSinkInterface<VideoFrame>* sink) {
250 rtc::CritScope cs(&lock_);
251 RTC_CHECK(sink_ == sink);
252 sink_ = nullptr;
253}
254
sprang867fb522015-08-03 04:38:41 -0700255void FrameGeneratorCapturer::ForceFrame() {
ilnikbaded152017-03-17 05:55:25 -0700256 // One-time non-repeating task,
Danil Chapovalovabd42732018-09-10 14:07:45 +0200257 task_queue_.PostTask([this] { InsertFrame(); });
sprang867fb522015-08-03 04:38:41 -0700258}
ilnikbaded152017-03-17 05:55:25 -0700259
sprangc5d62e22017-04-02 23:53:04 -0700260int FrameGeneratorCapturer::GetCurrentConfiguredFramerate() {
261 rtc::CritScope cs(&lock_);
Sebastian Janssonba3decf2018-08-30 11:19:23 +0200262 if (wanted_fps_ && *wanted_fps_ < target_capture_fps_)
sprangc5d62e22017-04-02 23:53:04 -0700263 return *wanted_fps_;
Sebastian Janssonba3decf2018-08-30 11:19:23 +0200264 return target_capture_fps_;
sprangc5d62e22017-04-02 23:53:04 -0700265}
266
ilnikbaded152017-03-17 05:55:25 -0700267} // namespace test
268} // namespace webrtc