1 /******************************************************************************
2     QtAV:  Multimedia framework based on Qt and FFmpeg
3     Copyright (C) 2012-2017 Wang Bin <wbsecg1@gmail.com>
4     theoribeiro <theo@fictix.com.br>
5 
6 *   This file is part of QtAV (from 2013)
7 
8     This library is free software; you can redistribute it and/or
9     modify it under the terms of the GNU Lesser General Public
10     License as published by the Free Software Foundation; either
11     version 2.1 of the License, or (at your option) any later version.
12 
13     This library is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16     Lesser General Public License for more details.
17 
18     You should have received a copy of the GNU Lesser General Public
19     License along with this library; if not, write to the Free Software
20     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21 ******************************************************************************/
22 
23 #ifndef QTAV_QML_QQUICKRENDERER_H
24 #define QTAV_QML_QQUICKRENDERER_H
25 
26 #include <QtAV/VideoRenderer.h>
27 #include <QtQuick/QQuickItem>
28 #include <QmlAV/QuickFilter.h>
29 
30 namespace QtAV {
31 class QQuickItemRendererPrivate;
32 class QQuickItemRenderer : public QQuickItem, public VideoRenderer
33 {
34     Q_OBJECT
35     Q_DISABLE_COPY(QQuickItemRenderer)
36     DPTR_DECLARE_PRIVATE(QQuickItemRenderer)
37     Q_PROPERTY(bool opengl READ isOpenGL WRITE setOpenGL NOTIFY openGLChanged)
38     Q_PROPERTY(QObject* source READ source WRITE setSource NOTIFY sourceChanged)
39     Q_PROPERTY(FillMode fillMode READ fillMode WRITE setFillMode NOTIFY fillModeChanged)
40     Q_PROPERTY(int orientation READ orientation WRITE setOrientation NOTIFY orientationChanged)
41     Q_PROPERTY(QRectF contentRect READ contentRect NOTIFY contentRectChanged)
42     Q_PROPERTY(QRectF sourceRect READ sourceRect NOTIFY videoFrameSizeChanged)
43     // regionOfInterest > sourceRect
44     Q_PROPERTY(QRectF regionOfInterest READ regionOfInterest WRITE setRegionOfInterest NOTIFY regionOfInterestChanged)
45     Q_PROPERTY(qreal sourceAspectRatio READ sourceAspectRatio NOTIFY sourceAspectRatioChanged)
46     Q_PROPERTY(QSize videoFrameSize READ videoFrameSize NOTIFY videoFrameSizeChanged)
47     Q_PROPERTY(QSize frameSize READ videoFrameSize NOTIFY videoFrameSizeChanged)
48     Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor NOTIFY backgroundColorChanged)
49     Q_PROPERTY(QQmlListProperty<QuickVideoFilter> filters READ filters)
50     Q_ENUMS(FillMode)
51 public:
52     enum FillMode {
53         Stretch            = Qt::IgnoreAspectRatio,
54         PreserveAspectFit  = Qt::KeepAspectRatio,
55         PreserveAspectCrop = Qt::KeepAspectRatioByExpanding
56     };
57 
58     explicit QQuickItemRenderer(QQuickItem *parent = 0);
59     VideoRendererId id() const Q_DECL_OVERRIDE;
60     bool isSupported(VideoFormat::PixelFormat pixfmt) const Q_DECL_OVERRIDE;
61 
62     QObject *source() const;
63     /*!
64      * \brief setSource
65      * \param source nullptr, an object of type AVPlayer or QmlAVPlayer
66      */
67     void setSource(QObject *source);
68 
69     FillMode fillMode() const;
70     void setFillMode(FillMode mode);
71 
72     QRectF contentRect() const;
73     QRectF sourceRect() const;
74 
75     Q_INVOKABLE QPointF mapPointToItem(const QPointF &point) const;
76     Q_INVOKABLE QRectF mapRectToItem(const QRectF &rectangle) const;
77     Q_INVOKABLE QPointF mapNormalizedPointToItem(const QPointF &point) const;
78     Q_INVOKABLE QRectF mapNormalizedRectToItem(const QRectF &rectangle) const;
79     Q_INVOKABLE QPointF mapPointToSource(const QPointF &point) const;
80     Q_INVOKABLE QRectF mapRectToSource(const QRectF &rectangle) const;
81     Q_INVOKABLE QPointF mapPointToSourceNormalized(const QPointF &point) const;
82     Q_INVOKABLE QRectF mapRectToSourceNormalized(const QRectF &rectangle) const;
83 
84     bool isOpenGL() const;
85     void setOpenGL(bool o);
86 
87     QQmlListProperty<QuickVideoFilter> filters();
88 Q_SIGNALS:
89     void sourceChanged();
90     void fillModeChanged(QQuickItemRenderer::FillMode);
91     void orientationChanged() Q_DECL_OVERRIDE;
92     void contentRectChanged() Q_DECL_OVERRIDE;
93     void regionOfInterestChanged() Q_DECL_OVERRIDE;
94     void openGLChanged();
95     void sourceAspectRatioChanged(qreal value) Q_DECL_OVERRIDE;
96     void videoFrameSizeChanged() Q_DECL_OVERRIDE;
97     void backgroundColorChanged() Q_DECL_OVERRIDE;
98 protected:
99     bool event(QEvent *e) Q_DECL_OVERRIDE;
100     void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) Q_DECL_OVERRIDE;
101 
102     bool receiveFrame(const VideoFrame &frame) Q_DECL_OVERRIDE;
103     void drawFrame() Q_DECL_OVERRIDE;
104     // QQuickItem interface
105     QSGNode *updatePaintNode(QSGNode *node, UpdatePaintNodeData *data) Q_DECL_OVERRIDE;
106 private slots:
107     void handleWindowChange(QQuickWindow *win);
108     void beforeRendering();
109     void afterRendering();
110 private:
111     bool onSetOrientation(int value) Q_DECL_OVERRIDE;
112 
113     static void vf_append(QQmlListProperty<QuickVideoFilter> *property, QuickVideoFilter *value);
114     static int vf_count(QQmlListProperty<QuickVideoFilter> *property);
115     static QuickVideoFilter *vf_at(QQmlListProperty<QuickVideoFilter> *property, int index);
116     static void vf_clear(QQmlListProperty<QuickVideoFilter> *property);
117 };
118 typedef QQuickItemRenderer VideoRendererQQuickItem;
119 }
120 QML_DECLARE_TYPE(QtAV::QQuickItemRenderer)
121 
122 #endif // QTAV_QML_QQUICKRENDERER_H
123