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
qwindowcapture.cpp
Go to the documentation of this file.
1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
7#include "private/qobject_p.h"
8#include "private/qplatformsurfacecapture_p.h"
9
11
12static QWindowCapture::Error toWindowCaptureError(QPlatformSurfaceCapture::Error error)
13{
14 return static_cast<QWindowCapture::Error>(error);
15}
16
23
24/*!
25 \class QWindowCapture
26 \inmodule QtMultimedia
27 \ingroup multimedia
28 \ingroup multimedia_video
29 \since 6.6
30
31 \brief This class is used for capturing a window.
32
33 The class captures a window. It is managed by
34 the QMediaCaptureSession class where the captured window can be displayed
35 in a video preview object or recorded to a file.
36
37 \include qwindowcapture-limitations.qdocinc {content} {Q}
38
39 \sa QMediaCaptureSession, QCapturableWindow
40*/
41/*!
42 \qmltype WindowCapture
43 \nativetype QWindowCapture
44 \inqmlmodule QtMultimedia
45 \ingroup multimedia_qml
46 \ingroup multimedia_video_qml
47 \since 6.6
48
49 \brief This type is used for capturing a window.
50
51 WindowCapture captures a window. It is managed by
52 MediaCaptureSession where the captured window can be displayed
53 in a video preview object or recorded to a file.
54
55 \include qwindowcapture-limitations.qdocinc {content} {}
56
57 \sa CaptureSession, CapturableWindow
58*/
59
60/*!
61 \enum QWindowCapture::Error
62
63 Enumerates error codes that can be signaled by the QWindowCapture class.
64 errorString() provides detailed information about the error cause.
65
66 \value NoError No error
67 \value InternalError Internal window capturing driver error
68 \value CapturingNotSupported Window capturing is not supported
69 \value CaptureFailed Capturing window failed
70 \value NotFound Selected window not found
71*/
72
73/*!
74 Constructs a new QWindowCapture object with \a parent.
75*/
76QWindowCapture::QWindowCapture(QObject *parent) : QObject(*new QWindowCapturePrivate, parent)
77{
78 Q_D(QWindowCapture);
79
80 qRegisterMetaType<QCapturableWindow>();
81
82 auto platformCapture = QPlatformMediaIntegration::instance()->createWindowCapture(this);
83
84 if (platformCapture) {
85 connect(platformCapture, &QPlatformSurfaceCapture::activeChanged, this,
86 &QWindowCapture::activeChanged);
87 connect(platformCapture, &QPlatformSurfaceCapture::errorChanged, this,
88 &QWindowCapture::errorChanged);
89 connect(platformCapture, &QPlatformSurfaceCapture::errorOccurred, this,
90 [this](QPlatformSurfaceCapture::Error error, const QString &errorString) {
91 emit errorOccurred(toWindowCaptureError(error), errorString);
92 });
93 connect(platformCapture,
94 qOverload<QCapturableWindow>(&QPlatformSurfaceCapture::sourceChanged), this,
95 &QWindowCapture::windowChanged);
96
97 connect(platformCapture, &QPlatformSurfaceCapture::frameRateChanged, this,
98 &QWindowCapture::frameRateChanged);
99
100 d->platformWindowCapture.reset(platformCapture);
101 }
102}
103
104/*!
105 Destroys the object.
106 */
107QWindowCapture::~QWindowCapture()
108{
109 Q_D(QWindowCapture);
110
111 d->platformWindowCapture.reset();
112
113 if (d->captureSession)
114 d->captureSession->setWindowCapture(nullptr);
115}
116
117/*!
118 \qmlmethod list<CapturableWindow> QtMultimedia::WindowCapture::capturableWindows()
119
120 Returns a list of CapturableWindow objects that is available for capturing.
121*/
122/*!
123 \fn QList<QCapturableWindow> QWindowCapture::capturableWindows()
124
125 Returns a list of QCapturableWindow objects that is available for capturing.
126 */
127QList<QCapturableWindow> QWindowCapture::capturableWindows()
128{
129 return QPlatformMediaIntegration::instance()->capturableWindowsList();
130}
131
132QMediaCaptureSession *QWindowCapture::captureSession() const
133{
134 Q_D(const QWindowCapture);
135
136 return d->captureSession;
137}
138
139/*!
140 \qmlproperty Window QtMultimedia::WindowCapture::window
141 Describes the window for capturing.
142
143 \sa QtMultimedia::WindowCapture::capturableWindows
144*/
145
146/*!
147 \property QWindowCapture::window
148 \brief the window for capturing.
149
150 \sa QWindowCapture::capturableWindows
151*/
152QCapturableWindow QWindowCapture::window() const
153{
154 Q_D(const QWindowCapture);
155
156 return d->platformWindowCapture ? d->platformWindowCapture->source<QCapturableWindow>()
157 : QCapturableWindow();
158}
159
160void QWindowCapture::setWindow(QCapturableWindow window)
161{
162 Q_D(QWindowCapture);
163
164 if (d->platformWindowCapture)
165 d->platformWindowCapture->setSource(window);
166}
167
168/*!
169 \qmlproperty bool QtMultimedia::WindowCapture::active
170 Describes whether the capturing is currently active.
171*/
172
173/*!
174 \property QWindowCapture::active
175 \brief whether the capturing is currently active.
176
177 \sa start(), stop()
178*/
179bool QWindowCapture::isActive() const
180{
181 Q_D(const QWindowCapture);
182
183 return d->platformWindowCapture && d->platformWindowCapture->isActive();
184}
185
186void QWindowCapture::setActive(bool active)
187{
188 Q_D(QWindowCapture);
189
190 if (d->platformWindowCapture)
191 d->platformWindowCapture->setActive(active);
192}
193
194/*!
195 \since 6.12
196 \property QWindowCapture::frameRate
197 \brief The target window capture framerate.
198
199 Actual frame rate depends on the platform. For platforms with fixed rate capture, this
200 frame rate is followed. For platforms with variable rate capture, this frame rate is either
201 used as the polling rate (maximum frame rate) or completely ignored.
202
203 If left unset, a platform-dependent default is used.
204*/
205void QWindowCapture::setFrameRate(std::optional<qreal> frameRate)
206{
207 Q_D(QWindowCapture);
208
209 if (d->platformWindowCapture)
210 d->platformWindowCapture->setFrameRate(frameRate);
211}
212
213std::optional<qreal> QWindowCapture::frameRate() const
214{
215 Q_D(const QWindowCapture);
216
217 return d->platformWindowCapture ? d->platformWindowCapture->frameRate() : std::nullopt;
218}
219
220void QWindowCapture::resetFrameRate()
221{
222 setFrameRate(std::nullopt);
223}
224
225/*!
226 \qmlmethod void QtMultimedia::WindowCapture::start()
227
228 Starts capturing the \l window.
229
230 This is equivalent to setting the \l active property to \c true.
231*/
232
233/*!
234 \fn void QWindowCapture::start()
235
236 Starts capturing the \l window.
237
238 This is equivalent to setting the \l active property to true.
239*/
240
241/*!
242 \qmlmethod void QtMultimedia::WindowCapture::stop()
243
244 Stops capturing.
245
246 This is equivalent to setting the \l active property to \c false.
247*/
248
249/*!
250 \fn void QWindowCapture::stop()
251
252 Stops capturing.
253
254 This is equivalent to setting the \l active property to false.
255*/
256
257
258/*!
259 \qmlproperty enumeration QtMultimedia::WindowCapture::error
260 Returns a code of the last error.
261*/
262
263/*!
264 \property QWindowCapture::error
265 \brief the code of the last error.
266*/
267QWindowCapture::Error QWindowCapture::error() const
268{
269 Q_D(const QWindowCapture);
270
271 return d->platformWindowCapture ? toWindowCaptureError(d->platformWindowCapture->error())
272 : CapturingNotSupported;
273}
274
275/*!
276 \fn void QWindowCapture::errorOccurred(QWindowCapture::Error error, const QString &errorString)
277
278 Signals when an \a error occurs, along with the \a errorString.
279*/
280/*!
281 \qmlproperty string QtMultimedia::WindowCapture::errorString
282 Returns a human readable string describing the cause of error.
283*/
284
285/*!
286 \property QWindowCapture::errorString
287 \brief a human readable string describing the cause of error.
288*/
289QString QWindowCapture::errorString() const
290{
291 Q_D(const QWindowCapture);
292
293 return d->platformWindowCapture
294 ? d->platformWindowCapture->errorString()
295 : QLatin1StringView("Capturing is not support on this platform");
296}
297
298void QWindowCapture::setCaptureSession(QMediaCaptureSession *captureSession)
299{
300 Q_D(QWindowCapture);
301
302 d->captureSession = captureSession;
303}
304
305QPlatformSurfaceCapture *QWindowCapture::platformWindowCapture() const
306{
307 Q_D(const QWindowCapture);
308
309 return d->platformWindowCapture.get();
310}
311
312QT_END_NAMESPACE
313
314#include "moc_qwindowcapture.cpp"
QMediaCaptureSession * captureSession
std::unique_ptr< QPlatformSurfaceCapture > platformWindowCapture
Combined button and popup list for selecting options.
static QT_BEGIN_NAMESPACE QWindowCapture::Error toWindowCaptureError(QPlatformSurfaceCapture::Error error)