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
qscreencapture.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 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 <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
13
14static QScreenCapture::Error toScreenCaptureError(QPlatformSurfaceCapture::Error error)
15{
16 return static_cast<QScreenCapture::Error>(error);
17}
18
25
26/*!
27 \class QScreenCapture
28 \inmodule QtMultimedia
29 \ingroup multimedia
30 \ingroup multimedia_video
31 \since 6.5
32
33 \brief This class is used for capturing a screen.
34
35 The class captures a screen. It is managed by
36 the \l QMediaCaptureSession class where the captured screen can be displayed
37 in a video preview object or recorded to a file.
38
39 The following snippet shows how to capture the primary screen and display
40 the result in a QVideoWidget:
41
42 \snippet multimedia-snippets/screencapturesnippets.cpp Basic setup
43
44 \include qscreencapture-limitations.qdocinc {content} {Q}
45
46 \sa QWindowCapture, QMediaCaptureSession
47*/
48/*!
49 \qmltype ScreenCapture
50 \nativetype QScreenCapture
51 \brief This type is used for capturing a screen.
52
53 \inqmlmodule QtMultimedia
54 \ingroup multimedia_qml
55 \ingroup multimedia_video_qml
56 \since 6.5
57
58 ScreenCapture captures a screen. It is managed by
59 \l{QtMultimedia::CaptureSession}{CaptureSession} where the captured
60 screen can be displayed in a video preview object or recorded to a
61 file.
62
63 The code below shows a simple capture session with ScreenCapture playing
64 back the captured primary screen view in \l {QtMultimedia::VideoOutput}{VideoOutput}.
65
66 \snippet multimedia-snippets/screencapturesnippets.qml Basic setup
67
68 \include qscreencapture-limitations.qdocinc {content} {}
69
70 \sa WindowCapture, CaptureSession
71*/
72
73QScreenCapture::QScreenCapture(QObject *parent)
74 : QObject(*new QScreenCapturePrivate, parent)
75{
76 Q_D(QScreenCapture);
77
78 auto platformCapture = QPlatformMediaIntegration::instance()->createScreenCapture(this);
79 if (platformCapture) {
80 connect(platformCapture, &QPlatformSurfaceCapture::activeChanged, this,
81 &QScreenCapture::activeChanged);
82 connect(platformCapture, &QPlatformSurfaceCapture::errorChanged, this,
83 &QScreenCapture::errorChanged);
84 connect(platformCapture, &QPlatformSurfaceCapture::errorOccurred, this,
85 [this](QPlatformSurfaceCapture::Error error, const QString &errorString) {
86 emit errorOccurred(toScreenCaptureError(error), errorString);
87 });
88
89 connect(platformCapture,
90 qOverload<QPlatformSurfaceCapture::ScreenSource>(
91 &QPlatformSurfaceCapture::sourceChanged),
92 this, &QScreenCapture::screenChanged);
93
94 connect(platformCapture, &QPlatformSurfaceCapture::frameRateChanged, this,
95 &QScreenCapture::maximumFrameRateChanged);
96
97 d->platformScreenCapture.reset(platformCapture);
98 }
99}
100
101QScreenCapture::~QScreenCapture()
102{
103 Q_D(QScreenCapture);
104
105 // Reset platformScreenCapture in the destructor to avoid having broken ref in the object.
106 d->platformScreenCapture.reset();
107
108 if (d->captureSession)
109 d->captureSession->setScreenCapture(nullptr);
110}
111
112/*!
113 \enum QScreenCapture::Error
114
115 Enumerates error codes that can be signaled by the QScreenCapture class.
116 errorString() provides detailed information about the error cause.
117
118 \value NoError No error
119 \value InternalError Internal screen capturing driver error
120 \value CapturingNotSupported Capturing is not supported
121 \value CaptureFailed Capturing screen failed
122 \value NotFound Selected screen not found
123*/
124
125/*!
126 Returns the capture session this QScreenCapture is connected to.
127
128 Use QMediaCaptureSession::setScreenCapture() to connect the screen capture
129 to a session.
130*/
131QMediaCaptureSession *QScreenCapture::captureSession() const
132{
133 Q_D(const QScreenCapture);
134
135 return d->captureSession;
136}
137
138/*!
139 \qmlproperty bool QtMultimedia::ScreenCapture::active
140 Describes whether the capturing is currently active.
141
142 \sa start(), stop()
143*/
144
145/*!
146 \property QScreenCapture::active
147 \brief whether the capturing is currently active.
148
149 \sa start(), stop()
150*/
151void QScreenCapture::setActive(bool active)
152{
153 Q_D(QScreenCapture);
154
155 if (d->platformScreenCapture)
156 d->platformScreenCapture->setActive(active);
157}
158
159bool QScreenCapture::isActive() const
160{
161 Q_D(const QScreenCapture);
162
163 return d->platformScreenCapture && d->platformScreenCapture->isActive();
164}
165
166/*!
167 \qmlproperty Screen QtMultimedia::ScreenCapture::screen
168 Describes the screen for capturing.
169
170 If null \c Screen is set, the primary screen will be selected
171 when the \c ScreenCapture instance gets activated.
172
173 \sa {QtQuick::Application::screens}{Application.screens}
174*/
175
176/*!
177 \property QScreenCapture::screen
178 \brief the screen for capturing.
179
180 If null \l QScreen is set, \l QGuiApplication::primaryScreen will be selected
181 when the \c QScreenCapture instance gets activated.
182
183 \sa QGuiApplication::screens(), QGuiApplication::primaryScreen()
184*/
185
186void QScreenCapture::setScreen(QScreen *screen)
187{
188 Q_D(QScreenCapture);
189
190 if (d->platformScreenCapture)
191 d->platformScreenCapture->setSource(QPlatformSurfaceCapture::ScreenSource(screen));
192}
193
194QScreen *QScreenCapture::screen() const
195{
196 Q_D(const QScreenCapture);
197
198 return d->platformScreenCapture
199 ? d->platformScreenCapture->source<QPlatformSurfaceCapture::ScreenSource>()
200 : nullptr;
201}
202
203/*!
204 \qmlsignal QtMultimedia::ScreenCapture::errorChanged()
205
206 This signal is emitted when the \l{error} or \l{errorString} properties are changed.
207
208 This signal is not emitted whenever multiple identical errors are raised. To track such
209 errors, use the signal \l errorOccurred.
210*/
211
212/*!
213 \fn void QScreenCapture::errorChanged()
214
215 This signal is emitted when the \l{error} or \l{errorString} properties are changed.
216
217 This signal is not emitted whenever multiple identical errors are raised. To track such
218 errors, use the signal \l errorOccurred.
219*/
220
221/*!
222 \qmlproperty enumeration QtMultimedia::ScreenCapture::error
223 Returns a code of the last error.
224
225 \qmlenumeratorsfrom QScreenCapture::Error
226*/
227
228/*!
229 \property QScreenCapture::error
230 \brief the code of the last error.
231*/
232QScreenCapture::Error QScreenCapture::error() const
233{
234 Q_D(const QScreenCapture);
235
236 return d->platformScreenCapture ? toScreenCaptureError(d->platformScreenCapture->error())
237 : CapturingNotSupported;
238}
239
240/*!
241 \qmlsignal QtMultimedia::ScreenCapture::errorOccurred(int error, string errorString)
242
243 Signals when an \a error occurs, along with the \a errorString.
244
245 For the error parameter, see the enumeration table in
246 \l {QtMultimedia::ScreenCapture::error}{error} for what values may
247 be passed.
248
249 \sa {QtMultimedia::ScreenCapture::error}{error}
250*/
251/*!
252 \fn void QScreenCapture::errorOccurred(QScreenCapture::Error error, const QString &errorString)
253
254 Signals when an \a error occurs, along with the \a errorString.
255*/
256/*!
257 \qmlproperty string QtMultimedia::ScreenCapture::errorString
258 Returns a human readable string describing the cause of error.
259*/
260
261/*!
262 \property QScreenCapture::errorString
263 \brief a human readable string describing the cause of error.
264*/
265QString QScreenCapture::errorString() const
266{
267 Q_D(const QScreenCapture);
268
269 return d->platformScreenCapture ? d->platformScreenCapture->errorString()
270 : QLatin1StringView("Capturing is not supported on this platform");
271}
272
273/*!
274 \since 6.12
275 \property QScreenCapture::maximumFrameRate
276 \brief The screen capture frame rate upper limit.
277
278 This can be set to override the capture frame rate used by default based on
279 e.g. display refresh rate, but only as an upper limit since screen capture
280 produces frames at a variable rate. Setting this higher than the display
281 refresh rate is not recommended and can cause errors.
282
283 Any changes to this property are applied the next time the QScreenCapture
284 goes active.
285*/
286void QScreenCapture::setMaximumFrameRate(std::optional<qreal> frameRate)
287{
288 Q_D(QScreenCapture);
289
290 if (d->platformScreenCapture)
291 d->platformScreenCapture->setFrameRate(frameRate);
292}
293
294std::optional<qreal> QScreenCapture::maximumFrameRate() const
295{
296 Q_D(const QScreenCapture);
297
298 return d->platformScreenCapture ? d->platformScreenCapture->frameRate() : std::nullopt;
299}
300
301void QScreenCapture::resetMaximumFrameRate()
302{
303 setMaximumFrameRate(std::nullopt);
304}
305
306/*!
307 \qmlmethod void QtMultimedia::ScreenCapture::start()
308
309 Starts capturing the \l screen.
310
311 This is equivalent to setting the \l active property to \c true.
312*/
313
314/*!
315 \fn void QScreenCapture::start()
316
317 Starts capturing the \l screen.
318
319 This is equivalent to setting the \l active property to true.
320*/
321
322/*!
323 \qmlmethod void QtMultimedia::ScreenCapture::stop()
324
325 Stops capturing.
326
327 This is equivalent to setting the \l active property to \c false.
328*/
329
330/*!
331 \fn void QScreenCapture::stop()
332
333 Stops capturing.
334
335 This is equivalent to setting the \l active property to false.
336*/
337/*!
338 \internal
339*/
340void QScreenCapture::setCaptureSession(QMediaCaptureSession *captureSession)
341{
342 Q_D(QScreenCapture);
343
344 d->captureSession = captureSession;
345}
346
347/*!
348 \internal
349*/
350class QPlatformSurfaceCapture *QScreenCapture::platformScreenCapture() const
351{
352 Q_D(const QScreenCapture);
353
354 return d->platformScreenCapture.get();
355}
356
357QT_END_NAMESPACE
358
359#include "moc_qscreencapture.cpp"
QMediaCaptureSession * captureSession
std::unique_ptr< QPlatformSurfaceCapture > platformScreenCapture
Combined button and popup list for selecting options.
static QT_BEGIN_NAMESPACE QScreenCapture::Error toScreenCaptureError(QPlatformSurfaceCapture::Error error)