blob: 3e2a4cb708fec873590c5c9ba9374f7f327b7e43 [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"
Per Kjellanderf5770a02022-01-12 13:43:31 +010023#include "rtc_base/logging.h"
Artem Titov33f9d2b2019-12-05 15:59:00 +010024#include "rtc_base/random.h"
Markus Handella5a4be12020-07-08 16:09:21 +020025#include "rtc_base/synchronization/mutex.h"
Artem Titov33f9d2b2019-12-05 15:59:00 +010026#include "system_wrappers/include/clock.h"
andresp@webrtc.orgab654952013-09-19 12:14:03 +000027
28namespace webrtc {
29namespace test {
30
Artem Titov33f9d2b2019-12-05 15:59:00 +010031// 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.
34class SquareGenerator : public FrameGeneratorInterface {
perkja49cbd32016-09-16 07:53:41 -070035 public:
Artem Titov33f9d2b2019-12-05 15:59:00 +010036 SquareGenerator(int width, int height, OutputType type, int num_squares);
perkja49cbd32016-09-16 07:53:41 -070037
Artem Titov33f9d2b2019-12-05 15:59:00 +010038 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 };
perkja49cbd32016-09-16 07:53:41 -070060
Markus Handella5a4be12020-07-08 16:09:21 +020061 Mutex mutex_;
Artem Titov33f9d2b2019-12-05 15:59:00 +010062 const OutputType type_;
Markus Handella5a4be12020-07-08 16:09:21 +020063 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_);
perkja49cbd32016-09-16 07:53:41 -070066};
67
Artem Titov33f9d2b2019-12-05 15:59:00 +010068class YuvFileGenerator : public FrameGeneratorInterface {
andresp@webrtc.orgab654952013-09-19 12:14:03 +000069 public:
Artem Titov33f9d2b2019-12-05 15:59:00 +010070 YuvFileGenerator(std::vector<FILE*> files,
71 size_t width,
72 size_t height,
73 int frame_repeat_count);
andresp@webrtc.orgab654952013-09-19 12:14:03 +000074
Artem Titov33f9d2b2019-12-05 15:59:00 +010075 ~YuvFileGenerator();
Emircan Uysaler207a75d2018-03-12 14:12:14 -070076
Artem Titov33f9d2b2019-12-05 15:59:00 +010077 VideoFrameData NextFrame() override;
78 void ChangeResolution(size_t width, size_t height) override {
Per Kjellanderf5770a02022-01-12 13:43:31 +010079 RTC_LOG(LS_WARNING)
80 << "ScrollingImageFrameGenerator::ChangeResolution not implemented";
Artem Titov33f9d2b2019-12-05 15:59:00 +010081 }
sprang@webrtc.org131bea82015-02-18 12:46:06 +000082
Artem Titov33f9d2b2019-12-05 15:59:00 +010083 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();
sprangd6358952015-07-29 07:58:13 -070087
Artem Titov33f9d2b2019-12-05 15:59:00 +010088 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.orgab654952013-09-19 12:14:03 +000098};
Artem Titov33f9d2b2019-12-05 15:59:00 +010099
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.
103class 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 Kjellanderf5770a02022-01-12 13:43:31 +0100109 RTC_LOG(LS_WARNING) << "SlideGenerator::ChangeResolution not implemented";
Artem Titov33f9d2b2019-12-05 15:59:00 +0100110 }
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
125class 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 Kjellanderf5770a02022-01-12 13:43:31 +0100139 RTC_LOG(LS_WARNING)
140 << "ScrollingImageFrameGenerator::ChangeResolution not implemented";
Artem Titov33f9d2b2019-12-05 15:59:00 +0100141 }
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.orgab654952013-09-19 12:14:03 +0000162} // namespace test
163} // namespace webrtc
164
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200165#endif // TEST_FRAME_GENERATOR_H_