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
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 <qchronotimer.h>
7#include <qelapsedtimer.h>
8#include <qloggingcategory.h>
9#include <qthread.h>
10#include <qtimer.h>
11
13
14Q_STATIC_LOGGING_CATEGORY(qLcScreenCaptureGrabber, "qt.multimedia.ffmpeg.surfacecapturegrabber");
15
17{
18public:
19 auto measure()
20 {
21 m_elapsedTimer.start();
22 return qScopeGuard([&]() {
23 const auto nsecsElapsed = m_elapsedTimer.nsecsElapsed();
24 ++m_number;
25 m_wholeTime += nsecsElapsed;
26
27#ifdef DUMP_SCREEN_CAPTURE_PROFILING
28 qDebug() << "screen grabbing time:" << nsecsElapsed << "avg:" << avgTime()
29 << "number:" << m_number;
30#endif
31 });
32 }
33
34 qreal avgTime() const
35 {
36 return m_number ? m_wholeTime / (m_number * 1000000.) : 0.;
37 }
38
39 qint64 number() const
40 {
41 return m_number;
42 }
43
44private:
45 QElapsedTimer m_elapsedTimer;
46 qint64 m_wholeTime = 0;
47 qint64 m_number = 0;
48};
49
57
59{
60public:
62 : m_grabber(grabber)
63 {}
64
65protected:
67 {
69
71 return;
72
73 exec();
75 }
76
77private:
79};
80
82{
83 setFrameRate(DefaultScreenCaptureFrameRate);
84
85 if (threadPolicy == CreateGrabbingThread)
86 m_thread = std::make_unique<GrabbingThread>(*this);
87}
88
90{
91 if (m_thread)
92 m_thread->start();
95}
96
98
100{
101 rate = qBound(MinScreenCaptureFrameRate, rate, MaxScreenCaptureFrameRate);
102 if (std::exchange(m_rate, rate) != rate) {
103 qCDebug(qLcScreenCaptureGrabber) << "Screen capture rate has been changed:" << m_rate;
104
106 }
107}
108
110{
111 return m_rate;
112}
113
115{
116 if (m_thread)
117 {
118 m_thread->quit();
119 m_thread->wait();
120 }
122 {
124 }
125}
126
127void QFFmpegSurfaceCaptureGrabber::updateError(QPlatformSurfaceCapture::Error error,
128 const QString &description)
129{
130 const auto prevError = std::exchange(m_prevError, error);
131
132 if (error != QPlatformSurfaceCapture::NoError
133 || prevError != QPlatformSurfaceCapture::NoError) {
134 emit errorUpdated(error, description);
135 }
136
138}
139
141{
142 using namespace std::chrono;
143
144 const qreal rate = m_prevError && *m_prevError != QPlatformSurfaceCapture::NoError
145 ? MinScreenCaptureFrameRate
146 : m_rate;
147 const auto interval = round<nanoseconds>(nanoseconds(1s) / rate);
148
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
174 updateError(QPlatformSurfaceCapture::NoError);
175
176 emit frameGrabbed(frame);
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
200QT_END_NAMESPACE
201
202#include "moc_qffmpegsurfacecapturegrabber_p.cpp"
GrabbingThread(QFFmpegSurfaceCaptureGrabber &grabber)
~QFFmpegSurfaceCaptureGrabber() override
QFFmpegSurfaceCaptureGrabber(ThreadPolicy threadPolicy=CreateGrabbingThread)
void updateError(QPlatformSurfaceCapture::Error error, const QString &description={})
Combined button and popup list for selecting options.
#define qCDebug(category,...)
#define Q_STATIC_LOGGING_CATEGORY(name,...)