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
6#include <private/qplatformmediaintegration_p.h>
7#include <private/qplatformsurfacecapture_p.h>
8#include <private/qobject_p.h>
9
11
12static QScreenCapture::Error toScreenCaptureError(QPlatformSurfaceCapture::Error error)
13{
14 return static_cast<QScreenCapture::Error>(error);
15}
16
18{
19public:
20 QMediaCaptureSession *captureSession = nullptr;
22};
23
24/*!
25 \class QScreenCapture
26 \inmodule QtMultimedia
27 \ingroup multimedia
28 \ingroup multimedia_video
29 \since 6.5
30
31 \brief This class is used for capturing a screen.
32
33 The class captures a screen. It is managed by
34 the QMediaCaptureSession class where the captured screen can be displayed
35 in a video preview object or recorded to a file.
36
37 \snippet multimedia-snippets/media.cpp Media recorder
38
39 \include qscreencapture-limitations.qdocinc {content} {Q}
40
41 \sa QWindowCapture, QMediaCaptureSession
42*/
43/*!
44 \qmltype ScreenCapture
45 \nativetype QScreenCapture
46 \brief This type is used for capturing a screen.
47
48 \inqmlmodule QtMultimedia
49 \ingroup multimedia_qml
50 \ingroup multimedia_video_qml
51
52 ScreenCapture captures a screen. It is managed by
53 MediaCaptureSession where the captured screen can be displayed
54 in a video preview object or recorded to a file.
55
56 \since 6.5
57 The code below shows a simple capture session with ScreenCapture playing
58 back the captured primary screen view in VideoOutput.
59
60\qml
61 CaptureSession {
62 id: captureSession
63 screenCapture: ScreenCapture {
64 id: screenCapture
65 active: true
66 }
67 videoOutput: VideoOutput {
68 id: videoOutput
69 }
70 }
71\endqml
72
73 \include qscreencapture-limitations.qdocinc {content} {}
74
75 \sa WindowCapture, CaptureSession
76*/
77
78QScreenCapture::QScreenCapture(QObject *parent)
79 : QObject(*new QScreenCapturePrivate, parent)
80{
81 Q_D(QScreenCapture);
82
83 auto platformCapture = QPlatformMediaIntegration::instance()->createScreenCapture(this);
84 if (platformCapture) {
85 connect(platformCapture, &QPlatformSurfaceCapture::activeChanged, this,
86 &QScreenCapture::activeChanged);
87 connect(platformCapture, &QPlatformSurfaceCapture::errorChanged, this,
88 &QScreenCapture::errorChanged);
89 connect(platformCapture, &QPlatformSurfaceCapture::errorOccurred, this,
90 [this](QPlatformSurfaceCapture::Error error, const QString &errorString) {
91 emit errorOccurred(toScreenCaptureError(error), errorString);
92 });
93
94 connect(platformCapture,
95 qOverload<QPlatformSurfaceCapture::ScreenSource>(
96 &QPlatformSurfaceCapture::sourceChanged),
97 this, &QScreenCapture::screenChanged);
98
99 connect(platformCapture, &QPlatformSurfaceCapture::frameRateChanged, this,
100 &QScreenCapture::frameRateChanged);
101
102 d->platformScreenCapture.reset(platformCapture);
103 }
104}
105
106QScreenCapture::~QScreenCapture()
107{
108 Q_D(QScreenCapture);
109
110 // Reset platformScreenCapture in the destructor to avoid having broken ref in the object.
111 d->platformScreenCapture.reset();
112
113 if (d->captureSession)
114 d->captureSession->setScreenCapture(nullptr);
115}
116
117/*!
118 \enum QScreenCapture::Error
119
120 Enumerates error codes that can be signaled by the QScreenCapture class.
121 errorString() provides detailed information about the error cause.
122
123 \value NoError No error
124 \value InternalError Internal screen capturing driver error
125 \value CapturingNotSupported Capturing is not supported
126 \value CaptureFailed Capturing screen failed
127 \value NotFound Selected screen not found
128*/
129
130/*!
131 Returns the capture session this QScreenCapture is connected to.
132
133 Use QMediaCaptureSession::setScreenCapture() to connect the camera to
134 a session.
135*/
136QMediaCaptureSession *QScreenCapture::captureSession() const
137{
138 Q_D(const QScreenCapture);
139
140 return d->captureSession;
141}
142
143/*!
144 \qmlproperty bool QtMultimedia::ScreenCapture::active
145 Describes whether the capturing is currently active.
146*/
147
148/*!
149 \property QScreenCapture::active
150 \brief whether the capturing is currently active.
151*/
152void QScreenCapture::setActive(bool active)
153{
154 Q_D(QScreenCapture);
155
156 if (d->platformScreenCapture)
157 d->platformScreenCapture->setActive(active);
158}
159
160bool QScreenCapture::isActive() const
161{
162 Q_D(const QScreenCapture);
163
164 return d->platformScreenCapture && d->platformScreenCapture->isActive();
165}
166
167/*!
168 \qmlproperty Screen QtMultimedia::ScreenCapture::screen
169 Describes the screen for capturing.
170
171 If null \c Screen is set, the primary screen will be selected
172 when the \c ScreenCapture instance gets activated.
173*/
174
175/*!
176 \property QScreenCapture::screen
177 \brief the screen for capturing.
178
179 If null \l QScreen is set, \l QGuiApplication::primaryScreen will be selected
180 when the \c QScreenCapture instance gets activated.
181*/
182
183void QScreenCapture::setScreen(QScreen *screen)
184{
185 Q_D(QScreenCapture);
186
187 if (d->platformScreenCapture)
188 d->platformScreenCapture->setSource(QPlatformSurfaceCapture::ScreenSource(screen));
189}
190
191QScreen *QScreenCapture::screen() const
192{
193 Q_D(const QScreenCapture);
194
195 return d->platformScreenCapture
196 ? d->platformScreenCapture->source<QPlatformSurfaceCapture::ScreenSource>()
197 : nullptr;
198}
199
200/*!
201 \qmlproperty enumeration QtMultimedia::ScreenCapture::error
202 Returns a code of the last error.
203*/
204
205/*!
206 \property QScreenCapture::error
207 \brief the code of the last error.
208*/
209QScreenCapture::Error QScreenCapture::error() const
210{
211 Q_D(const QScreenCapture);
212
213 return d->platformScreenCapture ? toScreenCaptureError(d->platformScreenCapture->error())
214 : CapturingNotSupported;
215}
216
217/*!
218 \fn void QScreenCapture::errorOccurred(QScreenCapture::Error error, const QString &errorString)
219
220 Signals when an \a error occurs, along with the \a errorString.
221*/
222/*!
223 \qmlproperty string QtMultimedia::ScreenCapture::errorString
224 Returns a human readable string describing the cause of error.
225*/
226
227/*!
228 \property QScreenCapture::errorString
229 \brief a human readable string describing the cause of error.
230*/
231QString QScreenCapture::errorString() const
232{
233 Q_D(const QScreenCapture);
234
235 return d->platformScreenCapture ? d->platformScreenCapture->errorString()
236 : QLatin1StringView("Capturing is not support on this platform");
237}
238
239/*!
240 \since 6.12
241 \property QScreenCapture::frameRate
242 \brief The target screen capture framerate.
243
244 Actual frame rate depends on the platform. For platforms with fixed rate capture, this
245 frame rate is followed. For platforms with variable rate capture, this frame rate is either
246 used as the polling rate (maximum frame rate) or completely ignored.
247
248 If left unset, a platform-dependent default is used.
249*/
250void QScreenCapture::setFrameRate(std::optional<qreal> frameRate)
251{
252 Q_D(QScreenCapture);
253
254 if (d->platformScreenCapture)
255 d->platformScreenCapture->setFrameRate(frameRate);
256}
257
258std::optional<qreal> QScreenCapture::frameRate() const
259{
260 Q_D(const QScreenCapture);
261
262 return d->platformScreenCapture ? d->platformScreenCapture->frameRate() : std::nullopt;
263}
264
265void QScreenCapture::resetFrameRate()
266{
267 setFrameRate(std::nullopt);
268}
269
270/*!
271 \fn void QScreenCapture::start()
272
273 Starts screen capture.
274*/
275/*!
276 \fn void QScreenCapture::stop()
277
278 Stops screen capture.
279*/
280/*!
281 \internal
282*/
283void QScreenCapture::setCaptureSession(QMediaCaptureSession *captureSession)
284{
285 Q_D(QScreenCapture);
286
287 d->captureSession = captureSession;
288}
289
290/*!
291 \internal
292*/
293class QPlatformSurfaceCapture *QScreenCapture::platformScreenCapture() const
294{
295 Q_D(const QScreenCapture);
296
297 return d->platformScreenCapture.get();
298}
299
300QT_END_NAMESPACE
301
302#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)