Debian Codegraph @

tests/examples/qt6/qmloverlay/main.cpp

raw
1 // SPDX-License-Identifier: BSD-2-Clause
2 //
3 // Copyright (C) 2023, Matthew Waters <[email protected]>
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // a) Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 //
11 // b) Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in
13 // the documentation and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
25 // THE POSSIBILITY OF SUCH DAMAGE.
26
27 #include <QApplication>
28 #include <QQmlApplicationEngine>
29 #include <QQuickWindow>
30 #include <QQuickItem>
31 #include <QRunnable>
32 #include <QDirIterator>
33 #include <gst/gst.h>
34
35 class SetPlaying : public QRunnable
36 {
37 public:
38 SetPlaying(GstElement *);
39 ~SetPlaying();
40
41 void run ();
42
43 private:
44 GstElement * pipeline_;
45 };
46
47 SetPlaying::SetPlaying (GstElement * pipeline)
48 {
49 this->pipeline_ = pipeline ? static_cast<GstElement *> (gst_object_ref (pipeline)) : NULL;
50 }
51
52 SetPlaying::~SetPlaying ()
53 {
54 if (this->pipeline_)
55 gst_object_unref (this->pipeline_);
56 }
57
58 void
59 SetPlaying::run ()
60 {
61 if (this->pipeline_)
62 gst_element_set_state (this->pipeline_, GST_STATE_PLAYING);
63 }
64
65 static void
66 on_overlay_scene_initialized (GstElement * overlay, gpointer unused)
67 {
68 QQuickItem *rootObject;
69 GST_INFO ("scene initialized");
70 g_object_get (overlay, "root-item", &rootObject, NULL);
71 QQuickItem *videoItem = rootObject->findChild<QQuickItem *> ("inputVideoItem");
72 g_object_set (overlay, "widget", videoItem, NULL);
73 }
74
75 int main(int argc, char *argv[])
76 {
77 int ret;
78
79 gst_init (&argc, &argv);
80
81 {
82 QGuiApplication app(argc, argv);
83
84 QQuickWindow::setGraphicsApi(QSGRendererInterface::OpenGL);
85
86 GstElement *pipeline = gst_pipeline_new (NULL);
87 GstElement *src = gst_element_factory_make ("videotestsrc", NULL);
88 GstElement *capsfilter = gst_element_factory_make ("capsfilter", NULL);
89 GstCaps *caps = gst_caps_from_string ("video/x-raw,format=YV12");
90 g_object_set (capsfilter, "caps", caps, NULL);
91 gst_clear_caps (&caps);
92 GstElement *glupload = gst_element_factory_make ("glupload", NULL);
93 /* the plugin must be loaded before loading the qml file to register the
94 * GstGLVideoItem qml item */
95 GstElement *overlay = gst_element_factory_make ("qml6gloverlay", NULL);
96 GstElement *overlay2 = gst_element_factory_make ("qml6gloverlay", NULL);
97 GstElement *sink = gst_element_factory_make ("qml6glsink", NULL);
98
99 g_assert (src && glupload && overlay && sink);
100
101 gst_bin_add_many (GST_BIN (pipeline), src, capsfilter, glupload, overlay, overlay2, sink, NULL);
102 gst_element_link_many (src, capsfilter, glupload, overlay, overlay2, sink, NULL);
103
104 /* load qmlglsink output */
105 QQmlApplicationEngine engine;
106 engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
107
108 QQuickItem *videoItem;
109 QQuickWindow *rootObject;
110
111 /* find and set the videoItem on the sink */
112 rootObject = static_cast<QQuickWindow *> (engine.rootObjects().first());
113 videoItem = rootObject->findChild<QQuickItem *> ("videoItem");
114 g_assert (videoItem);
115 g_object_set(sink, "widget", videoItem, NULL);
116
117 QDirIterator it(":", QDirIterator::Subdirectories);
118 while (it.hasNext()) {
119 qDebug() << it.next();
120 }
121
122 QFile f(":/overlay.qml");
123 if(!f.open(QIODevice::ReadOnly)) {
124 qWarning() << "error: " << f.errorString();
125 return 1;
126 }
127 QByteArray overlay_scene = f.readAll();
128 qDebug() << overlay_scene;
129
130 QFile f2(":/overlay2.qml");
131 if(!f2.open(QIODevice::ReadOnly)) {
132 qWarning() << "error: " << f2.errorString();
133 return 1;
134 }
135 QByteArray overlay_scene2 = f2.readAll();
136 qDebug() << overlay_scene2;
137
138 /* load qmlgloverlay contents */
139 g_signal_connect (overlay, "qml-scene-initialized", G_CALLBACK (on_overlay_scene_initialized), NULL);
140 g_object_set (overlay, "qml-scene", overlay_scene.data(), NULL);
141
142 g_signal_connect (overlay2, "qml-scene-initialized", G_CALLBACK (on_overlay_scene_initialized), NULL);
143 g_object_set (overlay2, "qml-scene", overlay_scene2.data(), NULL);
144
145 rootObject->scheduleRenderJob (new SetPlaying (pipeline),
146 QQuickWindow::BeforeSynchronizingStage);
147
148 ret = app.exec();
149
150 gst_element_set_state (pipeline, GST_STATE_NULL);
151 gst_object_unref (pipeline);
152 }
153
154 gst_deinit ();
155
156 return ret;
157 }