gst-indent
[platform/upstream/gst-plugins-good.git] / examples / seeking / spider_seek.c
1 #include <stdlib.h>
2 #include <glib.h>
3 #include <gtk/gtk.h>
4 #include <gst/gst.h>
5 #include <string.h>
6
7 static GList *rate_pads = NULL;
8 static GList *seekable_elements = NULL;
9
10 static GstElement *pipeline;
11 static GtkAdjustment *adjustment;
12 static gboolean stats = FALSE;
13 static guint64 duration;
14
15 static guint update_id;
16
17 //#define SOURCE "gnomevfssrc"
18 #define SOURCE "filesrc"
19
20 #define UPDATE_INTERVAL 500
21
22 static GstElement *
23 make_spider_pipeline (const gchar * location, gboolean thread)
24 {
25   GstElement *pipeline;
26   GstElement *src, *decoder, *audiosink, *videosink, *a_thread, *v_thread,
27       *a_queue, *v_queue;
28
29   if (thread) {
30     pipeline = gst_thread_new ("app");
31   } else {
32     pipeline = gst_pipeline_new ("app");
33   }
34
35
36   src = gst_element_factory_make (SOURCE, "src");
37   decoder = gst_element_factory_make ("spider", "decoder");
38   a_thread = gst_thread_new ("a_thread");
39   a_queue = gst_element_factory_make ("queue", "a_queue");
40   audiosink = gst_element_factory_make ("osssink", "a_sink");
41   //g_object_set (G_OBJECT (audiosink), "fragment", 0x00180008, NULL);
42
43   v_thread = gst_thread_new ("v_thread");
44   v_queue = gst_element_factory_make ("queue", "v_queue");
45   videosink = gst_element_factory_make ("xvideosink", "v_sink");
46   //g_object_set (G_OBJECT (audiosink), "sync", FALSE, NULL);
47
48   g_object_set (G_OBJECT (src), "location", location, NULL);
49
50   gst_bin_add (GST_BIN (pipeline), src);
51   gst_bin_add (GST_BIN (pipeline), decoder);
52   gst_bin_add (GST_BIN (a_thread), a_queue);
53   gst_bin_add (GST_BIN (a_thread), audiosink);
54   gst_bin_add (GST_BIN (v_thread), v_queue);
55   gst_bin_add (GST_BIN (v_thread), videosink);
56   gst_bin_add (GST_BIN (pipeline), a_thread);
57   gst_bin_add (GST_BIN (pipeline), v_thread);
58
59   gst_element_link (src, decoder);
60   gst_element_link (v_queue, videosink);
61   gst_element_link (decoder, v_queue);
62   gst_element_link (a_queue, audiosink);
63   gst_element_link (decoder, a_queue);
64
65   seekable_elements = g_list_prepend (seekable_elements, videosink);
66   seekable_elements = g_list_prepend (seekable_elements, audiosink);
67   rate_pads =
68       g_list_prepend (rate_pads, gst_element_get_pad (audiosink, "sink"));
69   rate_pads =
70       g_list_prepend (rate_pads, gst_element_get_pad (videosink, "sink"));
71
72   return pipeline;
73 }
74
75 static gchar *
76 format_value (GtkScale * scale, gdouble value)
77 {
78   gint64 real;
79   gint64 seconds;
80   gint64 subseconds;
81
82   real = value * duration / 100;
83   seconds = (gint64) real / GST_SECOND;
84   subseconds = (gint64) real / (GST_SECOND / 100);
85
86   return g_strdup_printf ("%02lld:%02lld:%02lld",
87       seconds / 60, seconds % 60, subseconds % 100);
88 }
89
90 typedef struct
91 {
92   const gchar *name;
93   const GstFormat format;
94 } seek_format;
95
96 static seek_format seek_formats[] = {
97   {"tim", GST_FORMAT_TIME},
98   {"byt", GST_FORMAT_BYTES},
99   {"buf", GST_FORMAT_BUFFERS},
100   {"def", GST_FORMAT_DEFAULT},
101   {NULL, 0},
102 };
103
104 G_GNUC_UNUSED static void
105 query_rates (void)
106 {
107   GList *walk = rate_pads;
108
109   while (walk) {
110     GstPad *pad = GST_PAD (walk->data);
111     gint i = 0;
112
113     g_print ("rate/sec  %8.8s: ", GST_PAD_NAME (pad));
114     while (seek_formats[i].name) {
115       gint64 value;
116       GstFormat format;
117
118       format = seek_formats[i].format;
119
120       if (gst_pad_convert (pad, GST_FORMAT_TIME, GST_SECOND, &format, &value)) {
121         g_print ("%s %13lld | ", seek_formats[i].name, value);
122       } else {
123         g_print ("%s %13.13s | ", seek_formats[i].name, "*NA*");
124       }
125
126       i++;
127     }
128     g_print (" %s:%s\n", GST_DEBUG_PAD_NAME (pad));
129
130     walk = g_list_next (walk);
131   }
132 }
133
134 G_GNUC_UNUSED static void
135 query_durations ()
136 {
137   GList *walk = seekable_elements;
138
139   while (walk) {
140     GstElement *element = GST_ELEMENT (walk->data);
141     gint i = 0;
142
143     g_print ("durations %8.8s: ", GST_ELEMENT_NAME (element));
144     while (seek_formats[i].name) {
145       gboolean res;
146       gint64 value;
147       GstFormat format;
148
149       format = seek_formats[i].format;
150       res = gst_element_query (element, GST_QUERY_TOTAL, &format, &value);
151       if (res) {
152         g_print ("%s %13lld | ", seek_formats[i].name, value);
153       } else {
154         g_print ("%s %13.13s | ", seek_formats[i].name, "*NA*");
155       }
156       i++;
157     }
158     g_print (" %s\n", GST_ELEMENT_NAME (element));
159     walk = g_list_next (walk);
160   }
161 }
162
163 G_GNUC_UNUSED static void
164 query_positions ()
165 {
166   GList *walk = seekable_elements;
167
168   while (walk) {
169     GstElement *element = GST_ELEMENT (walk->data);
170     gint i = 0;
171
172     g_print ("positions %8.8s: ", GST_ELEMENT_NAME (element));
173     while (seek_formats[i].name) {
174       gboolean res;
175       gint64 value;
176       GstFormat format;
177
178       format = seek_formats[i].format;
179       res = gst_element_query (element, GST_QUERY_POSITION, &format, &value);
180       if (res) {
181         g_print ("%s %13lld | ", seek_formats[i].name, value);
182       } else {
183         g_print ("%s %13.13s | ", seek_formats[i].name, "*NA*");
184       }
185       i++;
186     }
187     g_print (" %s\n", GST_ELEMENT_NAME (element));
188     walk = g_list_next (walk);
189   }
190 }
191
192 static gboolean
193 update_scale (gpointer data)
194 {
195   GstClock *clock;
196   guint64 position;
197   GstFormat format = GST_FORMAT_TIME;
198
199   duration = 0;
200   clock = gst_bin_get_clock (GST_BIN (pipeline));
201
202   if (seekable_elements) {
203     GstElement *element = GST_ELEMENT (seekable_elements->data);
204
205     gst_element_query (element, GST_QUERY_TOTAL, &format, &duration);
206   }
207   position = gst_clock_get_time (clock);
208
209   if (stats) {
210     g_print ("clock:                  %13llu  (%s)\n", position,
211         gst_object_get_name (GST_OBJECT (clock)));
212     query_durations ();
213     query_positions ();
214     query_rates ();
215   }
216   if (duration > 0) {
217     gtk_adjustment_set_value (adjustment, position * 100.0 / duration);
218   }
219
220   return TRUE;
221 }
222
223 static gboolean
224 iterate (gpointer data)
225 {
226   gboolean res = TRUE;
227
228   res = gst_bin_iterate (GST_BIN (data));
229   if (!res) {
230     gtk_timeout_remove (update_id);
231     g_print ("stopping iterations\n");
232   }
233   return res;
234 }
235
236 static gboolean
237 start_seek (GtkWidget * widget, GdkEventButton * event, gpointer user_data)
238 {
239   gst_element_set_state (pipeline, GST_STATE_PAUSED);
240   gtk_timeout_remove (update_id);
241
242   return FALSE;
243 }
244
245 static gboolean
246 stop_seek (GtkWidget * widget, GdkEventButton * event, gpointer user_data)
247 {
248   gint64 real = gtk_range_get_value (GTK_RANGE (widget)) * duration / 100;
249   gboolean res;
250   GstEvent *s_event;
251   GList *walk = seekable_elements;
252
253   while (walk) {
254     GstElement *seekable = GST_ELEMENT (walk->data);
255
256     g_print ("seek to %lld on element %s\n", real, GST_ELEMENT_NAME (seekable));
257     s_event = gst_event_new_seek (GST_FORMAT_TIME |
258         GST_SEEK_METHOD_SET | GST_SEEK_FLAG_FLUSH, real);
259
260     res = gst_element_send_event (seekable, s_event);
261
262     walk = g_list_next (walk);
263   }
264
265   gst_element_set_state (pipeline, GST_STATE_PLAYING);
266   if (!GST_FLAG_IS_SET (pipeline, GST_BIN_SELF_SCHEDULABLE))
267     gtk_idle_add ((GtkFunction) iterate, pipeline);
268   update_id =
269       gtk_timeout_add (UPDATE_INTERVAL, (GtkFunction) update_scale, pipeline);
270
271   return FALSE;
272 }
273
274 static void
275 play_cb (GtkButton * button, gpointer data)
276 {
277   if (gst_element_get_state (pipeline) != GST_STATE_PLAYING) {
278     gst_element_set_state (pipeline, GST_STATE_PLAYING);
279     if (!GST_FLAG_IS_SET (pipeline, GST_BIN_SELF_SCHEDULABLE))
280       gtk_idle_add ((GtkFunction) iterate, pipeline);
281     update_id =
282         gtk_timeout_add (UPDATE_INTERVAL, (GtkFunction) update_scale, pipeline);
283   }
284 }
285
286 static void
287 pause_cb (GtkButton * button, gpointer data)
288 {
289   if (gst_element_get_state (pipeline) != GST_STATE_PAUSED) {
290     gst_element_set_state (pipeline, GST_STATE_PAUSED);
291     gtk_timeout_remove (update_id);
292   }
293 }
294
295 static void
296 stop_cb (GtkButton * button, gpointer data)
297 {
298   if (gst_element_get_state (pipeline) != GST_STATE_READY) {
299     gst_element_set_state (pipeline, GST_STATE_READY);
300     gtk_timeout_remove (update_id);
301   }
302 }
303
304 int
305 main (int argc, char **argv)
306 {
307   GtkWidget *window, *hbox, *vbox,
308       *play_button, *pause_button, *stop_button, *hscale;
309   gboolean threaded = FALSE;
310   struct poptOption options[] = {
311     {"threaded", 't', POPT_ARG_NONE | POPT_ARGFLAG_STRIP, &threaded, 0,
312         "Run the pipeline in a toplevel thread", NULL},
313     {"stats", 's', POPT_ARG_NONE | POPT_ARGFLAG_STRIP, &stats, 0,
314         "Show element stats", NULL},
315     POPT_TABLEEND
316   };
317
318   gst_init_with_popt_table (&argc, &argv, options);
319   gtk_init (&argc, &argv);
320
321   if (argc != 2) {
322     g_print ("usage: %s <filename>\n", argv[0]);
323     exit (-1);
324   }
325
326   pipeline = make_spider_pipeline (argv[1], threaded);
327
328   /* initialize gui elements ... */
329   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
330   hbox = gtk_hbox_new (FALSE, 0);
331   vbox = gtk_vbox_new (FALSE, 0);
332   play_button = gtk_button_new_with_label ("play");
333   pause_button = gtk_button_new_with_label ("pause");
334   stop_button = gtk_button_new_with_label ("stop");
335
336   adjustment =
337       GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.00, 100.0, 0.1, 1.0, 1.0));
338   hscale = gtk_hscale_new (adjustment);
339   gtk_scale_set_digits (GTK_SCALE (hscale), 2);
340   gtk_range_set_update_policy (GTK_RANGE (hscale), GTK_UPDATE_CONTINUOUS);
341
342   gtk_signal_connect (GTK_OBJECT (hscale),
343       "button_press_event", G_CALLBACK (start_seek), pipeline);
344   gtk_signal_connect (GTK_OBJECT (hscale),
345       "button_release_event", G_CALLBACK (stop_seek), pipeline);
346   gtk_signal_connect (GTK_OBJECT (hscale),
347       "format_value", G_CALLBACK (format_value), pipeline);
348
349   /* do the packing stuff ... */
350   gtk_window_set_default_size (GTK_WINDOW (window), 96, 96);
351   gtk_container_add (GTK_CONTAINER (window), vbox);
352   gtk_container_add (GTK_CONTAINER (vbox), hbox);
353   gtk_box_pack_start (GTK_BOX (hbox), play_button, FALSE, FALSE, 2);
354   gtk_box_pack_start (GTK_BOX (hbox), pause_button, FALSE, FALSE, 2);
355   gtk_box_pack_start (GTK_BOX (hbox), stop_button, FALSE, FALSE, 2);
356   gtk_box_pack_start (GTK_BOX (vbox), hscale, TRUE, TRUE, 2);
357
358   /* connect things ... */
359   g_signal_connect (G_OBJECT (play_button), "clicked", G_CALLBACK (play_cb),
360       pipeline);
361   g_signal_connect (G_OBJECT (pause_button), "clicked", G_CALLBACK (pause_cb),
362       pipeline);
363   g_signal_connect (G_OBJECT (stop_button), "clicked", G_CALLBACK (stop_cb),
364       pipeline);
365   g_signal_connect (G_OBJECT (window), "delete_event", gtk_main_quit, NULL);
366
367   /* show the gui. */
368   gtk_widget_show_all (window);
369
370   gtk_main ();
371
372   return 0;
373 }