1 /* GStreamer
2 * Copyright (C) <2010> Stefan Kost <ensonic@users.sf.net>
3 *
4 * qt-xoverlay: demonstrate overlay handling using qt
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 <glib.h>
27 #include <gst/gst.h>
28 #include <gst/video/videooverlay.h>
29
30 #include <QApplication>
31 #include <QTimer>
32 #include <QWidget>
33
34 /* slightly convoluted way to find a working video sink that's not a bin,
35 * one could use autovideosink from gst-plugins-good instead
36 */
37 static GstElement *
find_video_sink(void)38 find_video_sink (void)
39 {
40 GstStateChangeReturn sret;
41 GstElement *sink;
42
43 if ((sink = gst_element_factory_make ("xvimagesink", NULL))) {
44 sret = gst_element_set_state (sink, GST_STATE_READY);
45 if (sret == GST_STATE_CHANGE_SUCCESS)
46 return sink;
47
48 gst_element_set_state (sink, GST_STATE_NULL);
49 gst_object_unref (sink);
50 }
51
52 if ((sink = gst_element_factory_make ("ximagesink", NULL))) {
53 sret = gst_element_set_state (sink, GST_STATE_READY);
54 if (sret == GST_STATE_CHANGE_SUCCESS)
55 return sink;
56
57 gst_element_set_state (sink, GST_STATE_NULL);
58 gst_object_unref (sink);
59 }
60
61 if (strcmp (DEFAULT_VIDEOSINK, "xvimagesink") == 0 ||
62 strcmp (DEFAULT_VIDEOSINK, "ximagesink") == 0)
63 return NULL;
64
65 if ((sink = gst_element_factory_make (DEFAULT_VIDEOSINK, NULL))) {
66 if (GST_IS_BIN (sink)) {
67 gst_object_unref (sink);
68 return NULL;
69 }
70
71 sret = gst_element_set_state (sink, GST_STATE_READY);
72 if (sret == GST_STATE_CHANGE_SUCCESS)
73 return sink;
74
75 gst_element_set_state (sink, GST_STATE_NULL);
76 gst_object_unref (sink);
77 }
78
79 return NULL;
80 }
81
main(int argc,char * argv[])82 int main(int argc, char *argv[])
83 {
84 gst_init (&argc, &argv);
85 QApplication app(argc, argv);
86 app.setQuitOnLastWindowClosed(true);
87
88 /* prepare the pipeline */
89
90 GstElement *pipeline = gst_pipeline_new ("xvoverlay");
91 GstElement *src = gst_element_factory_make ("videotestsrc", NULL);
92 GstElement *sink = find_video_sink ();
93
94 if (sink == NULL)
95 g_error ("Couldn't find a working video sink.");
96
97 gst_bin_add_many (GST_BIN (pipeline), src, sink, NULL);
98 gst_element_link (src, sink);
99
100 /* prepare the ui */
101
102 QWidget window;
103 window.resize(320, 240);
104 window.setWindowTitle("GstVideoOverlay Qt demo");
105 window.show();
106
107 WId xwinid = window.winId();
108 gst_video_overlay_set_window_handle (GST_VIDEO_OVERLAY (sink), xwinid);
109
110 /* run the pipeline */
111
112 GstStateChangeReturn sret = gst_element_set_state (pipeline,
113 GST_STATE_PLAYING);
114 if (sret == GST_STATE_CHANGE_FAILURE) {
115 gst_element_set_state (pipeline, GST_STATE_NULL);
116 gst_object_unref (pipeline);
117 /* Exit application */
118 QTimer::singleShot(0, QApplication::activeWindow(), SLOT(quit()));
119 }
120
121 int ret = app.exec();
122
123 window.hide();
124 gst_element_set_state (pipeline, GST_STATE_NULL);
125 gst_object_unref (pipeline);
126
127 return ret;
128 }
129