You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

576 lines
20 KiB

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