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