blob: 281102d1b2b311a850c2272fe9da7faff08afe6a [file] [log] [blame]
andresp@webrtc.orgab654952013-09-19 12:14: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 */
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020010#include "test/frame_generator.h"
andresp@webrtc.orgab654952013-09-19 12:14:03 +000011
pbos@webrtc.org266c7b32013-10-15 09:15:47 +000012#include <string.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020013
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <cstdint>
15#include <cstdio>
kwibergbfefb032016-05-01 14:53:46 -070016#include <memory>
17
Mirko Bonadeid9708072019-01-25 20:26:48 +010018#include "api/scoped_refptr.h"
Emircan Uysaler0823eec2018-07-13 17:10:00 -070019#include "api/video/i010_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "api/video/i420_buffer.h"
Yves Gerey3e707812018-11-28 16:47:49 +010021#include "api/video/video_frame_buffer.h"
22#include "api/video/video_rotation.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "common_video/include/video_frame_buffer.h"
24#include "common_video/libyuv/include/webrtc_libyuv.h"
Yves Gerey3e707812018-11-28 16:47:49 +010025#include "rtc_base/bind.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "rtc_base/checks.h"
27#include "rtc_base/keep_ref_until_done.h"
28#include "rtc_base/random.h"
29#include "system_wrappers/include/clock.h"
30#include "test/frame_utils.h"
andresp@webrtc.orgab654952013-09-19 12:14:03 +000031
32namespace webrtc {
33namespace test {
34namespace {
35
Emircan Uysaler03e6ec92018-03-09 15:03:26 -080036// Helper method for keeping a reference to passed pointers.
37void KeepBufferRefs(rtc::scoped_refptr<webrtc::VideoFrameBuffer>,
38 rtc::scoped_refptr<webrtc::VideoFrameBuffer>) {}
39
erikvarga@webrtc.orgc774d5d2017-10-10 14:34:38 +020040// SquareGenerator is a FrameGenerator that draws a given amount of randomly
41// sized and colored squares. Between each new generated frame, the squares
42// are moved slightly towards the lower right corner.
perkja8ba1952017-02-27 06:52:10 -080043class SquareGenerator : public FrameGenerator {
pbos@webrtc.org266c7b32013-10-15 09:15:47 +000044 public:
Emircan Uysaler03e6ec92018-03-09 15:03:26 -080045 SquareGenerator(int width, int height, OutputType type, int num_squares)
46 : type_(type) {
perkjfa10b552016-10-02 23:45:26 -070047 ChangeResolution(width, height);
erikvarga@webrtc.orgc774d5d2017-10-10 14:34:38 +020048 for (int i = 0; i < num_squares; ++i) {
perkja8ba1952017-02-27 06:52:10 -080049 squares_.emplace_back(new Square(width, height, i + 1));
50 }
perkjfa10b552016-10-02 23:45:26 -070051 }
52
53 void ChangeResolution(size_t width, size_t height) override {
54 rtc::CritScope lock(&crit_);
perkja8ba1952017-02-27 06:52:10 -080055 width_ = static_cast<int>(width);
56 height_ = static_cast<int>(height);
nisse1996e3f2016-09-19 00:34:46 -070057 RTC_CHECK(width_ > 0);
58 RTC_CHECK(height_ > 0);
pbos@webrtc.org266c7b32013-10-15 09:15:47 +000059 }
60
Emircan Uysaler03e6ec92018-03-09 15:03:26 -080061 rtc::scoped_refptr<I420Buffer> CreateI420Buffer(int width, int height) {
62 rtc::scoped_refptr<I420Buffer> buffer(I420Buffer::Create(width, height));
63 memset(buffer->MutableDataY(), 127, height * buffer->StrideY());
Magnus Jedvert90e31902017-06-07 11:32:50 +020064 memset(buffer->MutableDataU(), 127,
65 buffer->ChromaHeight() * buffer->StrideU());
66 memset(buffer->MutableDataV(), 127,
67 buffer->ChromaHeight() * buffer->StrideV());
Emircan Uysaler03e6ec92018-03-09 15:03:26 -080068 return buffer;
69 }
70
71 VideoFrame* NextFrame() override {
72 rtc::CritScope lock(&crit_);
73
74 rtc::scoped_refptr<VideoFrameBuffer> buffer = nullptr;
75 switch (type_) {
Sebastian Janssoned0febf2019-07-26 15:58:11 +020076 case OutputType::kI420:
77 case OutputType::kI010: {
Emircan Uysaler03e6ec92018-03-09 15:03:26 -080078 buffer = CreateI420Buffer(width_, height_);
79 break;
80 }
Sebastian Janssoned0febf2019-07-26 15:58:11 +020081 case OutputType::kI420A: {
Emircan Uysaler03e6ec92018-03-09 15:03:26 -080082 rtc::scoped_refptr<I420Buffer> yuv_buffer =
83 CreateI420Buffer(width_, height_);
84 rtc::scoped_refptr<I420Buffer> axx_buffer =
85 CreateI420Buffer(width_, height_);
86 buffer = WrapI420ABuffer(
87 yuv_buffer->width(), yuv_buffer->height(), yuv_buffer->DataY(),
88 yuv_buffer->StrideY(), yuv_buffer->DataU(), yuv_buffer->StrideU(),
89 yuv_buffer->DataV(), yuv_buffer->StrideV(), axx_buffer->DataY(),
90 axx_buffer->StrideY(),
91 rtc::Bind(&KeepBufferRefs, yuv_buffer, axx_buffer));
92 break;
93 }
Sebastian Janssoned0febf2019-07-26 15:58:11 +020094 default:
95 RTC_NOTREACHED() << "The given output format is not supported.";
Emircan Uysaler03e6ec92018-03-09 15:03:26 -080096 }
nisse1996e3f2016-09-19 00:34:46 -070097
perkja8ba1952017-02-27 06:52:10 -080098 for (const auto& square : squares_)
99 square->Draw(buffer);
nisse1996e3f2016-09-19 00:34:46 -0700100
Sebastian Janssoned0febf2019-07-26 15:58:11 +0200101 if (type_ == OutputType::kI010) {
Emircan Uysaler0823eec2018-07-13 17:10:00 -0700102 buffer = I010Buffer::Copy(*buffer->ToI420());
103 }
104
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200105 frame_ =
106 std::make_unique<VideoFrame>(VideoFrame::Builder()
107 .set_video_frame_buffer(buffer)
108 .set_rotation(webrtc::kVideoRotation_0)
109 .set_timestamp_us(0)
110 .build());
nisse1996e3f2016-09-19 00:34:46 -0700111 return frame_.get();
pbos@webrtc.org266c7b32013-10-15 09:15:47 +0000112 }
113
114 private:
perkja8ba1952017-02-27 06:52:10 -0800115 class Square {
116 public:
117 Square(int width, int height, int seed)
118 : random_generator_(seed),
119 x_(random_generator_.Rand(0, width)),
120 y_(random_generator_.Rand(0, height)),
121 length_(random_generator_.Rand(1, width > 4 ? width / 4 : 1)),
122 yuv_y_(random_generator_.Rand(0, 255)),
123 yuv_u_(random_generator_.Rand(0, 255)),
Emircan Uysaler03e6ec92018-03-09 15:03:26 -0800124 yuv_v_(random_generator_.Rand(0, 255)),
125 yuv_a_(random_generator_.Rand(0, 255)) {}
perkja8ba1952017-02-27 06:52:10 -0800126
Emircan Uysaler03e6ec92018-03-09 15:03:26 -0800127 void Draw(const rtc::scoped_refptr<VideoFrameBuffer>& frame_buffer) {
128 RTC_DCHECK(frame_buffer->type() == VideoFrameBuffer::Type::kI420 ||
129 frame_buffer->type() == VideoFrameBuffer::Type::kI420A);
130 rtc::scoped_refptr<I420BufferInterface> buffer = frame_buffer->ToI420();
perkja8ba1952017-02-27 06:52:10 -0800131 x_ = (x_ + random_generator_.Rand(0, 4)) % (buffer->width() - length_);
132 y_ = (y_ + random_generator_.Rand(0, 4)) % (buffer->height() - length_);
Emircan Uysaler03e6ec92018-03-09 15:03:26 -0800133 for (int y = y_; y < y_ + length_; ++y) {
134 uint8_t* pos_y = (const_cast<uint8_t*>(buffer->DataY()) + x_ +
135 y * buffer->StrideY());
136 memset(pos_y, yuv_y_, length_);
137 }
perkjc8b08652017-03-22 04:57:53 -0700138
Emircan Uysaler03e6ec92018-03-09 15:03:26 -0800139 for (int y = y_; y < y_ + length_; y = y + 2) {
140 uint8_t* pos_u = (const_cast<uint8_t*>(buffer->DataU()) + x_ / 2 +
141 y / 2 * buffer->StrideU());
142 memset(pos_u, yuv_u_, length_ / 2);
143 uint8_t* pos_v = (const_cast<uint8_t*>(buffer->DataV()) + x_ / 2 +
144 y / 2 * buffer->StrideV());
145 memset(pos_v, yuv_v_, length_ / 2);
146 }
147
148 if (frame_buffer->type() == VideoFrameBuffer::Type::kI420)
149 return;
150
151 // Optionally draw on alpha plane if given.
152 const webrtc::I420ABufferInterface* yuva_buffer =
153 frame_buffer->GetI420A();
154 for (int y = y_; y < y_ + length_; ++y) {
155 uint8_t* pos_y = (const_cast<uint8_t*>(yuva_buffer->DataA()) + x_ +
156 y * yuva_buffer->StrideA());
157 memset(pos_y, yuv_a_, length_);
158 }
perkja8ba1952017-02-27 06:52:10 -0800159 }
160
161 private:
162 Random random_generator_;
163 int x_;
164 int y_;
165 const int length_;
166 const uint8_t yuv_y_;
167 const uint8_t yuv_u_;
168 const uint8_t yuv_v_;
Emircan Uysaler03e6ec92018-03-09 15:03:26 -0800169 const uint8_t yuv_a_;
perkja8ba1952017-02-27 06:52:10 -0800170 };
171
perkjfa10b552016-10-02 23:45:26 -0700172 rtc::CriticalSection crit_;
Emircan Uysaler03e6ec92018-03-09 15:03:26 -0800173 const OutputType type_;
danilchapa37de392017-09-09 04:17:22 -0700174 int width_ RTC_GUARDED_BY(&crit_);
175 int height_ RTC_GUARDED_BY(&crit_);
176 std::vector<std::unique_ptr<Square>> squares_ RTC_GUARDED_BY(&crit_);
177 std::unique_ptr<VideoFrame> frame_ RTC_GUARDED_BY(&crit_);
pbos@webrtc.org266c7b32013-10-15 09:15:47 +0000178};
179
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000180class YuvFileGenerator : public FrameGenerator {
181 public:
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000182 YuvFileGenerator(std::vector<FILE*> files,
183 size_t width,
184 size_t height,
185 int frame_repeat_count)
186 : file_index_(0),
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100187 frame_index_(std::numeric_limits<size_t>::max()),
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000188 files_(files),
189 width_(width),
190 height_(height),
nisseeb44b392017-04-28 07:18:05 -0700191 frame_size_(CalcBufferSize(VideoType::kI420,
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000192 static_cast<int>(width_),
193 static_cast<int>(height_))),
194 frame_buffer_(new uint8_t[frame_size_]),
195 frame_display_count_(frame_repeat_count),
196 current_display_count_(0) {
kwibergb890c95c2016-11-29 05:30:40 -0800197 RTC_DCHECK_GT(width, 0);
198 RTC_DCHECK_GT(height, 0);
199 RTC_DCHECK_GT(frame_repeat_count, 0);
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000200 }
201
Mirko Bonadeife055c12019-01-29 22:53:28 +0100202 ~YuvFileGenerator() override {
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000203 for (FILE* file : files_)
204 fclose(file);
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000205 }
206
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700207 VideoFrame* NextFrame() override {
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100208 // Empty update by default.
209 VideoFrame::UpdateRect update_rect{0, 0, 0, 0};
210 if (current_display_count_ == 0) {
211 const bool got_new_frame = ReadNextFrame();
212 // Full update on a new frame from file.
213 if (got_new_frame) {
214 update_rect = VideoFrame::UpdateRect{0, 0, static_cast<int>(width_),
215 static_cast<int>(height_)};
216 }
217 }
sprang@webrtc.org25dd1db2015-03-02 11:55:45 +0000218 if (++current_display_count_ >= frame_display_count_)
219 current_display_count_ = 0;
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000220
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200221 temp_frame_ = std::make_unique<VideoFrame>(
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100222 VideoFrame::Builder()
223 .set_video_frame_buffer(last_read_buffer_)
224 .set_rotation(webrtc::kVideoRotation_0)
225 .set_timestamp_us(0)
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100226 .set_update_rect(update_rect)
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100227 .build());
nisse1996e3f2016-09-19 00:34:46 -0700228 return temp_frame_.get();
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000229 }
pbos@webrtc.org724947b2013-12-11 16:26:16 +0000230
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100231 // Returns true if the new frame was loaded.
232 // False only in case of a single file with a single frame in it.
233 bool ReadNextFrame() {
234 size_t prev_frame_index = frame_index_;
235 size_t prev_file_index = file_index_;
nisse115bd152016-09-30 04:14:07 -0700236 last_read_buffer_ =
237 test::ReadI420Buffer(static_cast<int>(width_),
Jonas Olssona4d87372019-07-05 19:08:33 +0200238 static_cast<int>(height_), files_[file_index_]);
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100239 ++frame_index_;
nisse115bd152016-09-30 04:14:07 -0700240 if (!last_read_buffer_) {
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000241 // No more frames to read in this file, rewind and move to next file.
242 rewind(files_[file_index_]);
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100243
244 frame_index_ = 0;
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000245 file_index_ = (file_index_ + 1) % files_.size();
nisse115bd152016-09-30 04:14:07 -0700246 last_read_buffer_ =
247 test::ReadI420Buffer(static_cast<int>(width_),
Jonas Olssona4d87372019-07-05 19:08:33 +0200248 static_cast<int>(height_), files_[file_index_]);
nisse115bd152016-09-30 04:14:07 -0700249 RTC_CHECK(last_read_buffer_);
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000250 }
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100251 return frame_index_ != prev_frame_index || file_index_ != prev_file_index;
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000252 }
253
254 private:
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000255 size_t file_index_;
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100256 size_t frame_index_;
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000257 const std::vector<FILE*> files_;
258 const size_t width_;
259 const size_t height_;
260 const size_t frame_size_;
kwibergbfefb032016-05-01 14:53:46 -0700261 const std::unique_ptr<uint8_t[]> frame_buffer_;
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000262 const int frame_display_count_;
263 int current_display_count_;
nisse1996e3f2016-09-19 00:34:46 -0700264 rtc::scoped_refptr<I420Buffer> last_read_buffer_;
265 std::unique_ptr<VideoFrame> temp_frame_;
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000266};
sprangd6358952015-07-29 07:58:13 -0700267
erikvarga579de6f2017-08-29 09:12:57 -0700268// SlideGenerator works similarly to YuvFileGenerator but it fills the frames
269// with randomly sized and colored squares instead of reading their content
270// from files.
271class SlideGenerator : public FrameGenerator {
272 public:
273 SlideGenerator(int width, int height, int frame_repeat_count)
274 : width_(width),
275 height_(height),
276 frame_display_count_(frame_repeat_count),
277 current_display_count_(0),
278 random_generator_(1234) {
279 RTC_DCHECK_GT(width, 0);
280 RTC_DCHECK_GT(height, 0);
281 RTC_DCHECK_GT(frame_repeat_count, 0);
282 }
283
284 VideoFrame* NextFrame() override {
285 if (current_display_count_ == 0)
286 GenerateNewFrame();
287 if (++current_display_count_ >= frame_display_count_)
288 current_display_count_ = 0;
289
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200290 frame_ =
291 std::make_unique<VideoFrame>(VideoFrame::Builder()
292 .set_video_frame_buffer(buffer_)
293 .set_rotation(webrtc::kVideoRotation_0)
294 .set_timestamp_us(0)
295 .build());
erikvarga579de6f2017-08-29 09:12:57 -0700296 return frame_.get();
297 }
298
299 // Generates some randomly sized and colored squares scattered
300 // over the frame.
301 void GenerateNewFrame() {
302 // The squares should have a varying order of magnitude in order
303 // to simulate variation in the slides' complexity.
Jonas Olssona4d87372019-07-05 19:08:33 +0200304 const int kSquareNum = 1 << (4 + (random_generator_.Rand(0, 3) * 2));
erikvarga579de6f2017-08-29 09:12:57 -0700305
306 buffer_ = I420Buffer::Create(width_, height_);
307 memset(buffer_->MutableDataY(), 127, height_ * buffer_->StrideY());
308 memset(buffer_->MutableDataU(), 127,
309 buffer_->ChromaHeight() * buffer_->StrideU());
310 memset(buffer_->MutableDataV(), 127,
311 buffer_->ChromaHeight() * buffer_->StrideV());
312
313 for (int i = 0; i < kSquareNum; ++i) {
314 int length = random_generator_.Rand(1, width_ > 4 ? width_ / 4 : 1);
315 // Limit the length of later squares so that they don't overwrite the
316 // previous ones too much.
317 length = (length * (kSquareNum - i)) / kSquareNum;
318
319 int x = random_generator_.Rand(0, width_ - length);
320 int y = random_generator_.Rand(0, height_ - length);
321 uint8_t yuv_y = random_generator_.Rand(0, 255);
322 uint8_t yuv_u = random_generator_.Rand(0, 255);
323 uint8_t yuv_v = random_generator_.Rand(0, 255);
324
325 for (int yy = y; yy < y + length; ++yy) {
326 uint8_t* pos_y =
327 (buffer_->MutableDataY() + x + yy * buffer_->StrideY());
328 memset(pos_y, yuv_y, length);
329 }
330 for (int yy = y; yy < y + length; yy += 2) {
331 uint8_t* pos_u =
332 (buffer_->MutableDataU() + x / 2 + yy / 2 * buffer_->StrideU());
333 memset(pos_u, yuv_u, length / 2);
334 uint8_t* pos_v =
335 (buffer_->MutableDataV() + x / 2 + yy / 2 * buffer_->StrideV());
336 memset(pos_v, yuv_v, length / 2);
337 }
338 }
339 }
340
341 private:
342 const int width_;
343 const int height_;
344 const int frame_display_count_;
345 int current_display_count_;
346 Random random_generator_;
347 rtc::scoped_refptr<I420Buffer> buffer_;
348 std::unique_ptr<VideoFrame> frame_;
349};
350
sprangd6358952015-07-29 07:58:13 -0700351class ScrollingImageFrameGenerator : public FrameGenerator {
352 public:
353 ScrollingImageFrameGenerator(Clock* clock,
354 const std::vector<FILE*>& files,
355 size_t source_width,
356 size_t source_height,
357 size_t target_width,
358 size_t target_height,
359 int64_t scroll_time_ms,
360 int64_t pause_time_ms)
361 : clock_(clock),
362 start_time_(clock->TimeInMilliseconds()),
363 scroll_time_(scroll_time_ms),
364 pause_time_(pause_time_ms),
365 num_frames_(files.size()),
nissef122a852016-10-04 23:27:30 -0700366 target_width_(static_cast<int>(target_width)),
367 target_height_(static_cast<int>(target_height)),
sprangd6358952015-07-29 07:58:13 -0700368 current_frame_num_(num_frames_ - 1),
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100369 prev_frame_not_scrolled_(false),
sprangd6358952015-07-29 07:58:13 -0700370 current_source_frame_(nullptr),
371 file_generator_(files, source_width, source_height, 1) {
henrikg91d6ede2015-09-17 00:24:34 -0700372 RTC_DCHECK(clock_ != nullptr);
kwibergaf476c72016-11-28 15:21:39 -0800373 RTC_DCHECK_GT(num_frames_, 0);
henrikg91d6ede2015-09-17 00:24:34 -0700374 RTC_DCHECK_GE(source_height, target_height);
375 RTC_DCHECK_GE(source_width, target_width);
376 RTC_DCHECK_GE(scroll_time_ms, 0);
377 RTC_DCHECK_GE(pause_time_ms, 0);
378 RTC_DCHECK_GT(scroll_time_ms + pause_time_ms, 0);
sprangd6358952015-07-29 07:58:13 -0700379 }
380
Mirko Bonadeife055c12019-01-29 22:53:28 +0100381 ~ScrollingImageFrameGenerator() override {}
sprangd6358952015-07-29 07:58:13 -0700382
383 VideoFrame* NextFrame() override {
384 const int64_t kFrameDisplayTime = scroll_time_ + pause_time_;
385 const int64_t now = clock_->TimeInMilliseconds();
386 int64_t ms_since_start = now - start_time_;
387
388 size_t frame_num = (ms_since_start / kFrameDisplayTime) % num_frames_;
389 UpdateSourceFrame(frame_num);
390
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100391 bool cur_frame_not_scrolled;
392
sprangd6358952015-07-29 07:58:13 -0700393 double scroll_factor;
394 int64_t time_into_frame = ms_since_start % kFrameDisplayTime;
395 if (time_into_frame < scroll_time_) {
396 scroll_factor = static_cast<double>(time_into_frame) / scroll_time_;
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100397 cur_frame_not_scrolled = false;
sprangd6358952015-07-29 07:58:13 -0700398 } else {
399 scroll_factor = 1.0;
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100400 cur_frame_not_scrolled = true;
sprangd6358952015-07-29 07:58:13 -0700401 }
402 CropSourceToScrolledImage(scroll_factor);
403
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100404 bool same_scroll_position =
405 prev_frame_not_scrolled_ && cur_frame_not_scrolled;
406 if (!same_scroll_position && current_frame_) {
407 // If scrolling is not finished yet, force full frame update.
408 current_frame_->set_update_rect(
409 VideoFrame::UpdateRect{0, 0, target_width_, target_height_});
410 }
411 prev_frame_not_scrolled_ = cur_frame_not_scrolled;
412
nissedf2ceb82016-12-15 06:29:53 -0800413 return current_frame_ ? &*current_frame_ : nullptr;
sprangd6358952015-07-29 07:58:13 -0700414 }
415
416 void UpdateSourceFrame(size_t frame_num) {
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100417 VideoFrame::UpdateRect acc_update{0, 0, 0, 0};
Ilya Nikolaevskiy6cfb4032019-02-06 10:56:39 +0100418 while (current_frame_num_ != frame_num ||
419 current_source_frame_ == nullptr) {
sprangd6358952015-07-29 07:58:13 -0700420 current_source_frame_ = file_generator_.NextFrame();
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100421 if (current_source_frame_)
422 acc_update.Union(current_source_frame_->update_rect());
sprangd6358952015-07-29 07:58:13 -0700423 current_frame_num_ = (current_frame_num_ + 1) % num_frames_;
424 }
henrikg91d6ede2015-09-17 00:24:34 -0700425 RTC_DCHECK(current_source_frame_ != nullptr);
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100426 current_source_frame_->set_update_rect(acc_update);
sprangd6358952015-07-29 07:58:13 -0700427 }
428
429 void CropSourceToScrolledImage(double scroll_factor) {
nissef122a852016-10-04 23:27:30 -0700430 int scroll_margin_x = current_source_frame_->width() - target_width_;
sprangd6358952015-07-29 07:58:13 -0700431 int pixels_scrolled_x =
432 static_cast<int>(scroll_margin_x * scroll_factor + 0.5);
nissef122a852016-10-04 23:27:30 -0700433 int scroll_margin_y = current_source_frame_->height() - target_height_;
sprangd6358952015-07-29 07:58:13 -0700434 int pixels_scrolled_y =
435 static_cast<int>(scroll_margin_y * scroll_factor + 0.5);
436
Magnus Jedvert90e31902017-06-07 11:32:50 +0200437 rtc::scoped_refptr<I420BufferInterface> i420_buffer =
438 current_source_frame_->video_frame_buffer()->ToI420();
439 int offset_y =
440 (i420_buffer->StrideY() * pixels_scrolled_y) + pixels_scrolled_x;
441 int offset_u = (i420_buffer->StrideU() * (pixels_scrolled_y / 2)) +
sprangd6358952015-07-29 07:58:13 -0700442 (pixels_scrolled_x / 2);
Magnus Jedvert90e31902017-06-07 11:32:50 +0200443 int offset_v = (i420_buffer->StrideV() * (pixels_scrolled_y / 2)) +
sprangd6358952015-07-29 07:58:13 -0700444 (pixels_scrolled_x / 2);
445
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100446 VideoFrame::UpdateRect update_rect =
447 current_source_frame_->update_rect().IsEmpty()
448 ? VideoFrame::UpdateRect{0, 0, 0, 0}
449 : VideoFrame::UpdateRect{0, 0, target_width_, target_height_};
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100450 current_frame_ =
451 VideoFrame::Builder()
452 .set_video_frame_buffer(WrapI420Buffer(
453 target_width_, target_height_, &i420_buffer->DataY()[offset_y],
454 i420_buffer->StrideY(), &i420_buffer->DataU()[offset_u],
455 i420_buffer->StrideU(), &i420_buffer->DataV()[offset_v],
456 i420_buffer->StrideV(), KeepRefUntilDone(i420_buffer)))
457 .set_rotation(kVideoRotation_0)
458 .set_timestamp_us(0)
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100459 .set_update_rect(update_rect)
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100460 .build();
sprangd6358952015-07-29 07:58:13 -0700461 }
462
463 Clock* const clock_;
464 const int64_t start_time_;
465 const int64_t scroll_time_;
466 const int64_t pause_time_;
467 const size_t num_frames_;
nissef122a852016-10-04 23:27:30 -0700468 const int target_width_;
469 const int target_height_;
470
sprangd6358952015-07-29 07:58:13 -0700471 size_t current_frame_num_;
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100472 bool prev_frame_not_scrolled_;
sprangd6358952015-07-29 07:58:13 -0700473 VideoFrame* current_source_frame_;
Danil Chapovalov431abd92018-06-18 12:54:17 +0200474 absl::optional<VideoFrame> current_frame_;
sprangd6358952015-07-29 07:58:13 -0700475 YuvFileGenerator file_generator_;
476};
477
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000478} // namespace
479
perkja49cbd32016-09-16 07:53:41 -0700480FrameForwarder::FrameForwarder() : sink_(nullptr) {}
sprangb1ca0732017-02-01 08:38:12 -0800481FrameForwarder::~FrameForwarder() {}
perkja49cbd32016-09-16 07:53:41 -0700482
483void FrameForwarder::IncomingCapturedFrame(const VideoFrame& video_frame) {
484 rtc::CritScope lock(&crit_);
485 if (sink_)
486 sink_->OnFrame(video_frame);
487}
488
489void FrameForwarder::AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
490 const rtc::VideoSinkWants& wants) {
491 rtc::CritScope lock(&crit_);
492 RTC_DCHECK(!sink_ || sink_ == sink);
493 sink_ = sink;
perkj803d97f2016-11-01 11:45:46 -0700494 sink_wants_ = wants;
perkja49cbd32016-09-16 07:53:41 -0700495}
496
497void FrameForwarder::RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) {
498 rtc::CritScope lock(&crit_);
499 RTC_DCHECK_EQ(sink, sink_);
500 sink_ = nullptr;
501}
502
perkj803d97f2016-11-01 11:45:46 -0700503rtc::VideoSinkWants FrameForwarder::sink_wants() const {
504 rtc::CritScope lock(&crit_);
505 return sink_wants_;
506}
507
508bool FrameForwarder::has_sinks() const {
509 rtc::CritScope lock(&crit_);
510 return sink_ != nullptr;
511}
512
Artem Titovf50c6c22019-01-24 16:54:03 +0100513void FrameGenerator::ChangeResolution(size_t width, size_t height) {
514 RTC_NOTREACHED();
515}
516
perkja8ba1952017-02-27 06:52:10 -0800517std::unique_ptr<FrameGenerator> FrameGenerator::CreateSquareGenerator(
518 int width,
Emircan Uysaler03e6ec92018-03-09 15:03:26 -0800519 int height,
Danil Chapovalov431abd92018-06-18 12:54:17 +0200520 absl::optional<OutputType> type,
521 absl::optional<int> num_squares) {
erikvarga@webrtc.orgc774d5d2017-10-10 14:34:38 +0200522 return std::unique_ptr<FrameGenerator>(
Sebastian Janssoned0febf2019-07-26 15:58:11 +0200523 new SquareGenerator(width, height, type.value_or(OutputType::kI420),
Emircan Uysaler03e6ec92018-03-09 15:03:26 -0800524 num_squares.value_or(10)));
pbos@webrtc.org266c7b32013-10-15 09:15:47 +0000525}
526
erikvarga579de6f2017-08-29 09:12:57 -0700527std::unique_ptr<FrameGenerator> FrameGenerator::CreateSlideGenerator(
Jonas Olssona4d87372019-07-05 19:08:33 +0200528 int width,
529 int height,
530 int frame_repeat_count) {
531 return std::unique_ptr<FrameGenerator>(
532 new SlideGenerator(width, height, frame_repeat_count));
erikvarga579de6f2017-08-29 09:12:57 -0700533}
534
perkja8ba1952017-02-27 06:52:10 -0800535std::unique_ptr<FrameGenerator> FrameGenerator::CreateFromYuvFile(
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000536 std::vector<std::string> filenames,
537 size_t width,
538 size_t height,
539 int frame_repeat_count) {
kwibergb890c95c2016-11-29 05:30:40 -0800540 RTC_DCHECK(!filenames.empty());
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000541 std::vector<FILE*> files;
542 for (const std::string& filename : filenames) {
543 FILE* file = fopen(filename.c_str(), "rb");
Sebastian Jansson9eda3272018-09-13 16:23:03 +0200544 RTC_DCHECK(file != nullptr) << "Failed to open: '" << filename << "'\n";
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000545 files.push_back(file);
546 }
547
perkja8ba1952017-02-27 06:52:10 -0800548 return std::unique_ptr<FrameGenerator>(
549 new YuvFileGenerator(files, width, height, frame_repeat_count));
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000550}
551
perkja8ba1952017-02-27 06:52:10 -0800552std::unique_ptr<FrameGenerator>
553FrameGenerator::CreateScrollingInputFromYuvFiles(
sprangd6358952015-07-29 07:58:13 -0700554 Clock* clock,
555 std::vector<std::string> filenames,
556 size_t source_width,
557 size_t source_height,
558 size_t target_width,
559 size_t target_height,
560 int64_t scroll_time_ms,
561 int64_t pause_time_ms) {
kwibergb890c95c2016-11-29 05:30:40 -0800562 RTC_DCHECK(!filenames.empty());
sprangd6358952015-07-29 07:58:13 -0700563 std::vector<FILE*> files;
564 for (const std::string& filename : filenames) {
565 FILE* file = fopen(filename.c_str(), "rb");
henrikg91d6ede2015-09-17 00:24:34 -0700566 RTC_DCHECK(file != nullptr);
sprangd6358952015-07-29 07:58:13 -0700567 files.push_back(file);
568 }
569
perkja8ba1952017-02-27 06:52:10 -0800570 return std::unique_ptr<FrameGenerator>(new ScrollingImageFrameGenerator(
sprangd6358952015-07-29 07:58:13 -0700571 clock, files, source_width, source_height, target_width, target_height,
perkja8ba1952017-02-27 06:52:10 -0800572 scroll_time_ms, pause_time_ms));
sprangd6358952015-07-29 07:58:13 -0700573}
574
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000575} // namespace test
576} // namespace webrtc