frame_generator.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. #ifndef TEST_FRAME_GENERATOR_H_
  11. #define TEST_FRAME_GENERATOR_H_
  12. #include <memory>
  13. #include <string>
  14. #include <vector>
  15. #include "api/scoped_refptr.h"
  16. #include "api/test/frame_generator_interface.h"
  17. #include "api/video/i420_buffer.h"
  18. #include "api/video/video_frame.h"
  19. #include "api/video/video_frame_buffer.h"
  20. #include "api/video/video_source_interface.h"
  21. #include "rtc_base/random.h"
  22. #include "rtc_base/synchronization/mutex.h"
  23. #include "system_wrappers/include/clock.h"
  24. namespace webrtc {
  25. namespace test {
  26. // SquareGenerator is a FrameGenerator that draws a given amount of randomly
  27. // sized and colored squares. Between each new generated frame, the squares
  28. // are moved slightly towards the lower right corner.
  29. class SquareGenerator : public FrameGeneratorInterface {
  30. public:
  31. SquareGenerator(int width, int height, OutputType type, int num_squares);
  32. void ChangeResolution(size_t width, size_t height) override;
  33. VideoFrameData NextFrame() override;
  34. private:
  35. rtc::scoped_refptr<I420Buffer> CreateI420Buffer(int width, int height);
  36. class Square {
  37. public:
  38. Square(int width, int height, int seed);
  39. void Draw(const rtc::scoped_refptr<VideoFrameBuffer>& frame_buffer);
  40. private:
  41. Random random_generator_;
  42. int x_;
  43. int y_;
  44. const int length_;
  45. const uint8_t yuv_y_;
  46. const uint8_t yuv_u_;
  47. const uint8_t yuv_v_;
  48. const uint8_t yuv_a_;
  49. };
  50. Mutex mutex_;
  51. const OutputType type_;
  52. int width_ RTC_GUARDED_BY(&mutex_);
  53. int height_ RTC_GUARDED_BY(&mutex_);
  54. std::vector<std::unique_ptr<Square>> squares_ RTC_GUARDED_BY(&mutex_);
  55. };
  56. class YuvFileGenerator : public FrameGeneratorInterface {
  57. public:
  58. YuvFileGenerator(std::vector<FILE*> files,
  59. size_t width,
  60. size_t height,
  61. int frame_repeat_count);
  62. ~YuvFileGenerator();
  63. VideoFrameData NextFrame() override;
  64. void ChangeResolution(size_t width, size_t height) override {
  65. RTC_NOTREACHED();
  66. }
  67. private:
  68. // Returns true if the new frame was loaded.
  69. // False only in case of a single file with a single frame in it.
  70. bool ReadNextFrame();
  71. size_t file_index_;
  72. size_t frame_index_;
  73. const std::vector<FILE*> files_;
  74. const size_t width_;
  75. const size_t height_;
  76. const size_t frame_size_;
  77. const std::unique_ptr<uint8_t[]> frame_buffer_;
  78. const int frame_display_count_;
  79. int current_display_count_;
  80. rtc::scoped_refptr<I420Buffer> last_read_buffer_;
  81. };
  82. // SlideGenerator works similarly to YuvFileGenerator but it fills the frames
  83. // with randomly sized and colored squares instead of reading their content
  84. // from files.
  85. class SlideGenerator : public FrameGeneratorInterface {
  86. public:
  87. SlideGenerator(int width, int height, int frame_repeat_count);
  88. VideoFrameData NextFrame() override;
  89. void ChangeResolution(size_t width, size_t height) override {
  90. RTC_NOTREACHED();
  91. }
  92. private:
  93. // Generates some randomly sized and colored squares scattered
  94. // over the frame.
  95. void GenerateNewFrame();
  96. const int width_;
  97. const int height_;
  98. const int frame_display_count_;
  99. int current_display_count_;
  100. Random random_generator_;
  101. rtc::scoped_refptr<I420Buffer> buffer_;
  102. };
  103. class ScrollingImageFrameGenerator : public FrameGeneratorInterface {
  104. public:
  105. ScrollingImageFrameGenerator(Clock* clock,
  106. const std::vector<FILE*>& files,
  107. size_t source_width,
  108. size_t source_height,
  109. size_t target_width,
  110. size_t target_height,
  111. int64_t scroll_time_ms,
  112. int64_t pause_time_ms);
  113. ~ScrollingImageFrameGenerator() override = default;
  114. VideoFrameData NextFrame() override;
  115. void ChangeResolution(size_t width, size_t height) override {
  116. RTC_NOTREACHED();
  117. }
  118. private:
  119. void UpdateSourceFrame(size_t frame_num);
  120. void CropSourceToScrolledImage(double scroll_factor);
  121. Clock* const clock_;
  122. const int64_t start_time_;
  123. const int64_t scroll_time_;
  124. const int64_t pause_time_;
  125. const size_t num_frames_;
  126. const int target_width_;
  127. const int target_height_;
  128. size_t current_frame_num_;
  129. bool prev_frame_not_scrolled_;
  130. VideoFrameData current_source_frame_;
  131. VideoFrameData current_frame_;
  132. YuvFileGenerator file_generator_;
  133. };
  134. } // namespace test
  135. } // namespace webrtc
  136. #endif // TEST_FRAME_GENERATOR_H_