[511/906] tests: update for 1.0
[platform/upstream/gst-plugins-good.git] / tests / examples / gtk / fxtest / fxtest.c
1 /*
2  * GStreamer
3  * Copyright (C) 2008-2009 Filippo Argiolas <filippo.argiolas@gmail.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., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #define GLIB_DISABLE_DEPRECATION_WARNINGS
22
23 #include <gst/gst.h>
24 #include <gtk/gtk.h>
25 #include <gdk/gdk.h>
26 #include <gdk-pixbuf/gdk-pixbuf.h>
27
28 #include "../gstgtk.h"
29
30 #include <gst/video/videooverlay.h>
31
32
33 /* TODO: use video overlay in the proper way (like suggested in docs, see gtkvideooverlay example) */
34 static gboolean
35 expose_cb (GtkWidget * widget, GdkEventExpose * event, gpointer data)
36 {
37   GstVideoOverlay *overlay =
38       GST_VIDEO_OVERLAY (gst_bin_get_by_interface (GST_BIN (data),
39           GST_TYPE_VIDEO_OVERLAY));
40
41   gst_video_overlay_set_gtk_window (overlay, widget);
42
43   return FALSE;
44 }
45
46 static void
47 destroy_cb (GtkWidget * widget, GdkEvent * event, GstElement * pipeline)
48 {
49   g_message ("destroy callback");
50
51   gst_element_set_state (pipeline, GST_STATE_NULL);
52   gst_object_unref (pipeline);
53
54   gtk_main_quit ();
55 }
56
57 gboolean
58 apply_fx (GtkWidget * widget, gpointer data)
59 {
60   gchar *fx;
61   GEnumClass *p_class;
62
63 /* heeeellppppp!! */
64   p_class =
65       G_PARAM_SPEC_ENUM (g_object_class_find_property (G_OBJECT_GET_CLASS
66           (G_OBJECT (data)), "effect")
67       )->enum_class;
68
69   fx = gtk_combo_box_get_active_text (GTK_COMBO_BOX (widget));
70   g_print ("setting: %s - %s\n", fx, g_enum_get_value_by_nick (p_class,
71           fx)->value_name);
72   g_object_set (G_OBJECT (data), "effect", g_enum_get_value_by_nick (p_class,
73           fx)->value, NULL);
74   return FALSE;
75 }
76
77 gboolean
78 play_cb (GtkWidget * widget, gpointer data)
79 {
80   g_message ("playing");
81   gst_element_set_state (GST_ELEMENT (data), GST_STATE_PLAYING);
82   return FALSE;
83 }
84
85 gboolean
86 null_cb (GtkWidget * widget, gpointer data)
87 {
88   g_message ("nulling");
89   gst_element_set_state (GST_ELEMENT (data), GST_STATE_NULL);
90   return FALSE;
91 }
92
93 gboolean
94 ready_cb (GtkWidget * widget, gpointer data)
95 {
96   g_message ("readying");
97   gst_element_set_state (GST_ELEMENT (data), GST_STATE_READY);
98   return FALSE;
99 }
100
101 gboolean
102 pause_cb (GtkWidget * widget, gpointer data)
103 {
104   g_message ("pausing");
105   gst_element_set_state (GST_ELEMENT (data), GST_STATE_PAUSED);
106   return FALSE;
107 }
108
109 gint
110 main (gint argc, gchar * argv[])
111 {
112   GstStateChangeReturn ret;
113   GstElement *pipeline;
114   GstElement *uload, *filter, *sink;
115   GstElement *sourcebin;
116   GError *error = NULL;
117
118   GtkWidget *window;
119   GtkWidget *screen;
120   GtkWidget *vbox, *combo;
121   GtkWidget *hbox;
122   GtkWidget *play, *pause, *null, *ready;
123
124   gchar **source_desc_array = NULL;
125   gchar *source_desc = NULL;
126
127   GOptionContext *context;
128   GOptionEntry options[] = {
129     {"source-bin", 's', 0, G_OPTION_ARG_STRING_ARRAY, &source_desc_array,
130         "Use a custom source bin description (gst-launch style)", NULL}
131     ,
132     {NULL}
133   };
134
135   g_thread_init (NULL);
136
137   context = g_option_context_new (NULL);
138   g_option_context_add_main_entries (context, options, NULL);
139   g_option_context_add_group (context, gst_init_get_option_group ());
140   g_option_context_add_group (context, gtk_get_option_group (TRUE));
141   if (!g_option_context_parse (context, &argc, &argv, &error)) {
142     g_print ("Inizialization error: %s\n", GST_STR_NULL (error->message));
143     return -1;
144   }
145   g_option_context_free (context);
146
147   if (source_desc_array != NULL) {
148     source_desc = g_strjoinv (" ", source_desc_array);
149     g_strfreev (source_desc_array);
150   }
151   if (source_desc == NULL) {
152     source_desc =
153         g_strdup
154         ("videotestsrc ! video/x-raw, width=352, height=288 ! identity");
155   }
156
157   sourcebin =
158       gst_parse_bin_from_description (g_strdup (source_desc), TRUE, &error);
159   g_free (source_desc);
160   if (error) {
161     g_print ("Error while parsing source bin description: %s\n",
162         GST_STR_NULL (error->message));
163     return -1;
164   }
165
166   g_set_application_name ("gst-gl-effects test app");
167
168   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
169   gtk_container_set_border_width (GTK_CONTAINER (window), 3);
170
171   pipeline = gst_pipeline_new ("pipeline");
172
173   uload = gst_element_factory_make ("glupload", "glu");
174   filter = gst_element_factory_make ("gleffects", "flt");
175   sink = gst_element_factory_make ("glimagesink", "glsink");
176
177   gst_bin_add_many (GST_BIN (pipeline), sourcebin, uload, filter, sink, NULL);
178
179   if (!gst_element_link_many (sourcebin, uload, filter, sink, NULL)) {
180     g_print ("Failed to link one or more elements!\n");
181     return -1;
182   }
183
184   g_signal_connect (G_OBJECT (window), "delete-event",
185       G_CALLBACK (destroy_cb), pipeline);
186   g_signal_connect (G_OBJECT (window), "destroy-event",
187       G_CALLBACK (destroy_cb), pipeline);
188
189   screen = gtk_drawing_area_new ();
190
191   gtk_widget_set_size_request (screen, 640, 480);       // 500 x 376
192
193   vbox = gtk_vbox_new (FALSE, 2);
194
195   gtk_box_pack_start (GTK_BOX (vbox), screen, TRUE, TRUE, 0);
196
197   combo = gtk_combo_box_new_text ();
198
199   gtk_combo_box_append_text (GTK_COMBO_BOX (combo), "identity");
200   gtk_combo_box_append_text (GTK_COMBO_BOX (combo), "mirror");
201   gtk_combo_box_append_text (GTK_COMBO_BOX (combo), "squeeze");
202   gtk_combo_box_append_text (GTK_COMBO_BOX (combo), "stretch");
203   gtk_combo_box_append_text (GTK_COMBO_BOX (combo), "fisheye");
204   gtk_combo_box_append_text (GTK_COMBO_BOX (combo), "twirl");
205   gtk_combo_box_append_text (GTK_COMBO_BOX (combo), "bulge");
206   gtk_combo_box_append_text (GTK_COMBO_BOX (combo), "tunnel");
207   gtk_combo_box_append_text (GTK_COMBO_BOX (combo), "square");
208   gtk_combo_box_append_text (GTK_COMBO_BOX (combo), "heat");
209   gtk_combo_box_append_text (GTK_COMBO_BOX (combo), "xpro");
210   gtk_combo_box_append_text (GTK_COMBO_BOX (combo), "lumaxpro");
211   gtk_combo_box_append_text (GTK_COMBO_BOX (combo), "sepia");
212   gtk_combo_box_append_text (GTK_COMBO_BOX (combo), "xray");
213   gtk_combo_box_append_text (GTK_COMBO_BOX (combo), "sin");
214   gtk_combo_box_append_text (GTK_COMBO_BOX (combo), "glow");
215
216   g_signal_connect (G_OBJECT (combo), "changed", G_CALLBACK (apply_fx), filter);
217
218   gtk_box_pack_start (GTK_BOX (vbox), combo, FALSE, FALSE, 0);
219
220   hbox = gtk_hbox_new (FALSE, 0);
221
222   play = gtk_button_new_with_label ("PLAY");
223
224   g_signal_connect (G_OBJECT (play), "clicked", G_CALLBACK (play_cb), pipeline);
225
226   pause = gtk_button_new_with_label ("PAUSE");
227
228   g_signal_connect (G_OBJECT (pause), "clicked",
229       G_CALLBACK (pause_cb), pipeline);
230
231   null = gtk_button_new_with_label ("NULL");
232
233   g_signal_connect (G_OBJECT (null), "clicked", G_CALLBACK (null_cb), pipeline);
234
235   ready = gtk_button_new_with_label ("READY");
236
237   g_signal_connect (G_OBJECT (ready), "clicked",
238       G_CALLBACK (ready_cb), pipeline);
239
240   gtk_box_pack_start (GTK_BOX (hbox), null, TRUE, TRUE, 0);
241   gtk_box_pack_start (GTK_BOX (hbox), ready, TRUE, TRUE, 0);
242   gtk_box_pack_start (GTK_BOX (hbox), play, TRUE, TRUE, 0);
243   gtk_box_pack_start (GTK_BOX (hbox), pause, TRUE, TRUE, 0);
244
245   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
246
247   gtk_container_add (GTK_CONTAINER (window), vbox);
248
249   g_signal_connect (screen, "expose-event", G_CALLBACK (expose_cb), pipeline);
250
251   ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
252   if (ret == GST_STATE_CHANGE_FAILURE) {
253     g_print ("Failed to start up pipeline!\n");
254     return -1;
255   }
256
257   gtk_widget_show_all (GTK_WIDGET (window));
258
259   gtk_main ();
260
261   return 0;
262 }