Debian Codegraph @

tests/examples/qt6/qmlmixer/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_mixer_scene_initialized (GstElement * mixer, gpointer unused)
67 {
68 QQuickItem *rootObject;
69 GST_INFO ("scene initialized");
70 g_object_get (mixer, "root-item", &rootObject, NULL);
71
72 QQuickItem *videoItem0 = rootObject->findChild<QQuickItem *> ("inputVideoItem0");
73 GstPad *sink0 = gst_element_get_static_pad (mixer, "sink_0");
74 g_object_set (sink0, "widget", videoItem0, NULL);
75 gst_clear_object (&sink0);
76
77 QQuickItem *videoItem1 = rootObject->findChild<QQuickItem *> ("inputVideoItem1");
78 GstPad *sink1 = gst_element_get_static_pad (mixer, "sink_1");
79 g_object_set (sink1, "widget", videoItem1, NULL);
80 gst_clear_object (&sink1);
81 }
82
83 int main(int argc, char *argv[])
84 {
85 int ret;
86
87 gst_init (&argc, &argv);
88
89 {
90 QGuiApplication app(argc, argv);
91
92 QQuickWindow::setGraphicsApi(QSGRendererInterface::OpenGL);
93
94 GstElement *pipeline = gst_pipeline_new (NULL);
95 GstElement *src0 = gst_element_factory_make ("videotestsrc", NULL);
96 GstElement *capsfilter = gst_element_factory_make ("capsfilter", NULL);
97 GstCaps *caps = gst_caps_from_string ("video/x-raw,format=YV12");
98 g_object_set (capsfilter, "caps", caps, NULL);
99 gst_clear_caps (&caps);
100 GstElement *glupload0 = gst_element_factory_make ("glupload", NULL);
101 GstElement *src1 = gst_element_factory_make ("videotestsrc", NULL);
102 gst_util_set_object_arg ((GObject *) src1, "pattern", "ball");
103 GstElement *glupload1 = gst_element_factory_make ("glupload", NULL);
104 /* the plugin must be loaded before loading the qml file to register the
105 * GstGLVideoItem qml item */
106 GstElement *mixer = gst_element_factory_make ("qml6glmixer", NULL);
107 GstElement *sink = gst_element_factory_make ("qml6glsink", NULL);
108
109 g_assert (src0 && glupload0 && mixer && sink);
110
111 gst_bin_add_many (GST_BIN (pipeline), src0, capsfilter, glupload0, src1, glupload1, mixer, sink, NULL);
112 gst_element_link_many (src0, capsfilter, glupload0, mixer, sink, NULL);
113 gst_element_link_many (src1, glupload1, mixer, NULL);
114
115 /* load qmlglsink output */
116 QQmlApplicationEngine engine;
117 engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
118
119 QQuickItem *videoItem;
120 QQuickWindow *rootObject;
121
122 /* find and set the videoItem on the sink */
123 rootObject = static_cast<QQuickWindow *> (engine.rootObjects().first());
124 videoItem = rootObject->findChild<QQuickItem *> ("videoItem");
125 g_assert (videoItem);
126 g_object_set(sink, "widget", videoItem, NULL);
127
128 QDirIterator it(":", QDirIterator::Subdirectories);
129 while (it.hasNext()) {
130 qDebug() << it.next();
131 }
132
133 QFile f(":/mixer.qml");
134 if(!f.open(QIODevice::ReadOnly)) {
135 qWarning() << "error: " << f.errorString();
136 return 1;
137 }
138 QByteArray overlay_scene = f.readAll();
139 qDebug() << overlay_scene;
140
141 /* load qmlgloverlay contents */
142 g_signal_connect (mixer, "qml-scene-initialized", G_CALLBACK (on_mixer_scene_initialized), NULL);
143 g_object_set (mixer, "qml-scene", overlay_scene.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 }