blob: a8983b7ba20b0d9b733d2c752377d345456c8824 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/criticalsection.h"
17#include "rtc_base/logging.h"
18#include "rtc_base/platform_thread.h"
19#include "rtc_base/task_queue.h"
20#include "rtc_base/timeutils.h"
21#include "system_wrappers/include/clock.h"
22#include "test/frame_generator.h"
23#include "call/video_send_stream.h"
pbos@webrtc.org26d12102013-05-29 13:41:03 +000024
25namespace webrtc {
26namespace test {
27
ilnikbaded152017-03-17 05:55:25 -070028class FrameGeneratorCapturer::InsertFrameTask : public rtc::QueuedTask {
29 public:
30 // Repeats in |repeat_interval_ms|. One-time if |repeat_interval_ms| == 0.
31 InsertFrameTask(
32 webrtc::test::FrameGeneratorCapturer* frame_generator_capturer,
33 uint32_t repeat_interval_ms)
34 : frame_generator_capturer_(frame_generator_capturer),
35 repeat_interval_ms_(repeat_interval_ms),
36 intended_run_time_ms_(-1) {}
37
38 private:
39 bool Run() override {
sprangc5d62e22017-04-02 23:53:04 -070040 bool task_completed = true;
ilnikbaded152017-03-17 05:55:25 -070041 if (repeat_interval_ms_ > 0) {
sprangc5d62e22017-04-02 23:53:04 -070042 // This is not a one-off frame. Check if the frame interval for this
43 // task queue is the same same as the current configured frame rate.
44 uint32_t current_interval_ms =
45 1000 / frame_generator_capturer_->GetCurrentConfiguredFramerate();
46 if (repeat_interval_ms_ != current_interval_ms) {
47 // Frame rate has changed since task was started, create a new instance.
lliuuf9ed2352017-03-30 10:44:38 -070048 rtc::TaskQueue::Current()->PostDelayedTask(
sprangc5d62e22017-04-02 23:53:04 -070049 std::unique_ptr<rtc::QueuedTask>(new InsertFrameTask(
50 frame_generator_capturer_, current_interval_ms)),
51 current_interval_ms);
lliuuf9ed2352017-03-30 10:44:38 -070052 } else {
sprangc5d62e22017-04-02 23:53:04 -070053 // Schedule the next frame capture event to happen at approximately the
54 // correct absolute time point.
55 int64_t delay_ms;
56 int64_t time_now_ms = rtc::TimeMillis();
57 if (intended_run_time_ms_ > 0) {
58 delay_ms = time_now_ms - intended_run_time_ms_;
59 } else {
60 delay_ms = 0;
61 intended_run_time_ms_ = time_now_ms;
62 }
63 intended_run_time_ms_ += repeat_interval_ms_;
64 if (delay_ms < repeat_interval_ms_) {
65 rtc::TaskQueue::Current()->PostDelayedTask(
66 std::unique_ptr<rtc::QueuedTask>(this),
67 repeat_interval_ms_ - delay_ms);
68 } else {
69 rtc::TaskQueue::Current()->PostDelayedTask(
70 std::unique_ptr<rtc::QueuedTask>(this), 0);
Mirko Bonadei675513b2017-11-09 11:09:25 +010071 RTC_LOG(LS_ERROR)
sprangc5d62e22017-04-02 23:53:04 -070072 << "Frame Generator Capturer can't keep up with requested fps";
73 }
74 // Repost of this instance, make sure it is not deleted.
75 task_completed = false;
ilnikbaded152017-03-17 05:55:25 -070076 }
77 }
78 frame_generator_capturer_->InsertFrame();
79 // Task should be deleted only if it's not repeating.
sprangc5d62e22017-04-02 23:53:04 -070080 return task_completed;
ilnikbaded152017-03-17 05:55:25 -070081 }
82
83 webrtc::test::FrameGeneratorCapturer* const frame_generator_capturer_;
84 const uint32_t repeat_interval_ms_;
85 int64_t intended_run_time_ms_;
86};
87
perkja8ba1952017-02-27 06:52:10 -080088FrameGeneratorCapturer* FrameGeneratorCapturer::Create(int width,
89 int height,
Peter Boström4b91bd02015-06-26 06:58:16 +020090 int target_fps,
91 Clock* clock) {
sprangc5d62e22017-04-02 23:53:04 -070092 std::unique_ptr<FrameGeneratorCapturer> capturer(new FrameGeneratorCapturer(
93 clock, FrameGenerator::CreateSquareGenerator(width, height), target_fps));
94 if (!capturer->Init())
95 return nullptr;
pbos@webrtc.org26d12102013-05-29 13:41:03 +000096
sprangc5d62e22017-04-02 23:53:04 -070097 return capturer.release();
pbos@webrtc.org26d12102013-05-29 13:41:03 +000098}
99
erikvarga@webrtc.orgc774d5d2017-10-10 14:34:38 +0200100FrameGeneratorCapturer* FrameGeneratorCapturer::Create(int width,
101 int height,
102 int num_squares,
103 int target_fps,
104 Clock* clock) {
105 std::unique_ptr<FrameGeneratorCapturer> capturer(new FrameGeneratorCapturer(
106 clock, FrameGenerator::CreateSquareGenerator(width, height, num_squares),
107 target_fps));
108 if (!capturer->Init())
109 return nullptr;
110
111 return capturer.release();
112}
113
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000114FrameGeneratorCapturer* FrameGeneratorCapturer::CreateFromYuvFile(
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000115 const std::string& file_name,
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000116 size_t width,
117 size_t height,
118 int target_fps,
119 Clock* clock) {
sprangc5d62e22017-04-02 23:53:04 -0700120 std::unique_ptr<FrameGeneratorCapturer> capturer(new FrameGeneratorCapturer(
121 clock,
122 FrameGenerator::CreateFromYuvFile(std::vector<std::string>(1, file_name),
123 width, height, 1),
124 target_fps));
125 if (!capturer->Init())
126 return nullptr;
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000127
sprangc5d62e22017-04-02 23:53:04 -0700128 return capturer.release();
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000129}
130
erikvarga579de6f2017-08-29 09:12:57 -0700131FrameGeneratorCapturer* FrameGeneratorCapturer::CreateSlideGenerator(
132 int width,
133 int height,
134 int frame_repeat_count,
135 int target_fps,
136 Clock* clock) {
137 std::unique_ptr<FrameGeneratorCapturer> capturer(new FrameGeneratorCapturer(
138 clock, FrameGenerator::CreateSlideGenerator(width, height,
139 frame_repeat_count),
140 target_fps));
141 if (!capturer->Init())
142 return nullptr;
143
144 return capturer.release();
145}
146
perkja8ba1952017-02-27 06:52:10 -0800147FrameGeneratorCapturer::FrameGeneratorCapturer(
148 Clock* clock,
149 std::unique_ptr<FrameGenerator> frame_generator,
150 int target_fps)
perkja49cbd32016-09-16 07:53:41 -0700151 : clock_(clock),
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000152 sending_(false),
perkja49cbd32016-09-16 07:53:41 -0700153 sink_(nullptr),
perkj803d97f2016-11-01 11:45:46 -0700154 sink_wants_observer_(nullptr),
perkja8ba1952017-02-27 06:52:10 -0800155 frame_generator_(std::move(frame_generator)),
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000156 target_fps_(target_fps),
ilnikbaded152017-03-17 05:55:25 -0700157 first_frame_capture_time_(-1),
158 task_queue_("FrameGenCapQ",
159 rtc::TaskQueue::Priority::HIGH) {
perkja8ba1952017-02-27 06:52:10 -0800160 RTC_DCHECK(frame_generator_);
perkja49cbd32016-09-16 07:53:41 -0700161 RTC_DCHECK_GT(target_fps, 0);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000162}
163
164FrameGeneratorCapturer::~FrameGeneratorCapturer() {
165 Stop();
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000166}
167
Perba7dc722016-04-19 15:01:23 +0200168void FrameGeneratorCapturer::SetFakeRotation(VideoRotation rotation) {
169 rtc::CritScope cs(&lock_);
170 fake_rotation_ = rotation;
171}
172
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000173bool FrameGeneratorCapturer::Init() {
pbos@webrtc.org94015242013-10-16 11:05:37 +0000174 // This check is added because frame_generator_ might be file based and should
175 // not crash because a file moved.
sprangc5d62e22017-04-02 23:53:04 -0700176 if (frame_generator_.get() == nullptr)
pbos@webrtc.org94015242013-10-16 11:05:37 +0000177 return false;
178
sprangc5d62e22017-04-02 23:53:04 -0700179 int framerate_fps = GetCurrentConfiguredFramerate();
ilnikbaded152017-03-17 05:55:25 -0700180 task_queue_.PostDelayedTask(
181 std::unique_ptr<rtc::QueuedTask>(
sprangc5d62e22017-04-02 23:53:04 -0700182 new InsertFrameTask(this, 1000 / framerate_fps)),
183 1000 / framerate_fps);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000184
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000185 return true;
186}
187
188void FrameGeneratorCapturer::InsertFrame() {
sprangc5d62e22017-04-02 23:53:04 -0700189 rtc::CritScope cs(&lock_);
190 if (sending_) {
191 VideoFrame* frame = frame_generator_->NextFrame();
ilnik04f4d122017-06-19 07:18:55 -0700192 frame->set_timestamp_us(clock_->TimeInMicroseconds());
sprangc5d62e22017-04-02 23:53:04 -0700193 frame->set_ntp_time_ms(clock_->CurrentNtpInMilliseconds());
194 frame->set_rotation(fake_rotation_);
195 if (first_frame_capture_time_ == -1) {
196 first_frame_capture_time_ = frame->ntp_time_ms();
197 }
198
199 if (sink_) {
200 rtc::Optional<VideoFrame> out_frame = AdaptFrame(*frame);
201 if (out_frame)
202 sink_->OnFrame(*out_frame);
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000203 }
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000204 }
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000205}
206
207void FrameGeneratorCapturer::Start() {
Peter Boströmf2f82832015-05-01 13:00:41 +0200208 rtc::CritScope cs(&lock_);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000209 sending_ = true;
210}
211
212void FrameGeneratorCapturer::Stop() {
Peter Boströmf2f82832015-05-01 13:00:41 +0200213 rtc::CritScope cs(&lock_);
pbos@webrtc.org26d12102013-05-29 13:41:03 +0000214 sending_ = false;
215}
sprang867fb522015-08-03 04:38:41 -0700216
perkjfa10b552016-10-02 23:45:26 -0700217void FrameGeneratorCapturer::ChangeResolution(size_t width, size_t height) {
218 rtc::CritScope cs(&lock_);
219 frame_generator_->ChangeResolution(width, height);
220}
221
perkj803d97f2016-11-01 11:45:46 -0700222void FrameGeneratorCapturer::SetSinkWantsObserver(SinkWantsObserver* observer) {
223 rtc::CritScope cs(&lock_);
224 RTC_DCHECK(!sink_wants_observer_);
225 sink_wants_observer_ = observer;
226}
227
perkja49cbd32016-09-16 07:53:41 -0700228void FrameGeneratorCapturer::AddOrUpdateSink(
229 rtc::VideoSinkInterface<VideoFrame>* sink,
230 const rtc::VideoSinkWants& wants) {
231 rtc::CritScope cs(&lock_);
perkj803d97f2016-11-01 11:45:46 -0700232 RTC_CHECK(!sink_ || sink_ == sink);
perkja49cbd32016-09-16 07:53:41 -0700233 sink_ = sink;
perkj803d97f2016-11-01 11:45:46 -0700234 if (sink_wants_observer_)
235 sink_wants_observer_->OnSinkWantsChanged(sink, wants);
sprangc5d62e22017-04-02 23:53:04 -0700236
237 // Handle framerate within this class, just pass on resolution for possible
238 // adaptation.
239 rtc::VideoSinkWants resolution_wants = wants;
240 resolution_wants.max_framerate_fps = std::numeric_limits<int>::max();
241 VideoCapturer::AddOrUpdateSink(sink, resolution_wants);
242
243 // Ignore any requests for framerate higher than initially configured.
244 if (wants.max_framerate_fps < target_fps_) {
245 wanted_fps_.emplace(wants.max_framerate_fps);
246 } else {
247 wanted_fps_.reset();
248 }
perkja49cbd32016-09-16 07:53:41 -0700249}
250
251void FrameGeneratorCapturer::RemoveSink(
252 rtc::VideoSinkInterface<VideoFrame>* sink) {
253 rtc::CritScope cs(&lock_);
254 RTC_CHECK(sink_ == sink);
255 sink_ = nullptr;
256}
257
sprang867fb522015-08-03 04:38:41 -0700258void FrameGeneratorCapturer::ForceFrame() {
ilnikbaded152017-03-17 05:55:25 -0700259 // One-time non-repeating task,
260 // therefore repeat_interval_ms is 0 in InsertFrameTask()
261 task_queue_.PostTask(
262 std::unique_ptr<rtc::QueuedTask>(new InsertFrameTask(this, 0)));
sprang867fb522015-08-03 04:38:41 -0700263}
ilnikbaded152017-03-17 05:55:25 -0700264
sprangc5d62e22017-04-02 23:53:04 -0700265int FrameGeneratorCapturer::GetCurrentConfiguredFramerate() {
266 rtc::CritScope cs(&lock_);
267 if (wanted_fps_ && *wanted_fps_ < target_fps_)
268 return *wanted_fps_;
269 return target_fps_;
270}
271
ilnikbaded152017-03-17 05:55:25 -0700272} // namespace test
273} // namespace webrtc