Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qffmpegsurfacecapturegrabber.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 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
5
6#include <qelapsedtimer.h>
7#include <qloggingcategory.h>
8#include <qthread.h>
9#include <qtimer.h>
10
12
13static Q_LOGGING_CATEGORY(qLcScreenCaptureGrabber, "qt.multimedia.ffmpeg.surfacecapturegrabber");
14
15namespace {
16
17class GrabbingProfiler
18{
19public:
20 auto measure()
21 {
22 m_elapsedTimer.start();
23 return qScopeGuard([&]() {
24 const auto nsecsElapsed = m_elapsedTimer.nsecsElapsed();
25 ++m_number;
26 m_wholeTime += nsecsElapsed;
27
28#ifdef DUMP_SCREEN_CAPTURE_PROFILING
29 qDebug() << "screen grabbing time:" << nsecsElapsed << "avg:" << avgTime()
30 << "number:" << m_number;
31#endif
32 });
33 }
34
35 qreal avgTime() const
36 {
37 return m_number ? m_wholeTime / (m_number * 1000000.) : 0.;
38 }
39
40 qint64 number() const
41 {
42 return m_number;
43 }
44
45private:
46 QElapsedTimer m_elapsedTimer;
47 qint64 m_wholeTime = 0;
48 qint64 m_number = 0;
49};
50
51} // namespace
52
60
62{
63public:
65 : m_grabber(grabber)
66 {}
67
68protected:
69 void run() override
70 {
71 m_grabber.initializeGrabbingContext();
72
73 if (!m_grabber.isGrabbingContextInitialized())
74 return;
75
76 exec();
77 m_grabber.finalizeGrabbingContext();
78 }
79
80private:
82};
83
85{
87
88 if (threadPolicy == CreateGrabbingThread)
89 m_thread = std::make_unique<GrabbingThread>(*this);
90}
91
93{
94 if (m_thread)
95 m_thread->start();
98}
99
101
103{
105 if (std::exchange(m_rate, rate) != rate) {
106 qCDebug(qLcScreenCaptureGrabber) << "Screen capture rate has been changed:" << m_rate;
107
109 }
110}
111
113{
114 return m_rate;
115}
116
118{
119 if (m_thread)
120 {
121 m_thread->quit();
122 m_thread->wait();
123 }
125 {
127 }
128}
129
131 const QString &description)
132{
133 const auto prevError = std::exchange(m_prevError, error);
134
136 || prevError != QPlatformSurfaceCapture::NoError) {
137 emit errorUpdated(error, description);
138 }
139
141}
142
144{
145 const qreal rate = m_prevError && *m_prevError != QPlatformSurfaceCapture::NoError
147 : m_rate;
148 const int interval = static_cast<int>(1000 / rate);
149 if (m_context && m_context->timer.interval() != interval)
150 m_context->timer.setInterval(interval);
151}
152
154{
156 qCDebug(qLcScreenCaptureGrabber) << "screen capture started";
157
158 m_context = std::make_unique<GrabbingContext>();
159 m_context->timer.setTimerType(Qt::PreciseTimer);
161
162 m_context->elapsedTimer.start();
163
164 auto doGrab = [this]() {
165 auto measure = m_context->profiler.measure();
166
167 auto frame = grabFrame();
168
169 if (frame.isValid()) {
170 frame.setStartTime(m_context->lastFrameTime);
171 frame.setEndTime(m_context->elapsedTimer.nsecsElapsed() / 1000);
172 m_context->lastFrameTime = frame.endTime();
173
175
177 }
178 };
179
180 doGrab();
181
182 m_context->timer.callOnTimeout(&m_context->timer, doGrab);
183 m_context->timer.start();
184}
185
187{
189 qCDebug(qLcScreenCaptureGrabber)
190 << "end screen capture thread; avg grabbing time:" << m_context->profiler.avgTime()
191 << "ms, grabbings number:" << m_context->profiler.number();
192 m_context.reset();
193}
194
196{
197 return m_context != nullptr;
198}
199
201
202#include "moc_qffmpegsurfacecapturegrabber_p.cpp"
\inmodule QtCore
GrabbingThread(QFFmpegSurfaceCaptureGrabber &grabber)
void errorUpdated(QPlatformSurfaceCapture::Error error, const QString &description)
virtual QVideoFrame grabFrame()=0
void frameGrabbed(const QVideoFrame &)
~QFFmpegSurfaceCaptureGrabber() override
QFFmpegSurfaceCaptureGrabber(ThreadPolicy threadPolicy=CreateGrabbingThread)
void updateError(QPlatformSurfaceCapture::Error error, const QString &description={})
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
int exec()
Definition qthread.cpp:991
\inmodule QtCore
Definition qtimer.h:20
Combined button and popup list for selecting options.
@ PreciseTimer
DBusConnection const char DBusError * error
static constexpr qreal MinScreenCaptureFrameRate
static constexpr qreal MaxScreenCaptureFrameRate
static constexpr qreal DefaultScreenCaptureFrameRate
#define qDebug
[1]
Definition qlogging.h:164
#define Q_LOGGING_CATEGORY(name,...)
#define qCDebug(category,...)
constexpr const T & qBound(const T &min, const T &val, const T &max)
Definition qminmax.h:44
GLuint GLenum * rate
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
QScopeGuard< typename std::decay< F >::type > qScopeGuard(F &&f)
[qScopeGuard]
Definition qscopeguard.h:60
#define emit
long long qint64
Definition qtypes.h:60
double qreal
Definition qtypes.h:187
QFrame frame
[0]