1// Copyright (C) 2021 The Qt Company Ltd.
2// Copyright (C) 2016 Research In Motion
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5#ifndef QQUICKVIDEOOUTPUT_P_H
6#define QQUICKVIDEOOUTPUT_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtCore/qrect.h>
20#include <QtCore/qsharedpointer.h>
21#include <QtQuick/qquickitem.h>
22#include <QtCore/qpointer.h>
23#include <QtCore/qmutex.h>
24
25#include <private/qtmultimediaquickglobal_p.h>
26#include <qvideoframe.h>
27#include <qvideoframeformat.h>
28#include <qvideosink.h>
29
30QT_BEGIN_NAMESPACE
31
32class QQuickVideoBackend;
33class QVideoOutputOrientationHandler;
34class QVideoSink;
35class QSGVideoNode;
36class QVideoFrameTexturePool;
37using QVideoFrameTexturePoolWPtr = std::weak_ptr<QVideoFrameTexturePool>;
38
39class QQuickVideoSink : public QVideoSink
40{
41 Q_OBJECT
42 QML_NAMED_ELEMENT(VideoSink)
43public:
44 QQuickVideoSink(QObject *parent = nullptr) : QVideoSink(parent)
45 {
46 connect(sender: this, signal: &QVideoSink::videoFrameChanged, context: this, slot: &QQuickVideoSink::videoFrameChanged,
47 type: Qt::DirectConnection);
48 }
49
50Q_SIGNALS:
51 void videoFrameChanged();
52};
53
54class Q_MULTIMEDIAQUICK_EXPORT QQuickVideoOutput : public QQuickItem
55{
56 Q_OBJECT
57 Q_DISABLE_COPY(QQuickVideoOutput)
58 Q_PROPERTY(FillMode fillMode READ fillMode WRITE setFillMode NOTIFY fillModeChanged)
59 Q_PROPERTY(int orientation READ orientation WRITE setOrientation NOTIFY orientationChanged)
60 Q_PROPERTY(QRectF sourceRect READ sourceRect NOTIFY sourceRectChanged)
61 Q_PROPERTY(QRectF contentRect READ contentRect NOTIFY contentRectChanged)
62 Q_PROPERTY(QVideoSink* videoSink READ videoSink CONSTANT)
63 Q_MOC_INCLUDE(qvideosink.h)
64 Q_MOC_INCLUDE(qvideoframe.h)
65 QML_NAMED_ELEMENT(VideoOutput)
66
67public:
68
69 enum FillMode
70 {
71 Stretch = Qt::IgnoreAspectRatio,
72 PreserveAspectFit = Qt::KeepAspectRatio,
73 PreserveAspectCrop = Qt::KeepAspectRatioByExpanding
74 };
75 Q_ENUM(FillMode)
76
77 QQuickVideoOutput(QQuickItem *parent = 0);
78 ~QQuickVideoOutput() override;
79
80 Q_INVOKABLE QVideoSink *videoSink() const;
81
82 FillMode fillMode() const;
83 void setFillMode(FillMode mode);
84
85 int orientation() const;
86 void setOrientation(int);
87
88 QRectF sourceRect() const;
89 QRectF contentRect() const;
90
91Q_SIGNALS:
92 void sourceChanged();
93 void fillModeChanged(QQuickVideoOutput::FillMode);
94 void orientationChanged();
95 void sourceRectChanged();
96 void contentRectChanged();
97
98protected:
99 QSGNode *updatePaintNode(QSGNode *, UpdatePaintNodeData *) override;
100 void itemChange(ItemChange change, const ItemChangeData &changeData) override;
101 void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override;
102 void releaseResources() override;
103
104private:
105 QSize nativeSize() const;
106 void updateGeometry();
107 QRectF adjustedViewport() const;
108
109 void setFrame(const QVideoFrame &frame);
110
111 void initRhiForSink();
112 void updateHdr(QSGVideoNode *videoNode);
113
114private Q_SLOTS:
115 void _q_newFrame(QSize);
116 void _q_updateGeometry();
117 void _q_invalidateSceneGraph();
118 void _q_sceneGraphInitialized();
119 void _q_afterFrameEnd();
120
121private:
122 QSize m_nativeSize;
123
124 bool m_geometryDirty = true;
125 QRectF m_lastRect; // Cache of last rect to avoid recalculating geometry
126 QRectF m_contentRect; // Destination pixel coordinates, unclipped
127 int m_orientation = 0;
128 bool m_mirrored = false;
129 QtVideo::Rotation m_frameDisplayingRotation = QtVideo::Rotation::None;
130 Qt::AspectRatioMode m_aspectRatioMode = Qt::KeepAspectRatio;
131
132 QPointer<QQuickWindow> m_window;
133 QVideoSink *m_sink = nullptr;
134 QVideoFrameFormat m_videoFormat;
135
136 QVideoFrameTexturePoolWPtr m_texturePool;
137 QVideoFrame m_frame;
138 bool m_frameChanged = false;
139 QMutex m_frameMutex;
140 QRectF m_renderedRect; // Destination pixel coordinates, clipped
141 QRectF m_sourceTextureRect; // Source texture coordinates
142};
143
144QT_END_NAMESPACE
145
146#endif // QQUICKVIDEOOUTPUT_P_H
147

source code of qtmultimedia/src/multimediaquick/qquickvideooutput_p.h