andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 10 | #include "test/frame_generator.h" |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 | [diff] [blame] | 11 | |
pbos@webrtc.org | 266c7b3 | 2013-10-15 09:15:47 | [diff] [blame] | 12 | #include <math.h> |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 | [diff] [blame] | 13 | #include <stdio.h> |
pbos@webrtc.org | 266c7b3 | 2013-10-15 09:15:47 | [diff] [blame] | 14 | #include <string.h> |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 | [diff] [blame] | 15 | |
kwiberg | bfefb03 | 2016-05-01 21:53:46 | [diff] [blame] | 16 | #include <memory> |
| 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 18 | #include "api/video/i420_buffer.h" |
| 19 | #include "common_video/include/video_frame_buffer.h" |
| 20 | #include "common_video/libyuv/include/webrtc_libyuv.h" |
| 21 | #include "rtc_base/checks.h" |
| 22 | #include "rtc_base/keep_ref_until_done.h" |
| 23 | #include "rtc_base/random.h" |
| 24 | #include "system_wrappers/include/clock.h" |
| 25 | #include "test/frame_utils.h" |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 | [diff] [blame] | 26 | |
| 27 | namespace webrtc { |
| 28 | namespace test { |
| 29 | namespace { |
| 30 | |
Emircan Uysaler | 03e6ec9 | 2018-03-09 23:03:26 | [diff] [blame^] | 31 | // Helper method for keeping a reference to passed pointers. |
| 32 | void KeepBufferRefs(rtc::scoped_refptr<webrtc::VideoFrameBuffer>, |
| 33 | rtc::scoped_refptr<webrtc::VideoFrameBuffer>) {} |
| 34 | |
erikvarga@webrtc.org | c774d5d | 2017-10-10 12:34:38 | [diff] [blame] | 35 | // SquareGenerator is a FrameGenerator that draws a given amount of randomly |
| 36 | // sized and colored squares. Between each new generated frame, the squares |
| 37 | // are moved slightly towards the lower right corner. |
perkj | a8ba195 | 2017-02-27 14:52:10 | [diff] [blame] | 38 | class SquareGenerator : public FrameGenerator { |
pbos@webrtc.org | 266c7b3 | 2013-10-15 09:15:47 | [diff] [blame] | 39 | public: |
Emircan Uysaler | 03e6ec9 | 2018-03-09 23:03:26 | [diff] [blame^] | 40 | SquareGenerator(int width, int height, OutputType type, int num_squares) |
| 41 | : type_(type) { |
perkj | fa10b55 | 2016-10-03 06:45:26 | [diff] [blame] | 42 | ChangeResolution(width, height); |
erikvarga@webrtc.org | c774d5d | 2017-10-10 12:34:38 | [diff] [blame] | 43 | for (int i = 0; i < num_squares; ++i) { |
perkj | a8ba195 | 2017-02-27 14:52:10 | [diff] [blame] | 44 | squares_.emplace_back(new Square(width, height, i + 1)); |
| 45 | } |
perkj | fa10b55 | 2016-10-03 06:45:26 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void ChangeResolution(size_t width, size_t height) override { |
| 49 | rtc::CritScope lock(&crit_); |
perkj | a8ba195 | 2017-02-27 14:52:10 | [diff] [blame] | 50 | width_ = static_cast<int>(width); |
| 51 | height_ = static_cast<int>(height); |
nisse | 1996e3f | 2016-09-19 07:34:46 | [diff] [blame] | 52 | RTC_CHECK(width_ > 0); |
| 53 | RTC_CHECK(height_ > 0); |
pbos@webrtc.org | 266c7b3 | 2013-10-15 09:15:47 | [diff] [blame] | 54 | } |
| 55 | |
Emircan Uysaler | 03e6ec9 | 2018-03-09 23:03:26 | [diff] [blame^] | 56 | rtc::scoped_refptr<I420Buffer> CreateI420Buffer(int width, int height) { |
| 57 | rtc::scoped_refptr<I420Buffer> buffer(I420Buffer::Create(width, height)); |
| 58 | memset(buffer->MutableDataY(), 127, height * buffer->StrideY()); |
Magnus Jedvert | 90e3190 | 2017-06-07 09:32:50 | [diff] [blame] | 59 | memset(buffer->MutableDataU(), 127, |
| 60 | buffer->ChromaHeight() * buffer->StrideU()); |
| 61 | memset(buffer->MutableDataV(), 127, |
| 62 | buffer->ChromaHeight() * buffer->StrideV()); |
Emircan Uysaler | 03e6ec9 | 2018-03-09 23:03:26 | [diff] [blame^] | 63 | return buffer; |
| 64 | } |
| 65 | |
| 66 | VideoFrame* NextFrame() override { |
| 67 | rtc::CritScope lock(&crit_); |
| 68 | |
| 69 | rtc::scoped_refptr<VideoFrameBuffer> buffer = nullptr; |
| 70 | switch (type_) { |
| 71 | case OutputType::I420: { |
| 72 | buffer = CreateI420Buffer(width_, height_); |
| 73 | break; |
| 74 | } |
| 75 | case OutputType::I420A: { |
| 76 | rtc::scoped_refptr<I420Buffer> yuv_buffer = |
| 77 | CreateI420Buffer(width_, height_); |
| 78 | rtc::scoped_refptr<I420Buffer> axx_buffer = |
| 79 | CreateI420Buffer(width_, height_); |
| 80 | buffer = WrapI420ABuffer( |
| 81 | yuv_buffer->width(), yuv_buffer->height(), yuv_buffer->DataY(), |
| 82 | yuv_buffer->StrideY(), yuv_buffer->DataU(), yuv_buffer->StrideU(), |
| 83 | yuv_buffer->DataV(), yuv_buffer->StrideV(), axx_buffer->DataY(), |
| 84 | axx_buffer->StrideY(), |
| 85 | rtc::Bind(&KeepBufferRefs, yuv_buffer, axx_buffer)); |
| 86 | break; |
| 87 | } |
| 88 | } |
nisse | 1996e3f | 2016-09-19 07:34:46 | [diff] [blame] | 89 | |
perkj | a8ba195 | 2017-02-27 14:52:10 | [diff] [blame] | 90 | for (const auto& square : squares_) |
| 91 | square->Draw(buffer); |
nisse | 1996e3f | 2016-09-19 07:34:46 | [diff] [blame] | 92 | |
| 93 | frame_.reset(new VideoFrame(buffer, 0, 0, webrtc::kVideoRotation_0)); |
| 94 | return frame_.get(); |
pbos@webrtc.org | 266c7b3 | 2013-10-15 09:15:47 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | private: |
perkj | a8ba195 | 2017-02-27 14:52:10 | [diff] [blame] | 98 | class Square { |
| 99 | public: |
| 100 | Square(int width, int height, int seed) |
| 101 | : random_generator_(seed), |
| 102 | x_(random_generator_.Rand(0, width)), |
| 103 | y_(random_generator_.Rand(0, height)), |
| 104 | length_(random_generator_.Rand(1, width > 4 ? width / 4 : 1)), |
| 105 | yuv_y_(random_generator_.Rand(0, 255)), |
| 106 | yuv_u_(random_generator_.Rand(0, 255)), |
Emircan Uysaler | 03e6ec9 | 2018-03-09 23:03:26 | [diff] [blame^] | 107 | yuv_v_(random_generator_.Rand(0, 255)), |
| 108 | yuv_a_(random_generator_.Rand(0, 255)) {} |
perkj | a8ba195 | 2017-02-27 14:52:10 | [diff] [blame] | 109 | |
Emircan Uysaler | 03e6ec9 | 2018-03-09 23:03:26 | [diff] [blame^] | 110 | void Draw(const rtc::scoped_refptr<VideoFrameBuffer>& frame_buffer) { |
| 111 | RTC_DCHECK(frame_buffer->type() == VideoFrameBuffer::Type::kI420 || |
| 112 | frame_buffer->type() == VideoFrameBuffer::Type::kI420A); |
| 113 | rtc::scoped_refptr<I420BufferInterface> buffer = frame_buffer->ToI420(); |
perkj | a8ba195 | 2017-02-27 14:52:10 | [diff] [blame] | 114 | x_ = (x_ + random_generator_.Rand(0, 4)) % (buffer->width() - length_); |
| 115 | y_ = (y_ + random_generator_.Rand(0, 4)) % (buffer->height() - length_); |
Emircan Uysaler | 03e6ec9 | 2018-03-09 23:03:26 | [diff] [blame^] | 116 | for (int y = y_; y < y_ + length_; ++y) { |
| 117 | uint8_t* pos_y = (const_cast<uint8_t*>(buffer->DataY()) + x_ + |
| 118 | y * buffer->StrideY()); |
| 119 | memset(pos_y, yuv_y_, length_); |
| 120 | } |
perkj | c8b0865 | 2017-03-22 11:57:53 | [diff] [blame] | 121 | |
Emircan Uysaler | 03e6ec9 | 2018-03-09 23:03:26 | [diff] [blame^] | 122 | for (int y = y_; y < y_ + length_; y = y + 2) { |
| 123 | uint8_t* pos_u = (const_cast<uint8_t*>(buffer->DataU()) + x_ / 2 + |
| 124 | y / 2 * buffer->StrideU()); |
| 125 | memset(pos_u, yuv_u_, length_ / 2); |
| 126 | uint8_t* pos_v = (const_cast<uint8_t*>(buffer->DataV()) + x_ / 2 + |
| 127 | y / 2 * buffer->StrideV()); |
| 128 | memset(pos_v, yuv_v_, length_ / 2); |
| 129 | } |
| 130 | |
| 131 | if (frame_buffer->type() == VideoFrameBuffer::Type::kI420) |
| 132 | return; |
| 133 | |
| 134 | // Optionally draw on alpha plane if given. |
| 135 | const webrtc::I420ABufferInterface* yuva_buffer = |
| 136 | frame_buffer->GetI420A(); |
| 137 | for (int y = y_; y < y_ + length_; ++y) { |
| 138 | uint8_t* pos_y = (const_cast<uint8_t*>(yuva_buffer->DataA()) + x_ + |
| 139 | y * yuva_buffer->StrideA()); |
| 140 | memset(pos_y, yuv_a_, length_); |
| 141 | } |
perkj | a8ba195 | 2017-02-27 14:52:10 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | private: |
| 145 | Random random_generator_; |
| 146 | int x_; |
| 147 | int y_; |
| 148 | const int length_; |
| 149 | const uint8_t yuv_y_; |
| 150 | const uint8_t yuv_u_; |
| 151 | const uint8_t yuv_v_; |
Emircan Uysaler | 03e6ec9 | 2018-03-09 23:03:26 | [diff] [blame^] | 152 | const uint8_t yuv_a_; |
perkj | a8ba195 | 2017-02-27 14:52:10 | [diff] [blame] | 153 | }; |
| 154 | |
perkj | fa10b55 | 2016-10-03 06:45:26 | [diff] [blame] | 155 | rtc::CriticalSection crit_; |
Emircan Uysaler | 03e6ec9 | 2018-03-09 23:03:26 | [diff] [blame^] | 156 | const OutputType type_; |
danilchap | a37de39 | 2017-09-09 11:17:22 | [diff] [blame] | 157 | int width_ RTC_GUARDED_BY(&crit_); |
| 158 | int height_ RTC_GUARDED_BY(&crit_); |
| 159 | std::vector<std::unique_ptr<Square>> squares_ RTC_GUARDED_BY(&crit_); |
| 160 | std::unique_ptr<VideoFrame> frame_ RTC_GUARDED_BY(&crit_); |
pbos@webrtc.org | 266c7b3 | 2013-10-15 09:15:47 | [diff] [blame] | 161 | }; |
| 162 | |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 | [diff] [blame] | 163 | class YuvFileGenerator : public FrameGenerator { |
| 164 | public: |
sprang@webrtc.org | 131bea8 | 2015-02-18 12:46:06 | [diff] [blame] | 165 | YuvFileGenerator(std::vector<FILE*> files, |
| 166 | size_t width, |
| 167 | size_t height, |
| 168 | int frame_repeat_count) |
| 169 | : file_index_(0), |
| 170 | files_(files), |
| 171 | width_(width), |
| 172 | height_(height), |
nisse | eb44b39 | 2017-04-28 14:18:05 | [diff] [blame] | 173 | frame_size_(CalcBufferSize(VideoType::kI420, |
sprang@webrtc.org | 131bea8 | 2015-02-18 12:46:06 | [diff] [blame] | 174 | static_cast<int>(width_), |
| 175 | static_cast<int>(height_))), |
| 176 | frame_buffer_(new uint8_t[frame_size_]), |
| 177 | frame_display_count_(frame_repeat_count), |
| 178 | current_display_count_(0) { |
kwiberg | b890c95c | 2016-11-29 13:30:40 | [diff] [blame] | 179 | RTC_DCHECK_GT(width, 0); |
| 180 | RTC_DCHECK_GT(height, 0); |
| 181 | RTC_DCHECK_GT(frame_repeat_count, 0); |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | virtual ~YuvFileGenerator() { |
sprang@webrtc.org | 131bea8 | 2015-02-18 12:46:06 | [diff] [blame] | 185 | for (FILE* file : files_) |
| 186 | fclose(file); |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 | [diff] [blame] | 187 | } |
| 188 | |
Miguel Casas-Sanchez | 4765070 | 2015-05-30 00:21:40 | [diff] [blame] | 189 | VideoFrame* NextFrame() override { |
sprang@webrtc.org | 25dd1db | 2015-03-02 11:55:45 | [diff] [blame] | 190 | if (current_display_count_ == 0) |
| 191 | ReadNextFrame(); |
| 192 | if (++current_display_count_ >= frame_display_count_) |
| 193 | current_display_count_ = 0; |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 | [diff] [blame] | 194 | |
nisse | 1996e3f | 2016-09-19 07:34:46 | [diff] [blame] | 195 | temp_frame_.reset( |
| 196 | new VideoFrame(last_read_buffer_, 0, 0, webrtc::kVideoRotation_0)); |
| 197 | return temp_frame_.get(); |
sprang@webrtc.org | 131bea8 | 2015-02-18 12:46:06 | [diff] [blame] | 198 | } |
pbos@webrtc.org | 724947b | 2013-12-11 16:26:16 | [diff] [blame] | 199 | |
sprang@webrtc.org | 131bea8 | 2015-02-18 12:46:06 | [diff] [blame] | 200 | void ReadNextFrame() { |
nisse | 115bd15 | 2016-09-30 11:14:07 | [diff] [blame] | 201 | last_read_buffer_ = |
| 202 | test::ReadI420Buffer(static_cast<int>(width_), |
| 203 | static_cast<int>(height_), |
| 204 | files_[file_index_]); |
| 205 | if (!last_read_buffer_) { |
sprang@webrtc.org | 131bea8 | 2015-02-18 12:46:06 | [diff] [blame] | 206 | // No more frames to read in this file, rewind and move to next file. |
| 207 | rewind(files_[file_index_]); |
| 208 | file_index_ = (file_index_ + 1) % files_.size(); |
nisse | 115bd15 | 2016-09-30 11:14:07 | [diff] [blame] | 209 | last_read_buffer_ = |
| 210 | test::ReadI420Buffer(static_cast<int>(width_), |
| 211 | static_cast<int>(height_), |
| 212 | files_[file_index_]); |
| 213 | RTC_CHECK(last_read_buffer_); |
sprang@webrtc.org | 131bea8 | 2015-02-18 12:46:06 | [diff] [blame] | 214 | } |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | private: |
sprang@webrtc.org | 131bea8 | 2015-02-18 12:46:06 | [diff] [blame] | 218 | size_t file_index_; |
| 219 | const std::vector<FILE*> files_; |
| 220 | const size_t width_; |
| 221 | const size_t height_; |
| 222 | const size_t frame_size_; |
kwiberg | bfefb03 | 2016-05-01 21:53:46 | [diff] [blame] | 223 | const std::unique_ptr<uint8_t[]> frame_buffer_; |
sprang@webrtc.org | 131bea8 | 2015-02-18 12:46:06 | [diff] [blame] | 224 | const int frame_display_count_; |
| 225 | int current_display_count_; |
nisse | 1996e3f | 2016-09-19 07:34:46 | [diff] [blame] | 226 | rtc::scoped_refptr<I420Buffer> last_read_buffer_; |
| 227 | std::unique_ptr<VideoFrame> temp_frame_; |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 | [diff] [blame] | 228 | }; |
sprang | d635895 | 2015-07-29 14:58:13 | [diff] [blame] | 229 | |
erikvarga | 579de6f | 2017-08-29 16:12:57 | [diff] [blame] | 230 | // SlideGenerator works similarly to YuvFileGenerator but it fills the frames |
| 231 | // with randomly sized and colored squares instead of reading their content |
| 232 | // from files. |
| 233 | class SlideGenerator : public FrameGenerator { |
| 234 | public: |
| 235 | SlideGenerator(int width, int height, int frame_repeat_count) |
| 236 | : width_(width), |
| 237 | height_(height), |
| 238 | frame_display_count_(frame_repeat_count), |
| 239 | current_display_count_(0), |
| 240 | random_generator_(1234) { |
| 241 | RTC_DCHECK_GT(width, 0); |
| 242 | RTC_DCHECK_GT(height, 0); |
| 243 | RTC_DCHECK_GT(frame_repeat_count, 0); |
| 244 | } |
| 245 | |
| 246 | VideoFrame* NextFrame() override { |
| 247 | if (current_display_count_ == 0) |
| 248 | GenerateNewFrame(); |
| 249 | if (++current_display_count_ >= frame_display_count_) |
| 250 | current_display_count_ = 0; |
| 251 | |
| 252 | frame_.reset( |
| 253 | new VideoFrame(buffer_, 0, 0, webrtc::kVideoRotation_0)); |
| 254 | return frame_.get(); |
| 255 | } |
| 256 | |
| 257 | // Generates some randomly sized and colored squares scattered |
| 258 | // over the frame. |
| 259 | void GenerateNewFrame() { |
| 260 | // The squares should have a varying order of magnitude in order |
| 261 | // to simulate variation in the slides' complexity. |
Erik Språng | 3fed5db | 2017-11-16 09:39:25 | [diff] [blame] | 262 | const int kSquareNum = 1 << (4 + (random_generator_.Rand(0, 3) * 2)); |
erikvarga | 579de6f | 2017-08-29 16:12:57 | [diff] [blame] | 263 | |
| 264 | buffer_ = I420Buffer::Create(width_, height_); |
| 265 | memset(buffer_->MutableDataY(), 127, height_ * buffer_->StrideY()); |
| 266 | memset(buffer_->MutableDataU(), 127, |
| 267 | buffer_->ChromaHeight() * buffer_->StrideU()); |
| 268 | memset(buffer_->MutableDataV(), 127, |
| 269 | buffer_->ChromaHeight() * buffer_->StrideV()); |
| 270 | |
| 271 | for (int i = 0; i < kSquareNum; ++i) { |
| 272 | int length = random_generator_.Rand(1, width_ > 4 ? width_ / 4 : 1); |
| 273 | // Limit the length of later squares so that they don't overwrite the |
| 274 | // previous ones too much. |
| 275 | length = (length * (kSquareNum - i)) / kSquareNum; |
| 276 | |
| 277 | int x = random_generator_.Rand(0, width_ - length); |
| 278 | int y = random_generator_.Rand(0, height_ - length); |
| 279 | uint8_t yuv_y = random_generator_.Rand(0, 255); |
| 280 | uint8_t yuv_u = random_generator_.Rand(0, 255); |
| 281 | uint8_t yuv_v = random_generator_.Rand(0, 255); |
| 282 | |
| 283 | for (int yy = y; yy < y + length; ++yy) { |
| 284 | uint8_t* pos_y = |
| 285 | (buffer_->MutableDataY() + x + yy * buffer_->StrideY()); |
| 286 | memset(pos_y, yuv_y, length); |
| 287 | } |
| 288 | for (int yy = y; yy < y + length; yy += 2) { |
| 289 | uint8_t* pos_u = |
| 290 | (buffer_->MutableDataU() + x / 2 + yy / 2 * buffer_->StrideU()); |
| 291 | memset(pos_u, yuv_u, length / 2); |
| 292 | uint8_t* pos_v = |
| 293 | (buffer_->MutableDataV() + x / 2 + yy / 2 * buffer_->StrideV()); |
| 294 | memset(pos_v, yuv_v, length / 2); |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | private: |
| 300 | const int width_; |
| 301 | const int height_; |
| 302 | const int frame_display_count_; |
| 303 | int current_display_count_; |
| 304 | Random random_generator_; |
| 305 | rtc::scoped_refptr<I420Buffer> buffer_; |
| 306 | std::unique_ptr<VideoFrame> frame_; |
| 307 | }; |
| 308 | |
sprang | d635895 | 2015-07-29 14:58:13 | [diff] [blame] | 309 | class ScrollingImageFrameGenerator : public FrameGenerator { |
| 310 | public: |
| 311 | ScrollingImageFrameGenerator(Clock* clock, |
| 312 | const std::vector<FILE*>& files, |
| 313 | size_t source_width, |
| 314 | size_t source_height, |
| 315 | size_t target_width, |
| 316 | size_t target_height, |
| 317 | int64_t scroll_time_ms, |
| 318 | int64_t pause_time_ms) |
| 319 | : clock_(clock), |
| 320 | start_time_(clock->TimeInMilliseconds()), |
| 321 | scroll_time_(scroll_time_ms), |
| 322 | pause_time_(pause_time_ms), |
| 323 | num_frames_(files.size()), |
nisse | f122a85 | 2016-10-05 06:27:30 | [diff] [blame] | 324 | target_width_(static_cast<int>(target_width)), |
| 325 | target_height_(static_cast<int>(target_height)), |
sprang | d635895 | 2015-07-29 14:58:13 | [diff] [blame] | 326 | current_frame_num_(num_frames_ - 1), |
| 327 | current_source_frame_(nullptr), |
| 328 | file_generator_(files, source_width, source_height, 1) { |
henrikg | 91d6ede | 2015-09-17 07:24:34 | [diff] [blame] | 329 | RTC_DCHECK(clock_ != nullptr); |
kwiberg | af476c7 | 2016-11-28 23:21:39 | [diff] [blame] | 330 | RTC_DCHECK_GT(num_frames_, 0); |
henrikg | 91d6ede | 2015-09-17 07:24:34 | [diff] [blame] | 331 | RTC_DCHECK_GE(source_height, target_height); |
| 332 | RTC_DCHECK_GE(source_width, target_width); |
| 333 | RTC_DCHECK_GE(scroll_time_ms, 0); |
| 334 | RTC_DCHECK_GE(pause_time_ms, 0); |
| 335 | RTC_DCHECK_GT(scroll_time_ms + pause_time_ms, 0); |
sprang | d635895 | 2015-07-29 14:58:13 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | virtual ~ScrollingImageFrameGenerator() {} |
| 339 | |
| 340 | VideoFrame* NextFrame() override { |
| 341 | const int64_t kFrameDisplayTime = scroll_time_ + pause_time_; |
| 342 | const int64_t now = clock_->TimeInMilliseconds(); |
| 343 | int64_t ms_since_start = now - start_time_; |
| 344 | |
| 345 | size_t frame_num = (ms_since_start / kFrameDisplayTime) % num_frames_; |
| 346 | UpdateSourceFrame(frame_num); |
| 347 | |
| 348 | double scroll_factor; |
| 349 | int64_t time_into_frame = ms_since_start % kFrameDisplayTime; |
| 350 | if (time_into_frame < scroll_time_) { |
| 351 | scroll_factor = static_cast<double>(time_into_frame) / scroll_time_; |
| 352 | } else { |
| 353 | scroll_factor = 1.0; |
| 354 | } |
| 355 | CropSourceToScrolledImage(scroll_factor); |
| 356 | |
nisse | df2ceb8 | 2016-12-15 14:29:53 | [diff] [blame] | 357 | return current_frame_ ? &*current_frame_ : nullptr; |
sprang | d635895 | 2015-07-29 14:58:13 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | void UpdateSourceFrame(size_t frame_num) { |
| 361 | while (current_frame_num_ != frame_num) { |
| 362 | current_source_frame_ = file_generator_.NextFrame(); |
| 363 | current_frame_num_ = (current_frame_num_ + 1) % num_frames_; |
| 364 | } |
henrikg | 91d6ede | 2015-09-17 07:24:34 | [diff] [blame] | 365 | RTC_DCHECK(current_source_frame_ != nullptr); |
sprang | d635895 | 2015-07-29 14:58:13 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | void CropSourceToScrolledImage(double scroll_factor) { |
nisse | f122a85 | 2016-10-05 06:27:30 | [diff] [blame] | 369 | int scroll_margin_x = current_source_frame_->width() - target_width_; |
sprang | d635895 | 2015-07-29 14:58:13 | [diff] [blame] | 370 | int pixels_scrolled_x = |
| 371 | static_cast<int>(scroll_margin_x * scroll_factor + 0.5); |
nisse | f122a85 | 2016-10-05 06:27:30 | [diff] [blame] | 372 | int scroll_margin_y = current_source_frame_->height() - target_height_; |
sprang | d635895 | 2015-07-29 14:58:13 | [diff] [blame] | 373 | int pixels_scrolled_y = |
| 374 | static_cast<int>(scroll_margin_y * scroll_factor + 0.5); |
| 375 | |
Magnus Jedvert | 90e3190 | 2017-06-07 09:32:50 | [diff] [blame] | 376 | rtc::scoped_refptr<I420BufferInterface> i420_buffer = |
| 377 | current_source_frame_->video_frame_buffer()->ToI420(); |
| 378 | int offset_y = |
| 379 | (i420_buffer->StrideY() * pixels_scrolled_y) + pixels_scrolled_x; |
| 380 | int offset_u = (i420_buffer->StrideU() * (pixels_scrolled_y / 2)) + |
sprang | d635895 | 2015-07-29 14:58:13 | [diff] [blame] | 381 | (pixels_scrolled_x / 2); |
Magnus Jedvert | 90e3190 | 2017-06-07 09:32:50 | [diff] [blame] | 382 | int offset_v = (i420_buffer->StrideV() * (pixels_scrolled_y / 2)) + |
sprang | d635895 | 2015-07-29 14:58:13 | [diff] [blame] | 383 | (pixels_scrolled_x / 2); |
| 384 | |
Oskar Sundbom | 3f6804d | 2017-11-16 09:54:58 | [diff] [blame] | 385 | current_frame_ = webrtc::VideoFrame( |
nisse | f0a7c5a | 2016-10-31 12:48:07 | [diff] [blame] | 386 | new rtc::RefCountedObject<webrtc::WrappedI420Buffer>( |
Magnus Jedvert | 90e3190 | 2017-06-07 09:32:50 | [diff] [blame] | 387 | target_width_, target_height_, &i420_buffer->DataY()[offset_y], |
| 388 | i420_buffer->StrideY(), &i420_buffer->DataU()[offset_u], |
| 389 | i420_buffer->StrideU(), &i420_buffer->DataV()[offset_v], |
| 390 | i420_buffer->StrideV(), KeepRefUntilDone(i420_buffer)), |
Oskar Sundbom | 3f6804d | 2017-11-16 09:54:58 | [diff] [blame] | 391 | kVideoRotation_0, 0); |
sprang | d635895 | 2015-07-29 14:58:13 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | Clock* const clock_; |
| 395 | const int64_t start_time_; |
| 396 | const int64_t scroll_time_; |
| 397 | const int64_t pause_time_; |
| 398 | const size_t num_frames_; |
nisse | f122a85 | 2016-10-05 06:27:30 | [diff] [blame] | 399 | const int target_width_; |
| 400 | const int target_height_; |
| 401 | |
sprang | d635895 | 2015-07-29 14:58:13 | [diff] [blame] | 402 | size_t current_frame_num_; |
| 403 | VideoFrame* current_source_frame_; |
nisse | df2ceb8 | 2016-12-15 14:29:53 | [diff] [blame] | 404 | rtc::Optional<VideoFrame> current_frame_; |
sprang | d635895 | 2015-07-29 14:58:13 | [diff] [blame] | 405 | YuvFileGenerator file_generator_; |
| 406 | }; |
| 407 | |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 | [diff] [blame] | 408 | } // namespace |
| 409 | |
perkj | a49cbd3 | 2016-09-16 14:53:41 | [diff] [blame] | 410 | FrameForwarder::FrameForwarder() : sink_(nullptr) {} |
sprang | b1ca073 | 2017-02-01 16:38:12 | [diff] [blame] | 411 | FrameForwarder::~FrameForwarder() {} |
perkj | a49cbd3 | 2016-09-16 14:53:41 | [diff] [blame] | 412 | |
| 413 | void FrameForwarder::IncomingCapturedFrame(const VideoFrame& video_frame) { |
| 414 | rtc::CritScope lock(&crit_); |
| 415 | if (sink_) |
| 416 | sink_->OnFrame(video_frame); |
| 417 | } |
| 418 | |
| 419 | void FrameForwarder::AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink, |
| 420 | const rtc::VideoSinkWants& wants) { |
| 421 | rtc::CritScope lock(&crit_); |
| 422 | RTC_DCHECK(!sink_ || sink_ == sink); |
| 423 | sink_ = sink; |
perkj | 803d97f | 2016-11-01 18:45:46 | [diff] [blame] | 424 | sink_wants_ = wants; |
perkj | a49cbd3 | 2016-09-16 14:53:41 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | void FrameForwarder::RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) { |
| 428 | rtc::CritScope lock(&crit_); |
| 429 | RTC_DCHECK_EQ(sink, sink_); |
| 430 | sink_ = nullptr; |
| 431 | } |
| 432 | |
perkj | 803d97f | 2016-11-01 18:45:46 | [diff] [blame] | 433 | rtc::VideoSinkWants FrameForwarder::sink_wants() const { |
| 434 | rtc::CritScope lock(&crit_); |
| 435 | return sink_wants_; |
| 436 | } |
| 437 | |
| 438 | bool FrameForwarder::has_sinks() const { |
| 439 | rtc::CritScope lock(&crit_); |
| 440 | return sink_ != nullptr; |
| 441 | } |
| 442 | |
perkj | a8ba195 | 2017-02-27 14:52:10 | [diff] [blame] | 443 | std::unique_ptr<FrameGenerator> FrameGenerator::CreateSquareGenerator( |
| 444 | int width, |
Emircan Uysaler | 03e6ec9 | 2018-03-09 23:03:26 | [diff] [blame^] | 445 | int height, |
| 446 | rtc::Optional<OutputType> type, |
| 447 | rtc::Optional<int> num_squares) { |
erikvarga@webrtc.org | c774d5d | 2017-10-10 12:34:38 | [diff] [blame] | 448 | return std::unique_ptr<FrameGenerator>( |
Emircan Uysaler | 03e6ec9 | 2018-03-09 23:03:26 | [diff] [blame^] | 449 | new SquareGenerator(width, height, type.value_or(OutputType::I420), |
| 450 | num_squares.value_or(10))); |
pbos@webrtc.org | 266c7b3 | 2013-10-15 09:15:47 | [diff] [blame] | 451 | } |
| 452 | |
erikvarga | 579de6f | 2017-08-29 16:12:57 | [diff] [blame] | 453 | std::unique_ptr<FrameGenerator> FrameGenerator::CreateSlideGenerator( |
| 454 | int width, int height, int frame_repeat_count) { |
| 455 | return std::unique_ptr<FrameGenerator>(new SlideGenerator( |
| 456 | width, height, frame_repeat_count)); |
| 457 | } |
| 458 | |
perkj | a8ba195 | 2017-02-27 14:52:10 | [diff] [blame] | 459 | std::unique_ptr<FrameGenerator> FrameGenerator::CreateFromYuvFile( |
sprang@webrtc.org | 131bea8 | 2015-02-18 12:46:06 | [diff] [blame] | 460 | std::vector<std::string> filenames, |
| 461 | size_t width, |
| 462 | size_t height, |
| 463 | int frame_repeat_count) { |
kwiberg | b890c95c | 2016-11-29 13:30:40 | [diff] [blame] | 464 | RTC_DCHECK(!filenames.empty()); |
sprang@webrtc.org | 131bea8 | 2015-02-18 12:46:06 | [diff] [blame] | 465 | std::vector<FILE*> files; |
| 466 | for (const std::string& filename : filenames) { |
| 467 | FILE* file = fopen(filename.c_str(), "rb"); |
henrikg | 91d6ede | 2015-09-17 07:24:34 | [diff] [blame] | 468 | RTC_DCHECK(file != nullptr); |
sprang@webrtc.org | 131bea8 | 2015-02-18 12:46:06 | [diff] [blame] | 469 | files.push_back(file); |
| 470 | } |
| 471 | |
perkj | a8ba195 | 2017-02-27 14:52:10 | [diff] [blame] | 472 | return std::unique_ptr<FrameGenerator>( |
| 473 | new YuvFileGenerator(files, width, height, frame_repeat_count)); |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 | [diff] [blame] | 474 | } |
| 475 | |
perkj | a8ba195 | 2017-02-27 14:52:10 | [diff] [blame] | 476 | std::unique_ptr<FrameGenerator> |
| 477 | FrameGenerator::CreateScrollingInputFromYuvFiles( |
sprang | d635895 | 2015-07-29 14:58:13 | [diff] [blame] | 478 | Clock* clock, |
| 479 | std::vector<std::string> filenames, |
| 480 | size_t source_width, |
| 481 | size_t source_height, |
| 482 | size_t target_width, |
| 483 | size_t target_height, |
| 484 | int64_t scroll_time_ms, |
| 485 | int64_t pause_time_ms) { |
kwiberg | b890c95c | 2016-11-29 13:30:40 | [diff] [blame] | 486 | RTC_DCHECK(!filenames.empty()); |
sprang | d635895 | 2015-07-29 14:58:13 | [diff] [blame] | 487 | std::vector<FILE*> files; |
| 488 | for (const std::string& filename : filenames) { |
| 489 | FILE* file = fopen(filename.c_str(), "rb"); |
henrikg | 91d6ede | 2015-09-17 07:24:34 | [diff] [blame] | 490 | RTC_DCHECK(file != nullptr); |
sprang | d635895 | 2015-07-29 14:58:13 | [diff] [blame] | 491 | files.push_back(file); |
| 492 | } |
| 493 | |
perkj | a8ba195 | 2017-02-27 14:52:10 | [diff] [blame] | 494 | return std::unique_ptr<FrameGenerator>(new ScrollingImageFrameGenerator( |
sprang | d635895 | 2015-07-29 14:58:13 | [diff] [blame] | 495 | clock, files, source_width, source_height, target_width, target_height, |
perkj | a8ba195 | 2017-02-27 14:52:10 | [diff] [blame] | 496 | scroll_time_ms, pause_time_ms)); |
sprang | d635895 | 2015-07-29 14:58:13 | [diff] [blame] | 497 | } |
| 498 | |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 | [diff] [blame] | 499 | } // namespace test |
| 500 | } // namespace webrtc |