1 #include <QApplication>
2 #include <QQmlApplicationEngine>
3 #include <QQuickWindow>
4 #include <QQuickItem>
5 #include <QRunnable>
6 #include <QDirIterator>
7 #include <gst/gst.h>
8
9 class SetPlaying : public QRunnable
10 {
11 public:
12 SetPlaying(GstElement *);
13 ~SetPlaying();
14
15 void run ();
16
17 private:
18 GstElement * pipeline_;
19 };
20
SetPlaying(GstElement * pipeline)21 SetPlaying::SetPlaying (GstElement * pipeline)
22 {
23 this->pipeline_ = pipeline ? static_cast<GstElement *> (gst_object_ref (pipeline)) : NULL;
24 }
25
~SetPlaying()26 SetPlaying::~SetPlaying ()
27 {
28 if (this->pipeline_)
29 gst_object_unref (this->pipeline_);
30 }
31
32 void
run()33 SetPlaying::run ()
34 {
35 if (this->pipeline_)
36 gst_element_set_state (this->pipeline_, GST_STATE_PLAYING);
37 }
38
39 static void
on_overlay_scene_initialized(GstElement * overlay,gpointer unused)40 on_overlay_scene_initialized (GstElement * overlay, gpointer unused)
41 {
42 QQuickItem *rootObject;
43 GST_INFO ("scene initialized");
44 g_object_get (overlay, "root-item", &rootObject, NULL);
45 QQuickItem *videoItem = rootObject->findChild<QQuickItem *> ("inputVideoItem");
46 g_object_set (overlay, "widget", videoItem, NULL);
47 }
48
main(int argc,char * argv[])49 int main(int argc, char *argv[])
50 {
51 int ret;
52
53 gst_init (&argc, &argv);
54
55 {
56 QGuiApplication app(argc, argv);
57
58 GstElement *pipeline = gst_pipeline_new (NULL);
59 GstElement *src = gst_element_factory_make ("videotestsrc", NULL);
60 GstElement *glupload = gst_element_factory_make ("glupload", NULL);
61 /* the plugin must be loaded before loading the qml file to register the
62 * GstGLVideoItem qml item */
63 GstElement *overlay = gst_element_factory_make ("qmlgloverlay", NULL);
64 GstElement *overlay2 = gst_element_factory_make ("qmlgloverlay", NULL);
65 GstElement *sink = gst_element_factory_make ("qmlglsink", NULL);
66
67 g_assert (src && glupload && overlay && sink);
68
69 gst_bin_add_many (GST_BIN (pipeline), src, glupload, overlay, overlay2, sink, NULL);
70 gst_element_link_many (src, glupload, overlay, overlay2, sink, NULL);
71
72 /* load qmlglsink output */
73 QQmlApplicationEngine engine;
74 engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
75
76 QQuickItem *videoItem;
77 QQuickWindow *rootObject;
78
79 /* find and set the videoItem on the sink */
80 rootObject = static_cast<QQuickWindow *> (engine.rootObjects().first());
81 videoItem = rootObject->findChild<QQuickItem *> ("videoItem");
82 g_assert (videoItem);
83 g_object_set(sink, "widget", videoItem, NULL);
84
85 QDirIterator it(":", QDirIterator::Subdirectories);
86 while (it.hasNext()) {
87 qDebug() << it.next();
88 }
89
90 QFile f(":/overlay.qml");
91 if(!f.open(QIODevice::ReadOnly)) {
92 qWarning() << "error: " << f.errorString();
93 return 1;
94 }
95 QByteArray overlay_scene = f.readAll();
96 qDebug() << overlay_scene;
97
98 QFile f2(":/overlay2.qml");
99 if(!f2.open(QIODevice::ReadOnly)) {
100 qWarning() << "error: " << f2.errorString();
101 return 1;
102 }
103 QByteArray overlay_scene2 = f2.readAll();
104 qDebug() << overlay_scene2;
105
106 /* load qmlgloverlay contents */
107 g_signal_connect (overlay, "qml-scene-initialized", G_CALLBACK (on_overlay_scene_initialized), NULL);
108 g_object_set (overlay, "qml-scene", overlay_scene.data(), NULL);
109
110 g_signal_connect (overlay2, "qml-scene-initialized", G_CALLBACK (on_overlay_scene_initialized), NULL);
111 g_object_set (overlay2, "qml-scene", overlay_scene2.data(), NULL);
112
113 rootObject->scheduleRenderJob (new SetPlaying (pipeline),
114 QQuickWindow::BeforeSynchronizingStage);
115
116 ret = app.exec();
117
118 gst_element_set_state (pipeline, GST_STATE_NULL);
119 gst_object_unref (pipeline);
120 }
121
122 gst_deinit ();
123
124 return ret;
125 }
126