1 /*
2  * GStreamer
3  * Copyright (C) 2014 Matthew Waters <matthew@centricular.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 #include <gtk/gtk.h>
22 #include <gst/gst.h>
23 
24 #include <gst/gl/gl.h>
25 
26 #if GST_GL_HAVE_WINDOW_X11 && defined (GDK_WINDOWING_X11)
27 #include <gst/gl/x11/gstgldisplay_x11.h>
28 #endif
29 
30 static void
button_state_null_cb(GtkWidget * widget,GstElement * pipeline)31 button_state_null_cb (GtkWidget * widget, GstElement * pipeline)
32 {
33   gst_element_set_state (pipeline, GST_STATE_NULL);
34   g_print ("GST_STATE_NULL\n");
35 }
36 
37 static void
button_state_ready_cb(GtkWidget * widget,GstElement * pipeline)38 button_state_ready_cb (GtkWidget * widget, GstElement * pipeline)
39 {
40   gst_element_set_state (pipeline, GST_STATE_READY);
41   g_print ("GST_STATE_READY\n");
42 }
43 
44 static void
button_state_paused_cb(GtkWidget * widget,GstElement * pipeline)45 button_state_paused_cb (GtkWidget * widget, GstElement * pipeline)
46 {
47   gst_element_set_state (pipeline, GST_STATE_PAUSED);
48   g_print ("GST_STATE_PAUSED\n");
49 }
50 
51 static void
button_state_playing_cb(GtkWidget * widget,GstElement * pipeline)52 button_state_playing_cb (GtkWidget * widget, GstElement * pipeline)
53 {
54   gst_element_set_state (pipeline, GST_STATE_PLAYING);
55   g_print ("GST_STATE_PLAYING\n");
56 }
57 
58 static void
end_stream_cb(GstBus * bus,GstMessage * message,GstElement * pipeline)59 end_stream_cb (GstBus * bus, GstMessage * message, GstElement * pipeline)
60 {
61   g_print ("End of stream\n");
62 
63   gst_element_set_state (pipeline, GST_STATE_NULL);
64   gst_object_unref (pipeline);
65 
66   gtk_main_quit ();
67 }
68 
69 static void
destroy_cb(GtkWidget * widget,GdkEvent * event,GstElement * pipeline)70 destroy_cb (GtkWidget * widget, GdkEvent * event, GstElement * pipeline)
71 {
72   g_print ("Close\n");
73 
74   gst_element_set_state (pipeline, GST_STATE_NULL);
75   gst_object_unref (pipeline);
76 
77   gtk_main_quit ();
78 }
79 
80 int
main(int argc,char * argv[])81 main (int argc, char *argv[])
82 {
83   GtkWidget *window, *window_control;
84   GtkWidget *button_state_null, *button_state_ready;
85   GtkWidget *button_state_paused, *button_state_playing;
86   GtkWidget *grid, *area;
87   GstElement *pipeline;
88   GstElement *videosrc, *upload, *effect, *videosink;
89   GstStateChangeReturn ret;
90   GstCaps *caps;
91   GstBus *bus;
92 
93 #if GST_GL_HAVE_WINDOW_X11 && defined(GDK_WINDOWING_X11)
94   XInitThreads ();
95 #endif
96 
97   gst_init (&argc, &argv);
98   gtk_init (&argc, &argv);
99 
100   pipeline = gst_pipeline_new ("pipeline");
101 
102   //window that contains an area where the video is drawn
103   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
104   gtk_window_set_default_size (GTK_WINDOW (window), 640, 480);
105   gtk_window_move (GTK_WINDOW (window), 300, 10);
106   gtk_window_set_title (GTK_WINDOW (window), "gtkgstwidget");
107 
108   //window to control the states
109   window_control = gtk_window_new (GTK_WINDOW_TOPLEVEL);
110   gtk_window_set_resizable (GTK_WINDOW (window_control), FALSE);
111   gtk_window_move (GTK_WINDOW (window_control), 10, 10);
112   grid = gtk_grid_new ();
113   gtk_container_add (GTK_CONTAINER (window_control), grid);
114 
115   //control state null
116   button_state_null = gtk_button_new_with_label ("GST_STATE_NULL");
117   g_signal_connect (G_OBJECT (button_state_null), "clicked",
118       G_CALLBACK (button_state_null_cb), pipeline);
119   gtk_grid_attach (GTK_GRID (grid), button_state_null, 0, 1, 1, 1);
120   gtk_widget_show (button_state_null);
121 
122   //control state ready
123   button_state_ready = gtk_button_new_with_label ("GST_STATE_READY");
124   g_signal_connect (G_OBJECT (button_state_ready), "clicked",
125       G_CALLBACK (button_state_ready_cb), pipeline);
126   gtk_grid_attach (GTK_GRID (grid), button_state_ready, 0, 2, 1, 1);
127   gtk_widget_show (button_state_ready);
128 
129   //control state paused
130   button_state_paused = gtk_button_new_with_label ("GST_STATE_PAUSED");
131   g_signal_connect (G_OBJECT (button_state_paused), "clicked",
132       G_CALLBACK (button_state_paused_cb), pipeline);
133   gtk_grid_attach (GTK_GRID (grid), button_state_paused, 0, 3, 1, 1);
134   gtk_widget_show (button_state_paused);
135 
136   //control state playing
137   button_state_playing = gtk_button_new_with_label ("GST_STATE_PLAYING");
138   g_signal_connect (G_OBJECT (button_state_playing), "clicked",
139       G_CALLBACK (button_state_playing_cb), pipeline);
140   gtk_grid_attach (GTK_GRID (grid), button_state_playing, 0, 4, 1, 1);
141   gtk_widget_show (button_state_playing);
142 
143   gtk_widget_show (grid);
144   gtk_widget_show (window_control);
145 
146   g_signal_connect (G_OBJECT (window), "delete-event", G_CALLBACK (destroy_cb),
147       pipeline);
148 
149   //configure the pipeline
150   videosrc = gst_element_factory_make ("videotestsrc", "videotestsrc");
151   upload = gst_element_factory_make ("glupload", "glupload");
152   effect = gst_element_factory_make ("glfiltercube", "glfiltercube");
153   videosink = gst_element_factory_make ("gtkglsink", "gtksink");
154 
155   g_object_get (videosink, "widget", &area, NULL);
156   gtk_container_add (GTK_CONTAINER (window), area);
157   g_object_unref (area);
158 
159   caps = gst_caps_new_simple ("video/x-raw", "width", G_TYPE_INT, 640,
160       "height", G_TYPE_INT, 480, "format", G_TYPE_STRING, "RGBA",
161       "framerate", GST_TYPE_FRACTION, 30, 1, NULL);
162 
163   gst_bin_add_many (GST_BIN (pipeline), videosrc, upload, effect, videosink,
164       NULL);
165 
166   if (!gst_element_link_filtered (videosrc, upload, caps)) {
167     gst_caps_unref (caps);
168     g_warning ("Failed to link videosrc to glfiltercube!\n");
169     return -1;
170   }
171   gst_caps_unref (caps);
172 
173   if (!gst_element_link_many (upload, effect, videosink, NULL)) {
174     g_warning ("Failed to link videosrc to glfiltercube!\n");
175     return -1;
176   }
177   //set window id on this event
178   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
179   g_signal_connect (bus, "message::error", G_CALLBACK (end_stream_cb),
180       pipeline);
181   g_signal_connect (bus, "message::warning", G_CALLBACK (end_stream_cb),
182       pipeline);
183   g_signal_connect (bus, "message::eos", G_CALLBACK (end_stream_cb), pipeline);
184   gst_object_unref (bus);
185 
186   //start
187   ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
188   if (ret == GST_STATE_CHANGE_FAILURE) {
189     g_print ("Failed to start up pipeline!\n");
190     return -1;
191   }
192 
193   gtk_widget_show_all (window);
194 
195   gtk_main ();
196 
197   gst_deinit ();
198 
199   return 0;
200 }
201