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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #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.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 24 | |
| 25 | namespace webrtc { |
| 26 | namespace test { |
| 27 | |
ilnik | baded15 | 2017-03-17 05:55:25 -0700 | [diff] [blame] | 28 | class 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 { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 40 | bool task_completed = true; |
ilnik | baded15 | 2017-03-17 05:55:25 -0700 | [diff] [blame] | 41 | if (repeat_interval_ms_ > 0) { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 42 | // 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. |
lliuu | f9ed235 | 2017-03-30 10:44:38 -0700 | [diff] [blame] | 48 | rtc::TaskQueue::Current()->PostDelayedTask( |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 49 | std::unique_ptr<rtc::QueuedTask>(new InsertFrameTask( |
| 50 | frame_generator_capturer_, current_interval_ms)), |
| 51 | current_interval_ms); |
lliuu | f9ed235 | 2017-03-30 10:44:38 -0700 | [diff] [blame] | 52 | } else { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 53 | // 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 Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 71 | RTC_LOG(LS_ERROR) |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 72 | << "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; |
ilnik | baded15 | 2017-03-17 05:55:25 -0700 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | frame_generator_capturer_->InsertFrame(); |
| 79 | // Task should be deleted only if it's not repeating. |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 80 | return task_completed; |
ilnik | baded15 | 2017-03-17 05:55:25 -0700 | [diff] [blame] | 81 | } |
| 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 | |
perkj | a8ba195 | 2017-02-27 06:52:10 -0800 | [diff] [blame] | 88 | FrameGeneratorCapturer* FrameGeneratorCapturer::Create(int width, |
| 89 | int height, |
Peter Boström | 4b91bd0 | 2015-06-26 06:58:16 +0200 | [diff] [blame] | 90 | int target_fps, |
| 91 | Clock* clock) { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 92 | std::unique_ptr<FrameGeneratorCapturer> capturer(new FrameGeneratorCapturer( |
| 93 | clock, FrameGenerator::CreateSquareGenerator(width, height), target_fps)); |
| 94 | if (!capturer->Init()) |
| 95 | return nullptr; |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 96 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 97 | return capturer.release(); |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 98 | } |
| 99 | |
erikvarga@webrtc.org | c774d5d | 2017-10-10 14:34:38 +0200 | [diff] [blame] | 100 | FrameGeneratorCapturer* 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.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 114 | FrameGeneratorCapturer* FrameGeneratorCapturer::CreateFromYuvFile( |
sprang@webrtc.org | 131bea8 | 2015-02-18 12:46:06 +0000 | [diff] [blame] | 115 | const std::string& file_name, |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 116 | size_t width, |
| 117 | size_t height, |
| 118 | int target_fps, |
| 119 | Clock* clock) { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 120 | 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.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 127 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 128 | return capturer.release(); |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 129 | } |
| 130 | |
erikvarga | 579de6f | 2017-08-29 09:12:57 -0700 | [diff] [blame] | 131 | FrameGeneratorCapturer* 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 | |
perkj | a8ba195 | 2017-02-27 06:52:10 -0800 | [diff] [blame] | 147 | FrameGeneratorCapturer::FrameGeneratorCapturer( |
| 148 | Clock* clock, |
| 149 | std::unique_ptr<FrameGenerator> frame_generator, |
| 150 | int target_fps) |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 151 | : clock_(clock), |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 152 | sending_(false), |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 153 | sink_(nullptr), |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 154 | sink_wants_observer_(nullptr), |
perkj | a8ba195 | 2017-02-27 06:52:10 -0800 | [diff] [blame] | 155 | frame_generator_(std::move(frame_generator)), |
wu@webrtc.org | cd70119 | 2014-04-24 22:10:24 +0000 | [diff] [blame] | 156 | target_fps_(target_fps), |
ilnik | baded15 | 2017-03-17 05:55:25 -0700 | [diff] [blame] | 157 | first_frame_capture_time_(-1), |
| 158 | task_queue_("FrameGenCapQ", |
| 159 | rtc::TaskQueue::Priority::HIGH) { |
perkj | a8ba195 | 2017-02-27 06:52:10 -0800 | [diff] [blame] | 160 | RTC_DCHECK(frame_generator_); |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 161 | RTC_DCHECK_GT(target_fps, 0); |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | FrameGeneratorCapturer::~FrameGeneratorCapturer() { |
| 165 | Stop(); |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Per | ba7dc72 | 2016-04-19 15:01:23 +0200 | [diff] [blame] | 168 | void FrameGeneratorCapturer::SetFakeRotation(VideoRotation rotation) { |
| 169 | rtc::CritScope cs(&lock_); |
| 170 | fake_rotation_ = rotation; |
| 171 | } |
| 172 | |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 173 | bool FrameGeneratorCapturer::Init() { |
pbos@webrtc.org | 9401524 | 2013-10-16 11:05:37 +0000 | [diff] [blame] | 174 | // This check is added because frame_generator_ might be file based and should |
| 175 | // not crash because a file moved. |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 176 | if (frame_generator_.get() == nullptr) |
pbos@webrtc.org | 9401524 | 2013-10-16 11:05:37 +0000 | [diff] [blame] | 177 | return false; |
| 178 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 179 | int framerate_fps = GetCurrentConfiguredFramerate(); |
ilnik | baded15 | 2017-03-17 05:55:25 -0700 | [diff] [blame] | 180 | task_queue_.PostDelayedTask( |
| 181 | std::unique_ptr<rtc::QueuedTask>( |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 182 | new InsertFrameTask(this, 1000 / framerate_fps)), |
| 183 | 1000 / framerate_fps); |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 184 | |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 185 | return true; |
| 186 | } |
| 187 | |
| 188 | void FrameGeneratorCapturer::InsertFrame() { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 189 | rtc::CritScope cs(&lock_); |
| 190 | if (sending_) { |
| 191 | VideoFrame* frame = frame_generator_->NextFrame(); |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 192 | frame->set_timestamp_us(clock_->TimeInMicroseconds()); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 193 | 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.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 203 | } |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 204 | } |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | void FrameGeneratorCapturer::Start() { |
Peter Boström | f2f8283 | 2015-05-01 13:00:41 +0200 | [diff] [blame] | 208 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 209 | sending_ = true; |
| 210 | } |
| 211 | |
| 212 | void FrameGeneratorCapturer::Stop() { |
Peter Boström | f2f8283 | 2015-05-01 13:00:41 +0200 | [diff] [blame] | 213 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | 26d1210 | 2013-05-29 13:41:03 +0000 | [diff] [blame] | 214 | sending_ = false; |
| 215 | } |
sprang | 867fb52 | 2015-08-03 04:38:41 -0700 | [diff] [blame] | 216 | |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 217 | void FrameGeneratorCapturer::ChangeResolution(size_t width, size_t height) { |
| 218 | rtc::CritScope cs(&lock_); |
| 219 | frame_generator_->ChangeResolution(width, height); |
| 220 | } |
| 221 | |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 222 | void FrameGeneratorCapturer::SetSinkWantsObserver(SinkWantsObserver* observer) { |
| 223 | rtc::CritScope cs(&lock_); |
| 224 | RTC_DCHECK(!sink_wants_observer_); |
| 225 | sink_wants_observer_ = observer; |
| 226 | } |
| 227 | |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 228 | void FrameGeneratorCapturer::AddOrUpdateSink( |
| 229 | rtc::VideoSinkInterface<VideoFrame>* sink, |
| 230 | const rtc::VideoSinkWants& wants) { |
| 231 | rtc::CritScope cs(&lock_); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 232 | RTC_CHECK(!sink_ || sink_ == sink); |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 233 | sink_ = sink; |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 234 | if (sink_wants_observer_) |
| 235 | sink_wants_observer_->OnSinkWantsChanged(sink, wants); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 236 | |
| 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 | } |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | void FrameGeneratorCapturer::RemoveSink( |
| 252 | rtc::VideoSinkInterface<VideoFrame>* sink) { |
| 253 | rtc::CritScope cs(&lock_); |
| 254 | RTC_CHECK(sink_ == sink); |
| 255 | sink_ = nullptr; |
| 256 | } |
| 257 | |
sprang | 867fb52 | 2015-08-03 04:38:41 -0700 | [diff] [blame] | 258 | void FrameGeneratorCapturer::ForceFrame() { |
ilnik | baded15 | 2017-03-17 05:55:25 -0700 | [diff] [blame] | 259 | // 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))); |
sprang | 867fb52 | 2015-08-03 04:38:41 -0700 | [diff] [blame] | 263 | } |
ilnik | baded15 | 2017-03-17 05:55:25 -0700 | [diff] [blame] | 264 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 265 | int 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 | |
ilnik | baded15 | 2017-03-17 05:55:25 -0700 | [diff] [blame] | 272 | } // namespace test |
| 273 | } // namespace webrtc |