summaryrefslogtreecommitdiffstats
path: root/src/plugins/multimedia/ffmpeg/qffmpegsurfacecapturegrabber_p.h
blob: 9a617bd7a7f15673fb3c0805f76836431ad64bf5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef QFFMPEGSURFACECAPTUREGRABBER_P_H
#define QFFMPEGSURFACECAPTUREGRABBER_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API. It exists purely as an
// implementation detail. This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include "qvideoframe.h"
#include "private/qplatformsurfacecapture_p.h"

#include <memory>
#include <optional>

QT_BEGIN_NAMESPACE

class QThread;

static constexpr qreal DefaultScreenCaptureFrameRate = 60.;

// Mac screens often support 120 frames per sec; it looks, this is not
// needed for the capturing now since it just affects CPI without valuable
// advantages. In the future, the frame rate should be customized by
// user's API.
static constexpr qreal MaxScreenCaptureFrameRate = 60.;
static constexpr qreal MinScreenCaptureFrameRate = 1.;

class QFFmpegSurfaceCaptureGrabber : public QObject
{
    Q_OBJECT
public:
    enum ThreadPolicy {
        UseCurrentThread,
        CreateGrabbingThread,
    };

    QFFmpegSurfaceCaptureGrabber(ThreadPolicy threadPolicy = CreateGrabbingThread);

    ~QFFmpegSurfaceCaptureGrabber() override;

    void start();
    void stop();

    template<typename Object, typename Method>
    void addFrameCallback(Object &object, Method method)
    {
        connect(this, &QFFmpegSurfaceCaptureGrabber::frameGrabbed,
                &object, method, Qt::DirectConnection);
    }

signals:
    void frameGrabbed(const QVideoFrame&);
    void errorUpdated(QPlatformSurfaceCapture::Error error, const QString &description);

protected:
    void updateError(QPlatformSurfaceCapture::Error error, const QString &description = {});

    virtual QVideoFrame grabFrame() = 0;

    void setFrameRate(qreal rate);

    qreal frameRate() const;

    void updateTimerInterval();

    virtual void initializeGrabbingContext();
    virtual void finalizeGrabbingContext();

    bool isGrabbingContextInitialized() const;

private:
    struct GrabbingContext;
    class GrabbingThread;

    std::unique_ptr<GrabbingContext> m_context;
    qreal m_rate = 0;
    std::optional<QPlatformSurfaceCapture::Error> m_prevError;
    std::unique_ptr<QThread> m_thread;
};

QT_END_NAMESPACE

#endif // QFFMPEGSURFACECAPTUREGRABBER_P_H