1 #include <QApplication>
2 #include <QQmlApplicationEngine>
3 #include <QQuickWindow>
4 #include <QQuickItem>
5 #include <QRunnable>
6 #include <gst/gst.h>
7 
8 class SetPlaying : public QRunnable
9 {
10 public:
11   SetPlaying(GstElement *);
12   ~SetPlaying();
13 
14   void run ();
15 
16 private:
17   GstElement * pipeline_;
18 };
19 
SetPlaying(GstElement * pipeline)20 SetPlaying::SetPlaying (GstElement * pipeline)
21 {
22   this->pipeline_ = pipeline ? static_cast<GstElement *> (gst_object_ref (pipeline)) : NULL;
23 }
24 
~SetPlaying()25 SetPlaying::~SetPlaying ()
26 {
27   if (this->pipeline_)
28     gst_object_unref (this->pipeline_);
29 }
30 
31 void
run()32 SetPlaying::run ()
33 {
34   if (this->pipeline_)
35     gst_element_set_state (this->pipeline_, GST_STATE_PLAYING);
36 }
37 
main(int argc,char * argv[])38 int main(int argc, char *argv[])
39 {
40   int ret;
41 
42   gst_init (&argc, &argv);
43 
44   {
45     QGuiApplication app(argc, argv);
46 
47     GstElement *pipeline = gst_pipeline_new (NULL);
48     GstElement *src = gst_element_factory_make ("videotestsrc", NULL);
49     GstElement *glupload = gst_element_factory_make ("glupload", NULL);
50     /* the plugin must be loaded before loading the qml file to register the
51      * GstGLVideoItem qml item */
52     GstElement *sink = gst_element_factory_make ("qmlglsink", NULL);
53 
54     g_assert (src && glupload && sink);
55 
56     gst_bin_add_many (GST_BIN (pipeline), src, glupload, sink, NULL);
57     gst_element_link_many (src, glupload, sink, NULL);
58 
59     QQmlApplicationEngine engine;
60     engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
61 
62     QQuickItem *videoItem;
63     QQuickWindow *rootObject;
64 
65     /* find and set the videoItem on the sink */
66     rootObject = static_cast<QQuickWindow *> (engine.rootObjects().first());
67     videoItem = rootObject->findChild<QQuickItem *> ("videoItem");
68     g_assert (videoItem);
69     g_object_set(sink, "widget", videoItem, NULL);
70 
71     rootObject->scheduleRenderJob (new SetPlaying (pipeline),
72         QQuickWindow::BeforeSynchronizingStage);
73 
74     ret = app.exec();
75 
76     gst_element_set_state (pipeline, GST_STATE_NULL);
77     gst_object_unref (pipeline);
78   }
79 
80   gst_deinit ();
81 
82   return ret;
83 }
84