Debian Codegraph @

tests/examples/qt6d3d11/qml6d3d11sink/qml6d3d11sink.cpp

raw symbols SCIP
1 /* GStreamer
2 * Copyright (C) 2023 Seungha Yang <[email protected]>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #include <QApplication>
21 #include <QQmlApplicationEngine>
22 #include <QQuickWindow>
23 #include <QQuickItem>
24 #include <QRunnable>
25
26 #include <gst/gst.h>
27
28 class PipelineRunner : public QRunnable
29 {
30 public:
31 PipelineRunner(GstElement * p);
32 ~PipelineRunner();
33
34 void run ();
35
36 private:
37 GstElement *pipeline;
38 };
39
40 PipelineRunner::PipelineRunner (GstElement * p)
41 {
42 pipeline = (GstElement *) gst_object_ref (p);
43 }
44
45 PipelineRunner::~PipelineRunner ()
46 {
47 gst_object_unref (this->pipeline);
48 }
49
50 void
51 PipelineRunner::run ()
52 {
53 gst_element_set_state (this->pipeline, GST_STATE_PLAYING);
54 }
55
56 struct AppData
57 {
58 QGuiApplication *app;
59 GstElement *pipeline;
60 GMainLoop *loop;
61 GMainContext *context;
62 };
63
64 static gboolean
65 message_cb (GstBus * bus, GstMessage * msg, AppData * data)
66 {
67 gboolean do_exit = FALSE;
68
69 switch (GST_MESSAGE_TYPE (msg)) {
70 case GST_MESSAGE_ERROR:
71 gst_println ("Got pipeline error");
72 do_exit = TRUE;
73 break;
74 case GST_MESSAGE_EOS:
75 gst_println ("Got pipeline EOS");
76 do_exit = TRUE;
77 break;
78 default:
79 break;
80 }
81
82 if (do_exit) {
83 g_main_loop_quit (data->loop);
84 data->app->quit ();
85 }
86
87 return G_SOURCE_CONTINUE;
88 }
89
90 static gpointer
91 pipeline_watch_thread (AppData * data)
92 {
93 GstBus *bus;
94
95 g_main_context_push_thread_default (data->context);
96
97 bus = gst_pipeline_get_bus (GST_PIPELINE (data->pipeline));
98 gst_bus_add_watch (bus, (GstBusFunc) message_cb, data);
99
100 g_main_loop_run (data->loop);
101
102 gst_bus_remove_watch (bus);
103 gst_object_unref (bus);
104 g_main_context_pop_thread_default (data->context);
105
106 return nullptr;
107 }
108
109 static int
110 run_application (int argc, char ** argv, GstElement * pipeline,
111 GstElement * sink)
112 {
113 QGuiApplication app (argc, argv);
114 GThread *bus_thread;
115 int ret;
116
117 /* NOTE: Default API on Windows is D3D11 already */
118 QQuickWindow::setGraphicsApi (QSGRendererInterface::Direct3D11);
119
120 QQmlApplicationEngine engine;
121 engine.load (QUrl (QStringLiteral ("qrc:/qml6d3d11sink.qml")));
122
123 QQuickItem *videoItem;
124 QQuickWindow *rootObject;
125 AppData app_data;
126
127 app_data.app = &app;
128 app_data.pipeline = pipeline;
129 app_data.context = g_main_context_new ();
130 app_data.loop = g_main_loop_new (app_data.context, FALSE);
131
132 /* find and set the videoItem on the sink */
133 rootObject = static_cast<QQuickWindow *> (engine.rootObjects().first());
134 videoItem = rootObject->findChild<QQuickItem *> ("videoItem");
135 g_assert (videoItem);
136 g_object_set (sink, "widget", videoItem, nullptr);
137
138 rootObject->scheduleRenderJob (new PipelineRunner (pipeline),
139 QQuickWindow::BeforeSynchronizingStage);
140
141 bus_thread = g_thread_new ("pipeline-watch-thread",
142 (GThreadFunc) pipeline_watch_thread, &app_data);
143
144 ret = app.exec();
145
146 g_main_loop_quit (app_data.loop);
147 g_thread_join (bus_thread);
148 g_main_loop_unref (app_data.loop);
149 g_main_context_unref (app_data.context);
150
151 gst_element_set_state (pipeline, GST_STATE_NULL);
152
153 return ret;
154 }
155
156 int
157 main(int argc, char *argv[])
158 {
159 GOptionContext *option_ctx;
160 gchar *uri = nullptr;
161 GError *err = nullptr;
162 gboolean ret;
163 GOptionEntry options[] = {
164 {"uri", 0, 0, G_OPTION_ARG_STRING, &uri, "URI to play", nullptr},
165 {nullptr, }
166 };
167 GstElement *pipeline = nullptr;
168 GstElement *sink = nullptr;
169 int exit_code;
170
171 option_ctx = g_option_context_new ("Qt6 Direct3D11 QML render");
172 g_option_context_add_main_entries (option_ctx, options, nullptr);
173 g_option_context_add_group (option_ctx, gst_init_get_option_group ());
174 ret = g_option_context_parse (option_ctx, &argc, &argv, &err);
175 g_option_context_free (option_ctx);
176
177 if (!ret) {
178 gst_printerrln ("option parsing failed: %s", err->message);
179 g_clear_error (&err);
180 return 1;
181 }
182
183 /* the plugin must be loaded before loading the qml file to register the
184 * GstD3D11Qt6VideoItem */
185 sink = gst_element_factory_make ("qml6d3d11sink", nullptr);
186
187 if (!uri) {
188 pipeline = gst_pipeline_new (nullptr);
189
190 GstElement *src = gst_element_factory_make ("videotestsrc", nullptr);
191 GstElement *upload = gst_element_factory_make ("d3d11upload", nullptr);
192 GstElement *capsfilter = gst_element_factory_make ("capsfilter", nullptr);
193 GstCaps *caps;
194
195 caps = gst_caps_from_string ("video/x-raw(memory:D3D11Memory),format=RGBA");
196 g_object_set (capsfilter, "caps", caps, nullptr);
197 gst_caps_unref (caps);
198
199 gst_bin_add_many (GST_BIN (pipeline),
200 src, upload, capsfilter, sink, nullptr);
201 gst_element_link_many (src, upload, capsfilter, sink, nullptr);
202 } else {
203 pipeline = gst_element_factory_make ("playbin", nullptr);
204
205 g_object_set (pipeline, "uri", uri, "video-sink", sink, nullptr);
206 }
207
208 exit_code = run_application (argc, argv, pipeline, sink);
209
210 gst_object_unref (pipeline);
211
212 gst_deinit ();
213
214 return exit_code;
215 }