blob: 276069ec05efeb8a9d2fc7dbc4139a0acf4456e4 [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
Artem Titov1ebfb6a2019-01-03 23:49:37 +010018#include "absl/memory/memory.h"
Mirko Bonadeid9708072019-01-25 20:26:48 +010019#include "api/scoped_refptr.h"
Emircan Uysaler0823eec2018-07-13 17:10:00 -070020#include "api/video/i010_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "api/video/i420_buffer.h"
Yves Gerey3e707812018-11-28 16:47:49 +010022#include "api/video/video_frame_buffer.h"
23#include "api/video/video_rotation.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "common_video/include/video_frame_buffer.h"
25#include "common_video/libyuv/include/webrtc_libyuv.h"
Yves Gerey3e707812018-11-28 16:47:49 +010026#include "rtc_base/bind.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "rtc_base/checks.h"
28#include "rtc_base/keep_ref_until_done.h"
29#include "rtc_base/random.h"
30#include "system_wrappers/include/clock.h"
31#include "test/frame_utils.h"
andresp@webrtc.orgab654952013-09-19 12:14:03 +000032
33namespace webrtc {
34namespace test {
35namespace {
36
Emircan Uysaler03e6ec92018-03-09 15:03:26 -080037// Helper method for keeping a reference to passed pointers.
38void KeepBufferRefs(rtc::scoped_refptr<webrtc::VideoFrameBuffer>,
39 rtc::scoped_refptr<webrtc::VideoFrameBuffer>) {}
40
erikvarga@webrtc.orgc774d5d2017-10-10 14:34:38 +020041// SquareGenerator is a FrameGenerator that draws a given amount of randomly
42// sized and colored squares. Between each new generated frame, the squares
43// are moved slightly towards the lower right corner.
perkja8ba1952017-02-27 06:52:10 -080044class SquareGenerator : public FrameGenerator {
pbos@webrtc.org266c7b32013-10-15 09:15:47 +000045 public:
Emircan Uysaler03e6ec92018-03-09 15:03:26 -080046 SquareGenerator(int width, int height, OutputType type, int num_squares)
47 : type_(type) {
perkjfa10b552016-10-02 23:45:26 -070048 ChangeResolution(width, height);
erikvarga@webrtc.orgc774d5d2017-10-10 14:34:38 +020049 for (int i = 0; i < num_squares; ++i) {
perkja8ba1952017-02-27 06:52:10 -080050 squares_.emplace_back(new Square(width, height, i + 1));
51 }
perkjfa10b552016-10-02 23:45:26 -070052 }
53
54 void ChangeResolution(size_t width, size_t height) override {
55 rtc::CritScope lock(&crit_);
perkja8ba1952017-02-27 06:52:10 -080056 width_ = static_cast<int>(width);
57 height_ = static_cast<int>(height);
nisse1996e3f2016-09-19 00:34:46 -070058 RTC_CHECK(width_ > 0);
59 RTC_CHECK(height_ > 0);
pbos@webrtc.org266c7b32013-10-15 09:15:47 +000060 }
61
Emircan Uysaler03e6ec92018-03-09 15:03:26 -080062 rtc::scoped_refptr<I420Buffer> CreateI420Buffer(int width, int height) {
63 rtc::scoped_refptr<I420Buffer> buffer(I420Buffer::Create(width, height));
64 memset(buffer->MutableDataY(), 127, height * buffer->StrideY());
Magnus Jedvert90e31902017-06-07 11:32:50 +020065 memset(buffer->MutableDataU(), 127,
66 buffer->ChromaHeight() * buffer->StrideU());
67 memset(buffer->MutableDataV(), 127,
68 buffer->ChromaHeight() * buffer->StrideV());
Emircan Uysaler03e6ec92018-03-09 15:03:26 -080069 return buffer;
70 }
71
72 VideoFrame* NextFrame() override {
73 rtc::CritScope lock(&crit_);
74
75 rtc::scoped_refptr<VideoFrameBuffer> buffer = nullptr;
76 switch (type_) {
Sebastian Janssoned0febf2019-07-26 15:58:11 +020077 case OutputType::kI420:
78 case OutputType::kI010: {
Emircan Uysaler03e6ec92018-03-09 15:03:26 -080079 buffer = CreateI420Buffer(width_, height_);
80 break;
81 }
Sebastian Janssoned0febf2019-07-26 15:58:11 +020082 case OutputType::kI420A: {
Emircan Uysaler03e6ec92018-03-09 15:03:26 -080083 rtc::scoped_refptr<I420Buffer> yuv_buffer =
84 CreateI420Buffer(width_, height_);
85 rtc::scoped_refptr<I420Buffer> axx_buffer =
86 CreateI420Buffer(width_, height_);
87 buffer = WrapI420ABuffer(
88 yuv_buffer->width(), yuv_buffer->height(), yuv_buffer->DataY(),
89 yuv_buffer->StrideY(), yuv_buffer->DataU(), yuv_buffer->StrideU(),
90 yuv_buffer->DataV(), yuv_buffer->StrideV(), axx_buffer->DataY(),
91 axx_buffer->StrideY(),
92 rtc::Bind(&KeepBufferRefs, yuv_buffer, axx_buffer));
93 break;
94 }
Sebastian Janssoned0febf2019-07-26 15:58:11 +020095 default:
96 RTC_NOTREACHED() << "The given output format is not supported.";
Emircan Uysaler03e6ec92018-03-09 15:03:26 -080097 }
nisse1996e3f2016-09-19 00:34:46 -070098
perkja8ba1952017-02-27 06:52:10 -080099 for (const auto& square : squares_)
100 square->Draw(buffer);
nisse1996e3f2016-09-19 00:34:46 -0700101
Sebastian Janssoned0febf2019-07-26 15:58:11 +0200102 if (type_ == OutputType::kI010) {
Emircan Uysaler0823eec2018-07-13 17:10:00 -0700103 buffer = I010Buffer::Copy(*buffer->ToI420());
104 }
105
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100106 frame_ = absl::make_unique<VideoFrame>(
107 VideoFrame::Builder()
108 .set_video_frame_buffer(buffer)
109 .set_rotation(webrtc::kVideoRotation_0)
110 .set_timestamp_us(0)
111 .build());
nisse1996e3f2016-09-19 00:34:46 -0700112 return frame_.get();
pbos@webrtc.org266c7b32013-10-15 09:15:47 +0000113 }
114
115 private:
perkja8ba1952017-02-27 06:52:10 -0800116 class Square {
117 public:
118 Square(int width, int height, int seed)
119 : random_generator_(seed),
120 x_(random_generator_.Rand(0, width)),
121 y_(random_generator_.Rand(0, height)),
122 length_(random_generator_.Rand(1, width > 4 ? width / 4 : 1)),
123 yuv_y_(random_generator_.Rand(0, 255)),
124 yuv_u_(random_generator_.Rand(0, 255)),
Emircan Uysaler03e6ec92018-03-09 15:03:26 -0800125 yuv_v_(random_generator_.Rand(0, 255)),
126 yuv_a_(random_generator_.Rand(0, 255)) {}
perkja8ba1952017-02-27 06:52:10 -0800127
Emircan Uysaler03e6ec92018-03-09 15:03:26 -0800128 void Draw(const rtc::scoped_refptr<VideoFrameBuffer>& frame_buffer) {
129 RTC_DCHECK(frame_buffer->type() == VideoFrameBuffer::Type::kI420 ||
130 frame_buffer->type() == VideoFrameBuffer::Type::kI420A);
131 rtc::scoped_refptr<I420BufferInterface> buffer = frame_buffer->ToI420();
perkja8ba1952017-02-27 06:52:10 -0800132 x_ = (x_ + random_generator_.Rand(0, 4)) % (buffer->width() - length_);
133 y_ = (y_ + random_generator_.Rand(0, 4)) % (buffer->height() - length_);
Emircan Uysaler03e6ec92018-03-09 15:03:26 -0800134 for (int y = y_; y < y_ + length_; ++y) {
135 uint8_t* pos_y = (const_cast<uint8_t*>(buffer->DataY()) + x_ +
136 y * buffer->StrideY());
137 memset(pos_y, yuv_y_, length_);
138 }
perkjc8b08652017-03-22 04:57:53 -0700139
Emircan Uysaler03e6ec92018-03-09 15:03:26 -0800140 for (int y = y_; y < y_ + length_; y = y + 2) {
141 uint8_t* pos_u = (const_cast<uint8_t*>(buffer->DataU()) + x_ / 2 +
142 y / 2 * buffer->StrideU());
143 memset(pos_u, yuv_u_, length_ / 2);
144 uint8_t* pos_v = (const_cast<uint8_t*>(buffer->DataV()) + x_ / 2 +
145 y / 2 * buffer->StrideV());
146 memset(pos_v, yuv_v_, length_ / 2);
147 }
148
149 if (frame_buffer->type() == VideoFrameBuffer::Type::kI420)
150 return;
151
152 // Optionally draw on alpha plane if given.
153 const webrtc::I420ABufferInterface* yuva_buffer =
154 frame_buffer->GetI420A();
155 for (int y = y_; y < y_ + length_; ++y) {
156 uint8_t* pos_y = (const_cast<uint8_t*>(yuva_buffer->DataA()) + x_ +
157 y * yuva_buffer->StrideA());
158 memset(pos_y, yuv_a_, length_);
159 }
perkja8ba1952017-02-27 06:52:10 -0800160 }
161
162 private:
163 Random random_generator_;
164 int x_;
165 int y_;
166 const int length_;
167 const uint8_t yuv_y_;
168 const uint8_t yuv_u_;
169 const uint8_t yuv_v_;
Emircan Uysaler03e6ec92018-03-09 15:03:26 -0800170 const uint8_t yuv_a_;
perkja8ba1952017-02-27 06:52:10 -0800171 };
172
perkjfa10b552016-10-02 23:45:26 -0700173 rtc::CriticalSection crit_;
Emircan Uysaler03e6ec92018-03-09 15:03:26 -0800174 const OutputType type_;
danilchapa37de392017-09-09 04:17:22 -0700175 int width_ RTC_GUARDED_BY(&crit_);
176 int height_ RTC_GUARDED_BY(&crit_);
177 std::vector<std::unique_ptr<Square>> squares_ RTC_GUARDED_BY(&crit_);
178 std::unique_ptr<VideoFrame> frame_ RTC_GUARDED_BY(&crit_);
pbos@webrtc.org266c7b32013-10-15 09:15:47 +0000179};
180
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000181class YuvFileGenerator : public FrameGenerator {
182 public:
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000183 YuvFileGenerator(std::vector<FILE*> files,
184 size_t width,
185 size_t height,
186 int frame_repeat_count)
187 : file_index_(0),
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100188 frame_index_(std::numeric_limits<size_t>::max()),
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000189 files_(files),
190 width_(width),
191 height_(height),
nisseeb44b392017-04-28 07:18:05 -0700192 frame_size_(CalcBufferSize(VideoType::kI420,
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000193 static_cast<int>(width_),
194 static_cast<int>(height_))),
195 frame_buffer_(new uint8_t[frame_size_]),
196 frame_display_count_(frame_repeat_count),
197 current_display_count_(0) {
kwibergb890c95c2016-11-29 05:30:40 -0800198 RTC_DCHECK_GT(width, 0);
199 RTC_DCHECK_GT(height, 0);
200 RTC_DCHECK_GT(frame_repeat_count, 0);
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000201 }
202
Mirko Bonadeife055c12019-01-29 22:53:28 +0100203 ~YuvFileGenerator() override {
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000204 for (FILE* file : files_)
205 fclose(file);
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000206 }
207
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700208 VideoFrame* NextFrame() override {
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100209 // Empty update by default.
210 VideoFrame::UpdateRect update_rect{0, 0, 0, 0};
211 if (current_display_count_ == 0) {
212 const bool got_new_frame = ReadNextFrame();
213 // Full update on a new frame from file.
214 if (got_new_frame) {
215 update_rect = VideoFrame::UpdateRect{0, 0, static_cast<int>(width_),
216 static_cast<int>(height_)};
217 }
218 }
sprang@webrtc.org25dd1db2015-03-02 11:55:45 +0000219 if (++current_display_count_ >= frame_display_count_)
220 current_display_count_ = 0;
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000221
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100222 temp_frame_ = absl::make_unique<VideoFrame>(
223 VideoFrame::Builder()
224 .set_video_frame_buffer(last_read_buffer_)
225 .set_rotation(webrtc::kVideoRotation_0)
226 .set_timestamp_us(0)
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100227 .set_update_rect(update_rect)
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100228 .build());
nisse1996e3f2016-09-19 00:34:46 -0700229 return temp_frame_.get();
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000230 }
pbos@webrtc.org724947b2013-12-11 16:26:16 +0000231
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100232 // Returns true if the new frame was loaded.
233 // False only in case of a single file with a single frame in it.
234 bool ReadNextFrame() {
235 size_t prev_frame_index = frame_index_;
236 size_t prev_file_index = file_index_;
nisse115bd152016-09-30 04:14:07 -0700237 last_read_buffer_ =
238 test::ReadI420Buffer(static_cast<int>(width_),
Jonas Olssona4d87372019-07-05 19:08:33 +0200239 static_cast<int>(height_), files_[file_index_]);
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100240 ++frame_index_;
nisse115bd152016-09-30 04:14:07 -0700241 if (!last_read_buffer_) {
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000242 // No more frames to read in this file, rewind and move to next file.
243 rewind(files_[file_index_]);
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100244
245 frame_index_ = 0;
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000246 file_index_ = (file_index_ + 1) % files_.size();
nisse115bd152016-09-30 04:14:07 -0700247 last_read_buffer_ =
248 test::ReadI420Buffer(static_cast<int>(width_),
Jonas Olssona4d87372019-07-05 19:08:33 +0200249 static_cast<int>(height_), files_[file_index_]);
nisse115bd152016-09-30 04:14:07 -0700250 RTC_CHECK(last_read_buffer_);
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000251 }
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100252 return frame_index_ != prev_frame_index || file_index_ != prev_file_index;
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000253 }
254
255 private:
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000256 size_t file_index_;
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100257 size_t frame_index_;
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000258 const std::vector<FILE*> files_;
259 const size_t width_;
260 const size_t height_;
261 const size_t frame_size_;
kwibergbfefb032016-05-01 14:53:46 -0700262 const std::unique_ptr<uint8_t[]> frame_buffer_;
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000263 const int frame_display_count_;
264 int current_display_count_;
nisse1996e3f2016-09-19 00:34:46 -0700265 rtc::scoped_refptr<I420Buffer> last_read_buffer_;
266 std::unique_ptr<VideoFrame> temp_frame_;
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000267};
sprangd6358952015-07-29 07:58:13 -0700268
erikvarga579de6f2017-08-29 09:12:57 -0700269// SlideGenerator works similarly to YuvFileGenerator but it fills the frames
270// with randomly sized and colored squares instead of reading their content
271// from files.
272class SlideGenerator : public FrameGenerator {
273 public:
274 SlideGenerator(int width, int height, int frame_repeat_count)
275 : width_(width),
276 height_(height),
277 frame_display_count_(frame_repeat_count),
278 current_display_count_(0),
279 random_generator_(1234) {
280 RTC_DCHECK_GT(width, 0);
281 RTC_DCHECK_GT(height, 0);
282 RTC_DCHECK_GT(frame_repeat_count, 0);
283 }
284
285 VideoFrame* NextFrame() override {
286 if (current_display_count_ == 0)
287 GenerateNewFrame();
288 if (++current_display_count_ >= frame_display_count_)
289 current_display_count_ = 0;
290
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100291 frame_ = absl::make_unique<VideoFrame>(
292 VideoFrame::Builder()
293 .set_video_frame_buffer(buffer_)
294 .set_rotation(webrtc::kVideoRotation_0)
295 .set_timestamp_us(0)
296 .build());
erikvarga579de6f2017-08-29 09:12:57 -0700297 return frame_.get();
298 }
299
300 // Generates some randomly sized and colored squares scattered
301 // over the frame.
302 void GenerateNewFrame() {
303 // The squares should have a varying order of magnitude in order
304 // to simulate variation in the slides' complexity.
Jonas Olssona4d87372019-07-05 19:08:33 +0200305 const int kSquareNum = 1 << (4 + (random_generator_.Rand(0, 3) * 2));
erikvarga579de6f2017-08-29 09:12:57 -0700306
307 buffer_ = I420Buffer::Create(width_, height_);
308 memset(buffer_->MutableDataY(), 127, height_ * buffer_->StrideY());
309 memset(buffer_->MutableDataU(), 127,
310 buffer_->ChromaHeight() * buffer_->StrideU());
311 memset(buffer_->MutableDataV(), 127,
312 buffer_->ChromaHeight() * buffer_->StrideV());
313
314 for (int i = 0; i < kSquareNum; ++i) {
315 int length = random_generator_.Rand(1, width_ > 4 ? width_ / 4 : 1);
316 // Limit the length of later squares so that they don't overwrite the
317 // previous ones too much.
318 length = (length * (kSquareNum - i)) / kSquareNum;
319
320 int x = random_generator_.Rand(0, width_ - length);
321 int y = random_generator_.Rand(0, height_ - length);
322 uint8_t yuv_y = random_generator_.Rand(0, 255);
323 uint8_t yuv_u = random_generator_.Rand(0, 255);
324 uint8_t yuv_v = random_generator_.Rand(0, 255);
325
326 for (int yy = y; yy < y + length; ++yy) {
327 uint8_t* pos_y =
328 (buffer_->MutableDataY() + x + yy * buffer_->StrideY());
329 memset(pos_y, yuv_y, length);
330 }
331 for (int yy = y; yy < y + length; yy += 2) {
332 uint8_t* pos_u =
333 (buffer_->MutableDataU() + x / 2 + yy / 2 * buffer_->StrideU());
334 memset(pos_u, yuv_u, length / 2);
335 uint8_t* pos_v =
336 (buffer_->MutableDataV() + x / 2 + yy / 2 * buffer_->StrideV());
337 memset(pos_v, yuv_v, length / 2);
338 }
339 }
340 }
341
342 private:
343 const int width_;
344 const int height_;
345 const int frame_display_count_;
346 int current_display_count_;
347 Random random_generator_;
348 rtc::scoped_refptr<I420Buffer> buffer_;
349 std::unique_ptr<VideoFrame> frame_;
350};
351
sprangd6358952015-07-29 07:58:13 -0700352class ScrollingImageFrameGenerator : public FrameGenerator {
353 public:
354 ScrollingImageFrameGenerator(Clock* clock,
355 const std::vector<FILE*>& files,
356 size_t source_width,
357 size_t source_height,
358 size_t target_width,
359 size_t target_height,
360 int64_t scroll_time_ms,
361 int64_t pause_time_ms)
362 : clock_(clock),
363 start_time_(clock->TimeInMilliseconds()),
364 scroll_time_(scroll_time_ms),
365 pause_time_(pause_time_ms),
366 num_frames_(files.size()),
nissef122a852016-10-04 23:27:30 -0700367 target_width_(static_cast<int>(target_width)),
368 target_height_(static_cast<int>(target_height)),
sprangd6358952015-07-29 07:58:13 -0700369 current_frame_num_(num_frames_ - 1),
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100370 prev_frame_not_scrolled_(false),
sprangd6358952015-07-29 07:58:13 -0700371 current_source_frame_(nullptr),
372 file_generator_(files, source_width, source_height, 1) {
henrikg91d6ede2015-09-17 00:24:34 -0700373 RTC_DCHECK(clock_ != nullptr);
kwibergaf476c72016-11-28 15:21:39 -0800374 RTC_DCHECK_GT(num_frames_, 0);
henrikg91d6ede2015-09-17 00:24:34 -0700375 RTC_DCHECK_GE(source_height, target_height);
376 RTC_DCHECK_GE(source_width, target_width);
377 RTC_DCHECK_GE(scroll_time_ms, 0);
378 RTC_DCHECK_GE(pause_time_ms, 0);
379 RTC_DCHECK_GT(scroll_time_ms + pause_time_ms, 0);
sprangd6358952015-07-29 07:58:13 -0700380 }
381
Mirko Bonadeife055c12019-01-29 22:53:28 +0100382 ~ScrollingImageFrameGenerator() override {}
sprangd6358952015-07-29 07:58:13 -0700383
384 VideoFrame* NextFrame() override {
385 const int64_t kFrameDisplayTime = scroll_time_ + pause_time_;
386 const int64_t now = clock_->TimeInMilliseconds();
387 int64_t ms_since_start = now - start_time_;
388
389 size_t frame_num = (ms_since_start / kFrameDisplayTime) % num_frames_;
390 UpdateSourceFrame(frame_num);
391
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100392 bool cur_frame_not_scrolled;
393
sprangd6358952015-07-29 07:58:13 -0700394 double scroll_factor;
395 int64_t time_into_frame = ms_since_start % kFrameDisplayTime;
396 if (time_into_frame < scroll_time_) {
397 scroll_factor = static_cast<double>(time_into_frame) / scroll_time_;
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100398 cur_frame_not_scrolled = false;
sprangd6358952015-07-29 07:58:13 -0700399 } else {
400 scroll_factor = 1.0;
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100401 cur_frame_not_scrolled = true;
sprangd6358952015-07-29 07:58:13 -0700402 }
403 CropSourceToScrolledImage(scroll_factor);
404
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100405 bool same_scroll_position =
406 prev_frame_not_scrolled_ && cur_frame_not_scrolled;
407 if (!same_scroll_position && current_frame_) {
408 // If scrolling is not finished yet, force full frame update.
409 current_frame_->set_update_rect(
410 VideoFrame::UpdateRect{0, 0, target_width_, target_height_});
411 }
412 prev_frame_not_scrolled_ = cur_frame_not_scrolled;
413
nissedf2ceb82016-12-15 06:29:53 -0800414 return current_frame_ ? &*current_frame_ : nullptr;
sprangd6358952015-07-29 07:58:13 -0700415 }
416
417 void UpdateSourceFrame(size_t frame_num) {
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100418 VideoFrame::UpdateRect acc_update{0, 0, 0, 0};
Ilya Nikolaevskiy6cfb4032019-02-06 10:56:39 +0100419 while (current_frame_num_ != frame_num ||
420 current_source_frame_ == nullptr) {
sprangd6358952015-07-29 07:58:13 -0700421 current_source_frame_ = file_generator_.NextFrame();
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100422 if (current_source_frame_)
423 acc_update.Union(current_source_frame_->update_rect());
sprangd6358952015-07-29 07:58:13 -0700424 current_frame_num_ = (current_frame_num_ + 1) % num_frames_;
425 }
henrikg91d6ede2015-09-17 00:24:34 -0700426 RTC_DCHECK(current_source_frame_ != nullptr);
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100427 current_source_frame_->set_update_rect(acc_update);
sprangd6358952015-07-29 07:58:13 -0700428 }
429
430 void CropSourceToScrolledImage(double scroll_factor) {
nissef122a852016-10-04 23:27:30 -0700431 int scroll_margin_x = current_source_frame_->width() - target_width_;
sprangd6358952015-07-29 07:58:13 -0700432 int pixels_scrolled_x =
433 static_cast<int>(scroll_margin_x * scroll_factor + 0.5);
nissef122a852016-10-04 23:27:30 -0700434 int scroll_margin_y = current_source_frame_->height() - target_height_;
sprangd6358952015-07-29 07:58:13 -0700435 int pixels_scrolled_y =
436 static_cast<int>(scroll_margin_y * scroll_factor + 0.5);
437
Magnus Jedvert90e31902017-06-07 11:32:50 +0200438 rtc::scoped_refptr<I420BufferInterface> i420_buffer =
439 current_source_frame_->video_frame_buffer()->ToI420();
440 int offset_y =
441 (i420_buffer->StrideY() * pixels_scrolled_y) + pixels_scrolled_x;
442 int offset_u = (i420_buffer->StrideU() * (pixels_scrolled_y / 2)) +
sprangd6358952015-07-29 07:58:13 -0700443 (pixels_scrolled_x / 2);
Magnus Jedvert90e31902017-06-07 11:32:50 +0200444 int offset_v = (i420_buffer->StrideV() * (pixels_scrolled_y / 2)) +
sprangd6358952015-07-29 07:58:13 -0700445 (pixels_scrolled_x / 2);
446
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100447 VideoFrame::UpdateRect update_rect =
448 current_source_frame_->update_rect().IsEmpty()
449 ? VideoFrame::UpdateRect{0, 0, 0, 0}
450 : VideoFrame::UpdateRect{0, 0, target_width_, target_height_};
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100451 current_frame_ =
452 VideoFrame::Builder()
453 .set_video_frame_buffer(WrapI420Buffer(
454 target_width_, target_height_, &i420_buffer->DataY()[offset_y],
455 i420_buffer->StrideY(), &i420_buffer->DataU()[offset_u],
456 i420_buffer->StrideU(), &i420_buffer->DataV()[offset_v],
457 i420_buffer->StrideV(), KeepRefUntilDone(i420_buffer)))
458 .set_rotation(kVideoRotation_0)
459 .set_timestamp_us(0)
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100460 .set_update_rect(update_rect)
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100461 .build();
sprangd6358952015-07-29 07:58:13 -0700462 }
463
464 Clock* const clock_;
465 const int64_t start_time_;
466 const int64_t scroll_time_;
467 const int64_t pause_time_;
468 const size_t num_frames_;
nissef122a852016-10-04 23:27:30 -0700469 const int target_width_;
470 const int target_height_;
471
sprangd6358952015-07-29 07:58:13 -0700472 size_t current_frame_num_;
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100473 bool prev_frame_not_scrolled_;
sprangd6358952015-07-29 07:58:13 -0700474 VideoFrame* current_source_frame_;
Danil Chapovalov431abd92018-06-18 12:54:17 +0200475 absl::optional<VideoFrame> current_frame_;
sprangd6358952015-07-29 07:58:13 -0700476 YuvFileGenerator file_generator_;
477};
478
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000479} // namespace
480
perkja49cbd32016-09-16 07:53:41 -0700481FrameForwarder::FrameForwarder() : sink_(nullptr) {}
sprangb1ca0732017-02-01 08:38:12 -0800482FrameForwarder::~FrameForwarder() {}
perkja49cbd32016-09-16 07:53:41 -0700483
484void FrameForwarder::IncomingCapturedFrame(const VideoFrame& video_frame) {
485 rtc::CritScope lock(&crit_);
486 if (sink_)
487 sink_->OnFrame(video_frame);
488}
489
490void FrameForwarder::AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
491 const rtc::VideoSinkWants& wants) {
492 rtc::CritScope lock(&crit_);
493 RTC_DCHECK(!sink_ || sink_ == sink);
494 sink_ = sink;
perkj803d97f2016-11-01 11:45:46 -0700495 sink_wants_ = wants;
perkja49cbd32016-09-16 07:53:41 -0700496}
497
498void FrameForwarder::RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) {
499 rtc::CritScope lock(&crit_);
500 RTC_DCHECK_EQ(sink, sink_);
501 sink_ = nullptr;
502}
503
perkj803d97f2016-11-01 11:45:46 -0700504rtc::VideoSinkWants FrameForwarder::sink_wants() const {
505 rtc::CritScope lock(&crit_);
506 return sink_wants_;
507}
508
509bool FrameForwarder::has_sinks() const {
510 rtc::CritScope lock(&crit_);
511 return sink_ != nullptr;
512}
513
Artem Titovf50c6c22019-01-24 16:54:03 +0100514void FrameGenerator::ChangeResolution(size_t width, size_t height) {
515 RTC_NOTREACHED();
516}
517
perkja8ba1952017-02-27 06:52:10 -0800518std::unique_ptr<FrameGenerator> FrameGenerator::CreateSquareGenerator(
519 int width,
Emircan Uysaler03e6ec92018-03-09 15:03:26 -0800520 int height,
Danil Chapovalov431abd92018-06-18 12:54:17 +0200521 absl::optional<OutputType> type,
522 absl::optional<int> num_squares) {
erikvarga@webrtc.orgc774d5d2017-10-10 14:34:38 +0200523 return std::unique_ptr<FrameGenerator>(
Sebastian Janssoned0febf2019-07-26 15:58:11 +0200524 new SquareGenerator(width, height, type.value_or(OutputType::kI420),
Emircan Uysaler03e6ec92018-03-09 15:03:26 -0800525 num_squares.value_or(10)));
pbos@webrtc.org266c7b32013-10-15 09:15:47 +0000526}
527
erikvarga579de6f2017-08-29 09:12:57 -0700528std::unique_ptr<FrameGenerator> FrameGenerator::CreateSlideGenerator(
Jonas Olssona4d87372019-07-05 19:08:33 +0200529 int width,
530 int height,
531 int frame_repeat_count) {
532 return std::unique_ptr<FrameGenerator>(
533 new SlideGenerator(width, height, frame_repeat_count));
erikvarga579de6f2017-08-29 09:12:57 -0700534}
535
perkja8ba1952017-02-27 06:52:10 -0800536std::unique_ptr<FrameGenerator> FrameGenerator::CreateFromYuvFile(
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000537 std::vector<std::string> filenames,
538 size_t width,
539 size_t height,
540 int frame_repeat_count) {
kwibergb890c95c2016-11-29 05:30:40 -0800541 RTC_DCHECK(!filenames.empty());
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000542 std::vector<FILE*> files;
543 for (const std::string& filename : filenames) {
544 FILE* file = fopen(filename.c_str(), "rb");
Sebastian Jansson9eda3272018-09-13 16:23:03 +0200545 RTC_DCHECK(file != nullptr) << "Failed to open: '" << filename << "'\n";
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000546 files.push_back(file);
547 }
548
perkja8ba1952017-02-27 06:52:10 -0800549 return std::unique_ptr<FrameGenerator>(
550 new YuvFileGenerator(files, width, height, frame_repeat_count));
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000551}
552
perkja8ba1952017-02-27 06:52:10 -0800553std::unique_ptr<FrameGenerator>
554FrameGenerator::CreateScrollingInputFromYuvFiles(
sprangd6358952015-07-29 07:58:13 -0700555 Clock* clock,
556 std::vector<std::string> filenames,
557 size_t source_width,
558 size_t source_height,
559 size_t target_width,
560 size_t target_height,
561 int64_t scroll_time_ms,
562 int64_t pause_time_ms) {
kwibergb890c95c2016-11-29 05:30:40 -0800563 RTC_DCHECK(!filenames.empty());
sprangd6358952015-07-29 07:58:13 -0700564 std::vector<FILE*> files;
565 for (const std::string& filename : filenames) {
566 FILE* file = fopen(filename.c_str(), "rb");
henrikg91d6ede2015-09-17 00:24:34 -0700567 RTC_DCHECK(file != nullptr);
sprangd6358952015-07-29 07:58:13 -0700568 files.push_back(file);
569 }
570
perkja8ba1952017-02-27 06:52:10 -0800571 return std::unique_ptr<FrameGenerator>(new ScrollingImageFrameGenerator(
sprangd6358952015-07-29 07:58:13 -0700572 clock, files, source_width, source_height, target_width, target_height,
perkja8ba1952017-02-27 06:52:10 -0800573 scroll_time_ms, pause_time_ms));
sprangd6358952015-07-29 07:58:13 -0700574}
575
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000576} // namespace test
577} // namespace webrtc