Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qffmpegsurfacecapturethread.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 <qtimer.h>
7#include <qloggingcategory.h>
8
10
11static Q_LOGGING_CATEGORY(qLcScreenCaptureThread, "qt.multimedia.ffmpeg.surfacecapturethread");
12
13namespace {
14
15class GrabbingProfiler
16{
17public:
18 auto measure()
19 {
20 m_elapsedTimer.start();
21 return qScopeGuard([&]() {
22 const auto nsecsElapsed = m_elapsedTimer.nsecsElapsed();
23 ++m_number;
24 m_wholeTime += nsecsElapsed;
25
26#ifdef DUMP_SCREEN_CAPTURE_PROFILING
27 qDebug() << "screen grabbing time:" << nsecsElapsed << "avg:" << avgTime()
28 << "number:" << m_number;
29#endif
30 });
31 }
32
33 qreal avgTime() const
34 {
35 return m_number ? m_wholeTime / (m_number * 1000000.) : 0.;
36 }
37
38 qint64 number() const
39 {
40 return m_number;
41 }
42
43private:
44 QElapsedTimer m_elapsedTimer;
45 qint64 m_wholeTime = 0;
46 qint64 m_number = 0;
47};
48
49} // namespace
50
52{
54}
55
57
59{
61 if (std::exchange(m_rate, rate) != rate) {
62 qCDebug(qLcScreenCaptureThread) << "Screen capture rate has been changed:" << m_rate;
63
65 }
66}
67
69{
70 return m_rate;
71}
72
74{
75 quit();
76 wait();
77}
78
80{
81 qCDebug(qLcScreenCaptureThread) << "start screen capture thread";
82
83 m_timer = std::make_unique<QTimer>();
84 // should be deleted in this thread
85 auto deleter = qScopeGuard([this]() { m_timer.reset(); });
86 m_timer->setTimerType(Qt::PreciseTimer);
88
89 QElapsedTimer elapsedTimer;
90 elapsedTimer.start();
91
92 qint64 lastFrameTime = 0;
93
94 GrabbingProfiler profiler;
95
96 auto doGrab = [&]() {
97 auto measure = profiler.measure();
98
99 auto frame = grabFrame();
100
101 if (frame.isValid()) {
102 frame.setStartTime(lastFrameTime);
103 frame.setEndTime(elapsedTimer.nsecsElapsed() / 1000);
104 lastFrameTime = frame.endTime();
105
107
109 }
110 };
111
112 doGrab();
113
114 m_timer->callOnTimeout(doGrab);
115 m_timer->start();
116
117 exec();
118
119 qCDebug(qLcScreenCaptureThread)
120 << "end screen capture thread; avg grabbing time:" << profiler.avgTime()
121 << "ms, grabbings number:" << profiler.number();
122}
123
125 const QString &description)
126{
127 const auto prevError = std::exchange(m_prevError, error);
128
130 || prevError != QPlatformSurfaceCapture::NoError) {
131 emit errorUpdated(error, description);
132 }
133
135}
136
138{
139 const qreal rate = m_prevError && *m_prevError != QPlatformSurfaceCapture::NoError
141 : m_rate;
142 const int interval = static_cast<int>(1000 / rate);
143 if (m_timer && m_timer->interval() != interval)
144 m_timer->setInterval(interval);
145}
146
148
149#include "moc_qffmpegsurfacecapturethread_p.cpp"
\inmodule QtCore
void start() noexcept
Starts this timer.
qint64 nsecsElapsed() const noexcept
virtual QVideoFrame grabFrame()=0
void frameGrabbed(const QVideoFrame &)
void errorUpdated(QPlatformSurfaceCapture::Error error, const QString &description)
~QFFmpegSurfaceCaptureThread() override
void updateError(QPlatformSurfaceCapture::Error error, const QString &description={})
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
bool wait(QDeadlineTimer deadline=QDeadlineTimer(QDeadlineTimer::Forever))
Definition qthread.cpp:950
int exec()
Definition qthread.cpp:918
void quit()
Definition qthread.cpp:935
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:160
#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
QScopeGuard< typename std::decay< F >::type > qScopeGuard(F &&f)
[qScopeGuard]
Definition qscopeguard.h:60
#define emit
long long qint64
Definition qtypes.h:55
double qreal
Definition qtypes.h:92
QFrame frame
[0]