blob: cd0bcc6d04f8d476575b4b80858a0d1c407f6c4e [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 */
pbos@webrtc.org724947b2013-12-11 16:26:16 +000010#include "webrtc/test/frame_generator.h"
andresp@webrtc.orgab654952013-09-19 12:14:03 +000011
pbos@webrtc.org266c7b32013-10-15 09:15:47 +000012#include <math.h>
andresp@webrtc.orgab654952013-09-19 12:14:03 +000013#include <stdio.h>
pbos@webrtc.org266c7b32013-10-15 09:15:47 +000014#include <string.h>
andresp@webrtc.orgab654952013-09-19 12:14:03 +000015
kwibergbfefb032016-05-01 14:53:46 -070016#include <memory>
17
nisseaf916892017-01-10 07:44:26 -080018#include "webrtc/api/video/i420_buffer.h"
sprang@webrtc.org131bea82015-02-18 12:46:06 +000019#include "webrtc/base/checks.h"
nissef0a7c5a2016-10-31 05:48:07 -070020#include "webrtc/base/keep_ref_until_done.h"
perkja8ba1952017-02-27 06:52:10 -080021#include "webrtc/base/random.h"
nisseaf916892017-01-10 07:44:26 -080022#include "webrtc/common_video/include/video_frame_buffer.h"
andresp@webrtc.orgab654952013-09-19 12:14:03 +000023#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010024#include "webrtc/system_wrappers/include/clock.h"
nisse115bd152016-09-30 04:14:07 -070025#include "webrtc/test/frame_utils.h"
andresp@webrtc.orgab654952013-09-19 12:14:03 +000026
27namespace webrtc {
28namespace test {
29namespace {
30
perkja8ba1952017-02-27 06:52:10 -080031// SquareGenerator is a FrameGenerator that draws 10 randomly sized and colored
32// squares. Between each new generated frame, the squares are moved slightly
33// towards the lower right corner.
34class SquareGenerator : public FrameGenerator {
pbos@webrtc.org266c7b32013-10-15 09:15:47 +000035 public:
perkja8ba1952017-02-27 06:52:10 -080036 SquareGenerator(int width, int height) {
perkjfa10b552016-10-02 23:45:26 -070037 ChangeResolution(width, height);
perkja8ba1952017-02-27 06:52:10 -080038 for (int i = 0; i < 10; ++i) {
39 squares_.emplace_back(new Square(width, height, i + 1));
40 }
perkjfa10b552016-10-02 23:45:26 -070041 }
42
43 void ChangeResolution(size_t width, size_t height) override {
44 rtc::CritScope lock(&crit_);
perkja8ba1952017-02-27 06:52:10 -080045 width_ = static_cast<int>(width);
46 height_ = static_cast<int>(height);
nisse1996e3f2016-09-19 00:34:46 -070047 RTC_CHECK(width_ > 0);
48 RTC_CHECK(height_ > 0);
49 half_width_ = (width_ + 1) / 2;
50 y_size_ = width_ * height_;
51 uv_size_ = half_width_ * ((height_ + 1) / 2);
pbos@webrtc.org266c7b32013-10-15 09:15:47 +000052 }
53
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070054 VideoFrame* NextFrame() override {
perkjfa10b552016-10-02 23:45:26 -070055 rtc::CritScope lock(&crit_);
pbos@webrtc.org266c7b32013-10-15 09:15:47 +000056
nisse1996e3f2016-09-19 00:34:46 -070057 // Ensure stride == width.
perkja8ba1952017-02-27 06:52:10 -080058 rtc::scoped_refptr<I420Buffer> buffer(
59 I420Buffer::Create(width_, height_, width_, half_width_, half_width_));
60 memset(buffer->MutableDataY(), 127, y_size_);
61 memset(buffer->MutableDataU(), 127, uv_size_);
62 memset(buffer->MutableDataV(), 127, uv_size_);
nisse1996e3f2016-09-19 00:34:46 -070063
perkja8ba1952017-02-27 06:52:10 -080064 for (const auto& square : squares_)
65 square->Draw(buffer);
nisse1996e3f2016-09-19 00:34:46 -070066
67 frame_.reset(new VideoFrame(buffer, 0, 0, webrtc::kVideoRotation_0));
68 return frame_.get();
pbos@webrtc.org266c7b32013-10-15 09:15:47 +000069 }
70
71 private:
perkja8ba1952017-02-27 06:52:10 -080072 class Square {
73 public:
74 Square(int width, int height, int seed)
75 : random_generator_(seed),
76 x_(random_generator_.Rand(0, width)),
77 y_(random_generator_.Rand(0, height)),
78 length_(random_generator_.Rand(1, width > 4 ? width / 4 : 1)),
79 yuv_y_(random_generator_.Rand(0, 255)),
80 yuv_u_(random_generator_.Rand(0, 255)),
81 yuv_v_(random_generator_.Rand(0, 255)) {}
82
83 void Draw(const rtc::scoped_refptr<I420Buffer>& buffer) {
84 x_ = (x_ + random_generator_.Rand(0, 4)) % (buffer->width() - length_);
85 y_ = (y_ + random_generator_.Rand(0, 4)) % (buffer->height() - length_);
86 for (int x = x_; x < x_ + length_; ++x) {
87 for (int y = y_; y < y_ + length_; ++y) {
88 uint8_t* pos_y = (buffer->MutableDataY() + x + y * buffer->StrideY());
89 *pos_y = yuv_y_;
90 uint8_t* pos_u =
91 (buffer->MutableDataU() + x / 2 + y / 2 * buffer->StrideU());
92 *pos_u = yuv_u_;
93 uint8_t* pos_v =
94 (buffer->MutableDataV() + x / 2 + y / 2 * buffer->StrideV());
95 *pos_v = yuv_v_;
96 }
97 }
98 }
99
100 private:
101 Random random_generator_;
102 int x_;
103 int y_;
104 const int length_;
105 const uint8_t yuv_y_;
106 const uint8_t yuv_u_;
107 const uint8_t yuv_v_;
108 };
109
perkjfa10b552016-10-02 23:45:26 -0700110 rtc::CriticalSection crit_;
perkja8ba1952017-02-27 06:52:10 -0800111 int width_ GUARDED_BY(&crit_);
112 int height_ GUARDED_BY(&crit_);
113 int half_width_ GUARDED_BY(&crit_);
perkjfa10b552016-10-02 23:45:26 -0700114 size_t y_size_ GUARDED_BY(&crit_);
115 size_t uv_size_ GUARDED_BY(&crit_);
perkja8ba1952017-02-27 06:52:10 -0800116 std::vector<std::unique_ptr<Square>> squares_ GUARDED_BY(&crit_);
perkjfa10b552016-10-02 23:45:26 -0700117 std::unique_ptr<VideoFrame> frame_ GUARDED_BY(&crit_);
pbos@webrtc.org266c7b32013-10-15 09:15:47 +0000118};
119
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000120class YuvFileGenerator : public FrameGenerator {
121 public:
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000122 YuvFileGenerator(std::vector<FILE*> files,
123 size_t width,
124 size_t height,
125 int frame_repeat_count)
126 : file_index_(0),
127 files_(files),
128 width_(width),
129 height_(height),
130 frame_size_(CalcBufferSize(kI420,
131 static_cast<int>(width_),
132 static_cast<int>(height_))),
133 frame_buffer_(new uint8_t[frame_size_]),
134 frame_display_count_(frame_repeat_count),
135 current_display_count_(0) {
kwibergb890c95c2016-11-29 05:30:40 -0800136 RTC_DCHECK_GT(width, 0);
137 RTC_DCHECK_GT(height, 0);
138 RTC_DCHECK_GT(frame_repeat_count, 0);
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000139 }
140
141 virtual ~YuvFileGenerator() {
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000142 for (FILE* file : files_)
143 fclose(file);
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000144 }
145
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700146 VideoFrame* NextFrame() override {
sprang@webrtc.org25dd1db2015-03-02 11:55:45 +0000147 if (current_display_count_ == 0)
148 ReadNextFrame();
149 if (++current_display_count_ >= frame_display_count_)
150 current_display_count_ = 0;
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000151
nisse1996e3f2016-09-19 00:34:46 -0700152 temp_frame_.reset(
153 new VideoFrame(last_read_buffer_, 0, 0, webrtc::kVideoRotation_0));
154 return temp_frame_.get();
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000155 }
pbos@webrtc.org724947b2013-12-11 16:26:16 +0000156
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000157 void ReadNextFrame() {
nisse115bd152016-09-30 04:14:07 -0700158 last_read_buffer_ =
159 test::ReadI420Buffer(static_cast<int>(width_),
160 static_cast<int>(height_),
161 files_[file_index_]);
162 if (!last_read_buffer_) {
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000163 // No more frames to read in this file, rewind and move to next file.
164 rewind(files_[file_index_]);
165 file_index_ = (file_index_ + 1) % files_.size();
nisse115bd152016-09-30 04:14:07 -0700166 last_read_buffer_ =
167 test::ReadI420Buffer(static_cast<int>(width_),
168 static_cast<int>(height_),
169 files_[file_index_]);
170 RTC_CHECK(last_read_buffer_);
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000171 }
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000172 }
173
174 private:
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000175 size_t file_index_;
176 const std::vector<FILE*> files_;
177 const size_t width_;
178 const size_t height_;
179 const size_t frame_size_;
kwibergbfefb032016-05-01 14:53:46 -0700180 const std::unique_ptr<uint8_t[]> frame_buffer_;
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000181 const int frame_display_count_;
182 int current_display_count_;
nisse1996e3f2016-09-19 00:34:46 -0700183 rtc::scoped_refptr<I420Buffer> last_read_buffer_;
184 std::unique_ptr<VideoFrame> temp_frame_;
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000185};
sprangd6358952015-07-29 07:58:13 -0700186
187class ScrollingImageFrameGenerator : public FrameGenerator {
188 public:
189 ScrollingImageFrameGenerator(Clock* clock,
190 const std::vector<FILE*>& files,
191 size_t source_width,
192 size_t source_height,
193 size_t target_width,
194 size_t target_height,
195 int64_t scroll_time_ms,
196 int64_t pause_time_ms)
197 : clock_(clock),
198 start_time_(clock->TimeInMilliseconds()),
199 scroll_time_(scroll_time_ms),
200 pause_time_(pause_time_ms),
201 num_frames_(files.size()),
nissef122a852016-10-04 23:27:30 -0700202 target_width_(static_cast<int>(target_width)),
203 target_height_(static_cast<int>(target_height)),
sprangd6358952015-07-29 07:58:13 -0700204 current_frame_num_(num_frames_ - 1),
205 current_source_frame_(nullptr),
206 file_generator_(files, source_width, source_height, 1) {
henrikg91d6ede2015-09-17 00:24:34 -0700207 RTC_DCHECK(clock_ != nullptr);
kwibergaf476c72016-11-28 15:21:39 -0800208 RTC_DCHECK_GT(num_frames_, 0);
henrikg91d6ede2015-09-17 00:24:34 -0700209 RTC_DCHECK_GE(source_height, target_height);
210 RTC_DCHECK_GE(source_width, target_width);
211 RTC_DCHECK_GE(scroll_time_ms, 0);
212 RTC_DCHECK_GE(pause_time_ms, 0);
213 RTC_DCHECK_GT(scroll_time_ms + pause_time_ms, 0);
sprangd6358952015-07-29 07:58:13 -0700214 }
215
216 virtual ~ScrollingImageFrameGenerator() {}
217
218 VideoFrame* NextFrame() override {
219 const int64_t kFrameDisplayTime = scroll_time_ + pause_time_;
220 const int64_t now = clock_->TimeInMilliseconds();
221 int64_t ms_since_start = now - start_time_;
222
223 size_t frame_num = (ms_since_start / kFrameDisplayTime) % num_frames_;
224 UpdateSourceFrame(frame_num);
225
226 double scroll_factor;
227 int64_t time_into_frame = ms_since_start % kFrameDisplayTime;
228 if (time_into_frame < scroll_time_) {
229 scroll_factor = static_cast<double>(time_into_frame) / scroll_time_;
230 } else {
231 scroll_factor = 1.0;
232 }
233 CropSourceToScrolledImage(scroll_factor);
234
nissedf2ceb82016-12-15 06:29:53 -0800235 return current_frame_ ? &*current_frame_ : nullptr;
sprangd6358952015-07-29 07:58:13 -0700236 }
237
238 void UpdateSourceFrame(size_t frame_num) {
239 while (current_frame_num_ != frame_num) {
240 current_source_frame_ = file_generator_.NextFrame();
241 current_frame_num_ = (current_frame_num_ + 1) % num_frames_;
242 }
henrikg91d6ede2015-09-17 00:24:34 -0700243 RTC_DCHECK(current_source_frame_ != nullptr);
sprangd6358952015-07-29 07:58:13 -0700244 }
245
246 void CropSourceToScrolledImage(double scroll_factor) {
nissef122a852016-10-04 23:27:30 -0700247 int scroll_margin_x = current_source_frame_->width() - target_width_;
sprangd6358952015-07-29 07:58:13 -0700248 int pixels_scrolled_x =
249 static_cast<int>(scroll_margin_x * scroll_factor + 0.5);
nissef122a852016-10-04 23:27:30 -0700250 int scroll_margin_y = current_source_frame_->height() - target_height_;
sprangd6358952015-07-29 07:58:13 -0700251 int pixels_scrolled_y =
252 static_cast<int>(scroll_margin_y * scroll_factor + 0.5);
253
nissec9c142f2016-05-17 04:05:47 -0700254 int offset_y = (current_source_frame_->video_frame_buffer()->StrideY() *
sprangd6358952015-07-29 07:58:13 -0700255 pixels_scrolled_y) +
256 pixels_scrolled_x;
nissec9c142f2016-05-17 04:05:47 -0700257 int offset_u = (current_source_frame_->video_frame_buffer()->StrideU() *
sprangd6358952015-07-29 07:58:13 -0700258 (pixels_scrolled_y / 2)) +
259 (pixels_scrolled_x / 2);
nissec9c142f2016-05-17 04:05:47 -0700260 int offset_v = (current_source_frame_->video_frame_buffer()->StrideV() *
sprangd6358952015-07-29 07:58:13 -0700261 (pixels_scrolled_y / 2)) +
262 (pixels_scrolled_x / 2);
263
nissef0a7c5a2016-10-31 05:48:07 -0700264 rtc::scoped_refptr<VideoFrameBuffer> frame_buffer(
265 current_source_frame_->video_frame_buffer());
nissedf2ceb82016-12-15 06:29:53 -0800266 current_frame_ = rtc::Optional<webrtc::VideoFrame>(webrtc::VideoFrame(
nissef0a7c5a2016-10-31 05:48:07 -0700267 new rtc::RefCountedObject<webrtc::WrappedI420Buffer>(
268 target_width_, target_height_,
269 &frame_buffer->DataY()[offset_y], frame_buffer->StrideY(),
270 &frame_buffer->DataU()[offset_u], frame_buffer->StrideU(),
271 &frame_buffer->DataV()[offset_v], frame_buffer->StrideV(),
272 KeepRefUntilDone(frame_buffer)),
nissedf2ceb82016-12-15 06:29:53 -0800273 kVideoRotation_0, 0));
sprangd6358952015-07-29 07:58:13 -0700274 }
275
276 Clock* const clock_;
277 const int64_t start_time_;
278 const int64_t scroll_time_;
279 const int64_t pause_time_;
280 const size_t num_frames_;
nissef122a852016-10-04 23:27:30 -0700281 const int target_width_;
282 const int target_height_;
283
sprangd6358952015-07-29 07:58:13 -0700284 size_t current_frame_num_;
285 VideoFrame* current_source_frame_;
nissedf2ceb82016-12-15 06:29:53 -0800286 rtc::Optional<VideoFrame> current_frame_;
sprangd6358952015-07-29 07:58:13 -0700287 YuvFileGenerator file_generator_;
288};
289
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000290} // namespace
291
perkja49cbd32016-09-16 07:53:41 -0700292FrameForwarder::FrameForwarder() : sink_(nullptr) {}
sprangb1ca0732017-02-01 08:38:12 -0800293FrameForwarder::~FrameForwarder() {}
perkja49cbd32016-09-16 07:53:41 -0700294
295void FrameForwarder::IncomingCapturedFrame(const VideoFrame& video_frame) {
296 rtc::CritScope lock(&crit_);
297 if (sink_)
298 sink_->OnFrame(video_frame);
299}
300
301void FrameForwarder::AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
302 const rtc::VideoSinkWants& wants) {
303 rtc::CritScope lock(&crit_);
304 RTC_DCHECK(!sink_ || sink_ == sink);
305 sink_ = sink;
perkj803d97f2016-11-01 11:45:46 -0700306 sink_wants_ = wants;
perkja49cbd32016-09-16 07:53:41 -0700307}
308
309void FrameForwarder::RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) {
310 rtc::CritScope lock(&crit_);
311 RTC_DCHECK_EQ(sink, sink_);
312 sink_ = nullptr;
313}
314
perkj803d97f2016-11-01 11:45:46 -0700315rtc::VideoSinkWants FrameForwarder::sink_wants() const {
316 rtc::CritScope lock(&crit_);
317 return sink_wants_;
318}
319
320bool FrameForwarder::has_sinks() const {
321 rtc::CritScope lock(&crit_);
322 return sink_ != nullptr;
323}
324
perkja8ba1952017-02-27 06:52:10 -0800325std::unique_ptr<FrameGenerator> FrameGenerator::CreateSquareGenerator(
326 int width,
327 int height) {
328 return std::unique_ptr<FrameGenerator>(new SquareGenerator(width, height));
pbos@webrtc.org266c7b32013-10-15 09:15:47 +0000329}
330
perkja8ba1952017-02-27 06:52:10 -0800331std::unique_ptr<FrameGenerator> FrameGenerator::CreateFromYuvFile(
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000332 std::vector<std::string> filenames,
333 size_t width,
334 size_t height,
335 int frame_repeat_count) {
kwibergb890c95c2016-11-29 05:30:40 -0800336 RTC_DCHECK(!filenames.empty());
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000337 std::vector<FILE*> files;
338 for (const std::string& filename : filenames) {
339 FILE* file = fopen(filename.c_str(), "rb");
henrikg91d6ede2015-09-17 00:24:34 -0700340 RTC_DCHECK(file != nullptr);
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000341 files.push_back(file);
342 }
343
perkja8ba1952017-02-27 06:52:10 -0800344 return std::unique_ptr<FrameGenerator>(
345 new YuvFileGenerator(files, width, height, frame_repeat_count));
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000346}
347
perkja8ba1952017-02-27 06:52:10 -0800348std::unique_ptr<FrameGenerator>
349FrameGenerator::CreateScrollingInputFromYuvFiles(
sprangd6358952015-07-29 07:58:13 -0700350 Clock* clock,
351 std::vector<std::string> filenames,
352 size_t source_width,
353 size_t source_height,
354 size_t target_width,
355 size_t target_height,
356 int64_t scroll_time_ms,
357 int64_t pause_time_ms) {
kwibergb890c95c2016-11-29 05:30:40 -0800358 RTC_DCHECK(!filenames.empty());
sprangd6358952015-07-29 07:58:13 -0700359 std::vector<FILE*> files;
360 for (const std::string& filename : filenames) {
361 FILE* file = fopen(filename.c_str(), "rb");
henrikg91d6ede2015-09-17 00:24:34 -0700362 RTC_DCHECK(file != nullptr);
sprangd6358952015-07-29 07:58:13 -0700363 files.push_back(file);
364 }
365
perkja8ba1952017-02-27 06:52:10 -0800366 return std::unique_ptr<FrameGenerator>(new ScrollingImageFrameGenerator(
sprangd6358952015-07-29 07:58:13 -0700367 clock, files, source_width, source_height, target_width, target_height,
perkja8ba1952017-02-27 06:52:10 -0800368 scroll_time_ms, pause_time_ms));
sprangd6358952015-07-29 07:58:13 -0700369}
370
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000371} // namespace test
372} // namespace webrtc