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 | */ |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 10 | #ifndef TEST_FRAME_GENERATOR_H_ |
| 11 | #define TEST_FRAME_GENERATOR_H_ |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 12 | |
perkj | a8ba195 | 2017-02-27 06:52:10 -0800 | [diff] [blame] | 13 | #include <memory> |
sprang@webrtc.org | 131bea8 | 2015-02-18 12:46:06 +0000 | [diff] [blame] | 14 | #include <string> |
| 15 | #include <vector> |
| 16 | |
Artem Titov | 33f9d2b | 2019-12-05 15:59:00 +0100 | [diff] [blame] | 17 | #include "api/scoped_refptr.h" |
Artem Titov | 503d723 | 2019-12-04 12:37:13 +0100 | [diff] [blame] | 18 | #include "api/test/frame_generator_interface.h" |
Artem Titov | 33f9d2b | 2019-12-05 15:59:00 +0100 | [diff] [blame] | 19 | #include "api/video/i420_buffer.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "api/video/video_frame.h" |
Artem Titov | 33f9d2b | 2019-12-05 15:59:00 +0100 | [diff] [blame] | 21 | #include "api/video/video_frame_buffer.h" |
Niels Möller | 0327c2d | 2018-05-21 14:09:31 +0200 | [diff] [blame] | 22 | #include "api/video/video_source_interface.h" |
Per Kjellander | f5770a0 | 2022-01-12 13:43:31 +0100 | [diff] [blame] | 23 | #include "rtc_base/logging.h" |
Artem Titov | 33f9d2b | 2019-12-05 15:59:00 +0100 | [diff] [blame] | 24 | #include "rtc_base/random.h" |
Markus Handell | a5a4be1 | 2020-07-08 16:09:21 +0200 | [diff] [blame] | 25 | #include "rtc_base/synchronization/mutex.h" |
Artem Titov | 33f9d2b | 2019-12-05 15:59:00 +0100 | [diff] [blame] | 26 | #include "system_wrappers/include/clock.h" |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 27 | |
| 28 | namespace webrtc { |
| 29 | namespace test { |
| 30 | |
Artem Titov | 33f9d2b | 2019-12-05 15:59:00 +0100 | [diff] [blame] | 31 | // SquareGenerator is a FrameGenerator that draws a given amount of randomly |
| 32 | // sized and colored squares. Between each new generated frame, the squares |
| 33 | // are moved slightly towards the lower right corner. |
| 34 | class SquareGenerator : public FrameGeneratorInterface { |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 35 | public: |
Artem Titov | 33f9d2b | 2019-12-05 15:59:00 +0100 | [diff] [blame] | 36 | SquareGenerator(int width, int height, OutputType type, int num_squares); |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 37 | |
Artem Titov | 33f9d2b | 2019-12-05 15:59:00 +0100 | [diff] [blame] | 38 | void ChangeResolution(size_t width, size_t height) override; |
| 39 | VideoFrameData NextFrame() override; |
| 40 | |
| 41 | private: |
| 42 | rtc::scoped_refptr<I420Buffer> CreateI420Buffer(int width, int height); |
| 43 | |
| 44 | class Square { |
| 45 | public: |
| 46 | Square(int width, int height, int seed); |
| 47 | |
| 48 | void Draw(const rtc::scoped_refptr<VideoFrameBuffer>& frame_buffer); |
| 49 | |
| 50 | private: |
| 51 | Random random_generator_; |
| 52 | int x_; |
| 53 | int y_; |
| 54 | const int length_; |
| 55 | const uint8_t yuv_y_; |
| 56 | const uint8_t yuv_u_; |
| 57 | const uint8_t yuv_v_; |
| 58 | const uint8_t yuv_a_; |
| 59 | }; |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 60 | |
Markus Handell | a5a4be1 | 2020-07-08 16:09:21 +0200 | [diff] [blame] | 61 | Mutex mutex_; |
Artem Titov | 33f9d2b | 2019-12-05 15:59:00 +0100 | [diff] [blame] | 62 | const OutputType type_; |
Markus Handell | a5a4be1 | 2020-07-08 16:09:21 +0200 | [diff] [blame] | 63 | int width_ RTC_GUARDED_BY(&mutex_); |
| 64 | int height_ RTC_GUARDED_BY(&mutex_); |
| 65 | std::vector<std::unique_ptr<Square>> squares_ RTC_GUARDED_BY(&mutex_); |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
Artem Titov | 33f9d2b | 2019-12-05 15:59:00 +0100 | [diff] [blame] | 68 | class YuvFileGenerator : public FrameGeneratorInterface { |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 69 | public: |
Artem Titov | 33f9d2b | 2019-12-05 15:59:00 +0100 | [diff] [blame] | 70 | YuvFileGenerator(std::vector<FILE*> files, |
| 71 | size_t width, |
| 72 | size_t height, |
| 73 | int frame_repeat_count); |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 74 | |
Artem Titov | 33f9d2b | 2019-12-05 15:59:00 +0100 | [diff] [blame] | 75 | ~YuvFileGenerator(); |
Emircan Uysaler | 207a75d | 2018-03-12 14:12:14 -0700 | [diff] [blame] | 76 | |
Artem Titov | 33f9d2b | 2019-12-05 15:59:00 +0100 | [diff] [blame] | 77 | VideoFrameData NextFrame() override; |
| 78 | void ChangeResolution(size_t width, size_t height) override { |
Per Kjellander | f5770a0 | 2022-01-12 13:43:31 +0100 | [diff] [blame] | 79 | RTC_LOG(LS_WARNING) |
| 80 | << "ScrollingImageFrameGenerator::ChangeResolution not implemented"; |
Artem Titov | 33f9d2b | 2019-12-05 15:59:00 +0100 | [diff] [blame] | 81 | } |
sprang@webrtc.org | 131bea8 | 2015-02-18 12:46:06 +0000 | [diff] [blame] | 82 | |
Artem Titov | 33f9d2b | 2019-12-05 15:59:00 +0100 | [diff] [blame] | 83 | private: |
| 84 | // Returns true if the new frame was loaded. |
| 85 | // False only in case of a single file with a single frame in it. |
| 86 | bool ReadNextFrame(); |
sprang | d635895 | 2015-07-29 07:58:13 -0700 | [diff] [blame] | 87 | |
Artem Titov | 33f9d2b | 2019-12-05 15:59:00 +0100 | [diff] [blame] | 88 | size_t file_index_; |
| 89 | size_t frame_index_; |
| 90 | const std::vector<FILE*> files_; |
| 91 | const size_t width_; |
| 92 | const size_t height_; |
| 93 | const size_t frame_size_; |
| 94 | const std::unique_ptr<uint8_t[]> frame_buffer_; |
| 95 | const int frame_display_count_; |
| 96 | int current_display_count_; |
| 97 | rtc::scoped_refptr<I420Buffer> last_read_buffer_; |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 98 | }; |
Artem Titov | 33f9d2b | 2019-12-05 15:59:00 +0100 | [diff] [blame] | 99 | |
| 100 | // SlideGenerator works similarly to YuvFileGenerator but it fills the frames |
| 101 | // with randomly sized and colored squares instead of reading their content |
| 102 | // from files. |
| 103 | class SlideGenerator : public FrameGeneratorInterface { |
| 104 | public: |
| 105 | SlideGenerator(int width, int height, int frame_repeat_count); |
| 106 | |
| 107 | VideoFrameData NextFrame() override; |
| 108 | void ChangeResolution(size_t width, size_t height) override { |
Per Kjellander | f5770a0 | 2022-01-12 13:43:31 +0100 | [diff] [blame] | 109 | RTC_LOG(LS_WARNING) << "SlideGenerator::ChangeResolution not implemented"; |
Artem Titov | 33f9d2b | 2019-12-05 15:59:00 +0100 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | private: |
| 113 | // Generates some randomly sized and colored squares scattered |
| 114 | // over the frame. |
| 115 | void GenerateNewFrame(); |
| 116 | |
| 117 | const int width_; |
| 118 | const int height_; |
| 119 | const int frame_display_count_; |
| 120 | int current_display_count_; |
| 121 | Random random_generator_; |
| 122 | rtc::scoped_refptr<I420Buffer> buffer_; |
| 123 | }; |
| 124 | |
| 125 | class ScrollingImageFrameGenerator : public FrameGeneratorInterface { |
| 126 | public: |
| 127 | ScrollingImageFrameGenerator(Clock* clock, |
| 128 | const std::vector<FILE*>& files, |
| 129 | size_t source_width, |
| 130 | size_t source_height, |
| 131 | size_t target_width, |
| 132 | size_t target_height, |
| 133 | int64_t scroll_time_ms, |
| 134 | int64_t pause_time_ms); |
| 135 | ~ScrollingImageFrameGenerator() override = default; |
| 136 | |
| 137 | VideoFrameData NextFrame() override; |
| 138 | void ChangeResolution(size_t width, size_t height) override { |
Per Kjellander | f5770a0 | 2022-01-12 13:43:31 +0100 | [diff] [blame] | 139 | RTC_LOG(LS_WARNING) |
| 140 | << "ScrollingImageFrameGenerator::ChangeResolution not implemented"; |
Artem Titov | 33f9d2b | 2019-12-05 15:59:00 +0100 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | private: |
| 144 | void UpdateSourceFrame(size_t frame_num); |
| 145 | void CropSourceToScrolledImage(double scroll_factor); |
| 146 | |
| 147 | Clock* const clock_; |
| 148 | const int64_t start_time_; |
| 149 | const int64_t scroll_time_; |
| 150 | const int64_t pause_time_; |
| 151 | const size_t num_frames_; |
| 152 | const int target_width_; |
| 153 | const int target_height_; |
| 154 | |
| 155 | size_t current_frame_num_; |
| 156 | bool prev_frame_not_scrolled_; |
| 157 | VideoFrameData current_source_frame_; |
| 158 | VideoFrameData current_frame_; |
| 159 | YuvFileGenerator file_generator_; |
| 160 | }; |
| 161 | |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 162 | } // namespace test |
| 163 | } // namespace webrtc |
| 164 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 165 | #endif // TEST_FRAME_GENERATOR_H_ |