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
7#include "private/qobject_p.h"
8#include "private/qplatformsurfacecapture_p.h"
9
11
12static QWindowCapture::Error toWindowCaptureError(QPlatformSurfaceCapture::Error error)
13{
14 return static_cast<QWindowCapture::Error>(error);
15}
16
23
24/*!
25 \class QWindowCapture
26 \inmodule QtMultimedia
27 \ingroup multimedia
28 \ingroup multimedia_video
29 \since 6.6
30
31 \brief This class is used for capturing a window.
32
33 The class captures a window. It is managed by
34 the QMediaCaptureSession class where the captured window can be displayed
35 in a video preview object or recorded to a file.
36
37 \include qwindowcapture-limitations.qdocinc {content} {Q}
38
39 \sa QMediaCaptureSession, QCapturableWindow
40*/
41/*!
42 \qmltype WindowCapture
43 \nativetype QWindowCapture
44 \inqmlmodule QtMultimedia
45 \ingroup multimedia_qml
46 \ingroup multimedia_video_qml
47 \since 6.6
48
49 \brief This type is used for capturing a window.
50
51 WindowCapture captures a window. It is managed by
52 MediaCaptureSession where the captured window can be displayed
53 in a video preview object or recorded to a file.
54
55 \include qwindowcapture-limitations.qdocinc {content} {}
56
57 \sa CaptureSession, CapturableWindow
58*/
59
60/*!
61 \enum QWindowCapture::Error
62
63 Enumerates error codes that can be signaled by the QWindowCapture class.
64 errorString() provides detailed information about the error cause.
65
66 \value NoError No error
67 \value InternalError Internal window capturing driver error
68 \value CapturingNotSupported Window capturing is not supported
69 \value CaptureFailed Capturing window failed
70 \value NotFound Selected window not found
71*/
72
73/*!
74 Constructs a new QWindowCapture object with \a parent.
75*/
76QWindowCapture::QWindowCapture(QObject *parent) : QObject(*new QWindowCapturePrivate, parent)
77{
78 Q_D(QWindowCapture);
79
80 qRegisterMetaType<QCapturableWindow>();
81
82 auto platformCapture = QPlatformMediaIntegration::instance()->createWindowCapture(this);
83
84 if (platformCapture) {
85 connect(platformCapture, &QPlatformSurfaceCapture::activeChanged, this,
86 &QWindowCapture::activeChanged);
87 connect(platformCapture, &QPlatformSurfaceCapture::errorChanged, this,
88 &QWindowCapture::errorChanged);
89 connect(platformCapture, &QPlatformSurfaceCapture::errorOccurred, this,
90 [this](QPlatformSurfaceCapture::Error error, QString errorString) {
91 emit errorOccurred(toWindowCaptureError(error), errorString);
92 });
93 connect(platformCapture,
94 qOverload<QCapturableWindow>(&QPlatformSurfaceCapture::sourceChanged), this,
95 &QWindowCapture::windowChanged);
96
97 d->platformWindowCapture.reset(platformCapture);
98 }
99}
100
101/*!
102 Destroys the object.
103 */
104QWindowCapture::~QWindowCapture()
105{
106 Q_D(QWindowCapture);
107
108 d->platformWindowCapture.reset();
109
110 if (d->captureSession)
111 d->captureSession->setWindowCapture(nullptr);
112}
113
114/*!
115 \qmlmethod list<CapturableWindow> QtMultimedia::WindowCapture::capturableWindows()
116
117 Returns a list of CapturableWindow objects that is available for capturing.
118*/
119/*!
120 \fn QList<QCapturableWindow> QWindowCapture::capturableWindows()
121
122 Returns a list of QCapturableWindow objects that is available for capturing.
123 */
124QList<QCapturableWindow> QWindowCapture::capturableWindows()
125{
126 return QPlatformMediaIntegration::instance()->capturableWindowsList();
127}
128
129QMediaCaptureSession *QWindowCapture::captureSession() const
130{
131 Q_D(const QWindowCapture);
132
133 return d->captureSession;
134}
135
136/*!
137 \qmlproperty Window QtMultimedia::WindowCapture::window
138 Describes the window for capturing.
139
140 \sa QtMultimedia::WindowCapture::capturableWindows
141*/
142
143/*!
144 \property QWindowCapture::window
145 \brief the window for capturing.
146
147 \sa QWindowCapture::capturableWindows
148*/
149QCapturableWindow QWindowCapture::window() const
150{
151 Q_D(const QWindowCapture);
152
153 return d->platformWindowCapture ? d->platformWindowCapture->source<QCapturableWindow>()
154 : QCapturableWindow();
155}
156
157void QWindowCapture::setWindow(QCapturableWindow window)
158{
159 Q_D(QWindowCapture);
160
161 if (d->platformWindowCapture)
162 d->platformWindowCapture->setSource(window);
163}
164
165/*!
166 \qmlproperty bool QtMultimedia::WindowCapture::active
167 Describes whether the capturing is currently active.
168*/
169
170/*!
171 \property QWindowCapture::active
172 \brief whether the capturing is currently active.
173
174 \sa start(), stop()
175*/
176bool QWindowCapture::isActive() const
177{
178 Q_D(const QWindowCapture);
179
180 return d->platformWindowCapture && d->platformWindowCapture->isActive();
181}
182
183void QWindowCapture::setActive(bool active)
184{
185 Q_D(QWindowCapture);
186
187 if (d->platformWindowCapture)
188 d->platformWindowCapture->setActive(active);
189}
190
191/*!
192 \qmlmethod void QtMultimedia::WindowCapture::start()
193
194 Starts capturing the \l window.
195
196 This is equivalent to setting the \l active property to \c true.
197*/
198
199/*!
200 \fn void QWindowCapture::start()
201
202 Starts capturing the \l window.
203
204 This is equivalent to setting the \l active property to true.
205*/
206
207/*!
208 \qmlmethod void QtMultimedia::WindowCapture::stop()
209
210 Stops capturing.
211
212 This is equivalent to setting the \l active property to \c false.
213*/
214
215/*!
216 \fn void QWindowCapture::stop()
217
218 Stops capturing.
219
220 This is equivalent to setting the \l active property to false.
221*/
222
223
224/*!
225 \qmlproperty enumeration QtMultimedia::WindowCapture::error
226 Returns a code of the last error.
227*/
228
229/*!
230 \property QWindowCapture::error
231 \brief the code of the last error.
232*/
233QWindowCapture::Error QWindowCapture::error() const
234{
235 Q_D(const QWindowCapture);
236
237 return d->platformWindowCapture ? toWindowCaptureError(d->platformWindowCapture->error())
238 : CapturingNotSupported;
239}
240
241/*!
242 \fn void QWindowCapture::errorOccurred(QWindowCapture::Error error, const QString &errorString)
243
244 Signals when an \a error occurs, along with the \a errorString.
245*/
246/*!
247 \qmlproperty string QtMultimedia::WindowCapture::errorString
248 Returns a human readable string describing the cause of error.
249*/
250
251/*!
252 \property QWindowCapture::errorString
253 \brief a human readable string describing the cause of error.
254*/
255QString QWindowCapture::errorString() const
256{
257 Q_D(const QWindowCapture);
258
259 return d->platformWindowCapture
260 ? d->platformWindowCapture->errorString()
261 : QLatin1StringView("Capturing is not support on this platform");
262}
263
264void QWindowCapture::setCaptureSession(QMediaCaptureSession *captureSession)
265{
266 Q_D(QWindowCapture);
267
268 d->captureSession = captureSession;
269}
270
271QPlatformSurfaceCapture *QWindowCapture::platformWindowCapture() const
272{
273 Q_D(const QWindowCapture);
274
275 return d->platformWindowCapture.get();
276}
277
278QT_END_NAMESPACE
279
280#include "moc_qwindowcapture.cpp"
QMediaCaptureSession * captureSession
std::unique_ptr< QPlatformSurfaceCapture > platformWindowCapture
Combined button and popup list for selecting options.
static QT_BEGIN_NAMESPACE QWindowCapture::Error toWindowCaptureError(QPlatformSurfaceCapture::Error error)