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

source code of qtmultimedia/src/multimedia/recording/qwindowcapture.cpp