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