blob: 6f59c1ed0b7bdf7f85e45ea06039982405c3cee6 [file] [log] [blame]
andresp@webrtc.orgab654952013-09-19 12:14:03 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020010#ifndef TEST_FRAME_GENERATOR_H_
11#define TEST_FRAME_GENERATOR_H_
andresp@webrtc.orgab654952013-09-19 12:14:03 +000012
perkja8ba1952017-02-27 06:52:10 -080013#include <memory>
sprang@webrtc.org131bea82015-02-18 12:46:06 +000014#include <string>
15#include <vector>
16
Artem Titov33f9d2b2019-12-05 15:59:00 +010017#include "api/scoped_refptr.h"
Artem Titov503d7232019-12-04 12:37:13 +010018#include "api/test/frame_generator_interface.h"
Artem Titov33f9d2b2019-12-05 15:59:00 +010019#include "api/video/i420_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "api/video/video_frame.h"
Artem Titov33f9d2b2019-12-05 15:59:00 +010021#include "api/video/video_frame_buffer.h"
Niels Möller0327c2d2018-05-21 14:09:31 +020022#include "api/video/video_source_interface.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "rtc_base/critical_section.h"
Artem Titov33f9d2b2019-12-05 15:59:00 +010024#include "rtc_base/random.h"
25#include "system_wrappers/include/clock.h"
andresp@webrtc.orgab654952013-09-19 12:14:03 +000026
27namespace webrtc {
28namespace test {
29
Artem Titov33f9d2b2019-12-05 15:59:00 +010030// SquareGenerator is a FrameGenerator that draws a given amount of randomly
31// sized and colored squares. Between each new generated frame, the squares
32// are moved slightly towards the lower right corner.
33class SquareGenerator : public FrameGeneratorInterface {
perkja49cbd32016-09-16 07:53:41 -070034 public:
Artem Titov33f9d2b2019-12-05 15:59:00 +010035 SquareGenerator(int width, int height, OutputType type, int num_squares);
perkja49cbd32016-09-16 07:53:41 -070036
Artem Titov33f9d2b2019-12-05 15:59:00 +010037 void ChangeResolution(size_t width, size_t height) override;
38 VideoFrameData NextFrame() override;
39
40 private:
41 rtc::scoped_refptr<I420Buffer> CreateI420Buffer(int width, int height);
42
43 class Square {
44 public:
45 Square(int width, int height, int seed);
46
47 void Draw(const rtc::scoped_refptr<VideoFrameBuffer>& frame_buffer);
48
49 private:
50 Random random_generator_;
51 int x_;
52 int y_;
53 const int length_;
54 const uint8_t yuv_y_;
55 const uint8_t yuv_u_;
56 const uint8_t yuv_v_;
57 const uint8_t yuv_a_;
58 };
perkja49cbd32016-09-16 07:53:41 -070059
60 rtc::CriticalSection crit_;
Artem Titov33f9d2b2019-12-05 15:59:00 +010061 const OutputType type_;
62 int width_ RTC_GUARDED_BY(&crit_);
63 int height_ RTC_GUARDED_BY(&crit_);
64 std::vector<std::unique_ptr<Square>> squares_ RTC_GUARDED_BY(&crit_);
perkja49cbd32016-09-16 07:53:41 -070065};
66
Artem Titov33f9d2b2019-12-05 15:59:00 +010067class YuvFileGenerator : public FrameGeneratorInterface {
andresp@webrtc.orgab654952013-09-19 12:14:03 +000068 public:
Artem Titov33f9d2b2019-12-05 15:59:00 +010069 YuvFileGenerator(std::vector<FILE*> files,
70 size_t width,
71 size_t height,
72 int frame_repeat_count);
andresp@webrtc.orgab654952013-09-19 12:14:03 +000073
Artem Titov33f9d2b2019-12-05 15:59:00 +010074 ~YuvFileGenerator();
Emircan Uysaler207a75d2018-03-12 14:12:14 -070075
Artem Titov33f9d2b2019-12-05 15:59:00 +010076 VideoFrameData NextFrame() override;
77 void ChangeResolution(size_t width, size_t height) override {
78 RTC_NOTREACHED();
79 }
sprang@webrtc.org131bea82015-02-18 12:46:06 +000080
Artem Titov33f9d2b2019-12-05 15:59:00 +010081 private:
82 // Returns true if the new frame was loaded.
83 // False only in case of a single file with a single frame in it.
84 bool ReadNextFrame();
sprangd6358952015-07-29 07:58:13 -070085
Artem Titov33f9d2b2019-12-05 15:59:00 +010086 size_t file_index_;
87 size_t frame_index_;
88 const std::vector<FILE*> files_;
89 const size_t width_;
90 const size_t height_;
91 const size_t frame_size_;
92 const std::unique_ptr<uint8_t[]> frame_buffer_;
93 const int frame_display_count_;
94 int current_display_count_;
95 rtc::scoped_refptr<I420Buffer> last_read_buffer_;
andresp@webrtc.orgab654952013-09-19 12:14:03 +000096};
Artem Titov33f9d2b2019-12-05 15:59:00 +010097
98// SlideGenerator works similarly to YuvFileGenerator but it fills the frames
99// with randomly sized and colored squares instead of reading their content
100// from files.
101class SlideGenerator : public FrameGeneratorInterface {
102 public:
103 SlideGenerator(int width, int height, int frame_repeat_count);
104
105 VideoFrameData NextFrame() override;
106 void ChangeResolution(size_t width, size_t height) override {
107 RTC_NOTREACHED();
108 }
109
110 private:
111 // Generates some randomly sized and colored squares scattered
112 // over the frame.
113 void GenerateNewFrame();
114
115 const int width_;
116 const int height_;
117 const int frame_display_count_;
118 int current_display_count_;
119 Random random_generator_;
120 rtc::scoped_refptr<I420Buffer> buffer_;
121};
122
123class ScrollingImageFrameGenerator : public FrameGeneratorInterface {
124 public:
125 ScrollingImageFrameGenerator(Clock* clock,
126 const std::vector<FILE*>& files,
127 size_t source_width,
128 size_t source_height,
129 size_t target_width,
130 size_t target_height,
131 int64_t scroll_time_ms,
132 int64_t pause_time_ms);
133 ~ScrollingImageFrameGenerator() override = default;
134
135 VideoFrameData NextFrame() override;
136 void ChangeResolution(size_t width, size_t height) override {
137 RTC_NOTREACHED();
138 }
139
140 private:
141 void UpdateSourceFrame(size_t frame_num);
142 void CropSourceToScrolledImage(double scroll_factor);
143
144 Clock* const clock_;
145 const int64_t start_time_;
146 const int64_t scroll_time_;
147 const int64_t pause_time_;
148 const size_t num_frames_;
149 const int target_width_;
150 const int target_height_;
151
152 size_t current_frame_num_;
153 bool prev_frame_not_scrolled_;
154 VideoFrameData current_source_frame_;
155 VideoFrameData current_frame_;
156 YuvFileGenerator file_generator_;
157};
158
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000159} // namespace test
160} // namespace webrtc
161
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200162#endif // TEST_FRAME_GENERATOR_H_