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
4#include "platform/qplatformsurfacecapture_p.h"
5#include "qvideoframe.h"
6#include "qguiapplication.h"
7#include "qdebug.h"
8
9QT_BEGIN_NAMESPACE
10
11QPlatformSurfaceCapture::QPlatformSurfaceCapture(Source initialSource) : m_source(initialSource)
12{
13 Q_ASSERT(std::visit([](auto source) { return source == decltype(source){}; }, initialSource));
14 qRegisterMetaType<QVideoFrame>();
15}
16
17void QPlatformSurfaceCapture::setActive(bool active)
18{
19 if (m_active == active)
20 return;
21
22 if (!setActiveInternal(active))
23 return;
24
25 m_active = active;
26 emit activeChanged(active);
27}
28
29bool QPlatformSurfaceCapture::isActive() const
30{
31 return m_active;
32}
33
34void QPlatformSurfaceCapture::setSource(Source source)
35{
36 Q_ASSERT(source.index() == m_source.index());
37
38 if (m_source == source)
39 return;
40
41 if (m_active)
42 setActiveInternal(false);
43
44 m_source = source;
45
46 if (m_active && !setActiveInternal(true)) {
47 m_active = false;
48 emit activeChanged(false);
49 }
50
51 std::visit(visitor: [this](auto source) { emit sourceChanged(source); }, variants&: m_source);
52}
53
54QPlatformSurfaceCapture::Error QPlatformSurfaceCapture::error() const
55{
56 return m_error;
57}
58
59QString QPlatformSurfaceCapture::errorString() const
60{
61 return m_errorString;
62}
63
64void QPlatformSurfaceCapture::updateError(Error error, const QString &errorString)
65{
66 bool changed = error != m_error || errorString != m_errorString;
67 m_error = error;
68 m_errorString = errorString;
69 if (changed) {
70 if (m_error != NoError) {
71 emit errorOccurred(error, errorString);
72 qWarning() << "Screen capture fail:" << error << "," << errorString;
73 }
74
75 emit errorChanged();
76 }
77}
78
79bool QPlatformSurfaceCapture::checkScreenWithError(ScreenSource &screen)
80{
81 if (!screen)
82 screen = QGuiApplication::primaryScreen();
83
84 if (screen)
85 return true;
86
87 updateError(error: NotFound, errorString: QLatin1String("No screens found"));
88 return false;
89}
90
91QT_END_NAMESPACE
92
93#include "moc_qplatformsurfacecapture_p.cpp"
94

source code of qtmultimedia/src/multimedia/platform/qplatformsurfacecapture.cpp