Added gst-plugins-base-subtitles0.10-0.10.34 for Meego Harmattan 1.2
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / 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 (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   GdkWindow *video_window_xwindow;
96   GtkWidget *window, *video_window;
97   GstElement *pipeline, *src, *sink;
98   gulong embed_xid;
99   GstStateChangeReturn sret;
100
101   if (!g_thread_supported ())
102     g_thread_init (NULL);
103
104   gst_init (&argc, &argv);
105   gtk_init (&argc, &argv);
106
107   /* prepare the pipeline */
108
109   pipeline = gst_pipeline_new ("xvoverlay");
110   src = gst_element_factory_make ("videotestsrc", NULL);
111   sink = find_video_sink ();
112
113   if (sink == NULL)
114     g_error ("Couldn't find a working video sink.");
115
116   gst_bin_add_many (GST_BIN (pipeline), src, sink, NULL);
117   gst_element_link (src, sink);
118
119   /* prepare the ui */
120
121   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
122   g_signal_connect (G_OBJECT (window), "delete-event",
123       G_CALLBACK (window_closed), (gpointer) pipeline);
124   gtk_window_set_default_size (GTK_WINDOW (window), 320, 240);
125   gtk_window_set_title (GTK_WINDOW (window), "GstXOverlay Gtk+ demo");
126
127   video_window = gtk_drawing_area_new ();
128   gtk_widget_set_double_buffered (video_window, FALSE);
129   gtk_container_add (GTK_CONTAINER (window), video_window);
130   gtk_container_set_border_width (GTK_CONTAINER (window), 16);
131
132   gtk_widget_show_all (window);
133   gtk_widget_realize (window);
134
135   video_window_xwindow = gtk_widget_get_window (video_window);
136   embed_xid = GDK_WINDOW_XID (video_window_xwindow);
137   gst_x_overlay_set_window_handle (GST_X_OVERLAY (sink), embed_xid);
138
139   /* run the pipeline */
140
141   sret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
142   if (sret == GST_STATE_CHANGE_FAILURE)
143     gst_element_set_state (pipeline, GST_STATE_NULL);
144   else
145     gtk_main ();
146
147   gst_object_unref (pipeline);
148   return 0;
149 }