9a08c95fe7f5a0383eb21dfb8dd2f26ab084968e
[platform/upstream/gst-plugins-base.git] / tests / examples / overlay / gtk-xoverlay.c
1 /* GStreamer
2  * Copyright (C) <2010> Stefan Kost <ensonic@users.sf.net>
3  *
4  * gtk-xoverlay: demonstrate overlay handling using gtk
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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <glib.h>
27 #include <gdk/gdkx.h>
28 #include <gtk/gtk.h>
29
30 #include <gst/gst.h>
31 #include <gst/interfaces/xoverlay.h>
32
33 #include <string.h>
34
35 static void
36 window_closed (GtkWidget * widget, GdkEvent * event, gpointer user_data)
37 {
38   GstElement *pipeline = user_data;
39
40   gtk_widget_hide_all (widget);
41   gst_element_set_state (pipeline, GST_STATE_NULL);
42   gtk_main_quit ();
43 }
44
45 /* slightly convoluted way to find a working video sink that's not a bin,
46  * one could use autovideosink from gst-plugins-good instead
47  */
48 static GstElement *
49 find_video_sink (void)
50 {
51   GstStateChangeReturn sret;
52   GstElement *sink;
53
54   if ((sink = gst_element_factory_make ("xvimagesink", NULL))) {
55     sret = gst_element_set_state (sink, GST_STATE_READY);
56     if (sret == GST_STATE_CHANGE_SUCCESS)
57       return sink;
58
59     gst_element_set_state (sink, GST_STATE_NULL);
60   }
61   gst_object_unref (sink);
62
63   if ((sink = gst_element_factory_make ("ximagesink", NULL))) {
64     sret = gst_element_set_state (sink, GST_STATE_READY);
65     if (sret == GST_STATE_CHANGE_SUCCESS)
66       return sink;
67
68     gst_element_set_state (sink, GST_STATE_NULL);
69   }
70   gst_object_unref (sink);
71
72   if (strcmp (DEFAULT_VIDEOSINK, "xvimagesink") == 0 ||
73       strcmp (DEFAULT_VIDEOSINK, "ximagesink") == 0)
74     return NULL;
75
76   if ((sink = gst_element_factory_make (DEFAULT_VIDEOSINK, NULL))) {
77     if (GST_IS_BIN (sink)) {
78       gst_object_unref (sink);
79       return NULL;
80     }
81
82     sret = gst_element_set_state (sink, GST_STATE_READY);
83     if (sret == GST_STATE_CHANGE_SUCCESS)
84       return sink;
85
86     gst_element_set_state (sink, GST_STATE_NULL);
87   }
88   gst_object_unref (sink);
89   return NULL;
90 }
91
92 int
93 main (int argc, char **argv)
94 {
95   GtkWidget *window, *video_window;
96   GstElement *pipeline, *src, *sink;
97   gulong embed_xid;
98   GstStateChangeReturn sret;
99
100   if (!g_thread_supported ())
101     g_thread_init (NULL);
102
103   gst_init (&argc, &argv);
104   gtk_init (&argc, &argv);
105
106   /* prepare the pipeline */
107
108   pipeline = gst_pipeline_new ("xvoverlay");
109   src = gst_element_factory_make ("videotestsrc", NULL);
110   sink = find_video_sink ();
111
112   if (sink == NULL)
113     g_error ("Couldn't find a working video sink.");
114
115   gst_bin_add_many (GST_BIN (pipeline), src, sink, NULL);
116   gst_element_link (src, sink);
117
118   /* prepare the ui */
119
120   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
121   g_signal_connect (G_OBJECT (window), "delete-event",
122       G_CALLBACK (window_closed), (gpointer) pipeline);
123   gtk_window_set_default_size (GTK_WINDOW (window), 320, 240);
124   gtk_window_set_title (GTK_WINDOW (window), "GstXOverlay Gtk+ demo");
125
126   video_window = gtk_drawing_area_new ();
127   gtk_widget_set_double_buffered (video_window, FALSE);
128   gtk_container_add (GTK_CONTAINER (window), video_window);
129   gtk_container_set_border_width (GTK_CONTAINER (window), 16);
130
131   gtk_widget_show_all (window);
132   gtk_widget_realize (window);
133
134   embed_xid = GDK_WINDOW_XID (video_window->window);
135   gst_x_overlay_set_xwindow_id (GST_X_OVERLAY (sink), embed_xid);
136
137   /* run the pipeline */
138
139   sret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
140   if (sret == GST_STATE_CHANGE_FAILURE)
141     gst_element_set_state (pipeline, GST_STATE_NULL);
142   else
143     gtk_main ();
144
145   gst_object_unref (pipeline);
146   return 0;
147 }