Debian Codegraph @

tests/examples/qt6/qmlrendersrc/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 <QDirIterator>
32 #include <QTimer>
33 #include <gst/gst.h>
34
35 class SwitchFramerate : public QTimer {
36 public:
37 SwitchFramerate(GstElement *capsfilter) : QTimer() {
38 m_capsfilter = (GstElement *) gst_object_ref ((gpointer) capsfilter);
39 QObject::connect(this, &QTimer::timeout, this, QOverload<>::of(&SwitchFramerate::switchFramerate));
40 m_currentFramerate = 10;
41 }
42 ~SwitchFramerate() {
43 gst_object_unref (m_capsfilter);
44 }
45
46 void switchFramerate() {
47 GstCaps* caps;
48 g_object_get (m_capsfilter, "caps", &caps, NULL);
49 if (!caps)
50 return;
51
52 caps = gst_caps_make_writable (caps);
53 if (m_currentFramerate <= 10) {
54 m_currentFramerate = 20;
55 } else {
56 m_currentFramerate = 10;
57 }
58 gst_println ("changing framerate to %u", m_currentFramerate);
59 gst_caps_set_simple (caps, "framerate", GST_TYPE_FRACTION, m_currentFramerate, 1, NULL);
60 g_object_set (m_capsfilter, "caps", caps, NULL);
61 gst_clear_caps (&caps);
62 }
63
64 GstElement *m_capsfilter;
65 int m_currentFramerate;
66 };
67
68 int main(int argc, char *argv[])
69 {
70 int ret;
71 bool do_switch = false;
72
73 gst_init (&argc, &argv);
74
75 if (argc >= 2 && g_strcmp0(argv[1], "switch") == 0)
76 do_switch = true;
77
78 {
79 QGuiApplication app(argc, argv);
80
81 QQuickWindow::setGraphicsApi(QSGRendererInterface::OpenGL);
82
83 GstElement *pipeline = gst_pipeline_new (NULL);
84 GstElement *src = gst_element_factory_make ("qml6glrendersrc", NULL);
85 gst_util_set_object_arg (G_OBJECT (src), "max-framerate", "10/1");
86 GstElement *capsfilter = gst_element_factory_make ("capsfilter", NULL);
87 GstCaps *caps;
88 if (do_switch)
89 caps = gst_caps_from_string ("video/x-raw(ANY),width=640,height=240,framerate=20/1");
90 else
91 caps = gst_caps_from_string ("video/x-raw(ANY),width=640,height=240,framerate=0/1");
92 g_object_set (capsfilter, "caps", caps, NULL);
93 gst_clear_caps (&caps);
94 GstElement *download = gst_element_factory_make ("identity", NULL);
95 GstElement *convert = gst_element_factory_make ("identity", NULL);
96 GstElement *sink = gst_element_factory_make ("glimagesink", NULL);
97
98 g_assert (src && capsfilter && download && convert && sink);
99
100 gst_bin_add_many (GST_BIN (pipeline), src, capsfilter, download, convert, sink, NULL);
101 gst_element_link_many (src, capsfilter, download, convert, sink, NULL);
102
103 /* load qmlglsink output */
104 QQmlApplicationEngine engine;
105 engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
106
107 QQuickItem *rootItem;
108
109 /* find and set the videoItem on the sink */
110 rootItem = static_cast<QQuickItem *> (engine.rootObjects().first());
111 g_object_set(src, "root-item", rootItem, NULL);
112
113 gst_element_set_state (pipeline, GST_STATE_PLAYING);
114
115 SwitchFramerate *timer = nullptr;
116 if (do_switch) {
117 SwitchFramerate *timer = new SwitchFramerate(capsfilter);
118 timer->start(2000);
119 }
120
121 ret = app.exec();
122
123 if (do_switch) {
124 delete timer;
125 }
126
127 gst_element_set_state (pipeline, GST_STATE_NULL);
128 gst_object_unref (pipeline);
129 }
130
131 gst_deinit ();
132
133 return ret;
134 }