1 /* GStreamer
2  * Copyright (C) <2010> Alexander Bokovoy <ab@samba.org>
3  *
4  * qtgv-xoverlay: demonstrate overlay handling using qt graphics view
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 
26 #include "qtgv-videooverlay.h"
27 
28 #include <QApplication>
29 #include <QTimer>
30 
31 #include <gst/video/videooverlay.h>
32 
SinkPipeline(QGraphicsView * parent)33 SinkPipeline::SinkPipeline(QGraphicsView *parent) : QObject(parent)
34 {
35   GstStateChangeReturn sret;
36 
37   pipeline = gst_pipeline_new ("xvoverlay");
38   src = gst_element_factory_make ("videotestsrc", NULL);
39 
40   if ((sink = gst_element_factory_make ("xvimagesink", NULL))) {
41     sret = gst_element_set_state (sink, GST_STATE_READY);
42     if (sret != GST_STATE_CHANGE_SUCCESS) {
43       gst_element_set_state (sink, GST_STATE_NULL);
44       gst_object_unref (sink);
45     }
46   } else if ((sink = gst_element_factory_make ("ximagesink", NULL))) {
47     sret = gst_element_set_state (sink, GST_STATE_READY);
48     if (sret != GST_STATE_CHANGE_SUCCESS) {
49       gst_element_set_state (sink, GST_STATE_NULL);
50       gst_object_unref (sink);
51     }
52   } else if (strcmp (DEFAULT_VIDEOSINK, "xvimagesink") != 0 &&
53              strcmp (DEFAULT_VIDEOSINK, "ximagesink") != 0) {
54     if ((sink = gst_element_factory_make (DEFAULT_VIDEOSINK, NULL))) {
55       if (!GST_IS_BIN (sink)) {
56         sret = gst_element_set_state (sink, GST_STATE_READY);
57         if (sret != GST_STATE_CHANGE_SUCCESS) {
58           gst_element_set_state (sink, GST_STATE_NULL);
59           gst_object_unref (sink);
60           sink = NULL;
61         }
62       } else {
63         gst_object_unref (sink);
64         sink = NULL;
65       }
66     }
67   }
68 
69   if (sink == NULL)
70     g_error ("Couldn't find a working video sink.");
71 
72   gst_bin_add_many (GST_BIN (pipeline), src, sink, NULL);
73   gst_element_link (src, sink);
74   xwinid = parent->winId();
75 }
76 
~SinkPipeline()77 SinkPipeline::~SinkPipeline()
78 {
79   gst_element_set_state (pipeline, GST_STATE_NULL);
80   gst_object_unref (pipeline);
81 }
82 
startPipeline()83 void SinkPipeline::startPipeline()
84 {
85   GstStateChangeReturn sret;
86 
87   /* we know what the video sink is in this case (xvimagesink), so we can
88    * just set it directly here now (instead of waiting for a
89    * prepare-window-handle element message in a sync bus handler and setting
90    * it there) */
91   gst_video_overlay_set_window_handle (GST_VIDEO_OVERLAY (sink), xwinid);
92 
93   sret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
94   if (sret == GST_STATE_CHANGE_FAILURE) {
95     gst_element_set_state (pipeline, GST_STATE_NULL);
96     gst_object_unref (pipeline);
97     /* Exit application */
98     QTimer::singleShot(0, QApplication::activeWindow(), SLOT(quit()));
99   }
100 }
101 
main(int argc,char ** argv)102 int main( int argc, char **argv )
103 {
104     QApplication app(argc, argv);
105 
106     QGraphicsScene scene;
107     scene.setSceneRect( -100.0, -100.0, 200.0, 200.0 );
108 
109     QGraphicsView view( &scene );
110     view.resize(320, 240);
111     view.setWindowTitle("GstVideoOverlay Qt GraphicsView demo");
112     view.show();
113 
114     gst_init (&argc, &argv);
115     SinkPipeline pipeline(&view);
116     pipeline.startPipeline();
117 
118     int ret = app.exec();
119 
120     view.hide();
121 
122     return ret;
123 }
124