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
qsurfacecapturegrabber_p.h
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
4#ifndef QSURFACECAPTUREGRABBER_P_H
5#define QSURFACECAPTUREGRABBER_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtMultimedia/qvideoframe.h>
19#include <QtMultimedia/private/qplatformsurfacecapture_p.h>
20#include <QtMultimedia/qtmultimediaglobal.h>
21
22#include <QtCore/qmutex.h>
23
24#include <chrono>
25#include <memory>
26#include <optional>
27
28QT_BEGIN_NAMESPACE
29
30class QThread;
31
32static constexpr qreal DefaultScreenCaptureFrameRate = 60.f;
33static constexpr qreal MinScreenCaptureFrameRate = 1.f;
34
35class Q_MULTIMEDIA_EXPORT QSurfaceCaptureGrabber : public QObject
36{
37 Q_OBJECT
38public:
39 enum ThreadPolicy {
40 UseCurrentThread,
41 CreateGrabbingThread,
42 };
43
44 ~QSurfaceCaptureGrabber() override;
45
46 void start();
47 void stop();
48
49 template<typename Object, typename Method>
50 void addFrameCallback(Object *object, Method method)
51 {
52 connect(this, &QSurfaceCaptureGrabber::frameGrabbed, object, method,
53 Qt::DirectConnection);
54 }
55
56 void setFrameRate(std::optional<qreal>);
57 qreal frameRate() const;
58
59 // Owning thread before start(), grabbing thread after.
60 qreal activeFrameRate() const;
61
62Q_SIGNALS:
63 void frameGrabbed(const QVideoFrame&);
64 void errorUpdated(QPlatformSurfaceCapture::Error error, const QString &description);
65
66protected:
67 QSurfaceCaptureGrabber(ThreadPolicy threadPolicy = CreateGrabbingThread);
68
69 void updateError(QPlatformSurfaceCapture::Error error, const QString &description = {});
70
71 virtual QVideoFrame grabFrame() = 0;
72
73 void updateTimerInterval();
74 void setTimerInterval(std::chrono::nanoseconds interval, quint64 generation);
75
76 virtual void initializeGrabbingContext();
77 virtual void finalizeGrabbingContext();
78
79 bool isGrabbingContextInitialized() const;
80
81 void injectContextToGrabbingThread(QObject *context);
82
83private:
84 class GrabbingProfiler;
85 struct GrabbingContext;
86 class GrabbingThread;
87
88 mutable QMutex m_mutex;
89 std::unique_ptr<GrabbingContext> m_context;
90 std::optional<QPlatformSurfaceCapture::Error> m_prevError;
91 std::unique_ptr<QThread> m_thread;
92 std::chrono::nanoseconds m_timerInterval{ 0 };
93 quint64 m_contextGeneration{ 0 };
94
95 qreal m_rate{ DefaultScreenCaptureFrameRate };
96 // Locked in start()/updateTimerInterval(); lock-free on the grabbing thread.
97 qreal m_activeRate{ DefaultScreenCaptureFrameRate };
98};
99
100QT_END_NAMESPACE
101
102#endif // QSURFACECAPTUREGRABBER_P_H
QT_BEGIN_NAMESPACE Q_STATIC_LOGGING_CATEGORY(lcSynthesizedIterableAccess, "qt.iterable.synthesized", QtWarningMsg)
static constexpr qreal MinScreenCaptureFrameRate
static constexpr qreal DefaultScreenCaptureFrameRate