Debian Codegraph @

tests/examples/qt6/qmloverlayitem/main.cpp

raw
1 // SPDX-License-Identifier: BSD-2-Clause
2 //
3 // Copyright (C) 2025, 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 int main(int argc, char *argv[])
66 {
67 int ret;
68
69 gst_init (&argc, &argv);
70
71 {
72 QGuiApplication app(argc, argv);
73
74 QQuickWindow::setGraphicsApi(QSGRendererInterface::OpenGL);
75
76 GstElement *pipeline = gst_pipeline_new (NULL);
77 GstElement *src = gst_element_factory_make ("videotestsrc", NULL);
78 GstElement *capsfilter = gst_element_factory_make ("capsfilter", NULL);
79 GstCaps *caps = gst_caps_from_string ("video/x-raw,format=RGBA");
80 g_object_set (capsfilter, "caps", caps, NULL);
81 gst_clear_caps (&caps);
82 GstElement *glupload = gst_element_factory_make ("glupload", NULL);
83 /* the plugin must be loaded before loading the qml file to register the
84 * GstGLVideoItem qml item */
85 GstElement *overlay = gst_element_factory_make ("qml6gloverlay", NULL);
86 GstElement *sink = gst_element_factory_make ("qml6glsink", NULL);
87
88 g_assert (src && glupload && overlay && sink);
89
90 gst_bin_add_many (GST_BIN (pipeline), src, capsfilter, glupload, overlay, sink, NULL);
91 gst_element_link_many (src, capsfilter, glupload, overlay, sink, NULL);
92
93 /* load qmlglsink output */
94 QQmlApplicationEngine engine;
95 engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
96
97 QQuickItem *videoItem;
98 QQuickWindow *rootObject;
99
100 /* find and set the videoItem on the sink */
101 rootObject = static_cast<QQuickWindow *> (engine.rootObjects().first());
102 videoItem = rootObject->findChild<QQuickItem *> ("videoItem");
103 g_assert (videoItem);
104 g_object_set(sink, "widget", videoItem, NULL);
105
106 /* load qmlgloverlay contents */
107 QQmlComponent overlay_component = QQmlComponent(&engine, QUrl(QStringLiteral("qrc:/overlay.qml")));
108 QQuickItem *overlay_item = qobject_cast<QQuickItem *>(overlay_component.create());
109 QQuickItem *inputVideoItem = overlay_item->findChild<QQuickItem *> ("inputVideoItem");
110 g_assert (inputVideoItem);
111 g_object_set (overlay, "root-item", overlay_item, "widget", inputVideoItem, 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 }