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 static void
button_state_null_cb(GtkWidget * widget,GstElement * pipeline)25 button_state_null_cb (GtkWidget * widget, GstElement * pipeline)
26 {
27   gst_element_set_state (pipeline, GST_STATE_NULL);
28   g_print ("GST_STATE_NULL\n");
29 }
30 
31 static void
button_state_ready_cb(GtkWidget * widget,GstElement * pipeline)32 button_state_ready_cb (GtkWidget * widget, GstElement * pipeline)
33 {
34   gst_element_set_state (pipeline, GST_STATE_READY);
35   g_print ("GST_STATE_READY\n");
36 }
37 
38 static void
button_state_paused_cb(GtkWidget * widget,GstElement * pipeline)39 button_state_paused_cb (GtkWidget * widget, GstElement * pipeline)
40 {
41   gst_element_set_state (pipeline, GST_STATE_PAUSED);
42   g_print ("GST_STATE_PAUSED\n");
43 }
44 
45 static void
button_state_playing_cb(GtkWidget * widget,GstElement * pipeline)46 button_state_playing_cb (GtkWidget * widget, GstElement * pipeline)
47 {
48   gst_element_set_state (pipeline, GST_STATE_PLAYING);
49   g_print ("GST_STATE_PLAYING\n");
50 }
51 
52 static void
end_stream_cb(GstBus * bus,GstMessage * message,GstElement * pipeline)53 end_stream_cb (GstBus * bus, GstMessage * message, GstElement * pipeline)
54 {
55   g_print ("End of stream\n");
56 
57   gst_element_set_state (pipeline, GST_STATE_NULL);
58   gst_object_unref (pipeline);
59 
60   gtk_main_quit ();
61 }
62 
63 static void
destroy_cb(GtkWidget * widget,GdkEvent * event,GstElement * pipeline)64 destroy_cb (GtkWidget * widget, GdkEvent * event, GstElement * pipeline)
65 {
66   g_print ("Close\n");
67 
68   gst_element_set_state (pipeline, GST_STATE_NULL);
69   gst_object_unref (pipeline);
70 
71   gtk_main_quit ();
72 }
73 
74 int
main(int argc,char * argv[])75 main (int argc, char *argv[])
76 {
77   GtkWidget *window, *window_control;
78   GtkWidget *button_state_null, *button_state_ready;
79   GtkWidget *button_state_paused, *button_state_playing;
80   GtkWidget *grid, *area;
81   GstElement *pipeline;
82   GstElement *videosrc, *videosink;
83   GstStateChangeReturn ret;
84   GstCaps *caps;
85   GstBus *bus;
86 
87   gst_init (&argc, &argv);
88   gtk_init (&argc, &argv);
89 
90   pipeline = gst_pipeline_new ("pipeline");
91 
92   //window that contains an area where the video is drawn
93   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
94   gtk_window_set_default_size (GTK_WINDOW (window), 640, 480);
95   gtk_window_move (GTK_WINDOW (window), 300, 10);
96   gtk_window_set_title (GTK_WINDOW (window), "gtkgstwidget");
97 
98   //window to control the states
99   window_control = gtk_window_new (GTK_WINDOW_TOPLEVEL);
100   gtk_window_set_resizable (GTK_WINDOW (window_control), FALSE);
101   gtk_window_move (GTK_WINDOW (window_control), 10, 10);
102   grid = gtk_grid_new ();
103   gtk_container_add (GTK_CONTAINER (window_control), grid);
104 
105   //control state null
106   button_state_null = gtk_button_new_with_label ("GST_STATE_NULL");
107   g_signal_connect (G_OBJECT (button_state_null), "clicked",
108       G_CALLBACK (button_state_null_cb), pipeline);
109   gtk_grid_attach (GTK_GRID (grid), button_state_null, 0, 1, 1, 1);
110   gtk_widget_show (button_state_null);
111 
112   //control state ready
113   button_state_ready = gtk_button_new_with_label ("GST_STATE_READY");
114   g_signal_connect (G_OBJECT (button_state_ready), "clicked",
115       G_CALLBACK (button_state_ready_cb), pipeline);
116   gtk_grid_attach (GTK_GRID (grid), button_state_ready, 0, 2, 1, 1);
117   gtk_widget_show (button_state_ready);
118 
119   //control state paused
120   button_state_paused = gtk_button_new_with_label ("GST_STATE_PAUSED");
121   g_signal_connect (G_OBJECT (button_state_paused), "clicked",
122       G_CALLBACK (button_state_paused_cb), pipeline);
123   gtk_grid_attach (GTK_GRID (grid), button_state_paused, 0, 3, 1, 1);
124   gtk_widget_show (button_state_paused);
125 
126   //control state playing
127   button_state_playing = gtk_button_new_with_label ("GST_STATE_PLAYING");
128   g_signal_connect (G_OBJECT (button_state_playing), "clicked",
129       G_CALLBACK (button_state_playing_cb), pipeline);
130   gtk_grid_attach (GTK_GRID (grid), button_state_playing, 0, 4, 1, 1);
131   gtk_widget_show (button_state_playing);
132 
133   gtk_widget_show (grid);
134   gtk_widget_show (window_control);
135 
136   g_signal_connect (G_OBJECT (window), "delete-event", G_CALLBACK (destroy_cb),
137       pipeline);
138 
139   //configure the pipeline
140   videosrc = gst_element_factory_make ("videotestsrc", "videotestsrc");
141   videosink = gst_element_factory_make ("gtksink", "gtksink");
142 
143   g_object_get (videosink, "widget", &area, NULL);
144   gtk_container_add (GTK_CONTAINER (window), area);
145   g_object_unref (area);
146 
147   gtk_widget_realize (area);
148 
149   caps = gst_caps_new_simple ("video/x-raw",
150       "width", G_TYPE_INT, 640,
151       "height", G_TYPE_INT, 480, "format", G_TYPE_STRING, "BGRA", NULL);
152 
153   gst_bin_add_many (GST_BIN (pipeline), videosrc, videosink, NULL);
154 
155   if (!gst_element_link_filtered (videosrc, videosink, caps)) {
156     gst_caps_unref (caps);
157     g_warning ("Failed to link videosrc to glfiltercube!\n");
158     return -1;
159   }
160   gst_caps_unref (caps);
161 
162   //set window id on this event
163   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
164   g_signal_connect (bus, "message::error", G_CALLBACK (end_stream_cb),
165       pipeline);
166   g_signal_connect (bus, "message::warning", G_CALLBACK (end_stream_cb),
167       pipeline);
168   g_signal_connect (bus, "message::eos", G_CALLBACK (end_stream_cb), pipeline);
169   gst_object_unref (bus);
170 
171   //start
172   ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
173   if (ret == GST_STATE_CHANGE_FAILURE) {
174     g_print ("Failed to start up pipeline!\n");
175     return -1;
176   }
177 
178   gtk_widget_show_all (window);
179 
180   gtk_main ();
181 
182   gst_deinit ();
183 
184   return 0;
185 }
186