-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathVideoTextureItem.cpp
108 lines (91 loc) · 2.93 KB
/
VideoTextureItem.cpp
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* Copyright (c) 2020-2023 WangBin <wbsecg1 at gmail.com>
* MDK SDK in QtQuick RHI
*/
#include "VideoTextureItem.h"
#include <QtQuick/QQuickWindow>
#include <QGuiApplication>
#if __has_include(<QX11Info>)
#include <QX11Info>
#endif
#include "VideoTextureNode.h" // TODO: remove
#include "mdk/Player.h"
using namespace std;
VideoTextureNode* createNode(VideoTextureItem* item);
VideoTextureNode* createNodePriv(VideoTextureItem* item);
VideoTextureItem::VideoTextureItem()
{
#ifdef QX11INFO_X11_H
SetGlobalOption("X11Display", QX11Info::display());
qDebug("X11 display: %p", QX11Info::display());
#elif (QT_FEATURE_xcb + 0 == 1) && (QT_VERSION >= QT_VERSION_CHECK(6, 2, 0))
const auto xdisp = qGuiApp->nativeInterface<QNativeInterface::QX11Application>()->display();
SetGlobalOption("X11Display", xdisp);
qDebug("X11 display: %p", xdisp);
#endif
setFlag(ItemHasContents, true);
m_player = make_shared<Player>();
m_player->setDecoders(MediaType::Video, {"VT", "MFT:d3d=11", "VAAPI", "FFmpeg"});
m_player->setRenderCallback([=](void *){
QMetaObject::invokeMethod(this, "update");
});
// window() is null here
}
VideoTextureItem::~VideoTextureItem() = default;
// The beauty of using a true QSGNode: no need for complicated cleanup
// arrangements, unlike in other examples like metalunderqml, because the
// scenegraph will handle destroying the node at the appropriate time.
void VideoTextureItem::invalidateSceneGraph() // called on the render thread when the scenegraph is invalidated
{
m_node = nullptr;
}
void VideoTextureItem::releaseResources() // called on the gui thread if the item is removed from scene
{
m_node = nullptr;
}
QSGNode *VideoTextureItem::updatePaintNode(QSGNode *node, UpdatePaintNodeData *)
{
auto n = static_cast<VideoTextureNode*>(node);
if (!n && (width() <= 0 || height() <= 0))
return nullptr;
if (!n) {
m_node = createNode(this);
n = m_node;
}
m_node->sync();
window()->update(); // ensure getting to beforeRendering() at some point
return n;
}
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
void VideoTextureItem::geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry)
#else
void VideoTextureItem::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
#endif
{
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
QQuickItem::geometryChange(newGeometry, oldGeometry);
#else
QQuickItem::geometryChanged(newGeometry, oldGeometry);
#endif
if (newGeometry.size() != oldGeometry.size())
update();
}
void VideoTextureItem::setSource(const QString & s)
{
m_player->setMedia(s.toUtf8().data());
m_source = s;
emit sourceChanged();
if (autoPlay())
play();
}
void VideoTextureItem::setAutoPlay(bool value)
{
if (m_autoPlay == value)
return;
m_autoPlay = value;
emit autoPlayChanged();
}
void VideoTextureItem::play()
{
m_player->set(PlaybackState::Playing);
}