| 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 | |
| 30 | QT_BEGIN_NAMESPACE |
| 31 | |
| 32 | class QQuickVideoBackend; |
| 33 | class QVideoOutputOrientationHandler; |
| 34 | class QVideoSink; |
| 35 | class QSGVideoNode; |
| 36 | class QVideoFrameTexturePool; |
| 37 | using QVideoFrameTexturePoolWPtr = std::weak_ptr<QVideoFrameTexturePool>; |
| 38 | |
| 39 | class QQuickVideoSink : public QVideoSink |
| 40 | { |
| 41 | Q_OBJECT |
| 42 | QML_NAMED_ELEMENT(VideoSink) |
| 43 | public: |
| 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 | |
| 50 | Q_SIGNALS: |
| 51 | void videoFrameChanged(); |
| 52 | }; |
| 53 | |
| 54 | class 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 | |
| 67 | public: |
| 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 | |
| 91 | Q_SIGNALS: |
| 92 | void sourceChanged(); |
| 93 | void fillModeChanged(QQuickVideoOutput::FillMode); |
| 94 | void orientationChanged(); |
| 95 | void sourceRectChanged(); |
| 96 | void contentRectChanged(); |
| 97 | |
| 98 | protected: |
| 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 | |
| 104 | private: |
| 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 | |
| 114 | private Q_SLOTS: |
| 115 | void _q_newFrame(QSize); |
| 116 | void _q_updateGeometry(); |
| 117 | void _q_invalidateSceneGraph(); |
| 118 | void _q_sceneGraphInitialized(); |
| 119 | void _q_afterFrameEnd(); |
| 120 | |
| 121 | private: |
| 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 | |
| 144 | QT_END_NAMESPACE |
| 145 | |
| 146 | #endif // QQUICKVIDEOOUTPUT_P_H |
| 147 | |