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, 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 d->platformScreenCapture.reset(platformCapture);
100 }
101}
102
103QScreenCapture::~QScreenCapture()
104{
105 Q_D(QScreenCapture);
106
107 // Reset platformScreenCapture in the destructor to avoid having broken ref in the object.
108 d->platformScreenCapture.reset();
109
110 if (d->captureSession)
111 d->captureSession->setScreenCapture(nullptr);
112}
113
114/*!
115 \enum QScreenCapture::Error
116
117 Enumerates error codes that can be signaled by the QScreenCapture class.
118 errorString() provides detailed information about the error cause.
119
120 \value NoError No error
121 \value InternalError Internal screen capturing driver error
122 \value CapturingNotSupported Capturing is not supported
123 \value CaptureFailed Capturing screen failed
124 \value NotFound Selected screen not found
125*/
126
127/*!
128 Returns the capture session this QScreenCapture is connected to.
129
130 Use QMediaCaptureSession::setScreenCapture() to connect the camera to
131 a session.
132*/
133QMediaCaptureSession *QScreenCapture::captureSession() const
134{
135 Q_D(const QScreenCapture);
136
137 return d->captureSession;
138}
139
140/*!
141 \qmlproperty bool QtMultimedia::ScreenCapture::active
142 Describes whether the capturing is currently active.
143*/
144
145/*!
146 \property QScreenCapture::active
147 \brief whether the capturing is currently active.
148*/
149void QScreenCapture::setActive(bool active)
150{
151 Q_D(QScreenCapture);
152
153 if (d->platformScreenCapture)
154 d->platformScreenCapture->setActive(active);
155}
156
157bool QScreenCapture::isActive() const
158{
159 Q_D(const QScreenCapture);
160
161 return d->platformScreenCapture && d->platformScreenCapture->isActive();
162}
163
164/*!
165 \qmlproperty Screen QtMultimedia::ScreenCapture::screen
166 Describes the screen for capturing.
167
168 If null \c Screen is set, the primary screen will be selected
169 when the \c ScreenCapture instance gets activated.
170*/
171
172/*!
173 \property QScreenCapture::screen
174 \brief the screen for capturing.
175
176 If null \l QScreen is set, \l QGuiApplication::primaryScreen will be selected
177 when the \c QScreenCapture instance gets activated.
178*/
179
180void QScreenCapture::setScreen(QScreen *screen)
181{
182 Q_D(QScreenCapture);
183
184 if (d->platformScreenCapture)
185 d->platformScreenCapture->setSource(QPlatformSurfaceCapture::ScreenSource(screen));
186}
187
188QScreen *QScreenCapture::screen() const
189{
190 Q_D(const QScreenCapture);
191
192 return d->platformScreenCapture
193 ? d->platformScreenCapture->source<QPlatformSurfaceCapture::ScreenSource>()
194 : nullptr;
195}
196
197/*!
198 \qmlproperty enumeration QtMultimedia::ScreenCapture::error
199 Returns a code of the last error.
200*/
201
202/*!
203 \property QScreenCapture::error
204 \brief the code of the last error.
205*/
206QScreenCapture::Error QScreenCapture::error() const
207{
208 Q_D(const QScreenCapture);
209
210 return d->platformScreenCapture ? toScreenCaptureError(d->platformScreenCapture->error())
211 : CapturingNotSupported;
212}
213
214/*!
215 \fn void QScreenCapture::errorOccurred(QScreenCapture::Error error, const QString &errorString)
216
217 Signals when an \a error occurs, along with the \a errorString.
218*/
219/*!
220 \qmlproperty string QtMultimedia::ScreenCapture::errorString
221 Returns a human readable string describing the cause of error.
222*/
223
224/*!
225 \property QScreenCapture::errorString
226 \brief a human readable string describing the cause of error.
227*/
228QString QScreenCapture::errorString() const
229{
230 Q_D(const QScreenCapture);
231
232 return d->platformScreenCapture ? d->platformScreenCapture->errorString()
233 : QLatin1StringView("Capturing is not support on this platform");
234}
235/*!
236 \fn void QScreenCapture::start()
237
238 Starts screen capture.
239*/
240/*!
241 \fn void QScreenCapture::stop()
242
243 Stops screen capture.
244*/
245/*!
246 \internal
247*/
248void QScreenCapture::setCaptureSession(QMediaCaptureSession *captureSession)
249{
250 Q_D(QScreenCapture);
251
252 d->captureSession = captureSession;
253}
254
255/*!
256 \internal
257*/
258class QPlatformSurfaceCapture *QScreenCapture::platformScreenCapture() const
259{
260 Q_D(const QScreenCapture);
261
262 return d->platformScreenCapture.get();
263}
264
265QT_END_NAMESPACE
266
267#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)