Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
framegenerator.cpp
Go to the documentation of this file.
1// Copyright (C) 2024 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
5
6#include <private/qplatformmediaintegration_p.h>
7
9
10void VideoGenerator::setPattern(ImagePattern pattern)
11{
12 m_pattern = pattern;
13}
14
16{
17 m_maxFrameCount = count;
18}
19
20void VideoGenerator::setSize(QSize size)
21{
22 m_size = size;
23}
24
25void VideoGenerator::setPixelFormat(QVideoFrameFormat::PixelFormat pixelFormat)
26{
27 m_pixelFormat = pixelFormat;
28}
29
30void VideoGenerator::setFrameRate(double rate)
31{
32 m_frameRate = rate;
33}
34
35void VideoGenerator::setPeriod(std::chrono::milliseconds period)
36{
37 m_period = period;
38}
39
40void VideoGenerator::setPresentationRotation(QtVideo::Rotation rotation)
41{
42 m_presentationRotation = rotation;
43}
44
46{
47 m_presentationMirrored = mirror;
48}
49
51{
52 m_emitEmptyFrameOnStop = true;
53}
54
55static void fillColoredSquares(QImage& image)
56{
57 QList<QColor> colors = { Qt::red, Qt::green, Qt::blue, Qt::yellow };
58 const int width = image.width();
59 const int height = image.height();
60
61 for (int j = 0; j < height; ++j) {
62 for (int i = 0; i < width; ++i) {
63 const int colorX = i < width / 2 ? 0 : 1;
64 const int colorY = j < height / 2 ? 0 : 1;
65 const int colorIndex = colorX + 2 * colorY;
66 image.setPixel(i, j, colors[colorIndex].rgb());
67 }
68 }
69}
70
72{
73 using namespace std::chrono;
74
75 QImage image(m_size, QImage::Format_ARGB32);
76 switch (m_pattern) {
77 case ImagePattern::SingleColor:
78 image.fill(colors[m_frameIndex % colors.size()]);
79 break;
80 case ImagePattern::ColoredSquares:
81 fillColoredSquares(image);
82 break;
83 }
84
85 QVideoFrame rgbFrame(image);
86 QVideoFrameFormat outputFormat { m_size, m_pixelFormat };
87 QVideoFrame frame =
88 QPlatformMediaIntegration::instance()->convertVideoFrame(rgbFrame, outputFormat);
89
90 if (m_frameRate)
91 frame.setStreamFrameRate(*m_frameRate);
92
93 if (m_period) {
94 frame.setStartTime(duration_cast<microseconds>(*m_period).count() * m_frameIndex);
95 frame.setEndTime(duration_cast<microseconds>(*m_period).count() * (m_frameIndex + 1));
96 }
97
98 if (m_presentationRotation)
99 frame.setRotation(*m_presentationRotation);
100
101 if (m_presentationMirrored)
102 frame.setMirrored(*m_presentationMirrored);
103
104 return frame;
105}
106
107void VideoGenerator::nextFrame()
108{
109 if (m_frameIndex == m_maxFrameCount) {
110 emit done();
111 if (m_emitEmptyFrameOnStop)
112 emit frameCreated({});
113 return;
114 }
115
116 const QVideoFrame frame = createFrame();
117 emit frameCreated(frame);
118 ++m_frameIndex;
119}
120
121QT_END_NAMESPACE
122
123#include "moc_framegenerator_p.cpp"
QVideoFrame createFrame()
void setSize(QSize size)
void setPeriod(std::chrono::milliseconds period)
void emitEmptyFrameOnStop()
void setPresentationMirrored(bool mirror)
void setFrameCount(int count)
void setFrameRate(double rate)
void setPresentationRotation(QtVideo::Rotation rotation)
void setPixelFormat(QVideoFrameFormat::PixelFormat pixelFormat)
static void fillColoredSquares(QImage &image)
Combined button and popup list for selecting options.