blob: 453270e774b06db120765f37bc8e5ce5030ceb5e [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),
186 files_(files),
187 width_(width),
188 height_(height),
nisseeb44b392017-04-28 07:18:05 -0700189 frame_size_(CalcBufferSize(VideoType::kI420,
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000190 static_cast<int>(width_),
191 static_cast<int>(height_))),
192 frame_buffer_(new uint8_t[frame_size_]),
193 frame_display_count_(frame_repeat_count),
194 current_display_count_(0) {
kwibergb890c95c2016-11-29 05:30:40 -0800195 RTC_DCHECK_GT(width, 0);
196 RTC_DCHECK_GT(height, 0);
197 RTC_DCHECK_GT(frame_repeat_count, 0);
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000198 }
199
Mirko Bonadeife055c12019-01-29 22:53:28 +0100200 ~YuvFileGenerator() override {
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000201 for (FILE* file : files_)
202 fclose(file);
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000203 }
204
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700205 VideoFrame* NextFrame() override {
sprang@webrtc.org25dd1db2015-03-02 11:55:45 +0000206 if (current_display_count_ == 0)
207 ReadNextFrame();
208 if (++current_display_count_ >= frame_display_count_)
209 current_display_count_ = 0;
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000210
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100211 temp_frame_ = absl::make_unique<VideoFrame>(
212 VideoFrame::Builder()
213 .set_video_frame_buffer(last_read_buffer_)
214 .set_rotation(webrtc::kVideoRotation_0)
215 .set_timestamp_us(0)
216 .build());
nisse1996e3f2016-09-19 00:34:46 -0700217 return temp_frame_.get();
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000218 }
pbos@webrtc.org724947b2013-12-11 16:26:16 +0000219
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000220 void ReadNextFrame() {
nisse115bd152016-09-30 04:14:07 -0700221 last_read_buffer_ =
222 test::ReadI420Buffer(static_cast<int>(width_),
223 static_cast<int>(height_),
224 files_[file_index_]);
225 if (!last_read_buffer_) {
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000226 // No more frames to read in this file, rewind and move to next file.
227 rewind(files_[file_index_]);
228 file_index_ = (file_index_ + 1) % files_.size();
nisse115bd152016-09-30 04:14:07 -0700229 last_read_buffer_ =
230 test::ReadI420Buffer(static_cast<int>(width_),
231 static_cast<int>(height_),
232 files_[file_index_]);
233 RTC_CHECK(last_read_buffer_);
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000234 }
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000235 }
236
237 private:
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000238 size_t file_index_;
239 const std::vector<FILE*> files_;
240 const size_t width_;
241 const size_t height_;
242 const size_t frame_size_;
kwibergbfefb032016-05-01 14:53:46 -0700243 const std::unique_ptr<uint8_t[]> frame_buffer_;
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000244 const int frame_display_count_;
245 int current_display_count_;
nisse1996e3f2016-09-19 00:34:46 -0700246 rtc::scoped_refptr<I420Buffer> last_read_buffer_;
247 std::unique_ptr<VideoFrame> temp_frame_;
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000248};
sprangd6358952015-07-29 07:58:13 -0700249
erikvarga579de6f2017-08-29 09:12:57 -0700250// SlideGenerator works similarly to YuvFileGenerator but it fills the frames
251// with randomly sized and colored squares instead of reading their content
252// from files.
253class SlideGenerator : public FrameGenerator {
254 public:
255 SlideGenerator(int width, int height, int frame_repeat_count)
256 : width_(width),
257 height_(height),
258 frame_display_count_(frame_repeat_count),
259 current_display_count_(0),
260 random_generator_(1234) {
261 RTC_DCHECK_GT(width, 0);
262 RTC_DCHECK_GT(height, 0);
263 RTC_DCHECK_GT(frame_repeat_count, 0);
264 }
265
266 VideoFrame* NextFrame() override {
267 if (current_display_count_ == 0)
268 GenerateNewFrame();
269 if (++current_display_count_ >= frame_display_count_)
270 current_display_count_ = 0;
271
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100272 frame_ = absl::make_unique<VideoFrame>(
273 VideoFrame::Builder()
274 .set_video_frame_buffer(buffer_)
275 .set_rotation(webrtc::kVideoRotation_0)
276 .set_timestamp_us(0)
277 .build());
erikvarga579de6f2017-08-29 09:12:57 -0700278 return frame_.get();
279 }
280
281 // Generates some randomly sized and colored squares scattered
282 // over the frame.
283 void GenerateNewFrame() {
284 // The squares should have a varying order of magnitude in order
285 // to simulate variation in the slides' complexity.
Erik Språng3fed5db2017-11-16 10:39:25 +0100286 const int kSquareNum = 1 << (4 + (random_generator_.Rand(0, 3) * 2));
erikvarga579de6f2017-08-29 09:12:57 -0700287
288 buffer_ = I420Buffer::Create(width_, height_);
289 memset(buffer_->MutableDataY(), 127, height_ * buffer_->StrideY());
290 memset(buffer_->MutableDataU(), 127,
291 buffer_->ChromaHeight() * buffer_->StrideU());
292 memset(buffer_->MutableDataV(), 127,
293 buffer_->ChromaHeight() * buffer_->StrideV());
294
295 for (int i = 0; i < kSquareNum; ++i) {
296 int length = random_generator_.Rand(1, width_ > 4 ? width_ / 4 : 1);
297 // Limit the length of later squares so that they don't overwrite the
298 // previous ones too much.
299 length = (length * (kSquareNum - i)) / kSquareNum;
300
301 int x = random_generator_.Rand(0, width_ - length);
302 int y = random_generator_.Rand(0, height_ - length);
303 uint8_t yuv_y = random_generator_.Rand(0, 255);
304 uint8_t yuv_u = random_generator_.Rand(0, 255);
305 uint8_t yuv_v = random_generator_.Rand(0, 255);
306
307 for (int yy = y; yy < y + length; ++yy) {
308 uint8_t* pos_y =
309 (buffer_->MutableDataY() + x + yy * buffer_->StrideY());
310 memset(pos_y, yuv_y, length);
311 }
312 for (int yy = y; yy < y + length; yy += 2) {
313 uint8_t* pos_u =
314 (buffer_->MutableDataU() + x / 2 + yy / 2 * buffer_->StrideU());
315 memset(pos_u, yuv_u, length / 2);
316 uint8_t* pos_v =
317 (buffer_->MutableDataV() + x / 2 + yy / 2 * buffer_->StrideV());
318 memset(pos_v, yuv_v, length / 2);
319 }
320 }
321 }
322
323 private:
324 const int width_;
325 const int height_;
326 const int frame_display_count_;
327 int current_display_count_;
328 Random random_generator_;
329 rtc::scoped_refptr<I420Buffer> buffer_;
330 std::unique_ptr<VideoFrame> frame_;
331};
332
sprangd6358952015-07-29 07:58:13 -0700333class ScrollingImageFrameGenerator : public FrameGenerator {
334 public:
335 ScrollingImageFrameGenerator(Clock* clock,
336 const std::vector<FILE*>& files,
337 size_t source_width,
338 size_t source_height,
339 size_t target_width,
340 size_t target_height,
341 int64_t scroll_time_ms,
342 int64_t pause_time_ms)
343 : clock_(clock),
344 start_time_(clock->TimeInMilliseconds()),
345 scroll_time_(scroll_time_ms),
346 pause_time_(pause_time_ms),
347 num_frames_(files.size()),
nissef122a852016-10-04 23:27:30 -0700348 target_width_(static_cast<int>(target_width)),
349 target_height_(static_cast<int>(target_height)),
sprangd6358952015-07-29 07:58:13 -0700350 current_frame_num_(num_frames_ - 1),
351 current_source_frame_(nullptr),
352 file_generator_(files, source_width, source_height, 1) {
henrikg91d6ede2015-09-17 00:24:34 -0700353 RTC_DCHECK(clock_ != nullptr);
kwibergaf476c72016-11-28 15:21:39 -0800354 RTC_DCHECK_GT(num_frames_, 0);
henrikg91d6ede2015-09-17 00:24:34 -0700355 RTC_DCHECK_GE(source_height, target_height);
356 RTC_DCHECK_GE(source_width, target_width);
357 RTC_DCHECK_GE(scroll_time_ms, 0);
358 RTC_DCHECK_GE(pause_time_ms, 0);
359 RTC_DCHECK_GT(scroll_time_ms + pause_time_ms, 0);
sprangd6358952015-07-29 07:58:13 -0700360 }
361
Mirko Bonadeife055c12019-01-29 22:53:28 +0100362 ~ScrollingImageFrameGenerator() override {}
sprangd6358952015-07-29 07:58:13 -0700363
364 VideoFrame* NextFrame() override {
365 const int64_t kFrameDisplayTime = scroll_time_ + pause_time_;
366 const int64_t now = clock_->TimeInMilliseconds();
367 int64_t ms_since_start = now - start_time_;
368
369 size_t frame_num = (ms_since_start / kFrameDisplayTime) % num_frames_;
370 UpdateSourceFrame(frame_num);
371
372 double scroll_factor;
373 int64_t time_into_frame = ms_since_start % kFrameDisplayTime;
374 if (time_into_frame < scroll_time_) {
375 scroll_factor = static_cast<double>(time_into_frame) / scroll_time_;
376 } else {
377 scroll_factor = 1.0;
378 }
379 CropSourceToScrolledImage(scroll_factor);
380
nissedf2ceb82016-12-15 06:29:53 -0800381 return current_frame_ ? &*current_frame_ : nullptr;
sprangd6358952015-07-29 07:58:13 -0700382 }
383
384 void UpdateSourceFrame(size_t frame_num) {
Ilya Nikolaevskiy6cfb4032019-02-06 10:56:39 +0100385 while (current_frame_num_ != frame_num ||
386 current_source_frame_ == nullptr) {
sprangd6358952015-07-29 07:58:13 -0700387 current_source_frame_ = file_generator_.NextFrame();
388 current_frame_num_ = (current_frame_num_ + 1) % num_frames_;
389 }
henrikg91d6ede2015-09-17 00:24:34 -0700390 RTC_DCHECK(current_source_frame_ != nullptr);
sprangd6358952015-07-29 07:58:13 -0700391 }
392
393 void CropSourceToScrolledImage(double scroll_factor) {
nissef122a852016-10-04 23:27:30 -0700394 int scroll_margin_x = current_source_frame_->width() - target_width_;
sprangd6358952015-07-29 07:58:13 -0700395 int pixels_scrolled_x =
396 static_cast<int>(scroll_margin_x * scroll_factor + 0.5);
nissef122a852016-10-04 23:27:30 -0700397 int scroll_margin_y = current_source_frame_->height() - target_height_;
sprangd6358952015-07-29 07:58:13 -0700398 int pixels_scrolled_y =
399 static_cast<int>(scroll_margin_y * scroll_factor + 0.5);
400
Magnus Jedvert90e31902017-06-07 11:32:50 +0200401 rtc::scoped_refptr<I420BufferInterface> i420_buffer =
402 current_source_frame_->video_frame_buffer()->ToI420();
403 int offset_y =
404 (i420_buffer->StrideY() * pixels_scrolled_y) + pixels_scrolled_x;
405 int offset_u = (i420_buffer->StrideU() * (pixels_scrolled_y / 2)) +
sprangd6358952015-07-29 07:58:13 -0700406 (pixels_scrolled_x / 2);
Magnus Jedvert90e31902017-06-07 11:32:50 +0200407 int offset_v = (i420_buffer->StrideV() * (pixels_scrolled_y / 2)) +
sprangd6358952015-07-29 07:58:13 -0700408 (pixels_scrolled_x / 2);
409
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100410 current_frame_ =
411 VideoFrame::Builder()
412 .set_video_frame_buffer(WrapI420Buffer(
413 target_width_, target_height_, &i420_buffer->DataY()[offset_y],
414 i420_buffer->StrideY(), &i420_buffer->DataU()[offset_u],
415 i420_buffer->StrideU(), &i420_buffer->DataV()[offset_v],
416 i420_buffer->StrideV(), KeepRefUntilDone(i420_buffer)))
417 .set_rotation(kVideoRotation_0)
418 .set_timestamp_us(0)
419 .build();
sprangd6358952015-07-29 07:58:13 -0700420 }
421
422 Clock* const clock_;
423 const int64_t start_time_;
424 const int64_t scroll_time_;
425 const int64_t pause_time_;
426 const size_t num_frames_;
nissef122a852016-10-04 23:27:30 -0700427 const int target_width_;
428 const int target_height_;
429
sprangd6358952015-07-29 07:58:13 -0700430 size_t current_frame_num_;
431 VideoFrame* current_source_frame_;
Danil Chapovalov431abd92018-06-18 12:54:17 +0200432 absl::optional<VideoFrame> current_frame_;
sprangd6358952015-07-29 07:58:13 -0700433 YuvFileGenerator file_generator_;
434};
435
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000436} // namespace
437
perkja49cbd32016-09-16 07:53:41 -0700438FrameForwarder::FrameForwarder() : sink_(nullptr) {}
sprangb1ca0732017-02-01 08:38:12 -0800439FrameForwarder::~FrameForwarder() {}
perkja49cbd32016-09-16 07:53:41 -0700440
441void FrameForwarder::IncomingCapturedFrame(const VideoFrame& video_frame) {
442 rtc::CritScope lock(&crit_);
443 if (sink_)
444 sink_->OnFrame(video_frame);
445}
446
447void FrameForwarder::AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
448 const rtc::VideoSinkWants& wants) {
449 rtc::CritScope lock(&crit_);
450 RTC_DCHECK(!sink_ || sink_ == sink);
451 sink_ = sink;
perkj803d97f2016-11-01 11:45:46 -0700452 sink_wants_ = wants;
perkja49cbd32016-09-16 07:53:41 -0700453}
454
455void FrameForwarder::RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) {
456 rtc::CritScope lock(&crit_);
457 RTC_DCHECK_EQ(sink, sink_);
458 sink_ = nullptr;
459}
460
perkj803d97f2016-11-01 11:45:46 -0700461rtc::VideoSinkWants FrameForwarder::sink_wants() const {
462 rtc::CritScope lock(&crit_);
463 return sink_wants_;
464}
465
466bool FrameForwarder::has_sinks() const {
467 rtc::CritScope lock(&crit_);
468 return sink_ != nullptr;
469}
470
Artem Titovf50c6c22019-01-24 16:54:03 +0100471void FrameGenerator::ChangeResolution(size_t width, size_t height) {
472 RTC_NOTREACHED();
473}
474
perkja8ba1952017-02-27 06:52:10 -0800475std::unique_ptr<FrameGenerator> FrameGenerator::CreateSquareGenerator(
476 int width,
Emircan Uysaler03e6ec92018-03-09 15:03:26 -0800477 int height,
Danil Chapovalov431abd92018-06-18 12:54:17 +0200478 absl::optional<OutputType> type,
479 absl::optional<int> num_squares) {
erikvarga@webrtc.orgc774d5d2017-10-10 14:34:38 +0200480 return std::unique_ptr<FrameGenerator>(
Emircan Uysaler03e6ec92018-03-09 15:03:26 -0800481 new SquareGenerator(width, height, type.value_or(OutputType::I420),
482 num_squares.value_or(10)));
pbos@webrtc.org266c7b32013-10-15 09:15:47 +0000483}
484
erikvarga579de6f2017-08-29 09:12:57 -0700485std::unique_ptr<FrameGenerator> FrameGenerator::CreateSlideGenerator(
486 int width, int height, int frame_repeat_count) {
487 return std::unique_ptr<FrameGenerator>(new SlideGenerator(
488 width, height, frame_repeat_count));
489}
490
perkja8ba1952017-02-27 06:52:10 -0800491std::unique_ptr<FrameGenerator> FrameGenerator::CreateFromYuvFile(
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000492 std::vector<std::string> filenames,
493 size_t width,
494 size_t height,
495 int frame_repeat_count) {
kwibergb890c95c2016-11-29 05:30:40 -0800496 RTC_DCHECK(!filenames.empty());
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000497 std::vector<FILE*> files;
498 for (const std::string& filename : filenames) {
499 FILE* file = fopen(filename.c_str(), "rb");
Sebastian Jansson9eda3272018-09-13 16:23:03 +0200500 RTC_DCHECK(file != nullptr) << "Failed to open: '" << filename << "'\n";
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000501 files.push_back(file);
502 }
503
perkja8ba1952017-02-27 06:52:10 -0800504 return std::unique_ptr<FrameGenerator>(
505 new YuvFileGenerator(files, width, height, frame_repeat_count));
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000506}
507
perkja8ba1952017-02-27 06:52:10 -0800508std::unique_ptr<FrameGenerator>
509FrameGenerator::CreateScrollingInputFromYuvFiles(
sprangd6358952015-07-29 07:58:13 -0700510 Clock* clock,
511 std::vector<std::string> filenames,
512 size_t source_width,
513 size_t source_height,
514 size_t target_width,
515 size_t target_height,
516 int64_t scroll_time_ms,
517 int64_t pause_time_ms) {
kwibergb890c95c2016-11-29 05:30:40 -0800518 RTC_DCHECK(!filenames.empty());
sprangd6358952015-07-29 07:58:13 -0700519 std::vector<FILE*> files;
520 for (const std::string& filename : filenames) {
521 FILE* file = fopen(filename.c_str(), "rb");
henrikg91d6ede2015-09-17 00:24:34 -0700522 RTC_DCHECK(file != nullptr);
sprangd6358952015-07-29 07:58:13 -0700523 files.push_back(file);
524 }
525
perkja8ba1952017-02-27 06:52:10 -0800526 return std::unique_ptr<FrameGenerator>(new ScrollingImageFrameGenerator(
sprangd6358952015-07-29 07:58:13 -0700527 clock, files, source_width, source_height, target_width, target_height,
perkja8ba1952017-02-27 06:52:10 -0800528 scroll_time_ms, pause_time_ms));
sprangd6358952015-07-29 07:58:13 -0700529}
530
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000531} // namespace test
532} // namespace webrtc