blob: 184fdd76ef03a7079dc4d8d1d6bcddf432e4e2a6 [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
Yves Gerey665174f2018-06-19 15:03:05 +020016#include "call/video_send_stream.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/criticalsection.h"
18#include "rtc_base/logging.h"
19#include "rtc_base/platform_thread.h"
20#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:
29 // Repeats in |repeat_interval_ms|. One-time if |repeat_interval_ms| == 0.
30 InsertFrameTask(
31 webrtc::test::FrameGeneratorCapturer* frame_generator_capturer,
32 uint32_t repeat_interval_ms)
33 : frame_generator_capturer_(frame_generator_capturer),
34 repeat_interval_ms_(repeat_interval_ms),
35 intended_run_time_ms_(-1) {}
36
37 private:
38 bool Run() override {
sprangc5d62e22017-04-02 23:53:04 -070039 bool task_completed = true;
ilnikbaded152017-03-17 05:55:25 -070040 if (repeat_interval_ms_ > 0) {
sprangc5d62e22017-04-02 23:53:04 -070041 // This is not a one-off frame. Check if the frame interval for this
42 // task queue is the same same as the current configured frame rate.
43 uint32_t current_interval_ms =
44 1000 / frame_generator_capturer_->GetCurrentConfiguredFramerate();
45 if (repeat_interval_ms_ != current_interval_ms) {
46 // Frame rate has changed since task was started, create a new instance.
lliuuf9ed2352017-03-30 10:44:38 -070047 rtc::TaskQueue::Current()->PostDelayedTask(
sprangc5d62e22017-04-02 23:53:04 -070048 std::unique_ptr<rtc::QueuedTask>(new InsertFrameTask(
49 frame_generator_capturer_, current_interval_ms)),
50 current_interval_ms);
lliuuf9ed2352017-03-30 10:44:38 -070051 } else {
sprangc5d62e22017-04-02 23:53:04 -070052 // Schedule the next frame capture event to happen at approximately the
53 // correct absolute time point.
54 int64_t delay_ms;
55 int64_t time_now_ms = rtc::TimeMillis();
56 if (intended_run_time_ms_ > 0) {
57 delay_ms = time_now_ms - intended_run_time_ms_;
58 } else {
59 delay_ms = 0;
60 intended_run_time_ms_ = time_now_ms;
61 }
62 intended_run_time_ms_ += repeat_interval_ms_;
63 if (delay_ms < repeat_interval_ms_) {
64 rtc::TaskQueue::Current()->PostDelayedTask(
65 std::unique_ptr<rtc::QueuedTask>(this),
66 repeat_interval_ms_ - delay_ms);
67 } else {
68 rtc::TaskQueue::Current()->PostDelayedTask(
69 std::unique_ptr<rtc::QueuedTask>(this), 0);
Mirko Bonadei675513b2017-11-09 11:09:25 +010070 RTC_LOG(LS_ERROR)
sprangc5d62e22017-04-02 23:53:04 -070071 << "Frame Generator Capturer can't keep up with requested fps";
72 }
73 // Repost of this instance, make sure it is not deleted.
74 task_completed = false;
ilnikbaded152017-03-17 05:55:25 -070075 }
76 }
77 frame_generator_capturer_->InsertFrame();
78 // Task should be deleted only if it's not repeating.
sprangc5d62e22017-04-02 23:53:04 -070079 return task_completed;
ilnikbaded152017-03-17 05:55:25 -070080 }
81
82 webrtc::test::FrameGeneratorCapturer* const frame_generator_capturer_;
83 const uint32_t repeat_interval_ms_;
84 int64_t intended_run_time_ms_;
85};
86
Emircan Uysaler03e6ec92018-03-09 15:03:26 -080087FrameGeneratorCapturer* FrameGeneratorCapturer::Create(
88 int width,
89 int height,
Danil Chapovalov431abd92018-06-18 12:54:17 +020090 absl::optional<FrameGenerator::OutputType> type,
91 absl::optional<int> num_squares,
Emircan Uysaler03e6ec92018-03-09 15:03:26 -080092 int target_fps,
93 Clock* clock) {
sprangc5d62e22017-04-02 23:53:04 -070094 std::unique_ptr<FrameGeneratorCapturer> capturer(new FrameGeneratorCapturer(
Emircan Uysaler03e6ec92018-03-09 15:03:26 -080095 clock,
96 FrameGenerator::CreateSquareGenerator(width, height, type, num_squares),
97 target_fps));
Taylor Brandstetter081136f2018-03-08 01:54:10 +000098 if (!capturer->Init())
99 return nullptr;
100
101 return capturer.release();
102}
103
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000104FrameGeneratorCapturer* FrameGeneratorCapturer::CreateFromYuvFile(
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000105 const std::string& file_name,
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000106 size_t width,
107 size_t height,
108 int target_fps,
109 Clock* clock) {
sprangc5d62e22017-04-02 23:53:04 -0700110 std::unique_ptr<FrameGeneratorCapturer> capturer(new FrameGeneratorCapturer(
111 clock,
112 FrameGenerator::CreateFromYuvFile(std::vector<std::string>(1, file_name),
113 width, height, 1),
114 target_fps));
115 if (!capturer->Init())
116 return nullptr;
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000117
sprangc5d62e22017-04-02 23:53:04 -0700118 return capturer.release();
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000119}
120
erikvarga579de6f2017-08-29 09:12:57 -0700121FrameGeneratorCapturer* FrameGeneratorCapturer::CreateSlideGenerator(
122 int width,
123 int height,
124 int frame_repeat_count,
125 int target_fps,
126 Clock* clock) {
127 std::unique_ptr<FrameGeneratorCapturer> capturer(new FrameGeneratorCapturer(
Yves Gerey665174f2018-06-19 15:03:05 +0200128 clock,
129 FrameGenerator::CreateSlideGenerator(width, height, frame_repeat_count),
erikvarga579de6f2017-08-29 09:12:57 -0700130 target_fps));
131 if (!capturer->Init())
132 return nullptr;
133
134 return capturer.release();
135}
136
perkja8ba1952017-02-27 06:52:10 -0800137FrameGeneratorCapturer::FrameGeneratorCapturer(
138 Clock* clock,
139 std::unique_ptr<FrameGenerator> frame_generator,
140 int target_fps)
perkja49cbd32016-09-16 07:53:41 -0700141 : clock_(clock),
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000142 sending_(false),
perkja49cbd32016-09-16 07:53:41 -0700143 sink_(nullptr),
perkj803d97f2016-11-01 11:45:46 -0700144 sink_wants_observer_(nullptr),
perkja8ba1952017-02-27 06:52:10 -0800145 frame_generator_(std::move(frame_generator)),
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000146 target_fps_(target_fps),
ilnikbaded152017-03-17 05:55:25 -0700147 first_frame_capture_time_(-1),
Yves Gerey665174f2018-06-19 15:03:05 +0200148 task_queue_("FrameGenCapQ", rtc::TaskQueue::Priority::HIGH) {
perkja8ba1952017-02-27 06:52:10 -0800149 RTC_DCHECK(frame_generator_);
perkja49cbd32016-09-16 07:53:41 -0700150 RTC_DCHECK_GT(target_fps, 0);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000151}
152
153FrameGeneratorCapturer::~FrameGeneratorCapturer() {
154 Stop();
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000155}
156
Perba7dc722016-04-19 15:01:23 +0200157void FrameGeneratorCapturer::SetFakeRotation(VideoRotation rotation) {
158 rtc::CritScope cs(&lock_);
159 fake_rotation_ = rotation;
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
sprangc5d62e22017-04-02 23:53:04 -0700168 int framerate_fps = GetCurrentConfiguredFramerate();
ilnikbaded152017-03-17 05:55:25 -0700169 task_queue_.PostDelayedTask(
170 std::unique_ptr<rtc::QueuedTask>(
sprangc5d62e22017-04-02 23:53:04 -0700171 new InsertFrameTask(this, 1000 / framerate_fps)),
172 1000 / framerate_fps);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000173
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_) {
180 VideoFrame* frame = frame_generator_->NextFrame();
ilnik04f4d122017-06-19 07:18:55 -0700181 frame->set_timestamp_us(clock_->TimeInMicroseconds());
sprangc5d62e22017-04-02 23:53:04 -0700182 frame->set_ntp_time_ms(clock_->CurrentNtpInMilliseconds());
183 frame->set_rotation(fake_rotation_);
184 if (first_frame_capture_time_ == -1) {
185 first_frame_capture_time_ = frame->ntp_time_ms();
186 }
187
188 if (sink_) {
Danil Chapovalov431abd92018-06-18 12:54:17 +0200189 absl::optional<VideoFrame> out_frame = AdaptFrame(*frame);
sprangc5d62e22017-04-02 23:53:04 -0700190 if (out_frame)
191 sink_->OnFrame(*out_frame);
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000192 }
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000193 }
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000194}
195
196void FrameGeneratorCapturer::Start() {
Peter Boströmf2f82832015-05-01 13:00:41 +0200197 rtc::CritScope cs(&lock_);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000198 sending_ = true;
199}
200
201void FrameGeneratorCapturer::Stop() {
Peter Boströmf2f82832015-05-01 13:00:41 +0200202 rtc::CritScope cs(&lock_);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000203 sending_ = false;
204}
sprang867fb522015-08-03 04:38:41 -0700205
perkjfa10b552016-10-02 23:45:26 -0700206void FrameGeneratorCapturer::ChangeResolution(size_t width, size_t height) {
207 rtc::CritScope cs(&lock_);
208 frame_generator_->ChangeResolution(width, height);
209}
210
perkj803d97f2016-11-01 11:45:46 -0700211void FrameGeneratorCapturer::SetSinkWantsObserver(SinkWantsObserver* observer) {
212 rtc::CritScope cs(&lock_);
213 RTC_DCHECK(!sink_wants_observer_);
214 sink_wants_observer_ = observer;
215}
216
perkja49cbd32016-09-16 07:53:41 -0700217void FrameGeneratorCapturer::AddOrUpdateSink(
218 rtc::VideoSinkInterface<VideoFrame>* sink,
219 const rtc::VideoSinkWants& wants) {
220 rtc::CritScope cs(&lock_);
perkj803d97f2016-11-01 11:45:46 -0700221 RTC_CHECK(!sink_ || sink_ == sink);
perkja49cbd32016-09-16 07:53:41 -0700222 sink_ = sink;
perkj803d97f2016-11-01 11:45:46 -0700223 if (sink_wants_observer_)
224 sink_wants_observer_->OnSinkWantsChanged(sink, wants);
sprangc5d62e22017-04-02 23:53:04 -0700225
226 // Handle framerate within this class, just pass on resolution for possible
227 // adaptation.
228 rtc::VideoSinkWants resolution_wants = wants;
229 resolution_wants.max_framerate_fps = std::numeric_limits<int>::max();
230 VideoCapturer::AddOrUpdateSink(sink, resolution_wants);
231
232 // Ignore any requests for framerate higher than initially configured.
233 if (wants.max_framerate_fps < target_fps_) {
234 wanted_fps_.emplace(wants.max_framerate_fps);
235 } else {
236 wanted_fps_.reset();
237 }
perkja49cbd32016-09-16 07:53:41 -0700238}
239
240void FrameGeneratorCapturer::RemoveSink(
241 rtc::VideoSinkInterface<VideoFrame>* sink) {
242 rtc::CritScope cs(&lock_);
243 RTC_CHECK(sink_ == sink);
244 sink_ = nullptr;
245}
246
sprang867fb522015-08-03 04:38:41 -0700247void FrameGeneratorCapturer::ForceFrame() {
ilnikbaded152017-03-17 05:55:25 -0700248 // One-time non-repeating task,
249 // therefore repeat_interval_ms is 0 in InsertFrameTask()
250 task_queue_.PostTask(
251 std::unique_ptr<rtc::QueuedTask>(new InsertFrameTask(this, 0)));
sprang867fb522015-08-03 04:38:41 -0700252}
ilnikbaded152017-03-17 05:55:25 -0700253
sprangc5d62e22017-04-02 23:53:04 -0700254int FrameGeneratorCapturer::GetCurrentConfiguredFramerate() {
255 rtc::CritScope cs(&lock_);
256 if (wanted_fps_ && *wanted_fps_ < target_fps_)
257 return *wanted_fps_;
258 return target_fps_;
259}
260
ilnikbaded152017-03-17 05:55:25 -0700261} // namespace test
262} // namespace webrtc