blob: 22cc00e681aa7434901726d4e020afa15ea5e482 [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>
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <cstdint>
14#include <cstdio>
kwibergbfefb032016-05-01 14:53:46 -070015#include <memory>
16
Artem Titov1ebfb6a2019-01-03 23:49:37 +010017#include "absl/memory/memory.h"
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"
23#include "common_types.h" // NOLINT(build/include)
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_) {
Emircan Uysaler0823eec2018-07-13 17:10:00 -070077 case OutputType::I420:
78 case OutputType::I010: {
Emircan Uysaler03e6ec92018-03-09 15:03:26 -080079 buffer = CreateI420Buffer(width_, height_);
80 break;
81 }
82 case OutputType::I420A: {
83 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 }
95 }
nisse1996e3f2016-09-19 00:34:46 -070096
perkja8ba1952017-02-27 06:52:10 -080097 for (const auto& square : squares_)
98 square->Draw(buffer);
nisse1996e3f2016-09-19 00:34:46 -070099
Emircan Uysaler0823eec2018-07-13 17:10:00 -0700100 if (type_ == OutputType::I010) {
101 buffer = I010Buffer::Copy(*buffer->ToI420());
102 }
103
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100104 frame_ = absl::make_unique<VideoFrame>(
105 VideoFrame::Builder()
106 .set_video_frame_buffer(buffer)
107 .set_rotation(webrtc::kVideoRotation_0)
108 .set_timestamp_us(0)
109 .build());
nisse1996e3f2016-09-19 00:34:46 -0700110 return frame_.get();
pbos@webrtc.org266c7b32013-10-15 09:15:47 +0000111 }
112
113 private:
perkja8ba1952017-02-27 06:52:10 -0800114 class Square {
115 public:
116 Square(int width, int height, int seed)
117 : random_generator_(seed),
118 x_(random_generator_.Rand(0, width)),
119 y_(random_generator_.Rand(0, height)),
120 length_(random_generator_.Rand(1, width > 4 ? width / 4 : 1)),
121 yuv_y_(random_generator_.Rand(0, 255)),
122 yuv_u_(random_generator_.Rand(0, 255)),
Emircan Uysaler03e6ec92018-03-09 15:03:26 -0800123 yuv_v_(random_generator_.Rand(0, 255)),
124 yuv_a_(random_generator_.Rand(0, 255)) {}
perkja8ba1952017-02-27 06:52:10 -0800125
Emircan Uysaler03e6ec92018-03-09 15:03:26 -0800126 void Draw(const rtc::scoped_refptr<VideoFrameBuffer>& frame_buffer) {
127 RTC_DCHECK(frame_buffer->type() == VideoFrameBuffer::Type::kI420 ||
128 frame_buffer->type() == VideoFrameBuffer::Type::kI420A);
129 rtc::scoped_refptr<I420BufferInterface> buffer = frame_buffer->ToI420();
perkja8ba1952017-02-27 06:52:10 -0800130 x_ = (x_ + random_generator_.Rand(0, 4)) % (buffer->width() - length_);
131 y_ = (y_ + random_generator_.Rand(0, 4)) % (buffer->height() - length_);
Emircan Uysaler03e6ec92018-03-09 15:03:26 -0800132 for (int y = y_; y < y_ + length_; ++y) {
133 uint8_t* pos_y = (const_cast<uint8_t*>(buffer->DataY()) + x_ +
134 y * buffer->StrideY());
135 memset(pos_y, yuv_y_, length_);
136 }
perkjc8b08652017-03-22 04:57:53 -0700137
Emircan Uysaler03e6ec92018-03-09 15:03:26 -0800138 for (int y = y_; y < y_ + length_; y = y + 2) {
139 uint8_t* pos_u = (const_cast<uint8_t*>(buffer->DataU()) + x_ / 2 +
140 y / 2 * buffer->StrideU());
141 memset(pos_u, yuv_u_, length_ / 2);
142 uint8_t* pos_v = (const_cast<uint8_t*>(buffer->DataV()) + x_ / 2 +
143 y / 2 * buffer->StrideV());
144 memset(pos_v, yuv_v_, length_ / 2);
145 }
146
147 if (frame_buffer->type() == VideoFrameBuffer::Type::kI420)
148 return;
149
150 // Optionally draw on alpha plane if given.
151 const webrtc::I420ABufferInterface* yuva_buffer =
152 frame_buffer->GetI420A();
153 for (int y = y_; y < y_ + length_; ++y) {
154 uint8_t* pos_y = (const_cast<uint8_t*>(yuva_buffer->DataA()) + x_ +
155 y * yuva_buffer->StrideA());
156 memset(pos_y, yuv_a_, length_);
157 }
perkja8ba1952017-02-27 06:52:10 -0800158 }
159
160 private:
161 Random random_generator_;
162 int x_;
163 int y_;
164 const int length_;
165 const uint8_t yuv_y_;
166 const uint8_t yuv_u_;
167 const uint8_t yuv_v_;
Emircan Uysaler03e6ec92018-03-09 15:03:26 -0800168 const uint8_t yuv_a_;
perkja8ba1952017-02-27 06:52:10 -0800169 };
170
perkjfa10b552016-10-02 23:45:26 -0700171 rtc::CriticalSection crit_;
Emircan Uysaler03e6ec92018-03-09 15:03:26 -0800172 const OutputType type_;
danilchapa37de392017-09-09 04:17:22 -0700173 int width_ RTC_GUARDED_BY(&crit_);
174 int height_ RTC_GUARDED_BY(&crit_);
175 std::vector<std::unique_ptr<Square>> squares_ RTC_GUARDED_BY(&crit_);
176 std::unique_ptr<VideoFrame> frame_ RTC_GUARDED_BY(&crit_);
pbos@webrtc.org266c7b32013-10-15 09:15:47 +0000177};
178
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000179class YuvFileGenerator : public FrameGenerator {
180 public:
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000181 YuvFileGenerator(std::vector<FILE*> files,
182 size_t width,
183 size_t height,
184 int frame_repeat_count)
185 : file_index_(0),
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100186 frame_index_(std::numeric_limits<size_t>::max()),
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000187 files_(files),
188 width_(width),
189 height_(height),
nisseeb44b392017-04-28 07:18:05 -0700190 frame_size_(CalcBufferSize(VideoType::kI420,
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000191 static_cast<int>(width_),
192 static_cast<int>(height_))),
193 frame_buffer_(new uint8_t[frame_size_]),
194 frame_display_count_(frame_repeat_count),
195 current_display_count_(0) {
kwibergb890c95c2016-11-29 05:30:40 -0800196 RTC_DCHECK_GT(width, 0);
197 RTC_DCHECK_GT(height, 0);
198 RTC_DCHECK_GT(frame_repeat_count, 0);
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000199 }
200
Mirko Bonadeife055c12019-01-29 22:53:28 +0100201 ~YuvFileGenerator() override {
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000202 for (FILE* file : files_)
203 fclose(file);
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000204 }
205
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700206 VideoFrame* NextFrame() override {
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100207 // Empty update by default.
208 VideoFrame::UpdateRect update_rect{0, 0, 0, 0};
209 if (current_display_count_ == 0) {
210 const bool got_new_frame = ReadNextFrame();
211 // Full update on a new frame from file.
212 if (got_new_frame) {
213 update_rect = VideoFrame::UpdateRect{0, 0, static_cast<int>(width_),
214 static_cast<int>(height_)};
215 }
216 }
sprang@webrtc.org25dd1db2015-03-02 11:55:45 +0000217 if (++current_display_count_ >= frame_display_count_)
218 current_display_count_ = 0;
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000219
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100220 temp_frame_ = absl::make_unique<VideoFrame>(
221 VideoFrame::Builder()
222 .set_video_frame_buffer(last_read_buffer_)
223 .set_rotation(webrtc::kVideoRotation_0)
224 .set_timestamp_us(0)
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100225 .set_update_rect(update_rect)
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100226 .build());
nisse1996e3f2016-09-19 00:34:46 -0700227 return temp_frame_.get();
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000228 }
pbos@webrtc.org724947b2013-12-11 16:26:16 +0000229
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100230 // Returns true if the new frame was loaded.
231 // False only in case of a single file with a single frame in it.
232 bool ReadNextFrame() {
233 size_t prev_frame_index = frame_index_;
234 size_t prev_file_index = file_index_;
nisse115bd152016-09-30 04:14:07 -0700235 last_read_buffer_ =
236 test::ReadI420Buffer(static_cast<int>(width_),
237 static_cast<int>(height_),
238 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_),
248 static_cast<int>(height_),
249 files_[file_index_]);
250 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.
Erik Språng3fed5db2017-11-16 10:39:25 +0100305 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>(
Emircan Uysaler03e6ec92018-03-09 15:03:26 -0800524 new SquareGenerator(width, height, type.value_or(OutputType::I420),
525 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(
529 int width, int height, int frame_repeat_count) {
530 return std::unique_ptr<FrameGenerator>(new SlideGenerator(
531 width, height, frame_repeat_count));
532}
533
perkja8ba1952017-02-27 06:52:10 -0800534std::unique_ptr<FrameGenerator> FrameGenerator::CreateFromYuvFile(
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000535 std::vector<std::string> filenames,
536 size_t width,
537 size_t height,
538 int frame_repeat_count) {
kwibergb890c95c2016-11-29 05:30:40 -0800539 RTC_DCHECK(!filenames.empty());
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000540 std::vector<FILE*> files;
541 for (const std::string& filename : filenames) {
542 FILE* file = fopen(filename.c_str(), "rb");
Sebastian Jansson9eda3272018-09-13 16:23:03 +0200543 RTC_DCHECK(file != nullptr) << "Failed to open: '" << filename << "'\n";
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000544 files.push_back(file);
545 }
546
perkja8ba1952017-02-27 06:52:10 -0800547 return std::unique_ptr<FrameGenerator>(
548 new YuvFileGenerator(files, width, height, frame_repeat_count));
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000549}
550
perkja8ba1952017-02-27 06:52:10 -0800551std::unique_ptr<FrameGenerator>
552FrameGenerator::CreateScrollingInputFromYuvFiles(
sprangd6358952015-07-29 07:58:13 -0700553 Clock* clock,
554 std::vector<std::string> filenames,
555 size_t source_width,
556 size_t source_height,
557 size_t target_width,
558 size_t target_height,
559 int64_t scroll_time_ms,
560 int64_t pause_time_ms) {
kwibergb890c95c2016-11-29 05:30:40 -0800561 RTC_DCHECK(!filenames.empty());
sprangd6358952015-07-29 07:58:13 -0700562 std::vector<FILE*> files;
563 for (const std::string& filename : filenames) {
564 FILE* file = fopen(filename.c_str(), "rb");
henrikg91d6ede2015-09-17 00:24:34 -0700565 RTC_DCHECK(file != nullptr);
sprangd6358952015-07-29 07:58:13 -0700566 files.push_back(file);
567 }
568
perkja8ba1952017-02-27 06:52:10 -0800569 return std::unique_ptr<FrameGenerator>(new ScrollingImageFrameGenerator(
sprangd6358952015-07-29 07:58:13 -0700570 clock, files, source_width, source_height, target_width, target_height,
perkja8ba1952017-02-27 06:52:10 -0800571 scroll_time_ms, pause_time_ms));
sprangd6358952015-07-29 07:58:13 -0700572}
573
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000574} // namespace test
575} // namespace webrtc