pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "test/frame_generator_capturer.h" |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 12 | |
ilnik | baded15 | 2017-03-17 05:55:25 -0700 | [diff] [blame] | 13 | #include <utility> |
| 14 | #include <vector> |
| 15 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 16 | #include "call/video_send_stream.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #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.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 23 | |
| 24 | namespace webrtc { |
| 25 | namespace test { |
| 26 | |
ilnik | baded15 | 2017-03-17 05:55:25 -0700 | [diff] [blame] | 27 | class 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 { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 39 | bool task_completed = true; |
ilnik | baded15 | 2017-03-17 05:55:25 -0700 | [diff] [blame] | 40 | if (repeat_interval_ms_ > 0) { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 41 | // 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. |
lliuu | f9ed235 | 2017-03-30 10:44:38 -0700 | [diff] [blame] | 47 | rtc::TaskQueue::Current()->PostDelayedTask( |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 48 | std::unique_ptr<rtc::QueuedTask>(new InsertFrameTask( |
| 49 | frame_generator_capturer_, current_interval_ms)), |
| 50 | current_interval_ms); |
lliuu | f9ed235 | 2017-03-30 10:44:38 -0700 | [diff] [blame] | 51 | } else { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 52 | // 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 Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 70 | RTC_LOG(LS_ERROR) |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 71 | << "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; |
ilnik | baded15 | 2017-03-17 05:55:25 -0700 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | frame_generator_capturer_->InsertFrame(); |
| 78 | // Task should be deleted only if it's not repeating. |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 79 | return task_completed; |
ilnik | baded15 | 2017-03-17 05:55:25 -0700 | [diff] [blame] | 80 | } |
| 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 Uysaler | 03e6ec9 | 2018-03-09 15:03:26 -0800 | [diff] [blame] | 87 | FrameGeneratorCapturer* FrameGeneratorCapturer::Create( |
| 88 | int width, |
| 89 | int height, |
Danil Chapovalov | 431abd9 | 2018-06-18 12:54:17 +0200 | [diff] [blame] | 90 | absl::optional<FrameGenerator::OutputType> type, |
| 91 | absl::optional<int> num_squares, |
Emircan Uysaler | 03e6ec9 | 2018-03-09 15:03:26 -0800 | [diff] [blame] | 92 | int target_fps, |
| 93 | Clock* clock) { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 94 | std::unique_ptr<FrameGeneratorCapturer> capturer(new FrameGeneratorCapturer( |
Emircan Uysaler | 03e6ec9 | 2018-03-09 15:03:26 -0800 | [diff] [blame] | 95 | clock, |
| 96 | FrameGenerator::CreateSquareGenerator(width, height, type, num_squares), |
| 97 | target_fps)); |
Taylor Brandstetter | 081136f | 2018-03-08 01:54:10 +0000 | [diff] [blame] | 98 | if (!capturer->Init()) |
| 99 | return nullptr; |
| 100 | |
| 101 | return capturer.release(); |
| 102 | } |
| 103 | |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 104 | FrameGeneratorCapturer* FrameGeneratorCapturer::CreateFromYuvFile( |
sprang@webrtc.org | 131bea8 | 2015-02-18 12:46:06 +0000 | [diff] [blame] | 105 | const std::string& file_name, |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 106 | size_t width, |
| 107 | size_t height, |
| 108 | int target_fps, |
| 109 | Clock* clock) { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 110 | 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.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 117 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 118 | return capturer.release(); |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 119 | } |
| 120 | |
erikvarga | 579de6f | 2017-08-29 09:12:57 -0700 | [diff] [blame] | 121 | FrameGeneratorCapturer* 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 128 | clock, |
| 129 | FrameGenerator::CreateSlideGenerator(width, height, frame_repeat_count), |
erikvarga | 579de6f | 2017-08-29 09:12:57 -0700 | [diff] [blame] | 130 | target_fps)); |
| 131 | if (!capturer->Init()) |
| 132 | return nullptr; |
| 133 | |
| 134 | return capturer.release(); |
| 135 | } |
| 136 | |
perkj | a8ba195 | 2017-02-27 06:52:10 -0800 | [diff] [blame] | 137 | FrameGeneratorCapturer::FrameGeneratorCapturer( |
| 138 | Clock* clock, |
| 139 | std::unique_ptr<FrameGenerator> frame_generator, |
| 140 | int target_fps) |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 141 | : clock_(clock), |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 142 | sending_(false), |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 143 | sink_(nullptr), |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 144 | sink_wants_observer_(nullptr), |
perkj | a8ba195 | 2017-02-27 06:52:10 -0800 | [diff] [blame] | 145 | frame_generator_(std::move(frame_generator)), |
wu@webrtc.org | cd70119 | 2014-04-24 22:10:24 +0000 | [diff] [blame] | 146 | target_fps_(target_fps), |
ilnik | baded15 | 2017-03-17 05:55:25 -0700 | [diff] [blame] | 147 | first_frame_capture_time_(-1), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 148 | task_queue_("FrameGenCapQ", rtc::TaskQueue::Priority::HIGH) { |
perkj | a8ba195 | 2017-02-27 06:52:10 -0800 | [diff] [blame] | 149 | RTC_DCHECK(frame_generator_); |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 150 | RTC_DCHECK_GT(target_fps, 0); |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | FrameGeneratorCapturer::~FrameGeneratorCapturer() { |
| 154 | Stop(); |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Per | ba7dc72 | 2016-04-19 15:01:23 +0200 | [diff] [blame] | 157 | void FrameGeneratorCapturer::SetFakeRotation(VideoRotation rotation) { |
| 158 | rtc::CritScope cs(&lock_); |
| 159 | fake_rotation_ = rotation; |
| 160 | } |
| 161 | |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 162 | bool FrameGeneratorCapturer::Init() { |
pbos@webrtc.org | 9401524 | 2013-10-16 11:05:37 +0000 | [diff] [blame] | 163 | // This check is added because frame_generator_ might be file based and should |
| 164 | // not crash because a file moved. |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 165 | if (frame_generator_.get() == nullptr) |
pbos@webrtc.org | 9401524 | 2013-10-16 11:05:37 +0000 | [diff] [blame] | 166 | return false; |
| 167 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 168 | int framerate_fps = GetCurrentConfiguredFramerate(); |
ilnik | baded15 | 2017-03-17 05:55:25 -0700 | [diff] [blame] | 169 | task_queue_.PostDelayedTask( |
| 170 | std::unique_ptr<rtc::QueuedTask>( |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 171 | new InsertFrameTask(this, 1000 / framerate_fps)), |
| 172 | 1000 / framerate_fps); |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 173 | |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 174 | return true; |
| 175 | } |
| 176 | |
| 177 | void FrameGeneratorCapturer::InsertFrame() { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 178 | rtc::CritScope cs(&lock_); |
| 179 | if (sending_) { |
| 180 | VideoFrame* frame = frame_generator_->NextFrame(); |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 181 | frame->set_timestamp_us(clock_->TimeInMicroseconds()); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 182 | 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 Chapovalov | 431abd9 | 2018-06-18 12:54:17 +0200 | [diff] [blame] | 189 | absl::optional<VideoFrame> out_frame = AdaptFrame(*frame); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 190 | if (out_frame) |
| 191 | sink_->OnFrame(*out_frame); |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 192 | } |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 193 | } |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | void FrameGeneratorCapturer::Start() { |
Peter Boström | f2f8283 | 2015-05-01 13:00:41 +0200 | [diff] [blame] | 197 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 198 | sending_ = true; |
| 199 | } |
| 200 | |
| 201 | void FrameGeneratorCapturer::Stop() { |
Peter Boström | f2f8283 | 2015-05-01 13:00:41 +0200 | [diff] [blame] | 202 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 203 | sending_ = false; |
| 204 | } |
sprang | 867fb52 | 2015-08-03 04:38:41 -0700 | [diff] [blame] | 205 | |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 206 | void FrameGeneratorCapturer::ChangeResolution(size_t width, size_t height) { |
| 207 | rtc::CritScope cs(&lock_); |
| 208 | frame_generator_->ChangeResolution(width, height); |
| 209 | } |
| 210 | |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 211 | void FrameGeneratorCapturer::SetSinkWantsObserver(SinkWantsObserver* observer) { |
| 212 | rtc::CritScope cs(&lock_); |
| 213 | RTC_DCHECK(!sink_wants_observer_); |
| 214 | sink_wants_observer_ = observer; |
| 215 | } |
| 216 | |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 217 | void FrameGeneratorCapturer::AddOrUpdateSink( |
| 218 | rtc::VideoSinkInterface<VideoFrame>* sink, |
| 219 | const rtc::VideoSinkWants& wants) { |
| 220 | rtc::CritScope cs(&lock_); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 221 | RTC_CHECK(!sink_ || sink_ == sink); |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 222 | sink_ = sink; |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 223 | if (sink_wants_observer_) |
| 224 | sink_wants_observer_->OnSinkWantsChanged(sink, wants); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 225 | |
| 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 | } |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | void FrameGeneratorCapturer::RemoveSink( |
| 241 | rtc::VideoSinkInterface<VideoFrame>* sink) { |
| 242 | rtc::CritScope cs(&lock_); |
| 243 | RTC_CHECK(sink_ == sink); |
| 244 | sink_ = nullptr; |
| 245 | } |
| 246 | |
sprang | 867fb52 | 2015-08-03 04:38:41 -0700 | [diff] [blame] | 247 | void FrameGeneratorCapturer::ForceFrame() { |
ilnik | baded15 | 2017-03-17 05:55:25 -0700 | [diff] [blame] | 248 | // 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))); |
sprang | 867fb52 | 2015-08-03 04:38:41 -0700 | [diff] [blame] | 252 | } |
ilnik | baded15 | 2017-03-17 05:55:25 -0700 | [diff] [blame] | 253 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 254 | int 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 | |
ilnik | baded15 | 2017-03-17 05:55:25 -0700 | [diff] [blame] | 261 | } // namespace test |
| 262 | } // namespace webrtc |