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
4#include <QtMultimedia/qwindowcapture.h>
5
6#include <QtCore/private/qobject_p.h>
7
8#include <QtMultimedia/qmediacapturesession.h>
9#include <QtMultimedia/private/qplatformmediaintegration_p.h>
10#include <QtMultimedia/private/qplatformsurfacecapture_p.h>
11
12QT_BEGIN_NAMESPACE
13
14static QWindowCapture::Error toWindowCaptureError(QPlatformSurfaceCapture::Error error)
15{
16 return static_cast<QWindowCapture::Error>(error);
17}
18
25
26/*!
27 \class QWindowCapture
28 \inmodule QtMultimedia
29 \ingroup multimedia
30 \ingroup multimedia_video
31 \since 6.6
32
33 \brief This class is used for capturing a window.
34
35 The class captures a window. It is managed by
36 the QMediaCaptureSession class where the captured window can be displayed
37 in a video preview object or recorded to a file.
38
39 \include qwindowcapture-limitations.qdocinc {content} {Q}
40
41 \sa QMediaCaptureSession, QCapturableWindow
42*/
43/*!
44 \qmltype WindowCapture
45 \nativetype QWindowCapture
46 \inqmlmodule QtMultimedia
47 \ingroup multimedia_qml
48 \ingroup multimedia_video_qml
49 \since 6.6
50
51 \brief This type is used for capturing a window.
52
53 WindowCapture captures a window. It is managed by
54 MediaCaptureSession where the captured window can be displayed
55 in a video preview object or recorded to a file.
56
57 \include qwindowcapture-limitations.qdocinc {content} {}
58
59 \sa CaptureSession, CapturableWindow
60*/
61
62/*!
63 \enum QWindowCapture::Error
64
65 Enumerates error codes that can be signaled by the QWindowCapture class.
66 errorString() provides detailed information about the error cause.
67
68 \value NoError No error
69 \value InternalError Internal window capturing driver error
70 \value CapturingNotSupported Window capturing is not supported
71 \value CaptureFailed Capturing window failed
72 \value NotFound Selected window not found
73*/
74
75/*!
76 Constructs a new QWindowCapture object with \a parent.
77*/
78QWindowCapture::QWindowCapture(QObject *parent) : QObject(*new QWindowCapturePrivate, parent)
79{
80 Q_D(QWindowCapture);
81
82 qRegisterMetaType<QCapturableWindow>();
83
84 auto platformCapture = QPlatformMediaIntegration::instance()->createWindowCapture(this);
85
86 if (platformCapture) {
87 connect(platformCapture, &QPlatformSurfaceCapture::activeChanged, this,
88 &QWindowCapture::activeChanged);
89 connect(platformCapture, &QPlatformSurfaceCapture::errorChanged, this,
90 &QWindowCapture::errorChanged);
91 connect(platformCapture, &QPlatformSurfaceCapture::errorOccurred, this,
92 [this](QPlatformSurfaceCapture::Error error, const QString &errorString) {
93 emit errorOccurred(toWindowCaptureError(error), errorString);
94 });
95 connect(platformCapture,
96 qOverload<QCapturableWindow>(&QPlatformSurfaceCapture::sourceChanged), this,
97 &QWindowCapture::windowChanged);
98
99 connect(platformCapture, &QPlatformSurfaceCapture::frameRateChanged, this,
100 &QWindowCapture::frameRateChanged);
101
102 d->platformWindowCapture.reset(platformCapture);
103 }
104}
105
106/*!
107 Destroys the object.
108 */
109QWindowCapture::~QWindowCapture()
110{
111 Q_D(QWindowCapture);
112
113 d->platformWindowCapture.reset();
114
115 if (d->captureSession)
116 d->captureSession->setWindowCapture(nullptr);
117}
118
119/*!
120 \qmlmethod list<CapturableWindow> QtMultimedia::WindowCapture::capturableWindows()
121
122 Returns a list of CapturableWindow objects that is available for capturing.
123
124 \note On macOS, invoking this method will trigger the "Screen Recording" permission
125 dialog. If permissions have not yet been granted, this method will return an empty list.
126 Invoking it multiple times will bring this dialog to the foreground.
127*/
128/*!
129 \fn QList<QCapturableWindow> QWindowCapture::capturableWindows()
130
131 Returns a list of QCapturableWindow objects that is available for capturing.
132
133 \note On macOS, invoking this method will trigger the "Screen Recording" permission
134 dialog. If permissions have not yet been granted, this method will return an empty list.
135 Invoking it multiple times will bring this dialog to the foreground.
136 */
137QList<QCapturableWindow> QWindowCapture::capturableWindows()
138{
139 return QPlatformMediaIntegration::instance()->capturableWindowsList();
140}
141
142QMediaCaptureSession *QWindowCapture::captureSession() const
143{
144 Q_D(const QWindowCapture);
145
146 return d->captureSession;
147}
148
149/*!
150 \qmlproperty Window QtMultimedia::WindowCapture::window
151 Describes the window for capturing.
152
153 Setting this property to an invalid window on an active
154 WindowCapture will cause it to go inactive and emit
155 an error.
156
157 \sa capturableWindows
158*/
159
160/*!
161 \property QWindowCapture::window
162 \brief the window for capturing.
163
164 Setting this property to an invalid window on an active
165 QWindowCapture will cause it to go inactive and emit
166 an error.
167
168 \sa QWindowCapture::capturableWindows
169*/
170QCapturableWindow QWindowCapture::window() const
171{
172 Q_D(const QWindowCapture);
173
174 return d->platformWindowCapture ? d->platformWindowCapture->source<QCapturableWindow>()
175 : QCapturableWindow();
176}
177
178void QWindowCapture::setWindow(QCapturableWindow window)
179{
180 Q_D(QWindowCapture);
181
182 if (d->platformWindowCapture)
183 d->platformWindowCapture->setSource(window);
184}
185
186/*!
187 \qmlproperty bool QtMultimedia::WindowCapture::active
188 Describes whether the capturing is currently active.
189*/
190
191/*!
192 \property QWindowCapture::active
193 \brief whether the capturing is currently active.
194
195 \sa start(), stop()
196*/
197bool QWindowCapture::isActive() const
198{
199 Q_D(const QWindowCapture);
200
201 return d->platformWindowCapture && d->platformWindowCapture->isActive();
202}
203
204void QWindowCapture::setActive(bool active)
205{
206 Q_D(QWindowCapture);
207
208 if (d->platformWindowCapture)
209 d->platformWindowCapture->setActive(active);
210}
211
212/*!
213 \since 6.12
214 \property QWindowCapture::frameRate
215 \brief The target window capture framerate.
216
217 Actual frame rate depends on the platform. For platforms with fixed rate capture, this
218 frame rate is followed. For platforms with variable rate capture, this frame rate is either
219 used as the polling rate (maximum frame rate) or completely ignored.
220
221 If left unset, a platform-dependent default is used.
222
223 Any changes to this property are applied the next time the QWindowCapture goes active.
224*/
225void QWindowCapture::setFrameRate(std::optional<qreal> frameRate)
226{
227 Q_D(QWindowCapture);
228
229 if (d->platformWindowCapture)
230 d->platformWindowCapture->setFrameRate(frameRate);
231}
232
233std::optional<qreal> QWindowCapture::frameRate() const
234{
235 Q_D(const QWindowCapture);
236
237 return d->platformWindowCapture ? d->platformWindowCapture->frameRate() : std::nullopt;
238}
239
240void QWindowCapture::resetFrameRate()
241{
242 setFrameRate(std::nullopt);
243}
244
245/*!
246 \qmlmethod void QtMultimedia::WindowCapture::start()
247
248 Starts capturing the \l window.
249
250 This is equivalent to setting the \l active property to \c true.
251*/
252
253/*!
254 \fn void QWindowCapture::start()
255
256 Starts capturing the \l window.
257
258 This is equivalent to setting the \l active property to true.
259*/
260
261/*!
262 \qmlmethod void QtMultimedia::WindowCapture::stop()
263
264 Stops capturing.
265
266 This is equivalent to setting the \l active property to \c false.
267*/
268
269/*!
270 \fn void QWindowCapture::stop()
271
272 Stops capturing.
273
274 This is equivalent to setting the \l active property to false.
275*/
276
277/*!
278 \qmlsignal QtMultimedia::WindowCapture::errorChanged()
279
280 This signal is emitted when the \l{error} or \l{errorString} properties are changed.
281
282 This signal is not emitted whenever multiple identical errors are raised. To track such
283 errors, use the signal \l errorOccurred.
284*/
285
286/*!
287 \fn void QWindowCapture::errorChanged()
288
289 This signal is emitted when the \l{error} or \l{errorString} properties are changed.
290
291 This signal is not emitted whenever multiple identical errors are raised. To track such
292 errors, use the signal \l errorOccurred.
293*/
294
295/*!
296 \qmlproperty enumeration QtMultimedia::WindowCapture::error
297 Returns a code of the last error.
298
299 \qmlenumeratorsfrom QWindowCapture::Error
300*/
301
302/*!
303 \property QWindowCapture::error
304 \brief the code of the last error.
305*/
306QWindowCapture::Error QWindowCapture::error() const
307{
308 Q_D(const QWindowCapture);
309
310 return d->platformWindowCapture ? toWindowCaptureError(d->platformWindowCapture->error())
311 : CapturingNotSupported;
312}
313
314/*!
315 \qmlsignal QtMultimedia::WindowCapture::errorOccurred(int error, string errorString)
316
317 Signals when an \a error occurs, along with the \a errorString.
318
319 For the error parameter, see the enumeration table in
320 \l {QtMultimedia::WindowCapture::error}{error} for what values may
321 be passed.
322
323 \sa {QtMultimedia::WindowCapture::error}{error}
324*/
325/*!
326 \fn void QWindowCapture::errorOccurred(QWindowCapture::Error error, const QString &errorString)
327
328 Signals when an \a error occurs, along with the \a errorString.
329*/
330/*!
331 \qmlproperty string QtMultimedia::WindowCapture::errorString
332 Returns a human readable string describing the cause of error.
333*/
334
335/*!
336 \property QWindowCapture::errorString
337 \brief a human readable string describing the cause of error.
338*/
339QString QWindowCapture::errorString() const
340{
341 Q_D(const QWindowCapture);
342
343 return d->platformWindowCapture
344 ? d->platformWindowCapture->errorString()
345 : QLatin1StringView("Capturing is not support on this platform");
346}
347
348void QWindowCapture::setCaptureSession(QMediaCaptureSession *captureSession)
349{
350 Q_D(QWindowCapture);
351
352 d->captureSession = captureSession;
353}
354
355QPlatformSurfaceCapture *QWindowCapture::platformWindowCapture() const
356{
357 Q_D(const QWindowCapture);
358
359 return d->platformWindowCapture.get();
360}
361
362QT_END_NAMESPACE
363
364#include "moc_qwindowcapture.cpp"
QMediaCaptureSession * captureSession
std::unique_ptr< QPlatformSurfaceCapture > platformWindowCapture