| 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 QFFMPEGSURFACECAPTUREGRABBER_P_H |
| 5 | #define QFFMPEGSURFACECAPTUREGRABBER_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 "qvideoframe.h" |
| 19 | #include <QtMultimedia/private/qplatformsurfacecapture_p.h> |
| 20 | |
| 21 | #include <memory> |
| 22 | #include <optional> |
| 23 | |
| 24 | QT_BEGIN_NAMESPACE |
| 25 | |
| 26 | class QThread; |
| 27 | |
| 28 | static constexpr qreal DefaultScreenCaptureFrameRate = 60.; |
| 29 | |
| 30 | // Mac screens often support 120 frames per sec; it looks, this is not |
| 31 | // needed for the capturing now since it just affects CPI without valuable |
| 32 | // advantages. In the future, the frame rate should be customized by |
| 33 | // user's API. |
| 34 | static constexpr qreal MaxScreenCaptureFrameRate = 60.; |
| 35 | static constexpr qreal MinScreenCaptureFrameRate = 1.; |
| 36 | |
| 37 | class QFFmpegSurfaceCaptureGrabber : public QObject |
| 38 | { |
| 39 | Q_OBJECT |
| 40 | public: |
| 41 | enum ThreadPolicy { |
| 42 | UseCurrentThread, |
| 43 | CreateGrabbingThread, |
| 44 | }; |
| 45 | |
| 46 | QFFmpegSurfaceCaptureGrabber(ThreadPolicy threadPolicy = CreateGrabbingThread); |
| 47 | |
| 48 | ~QFFmpegSurfaceCaptureGrabber() override; |
| 49 | |
| 50 | void start(); |
| 51 | void stop(); |
| 52 | |
| 53 | template<typename Object, typename Method> |
| 54 | void addFrameCallback(Object &object, Method method) |
| 55 | { |
| 56 | connect(this, &QFFmpegSurfaceCaptureGrabber::frameGrabbed, |
| 57 | &object, method, Qt::DirectConnection); |
| 58 | } |
| 59 | |
| 60 | signals: |
| 61 | void frameGrabbed(const QVideoFrame&); |
| 62 | void errorUpdated(QPlatformSurfaceCapture::Error error, const QString &description); |
| 63 | |
| 64 | protected: |
| 65 | void updateError(QPlatformSurfaceCapture::Error error, const QString &description = {}); |
| 66 | |
| 67 | virtual QVideoFrame grabFrame() = 0; |
| 68 | |
| 69 | void setFrameRate(qreal rate); |
| 70 | |
| 71 | qreal frameRate() const; |
| 72 | |
| 73 | void updateTimerInterval(); |
| 74 | |
| 75 | virtual void initializeGrabbingContext(); |
| 76 | virtual void finalizeGrabbingContext(); |
| 77 | |
| 78 | bool isGrabbingContextInitialized() const; |
| 79 | |
| 80 | private: |
| 81 | struct GrabbingContext; |
| 82 | class GrabbingThread; |
| 83 | |
| 84 | std::unique_ptr<GrabbingContext> m_context; |
| 85 | qreal m_rate = 0; |
| 86 | std::optional<QPlatformSurfaceCapture::Error> m_prevError; |
| 87 | std::unique_ptr<QThread> m_thread; |
| 88 | }; |
| 89 | |
| 90 | QT_END_NAMESPACE |
| 91 | |
| 92 | #endif // QFFMPEGSURFACECAPTUREGRABBER_P_H |
| 93 | |