Merge branch 'master' into 0.11
[platform/upstream/gst-plugins-base.git] / tests / examples / seek / seek.c
1 /* GStreamer
2  *
3  * seek.c: seeking sample application
4  *
5  * Copyright (C) 2005 Wim Taymans <wim@fluendo.com>
6  *               2006 Stefan Kost <ensonic@users.sf.net>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <stdlib.h>
29 #include <math.h>
30 #include <glib.h>
31 #include <gtk/gtk.h>
32 #include <gst/gst.h>
33 #include <string.h>
34
35 #include <gdk/gdk.h>
36 #if defined (GDK_WINDOWING_X11)
37 #include <gdk/gdkx.h>
38 #elif defined (GDK_WINDOWING_WIN32)
39 #include <gdk/gdkwin32.h>
40 #endif
41
42 #include <gst/interfaces/videooverlay.h>
43
44 #if (!GTK_CHECK_VERSION(2, 23, 0) || GTK_CHECK_VERSION(2, 90, 0)) && !GTK_CHECK_VERSION(2, 91, 1)
45 #define gtk_combo_box_text_new gtk_combo_box_new_text
46 #define gtk_combo_box_text_append_text gtk_combo_box_append_text
47 #define gtk_combo_box_text_remove gtk_combo_box_remove_text
48 #define GTK_COMBO_BOX_TEXT GTK_COMBO_BOX
49 #endif
50
51 GST_DEBUG_CATEGORY_STATIC (seek_debug);
52 #define GST_CAT_DEFAULT (seek_debug)
53
54 #if !GTK_CHECK_VERSION (2, 17, 7)
55 static void
56 gtk_widget_get_allocation (GtkWidget * w, GtkAllocation * a)
57 {
58   *a = w->allocation;
59 }
60 #endif
61
62 /* configuration */
63
64 #define SOURCE "filesrc"
65
66 static gchar *opt_audiosink_str;        /* NULL */
67 static gchar *opt_videosink_str;        /* NULL */
68
69 #define FILL_INTERVAL 100
70 //#define UPDATE_INTERVAL 500
71 //#define UPDATE_INTERVAL 100
72 #define UPDATE_INTERVAL 40
73
74 /* number of milliseconds to play for after a seek */
75 #define SCRUB_TIME 100
76
77 /* timeout for gst_element_get_state() after a seek */
78 #define SEEK_TIMEOUT 40 * GST_MSECOND
79
80 #define DEFAULT_VIDEO_HEIGHT 300
81
82 /* the state to go to when stop is pressed */
83 #define STOP_STATE      GST_STATE_READY
84
85 #define N_GRAD 1000.0
86
87 static GList *seekable_elements = NULL;
88
89 static gboolean accurate_seek = FALSE;
90 static gboolean keyframe_seek = FALSE;
91 static gboolean loop_seek = FALSE;
92 static gboolean flush_seek = TRUE;
93 static gboolean scrub = TRUE;
94 static gboolean play_scrub = FALSE;
95 static gboolean skip_seek = FALSE;
96 static gdouble rate = 1.0;
97
98 static GstElement *pipeline;
99 static gint pipeline_type;
100 static const gchar *pipeline_spec;
101 static gint64 position = -1;
102 static gint64 duration = -1;
103 static GtkAdjustment *adjustment;
104 static GtkWidget *hscale, *statusbar;
105 static guint status_id = 0;
106 static gboolean stats = FALSE;
107 static gboolean verbose = FALSE;
108
109 static gboolean is_live = FALSE;
110 static gboolean buffering = FALSE;
111 static GstBufferingMode mode;
112 static gint64 buffering_left;
113 static GstState state = GST_STATE_NULL;
114 static guint update_id = 0;
115 static guint seek_timeout_id = 0;
116 static gulong changed_id;
117 static guint fill_id = 0;
118
119 static gint n_video = 0, n_audio = 0, n_text = 0;
120 static gboolean need_streams = TRUE;
121 static GtkWidget *video_combo, *audio_combo, *text_combo, *vis_combo;
122 static GtkWidget *vis_checkbox, *video_checkbox, *audio_checkbox;
123 static GtkWidget *text_checkbox, *mute_checkbox, *volume_spinbutton;
124 static GtkWidget *skip_checkbox, *video_window, *download_checkbox;
125 static GtkWidget *buffer_checkbox, *rate_spinbutton;
126
127 static GStaticMutex state_mutex = G_STATIC_MUTEX_INIT;
128
129 static GtkWidget *format_combo, *step_amount_spinbutton, *step_rate_spinbutton;
130 static GtkWidget *shuttle_checkbox, *step_button;
131 static GtkWidget *shuttle_hscale;
132 static GtkAdjustment *shuttle_adjustment;
133
134 static GList *paths = NULL, *l = NULL;
135
136 /* we keep an array of the visualisation entries so that we can easily switch
137  * with the combo box index. */
138 typedef struct
139 {
140   GstElementFactory *factory;
141 } VisEntry;
142
143 static GArray *vis_entries;
144
145 static void clear_streams (GstElement * pipeline);
146 static void volume_notify_cb (GstElement * pipeline, GParamSpec * arg,
147     gpointer user_dat);
148
149 /* pipeline construction */
150
151 typedef struct
152 {
153   const gchar *padname;
154   GstPad *target;
155   GstElement *bin;
156 }
157 dyn_link;
158
159 static GstElement *
160 gst_element_factory_make_or_warn (const gchar * type, const gchar * name)
161 {
162   GstElement *element = gst_element_factory_make (type, name);
163
164   if (!element) {
165     g_warning ("Failed to create element %s of type %s", name, type);
166   }
167
168   return element;
169 }
170
171 static void
172 playerbin_set_uri (GstElement * player, const gchar * location)
173 {
174   gchar *uri;
175
176   /* Add "file://" prefix for convenience */
177   if (g_str_has_prefix (location, "/") || !gst_uri_is_valid (location)) {
178     uri = gst_filename_to_uri (location, NULL);
179     g_print ("Setting URI: %s\n", uri);
180     g_object_set (G_OBJECT (player), "uri", uri, NULL);
181     g_free (uri);
182   } else {
183     g_print ("Setting URI: %s\n", location);
184     g_object_set (G_OBJECT (player), "uri", location, NULL);
185   }
186 }
187
188 static GstElement *
189 construct_playbin (const gchar * name, const gchar * location)
190 {
191   GstElement *player;
192   GstElement *avsink;
193
194   player = gst_element_factory_make (name, "player");
195   g_assert (player);
196
197   playerbin_set_uri (player, location);
198
199   seekable_elements = g_list_prepend (seekable_elements, player);
200
201   avsink = gst_element_factory_make_or_warn (opt_audiosink_str, "a_sink");
202   if (avsink)
203     g_object_set (player, "audio-sink", avsink, NULL);
204
205   avsink = gst_element_factory_make_or_warn (opt_videosink_str, "v_sink");
206   if (avsink)
207     g_object_set (player, "video-sink", avsink, NULL);
208
209   return player;
210 }
211
212 static GstElement *
213 make_playbin_pipeline (const gchar * location)
214 {
215   GstElement *pipeline = construct_playbin ("playbin", location);
216
217   /* FIXME: this is not triggered, playbin is not forwarding it from the sink */
218   g_signal_connect (pipeline, "notify::volume", G_CALLBACK (volume_notify_cb),
219       NULL);
220   return pipeline;
221 }
222
223 #ifndef GST_DISABLE_PARSE
224 static GstElement *
225 make_parselaunch_pipeline (const gchar * description)
226 {
227   GstElement *pipeline;
228   GError *error = NULL;
229
230   pipeline = gst_parse_launch (description, &error);
231
232   seekable_elements = g_list_prepend (seekable_elements, pipeline);
233
234   return pipeline;
235 }
236 #endif
237
238 typedef struct
239 {
240   const gchar *name;
241   GstElement *(*func) (const gchar * location);
242 }
243 Pipeline;
244
245 static Pipeline pipelines[] = {
246   {"playbin", make_playbin_pipeline},
247 #ifndef GST_DISABLE_PARSE
248   {"parse-launch", make_parselaunch_pipeline},
249 #endif
250   {NULL, NULL},
251 };
252
253 #define NUM_TYPES       ((sizeof (pipelines) / sizeof (Pipeline)) - 1)
254
255 /* ui callbacks and helpers */
256
257 static gchar *
258 format_value (GtkScale * scale, gdouble value)
259 {
260   gint64 real;
261   gint64 seconds;
262   gint64 subseconds;
263
264   real = value * duration / N_GRAD;
265   seconds = (gint64) real / GST_SECOND;
266   subseconds = (gint64) real / (GST_SECOND / N_GRAD);
267
268   return g_strdup_printf ("%02" G_GINT64_FORMAT ":%02" G_GINT64_FORMAT ":%02"
269       G_GINT64_FORMAT, seconds / 60, seconds % 60, subseconds % 100);
270 }
271
272
273 static gchar *
274 shuttle_format_value (GtkScale * scale, gdouble value)
275 {
276   return g_strdup_printf ("%0.*g", gtk_scale_get_digits (scale), value);
277 }
278
279 typedef struct
280 {
281   const gchar *name;
282   const GstFormat format;
283 }
284 seek_format;
285
286 static seek_format seek_formats[] = {
287   {"tim", GST_FORMAT_TIME},
288   {"byt", GST_FORMAT_BYTES},
289   {"buf", GST_FORMAT_BUFFERS},
290   {"def", GST_FORMAT_DEFAULT},
291   {NULL, 0},
292 };
293
294 G_GNUC_UNUSED static void
295 query_positions_elems (void)
296 {
297   GList *walk = seekable_elements;
298
299   while (walk) {
300     GstElement *element = GST_ELEMENT (walk->data);
301     gint i = 0;
302
303     g_print ("positions %8.8s: ", GST_ELEMENT_NAME (element));
304     while (seek_formats[i].name) {
305       gint64 position, total;
306       GstFormat format;
307
308       format = seek_formats[i].format;
309
310       if (gst_element_query_position (element, format, &position) &&
311           gst_element_query_duration (element, format, &total)) {
312         g_print ("%s %13" G_GINT64_FORMAT " / %13" G_GINT64_FORMAT " | ",
313             seek_formats[i].name, position, total);
314       } else {
315         g_print ("%s %13.13s / %13.13s | ", seek_formats[i].name, "*NA*",
316             "*NA*");
317       }
318       i++;
319     }
320     g_print (" %s\n", GST_ELEMENT_NAME (element));
321
322     walk = g_list_next (walk);
323   }
324 }
325
326 static gboolean start_seek (GtkWidget * widget, GdkEventButton * event,
327     gpointer user_data);
328 static gboolean stop_seek (GtkWidget * widget, GdkEventButton * event,
329     gpointer user_data);
330 static void seek_cb (GtkWidget * widget);
331
332 static void
333 set_scale (gdouble value)
334 {
335   g_signal_handlers_block_by_func (hscale, (void *) start_seek,
336       (void *) pipeline);
337   g_signal_handlers_block_by_func (hscale, (void *) stop_seek,
338       (void *) pipeline);
339   g_signal_handlers_block_by_func (hscale, (void *) seek_cb, (void *) pipeline);
340   gtk_adjustment_set_value (adjustment, value);
341   g_signal_handlers_unblock_by_func (hscale, (void *) start_seek,
342       (void *) pipeline);
343   g_signal_handlers_unblock_by_func (hscale, (void *) stop_seek,
344       (void *) pipeline);
345   g_signal_handlers_unblock_by_func (hscale, (void *) seek_cb,
346       (void *) pipeline);
347   gtk_widget_queue_draw (hscale);
348 }
349
350 static gboolean
351 update_fill (gpointer data)
352 {
353   if (seekable_elements) {
354     GstElement *element = GST_ELEMENT (seekable_elements->data);
355     GstQuery *query;
356
357     query = gst_query_new_buffering (GST_FORMAT_PERCENT);
358     if (gst_element_query (element, query)) {
359       gint64 start, stop, buffering_total;
360       GstFormat format;
361       gdouble fill;
362       gboolean busy;
363       gint percent;
364       GstBufferingMode mode;
365       gint avg_in, avg_out;
366       gint64 buffering_left;
367
368       gst_query_parse_buffering_percent (query, &busy, &percent);
369       gst_query_parse_buffering_range (query, &format, &start, &stop,
370           &buffering_total);
371       gst_query_parse_buffering_stats (query, &mode, &avg_in, &avg_out,
372           &buffering_left);
373
374       /* note that we could start the playback when buffering_left < remaining
375        * playback time */
376       GST_DEBUG ("buffering total %" G_GINT64_FORMAT " ms, left %"
377           G_GINT64_FORMAT " ms", buffering_total, buffering_left);
378       GST_DEBUG ("start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT,
379           start, stop);
380
381       if (stop != -1)
382         fill = N_GRAD * stop / GST_FORMAT_PERCENT_MAX;
383       else
384         fill = N_GRAD;
385
386       gtk_range_set_fill_level (GTK_RANGE (hscale), fill);
387     }
388     gst_query_unref (query);
389   }
390   return TRUE;
391 }
392
393 static gboolean
394 update_scale (gpointer data)
395 {
396   if (seekable_elements) {
397     GstElement *element = GST_ELEMENT (seekable_elements->data);
398
399     gst_element_query_position (element, GST_FORMAT_TIME, &position);
400     gst_element_query_duration (element, GST_FORMAT_TIME, &duration);
401   }
402
403   if (stats) {
404     query_positions_elems ();
405   }
406
407   if (position >= duration)
408     duration = position;
409
410   if (duration > 0) {
411     set_scale (position * N_GRAD / duration);
412   }
413
414   /* FIXME: see make_playerbin2_pipeline() and volume_notify_cb() */
415   if (pipeline_type == 16) {
416     g_object_notify (G_OBJECT (pipeline), "volume");
417   }
418
419   return TRUE;
420 }
421
422 static void do_seek (GtkWidget * widget);
423 static void connect_bus_signals (GstElement * pipeline);
424 static void set_update_scale (gboolean active);
425 static void set_update_fill (gboolean active);
426
427 static gboolean
428 end_scrub (GtkWidget * widget)
429 {
430   GST_DEBUG ("end scrub, PAUSE");
431   gst_element_set_state (pipeline, GST_STATE_PAUSED);
432   seek_timeout_id = 0;
433
434   return FALSE;
435 }
436
437 static gboolean
438 send_event (GstEvent * event)
439 {
440   gboolean res = FALSE;
441   GList *walk = seekable_elements;
442
443   while (walk) {
444     GstElement *seekable = GST_ELEMENT (walk->data);
445
446     GST_DEBUG ("send event on element %s", GST_ELEMENT_NAME (seekable));
447
448     gst_event_ref (event);
449     res = gst_element_send_event (seekable, event);
450
451     walk = g_list_next (walk);
452   }
453   gst_event_unref (event);
454   return res;
455 }
456
457 static void
458 do_seek (GtkWidget * widget)
459 {
460   gint64 real;
461   gboolean res = FALSE;
462   GstEvent *s_event;
463   GstSeekFlags flags;
464
465   real = gtk_range_get_value (GTK_RANGE (widget)) * duration / N_GRAD;
466
467   GST_DEBUG ("value=%f, real=%" G_GINT64_FORMAT,
468       gtk_range_get_value (GTK_RANGE (widget)), real);
469
470   flags = 0;
471   if (flush_seek)
472     flags |= GST_SEEK_FLAG_FLUSH;
473   if (accurate_seek)
474     flags |= GST_SEEK_FLAG_ACCURATE;
475   if (keyframe_seek)
476     flags |= GST_SEEK_FLAG_KEY_UNIT;
477   if (loop_seek)
478     flags |= GST_SEEK_FLAG_SEGMENT;
479   if (skip_seek)
480     flags |= GST_SEEK_FLAG_SKIP;
481
482   if (rate >= 0) {
483     s_event = gst_event_new_seek (rate,
484         GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, real, GST_SEEK_TYPE_SET,
485         GST_CLOCK_TIME_NONE);
486     GST_DEBUG ("seek with rate %lf to %" GST_TIME_FORMAT " / %" GST_TIME_FORMAT,
487         rate, GST_TIME_ARGS (real), GST_TIME_ARGS (duration));
488   } else {
489     s_event = gst_event_new_seek (rate,
490         GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, G_GINT64_CONSTANT (0),
491         GST_SEEK_TYPE_SET, real);
492     GST_DEBUG ("seek with rate %lf to %" GST_TIME_FORMAT " / %" GST_TIME_FORMAT,
493         rate, GST_TIME_ARGS (0), GST_TIME_ARGS (real));
494   }
495
496   res = send_event (s_event);
497
498   if (res) {
499     if (flush_seek) {
500       gst_element_get_state (GST_ELEMENT (pipeline), NULL, NULL, SEEK_TIMEOUT);
501     } else {
502       set_update_scale (TRUE);
503     }
504   } else {
505     g_print ("seek failed\n");
506     set_update_scale (TRUE);
507   }
508 }
509
510 static void
511 seek_cb (GtkWidget * widget)
512 {
513   /* If the timer hasn't expired yet, then the pipeline is running */
514   if (play_scrub && seek_timeout_id != 0) {
515     GST_DEBUG ("do scrub seek, PAUSED");
516     gst_element_set_state (pipeline, GST_STATE_PAUSED);
517   }
518
519   GST_DEBUG ("do seek");
520   do_seek (widget);
521
522   if (play_scrub) {
523     GST_DEBUG ("do scrub seek, PLAYING");
524     gst_element_set_state (pipeline, GST_STATE_PLAYING);
525
526     if (seek_timeout_id == 0) {
527       seek_timeout_id =
528           g_timeout_add (SCRUB_TIME, (GSourceFunc) end_scrub, widget);
529     }
530   }
531 }
532
533 static void
534 set_update_fill (gboolean active)
535 {
536   GST_DEBUG ("fill scale is %d", active);
537
538   if (active) {
539     if (fill_id == 0) {
540       fill_id =
541           g_timeout_add (FILL_INTERVAL, (GSourceFunc) update_fill, pipeline);
542     }
543   } else {
544     if (fill_id) {
545       g_source_remove (fill_id);
546       fill_id = 0;
547     }
548   }
549 }
550
551 static void
552 set_update_scale (gboolean active)
553 {
554
555   GST_DEBUG ("update scale is %d", active);
556
557   if (active) {
558     if (update_id == 0) {
559       update_id =
560           g_timeout_add (UPDATE_INTERVAL, (GSourceFunc) update_scale, pipeline);
561     }
562   } else {
563     if (update_id) {
564       g_source_remove (update_id);
565       update_id = 0;
566     }
567   }
568 }
569
570 static gboolean
571 start_seek (GtkWidget * widget, GdkEventButton * event, gpointer user_data)
572 {
573   if (event->type != GDK_BUTTON_PRESS)
574     return FALSE;
575
576   set_update_scale (FALSE);
577
578   if (state == GST_STATE_PLAYING && flush_seek && scrub) {
579     GST_DEBUG ("start scrub seek, PAUSE");
580     gst_element_set_state (pipeline, GST_STATE_PAUSED);
581   }
582
583   if (changed_id == 0 && flush_seek && scrub) {
584     changed_id =
585         g_signal_connect (hscale, "value_changed", G_CALLBACK (seek_cb),
586         pipeline);
587   }
588
589   return FALSE;
590 }
591
592 static gboolean
593 stop_seek (GtkWidget * widget, GdkEventButton * event, gpointer user_data)
594 {
595   if (changed_id) {
596     g_signal_handler_disconnect (hscale, changed_id);
597     changed_id = 0;
598   }
599
600   if (!flush_seek || !scrub) {
601     GST_DEBUG ("do final seek");
602     do_seek (widget);
603   }
604
605   if (seek_timeout_id != 0) {
606     g_source_remove (seek_timeout_id);
607     seek_timeout_id = 0;
608     /* Still scrubbing, so the pipeline is playing, see if we need PAUSED
609      * instead. */
610     if (state == GST_STATE_PAUSED) {
611       GST_DEBUG ("stop scrub seek, PAUSED");
612       gst_element_set_state (pipeline, GST_STATE_PAUSED);
613     }
614   } else {
615     if (state == GST_STATE_PLAYING) {
616       GST_DEBUG ("stop scrub seek, PLAYING");
617       gst_element_set_state (pipeline, GST_STATE_PLAYING);
618     }
619   }
620
621   return FALSE;
622 }
623
624 static void
625 play_cb (GtkButton * button, gpointer data)
626 {
627   GstStateChangeReturn ret;
628
629   if (state != GST_STATE_PLAYING) {
630     g_print ("PLAY pipeline\n");
631     gtk_statusbar_pop (GTK_STATUSBAR (statusbar), status_id);
632
633     ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
634     switch (ret) {
635       case GST_STATE_CHANGE_FAILURE:
636         goto failed;
637       case GST_STATE_CHANGE_NO_PREROLL:
638         is_live = TRUE;
639         break;
640       default:
641         break;
642     }
643     state = GST_STATE_PLAYING;
644     gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Playing");
645   }
646
647   return;
648
649 failed:
650   {
651     g_print ("PLAY failed\n");
652     gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Play failed");
653   }
654 }
655
656 static void
657 pause_cb (GtkButton * button, gpointer data)
658 {
659   g_static_mutex_lock (&state_mutex);
660   if (state != GST_STATE_PAUSED) {
661     GstStateChangeReturn ret;
662
663     gtk_statusbar_pop (GTK_STATUSBAR (statusbar), status_id);
664     g_print ("PAUSE pipeline\n");
665     ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
666     switch (ret) {
667       case GST_STATE_CHANGE_FAILURE:
668         goto failed;
669       case GST_STATE_CHANGE_NO_PREROLL:
670         is_live = TRUE;
671         break;
672       default:
673         break;
674     }
675
676     state = GST_STATE_PAUSED;
677     gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Paused");
678   }
679   g_static_mutex_unlock (&state_mutex);
680
681   return;
682
683 failed:
684   {
685     g_static_mutex_unlock (&state_mutex);
686     g_print ("PAUSE failed\n");
687     gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Pause failed");
688   }
689 }
690
691 static void
692 stop_cb (GtkButton * button, gpointer data)
693 {
694   if (state != STOP_STATE) {
695     GstStateChangeReturn ret;
696
697     g_print ("READY pipeline\n");
698     gtk_statusbar_pop (GTK_STATUSBAR (statusbar), status_id);
699
700     g_static_mutex_lock (&state_mutex);
701     ret = gst_element_set_state (pipeline, STOP_STATE);
702     if (ret == GST_STATE_CHANGE_FAILURE)
703       goto failed;
704
705     state = STOP_STATE;
706     gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Stopped");
707
708     is_live = FALSE;
709     buffering = FALSE;
710     set_update_scale (FALSE);
711     set_scale (0.0);
712     set_update_fill (FALSE);
713
714     if (pipeline_type == 16)
715       clear_streams (pipeline);
716     g_static_mutex_unlock (&state_mutex);
717
718 #if 0
719     /* if one uses parse_launch, play, stop and play again it fails as all the
720      * pads after the demuxer can't be reconnected
721      */
722     if (!strcmp (pipelines[pipeline_type].name, "parse-launch")) {
723       gst_element_set_state (pipeline, GST_STATE_NULL);
724       gst_object_unref (pipeline);
725
726       g_list_free (seekable_elements);
727       seekable_elements = NULL;
728
729       pipeline = pipelines[pipeline_type].func (pipeline_spec);
730       g_assert (pipeline);
731       gst_element_set_state (pipeline, STOP_STATE);
732       connect_bus_signals (pipeline);
733     }
734 #endif
735   }
736   return;
737
738 failed:
739   {
740     g_static_mutex_unlock (&state_mutex);
741     g_print ("STOP failed\n");
742     gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Stop failed");
743   }
744 }
745
746 static void
747 accurate_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
748 {
749   accurate_seek = gtk_toggle_button_get_active (button);
750 }
751
752 static void
753 key_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
754 {
755   keyframe_seek = gtk_toggle_button_get_active (button);
756 }
757
758 static void
759 loop_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
760 {
761   loop_seek = gtk_toggle_button_get_active (button);
762   if (state == GST_STATE_PLAYING) {
763     do_seek (hscale);
764   }
765 }
766
767 static void
768 flush_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
769 {
770   flush_seek = gtk_toggle_button_get_active (button);
771 }
772
773 static void
774 scrub_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
775 {
776   scrub = gtk_toggle_button_get_active (button);
777 }
778
779 static void
780 play_scrub_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
781 {
782   play_scrub = gtk_toggle_button_get_active (button);
783 }
784
785 static void
786 skip_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
787 {
788   skip_seek = gtk_toggle_button_get_active (button);
789   if (state == GST_STATE_PLAYING) {
790     do_seek (hscale);
791   }
792 }
793
794 static void
795 rate_spinbutton_changed_cb (GtkSpinButton * button, GstPipeline * pipeline)
796 {
797   gboolean res = FALSE;
798   GstEvent *s_event;
799   GstSeekFlags flags;
800
801   rate = gtk_spin_button_get_value (button);
802
803   GST_DEBUG ("rate changed to %lf", rate);
804
805   flags = 0;
806   if (flush_seek)
807     flags |= GST_SEEK_FLAG_FLUSH;
808   if (loop_seek)
809     flags |= GST_SEEK_FLAG_SEGMENT;
810   if (accurate_seek)
811     flags |= GST_SEEK_FLAG_ACCURATE;
812   if (keyframe_seek)
813     flags |= GST_SEEK_FLAG_KEY_UNIT;
814   if (skip_seek)
815     flags |= GST_SEEK_FLAG_SKIP;
816
817   if (rate >= 0.0) {
818     s_event = gst_event_new_seek (rate,
819         GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, position,
820         GST_SEEK_TYPE_SET, GST_CLOCK_TIME_NONE);
821   } else {
822     s_event = gst_event_new_seek (rate,
823         GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, G_GINT64_CONSTANT (0),
824         GST_SEEK_TYPE_SET, position);
825   }
826
827   res = send_event (s_event);
828
829   if (res) {
830     if (flush_seek) {
831       gst_element_get_state (GST_ELEMENT (pipeline), NULL, NULL, SEEK_TIMEOUT);
832     }
833   } else
834     g_print ("seek failed\n");
835 }
836
837 static void
838 update_flag (GstPipeline * pipeline, gint num, gboolean state)
839 {
840   gint flags;
841
842   g_object_get (pipeline, "flags", &flags, NULL);
843   if (state)
844     flags |= (1 << num);
845   else
846     flags &= ~(1 << num);
847   g_object_set (pipeline, "flags", flags, NULL);
848 }
849
850 static void
851 vis_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
852 {
853   gboolean state;
854
855   state = gtk_toggle_button_get_active (button);
856   update_flag (pipeline, 3, state);
857   gtk_widget_set_sensitive (vis_combo, state);
858 }
859
860 static void
861 audio_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
862 {
863   gboolean state;
864
865   state = gtk_toggle_button_get_active (button);
866   update_flag (pipeline, 1, state);
867   gtk_widget_set_sensitive (audio_combo, state);
868 }
869
870 static void
871 video_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
872 {
873   gboolean state;
874
875   state = gtk_toggle_button_get_active (button);
876   update_flag (pipeline, 0, state);
877   gtk_widget_set_sensitive (video_combo, state);
878 }
879
880 static void
881 text_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
882 {
883   gboolean state;
884
885   state = gtk_toggle_button_get_active (button);
886   update_flag (pipeline, 2, state);
887   gtk_widget_set_sensitive (text_combo, state);
888 }
889
890 static void
891 mute_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
892 {
893   gboolean mute;
894
895   mute = gtk_toggle_button_get_active (button);
896   g_object_set (pipeline, "mute", mute, NULL);
897 }
898
899 static void
900 download_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
901 {
902   gboolean state;
903
904   state = gtk_toggle_button_get_active (button);
905   update_flag (pipeline, 7, state);
906 }
907
908 static void
909 buffer_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
910 {
911   gboolean state;
912
913   state = gtk_toggle_button_get_active (button);
914   update_flag (pipeline, 8, state);
915 }
916
917 static void
918 clear_streams (GstElement * pipeline)
919 {
920   gint i;
921
922   /* remove previous info */
923   for (i = 0; i < n_video; i++)
924     gtk_combo_box_text_remove (GTK_COMBO_BOX_TEXT (video_combo), 0);
925   for (i = 0; i < n_audio; i++)
926     gtk_combo_box_text_remove (GTK_COMBO_BOX_TEXT (audio_combo), 0);
927   for (i = 0; i < n_text; i++)
928     gtk_combo_box_text_remove (GTK_COMBO_BOX_TEXT (text_combo), 0);
929
930   n_audio = n_video = n_text = 0;
931   gtk_widget_set_sensitive (video_combo, FALSE);
932   gtk_widget_set_sensitive (audio_combo, FALSE);
933   gtk_widget_set_sensitive (text_combo, FALSE);
934
935   need_streams = TRUE;
936 }
937
938 static void
939 update_streams (GstPipeline * pipeline)
940 {
941   gint i;
942
943   if (pipeline_type == 16 && need_streams) {
944     GstTagList *tags;
945     gchar *name, *str;
946     gint active_idx;
947     gboolean state;
948
949     /* remove previous info */
950     clear_streams (GST_ELEMENT_CAST (pipeline));
951
952     /* here we get and update the different streams detected by playbin2 */
953     g_object_get (pipeline, "n-video", &n_video, NULL);
954     g_object_get (pipeline, "n-audio", &n_audio, NULL);
955     g_object_get (pipeline, "n-text", &n_text, NULL);
956
957     g_print ("video %d, audio %d, text %d\n", n_video, n_audio, n_text);
958
959     active_idx = 0;
960     for (i = 0; i < n_video; i++) {
961       g_signal_emit_by_name (pipeline, "get-video-tags", i, &tags);
962       if (tags) {
963         str = gst_structure_to_string ((GstStructure *) tags);
964         g_print ("video %d: %s\n", i, str);
965         g_free (str);
966       }
967       /* find good name for the label */
968       name = g_strdup_printf ("video %d", i + 1);
969       gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (video_combo), name);
970       g_free (name);
971     }
972     state = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (video_checkbox));
973     gtk_widget_set_sensitive (video_combo, state && n_video > 0);
974     gtk_combo_box_set_active (GTK_COMBO_BOX (video_combo), active_idx);
975
976     active_idx = 0;
977     for (i = 0; i < n_audio; i++) {
978       g_signal_emit_by_name (pipeline, "get-audio-tags", i, &tags);
979       if (tags) {
980         str = gst_structure_to_string ((GstStructure *) tags);
981         g_print ("audio %d: %s\n", i, str);
982         g_free (str);
983       }
984       /* find good name for the label */
985       name = g_strdup_printf ("audio %d", i + 1);
986       gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (audio_combo), name);
987       g_free (name);
988     }
989     state = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (audio_checkbox));
990     gtk_widget_set_sensitive (audio_combo, state && n_audio > 0);
991     gtk_combo_box_set_active (GTK_COMBO_BOX (audio_combo), active_idx);
992
993     active_idx = 0;
994     for (i = 0; i < n_text; i++) {
995       g_signal_emit_by_name (pipeline, "get-text-tags", i, &tags);
996
997       name = NULL;
998       if (tags) {
999         const GValue *value;
1000
1001         str = gst_structure_to_string ((GstStructure *) tags);
1002         g_print ("text %d: %s\n", i, str);
1003         g_free (str);
1004
1005         /* get the language code if we can */
1006         value = gst_tag_list_get_value_index (tags, GST_TAG_LANGUAGE_CODE, 0);
1007         if (value && G_VALUE_HOLDS_STRING (value)) {
1008           name = g_strdup_printf ("text %s", g_value_get_string (value));
1009         }
1010       }
1011       /* find good name for the label if we didn't use a tag */
1012       if (name == NULL)
1013         name = g_strdup_printf ("text %d", i + 1);
1014
1015       gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (text_combo), name);
1016       g_free (name);
1017     }
1018     state = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (text_checkbox));
1019     gtk_widget_set_sensitive (text_combo, state && n_text > 0);
1020     gtk_combo_box_set_active (GTK_COMBO_BOX (text_combo), active_idx);
1021
1022     need_streams = FALSE;
1023   }
1024 }
1025
1026 static void
1027 video_combo_cb (GtkComboBox * combo, GstPipeline * pipeline)
1028 {
1029   gint active;
1030
1031   active = gtk_combo_box_get_active (combo);
1032
1033   g_print ("setting current video track %d\n", active);
1034   g_object_set (pipeline, "current-video", active, NULL);
1035 }
1036
1037 static void
1038 audio_combo_cb (GtkComboBox * combo, GstPipeline * pipeline)
1039 {
1040   gint active;
1041
1042   active = gtk_combo_box_get_active (combo);
1043
1044   g_print ("setting current audio track %d\n", active);
1045   g_object_set (pipeline, "current-audio", active, NULL);
1046 }
1047
1048 static void
1049 text_combo_cb (GtkComboBox * combo, GstPipeline * pipeline)
1050 {
1051   gint active;
1052
1053   active = gtk_combo_box_get_active (combo);
1054
1055   g_print ("setting current text track %d\n", active);
1056   g_object_set (pipeline, "current-text", active, NULL);
1057 }
1058
1059 static gboolean
1060 filter_features (GstPluginFeature * feature, gpointer data)
1061 {
1062   GstElementFactory *f;
1063
1064   if (!GST_IS_ELEMENT_FACTORY (feature))
1065     return FALSE;
1066   f = GST_ELEMENT_FACTORY (feature);
1067   if (!g_strrstr (gst_element_factory_get_klass (f), "Visualization"))
1068     return FALSE;
1069
1070   return TRUE;
1071 }
1072
1073 static void
1074 init_visualization_features (void)
1075 {
1076   GList *list, *walk;
1077
1078   vis_entries = g_array_new (FALSE, FALSE, sizeof (VisEntry));
1079
1080   list = gst_registry_feature_filter (gst_registry_get_default (),
1081       filter_features, FALSE, NULL);
1082
1083   for (walk = list; walk; walk = g_list_next (walk)) {
1084     VisEntry entry;
1085     const gchar *name;
1086
1087     entry.factory = GST_ELEMENT_FACTORY (walk->data);
1088     name = gst_element_factory_get_longname (entry.factory);
1089
1090     g_array_append_val (vis_entries, entry);
1091     gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (vis_combo), name);
1092   }
1093   gtk_combo_box_set_active (GTK_COMBO_BOX (vis_combo), 0);
1094 }
1095
1096 static void
1097 vis_combo_cb (GtkComboBox * combo, GstPipeline * pipeline)
1098 {
1099   guint index;
1100   VisEntry *entry;
1101   GstElement *element;
1102
1103   /* get the selected index and get the factory for this index */
1104   index = gtk_combo_box_get_active (GTK_COMBO_BOX (vis_combo));
1105   if (vis_entries->len > 0) {
1106     entry = &g_array_index (vis_entries, VisEntry, index);
1107
1108     /* create an instance of the element from the factory */
1109     element = gst_element_factory_create (entry->factory, NULL);
1110     if (!element)
1111       return;
1112
1113     /* set vis plugin for playbin2 */
1114     g_object_set (pipeline, "vis-plugin", element, NULL);
1115   }
1116 }
1117
1118 static void
1119 volume_spinbutton_changed_cb (GtkSpinButton * button, GstPipeline * pipeline)
1120 {
1121   gdouble volume;
1122
1123   volume = gtk_spin_button_get_value (button);
1124
1125   g_object_set (pipeline, "volume", volume, NULL);
1126 }
1127
1128 static void
1129 volume_notify_cb (GstElement * pipeline, GParamSpec * arg, gpointer user_dat)
1130 {
1131   gdouble cur_volume, new_volume;
1132
1133   if (volume_spinbutton == NULL)
1134     return;
1135
1136   g_object_get (pipeline, "volume", &new_volume, NULL);
1137   cur_volume = gtk_spin_button_get_value (GTK_SPIN_BUTTON (volume_spinbutton));
1138   if (fabs (cur_volume - new_volume) > 0.001) {
1139     g_signal_handlers_block_by_func (volume_spinbutton,
1140         volume_spinbutton_changed_cb, pipeline);
1141     gtk_spin_button_set_value (GTK_SPIN_BUTTON (volume_spinbutton), new_volume);
1142     g_signal_handlers_unblock_by_func (volume_spinbutton,
1143         volume_spinbutton_changed_cb, pipeline);
1144   }
1145 }
1146
1147 static void
1148 shot_cb (GtkButton * button, gpointer data)
1149 {
1150   GstBuffer *buffer;
1151   GstCaps *caps;
1152
1153   /* convert to our desired format (RGB24) */
1154   caps = gst_caps_new_simple ("video/x-raw-rgb",
1155       "bpp", G_TYPE_INT, 24, "depth", G_TYPE_INT, 24,
1156       /* Note: we don't ask for a specific width/height here, so that
1157        * videoscale can adjust dimensions from a non-1/1 pixel aspect
1158        * ratio to a 1/1 pixel-aspect-ratio */
1159       "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
1160       "endianness", G_TYPE_INT, G_BIG_ENDIAN,
1161       "red_mask", G_TYPE_INT, 0xff0000,
1162       "green_mask", G_TYPE_INT, 0x00ff00,
1163       "blue_mask", G_TYPE_INT, 0x0000ff, NULL);
1164
1165   /* convert the latest frame to the requested format */
1166   g_signal_emit_by_name (pipeline, "convert-frame", caps, &buffer);
1167   gst_caps_unref (caps);
1168
1169   if (buffer) {
1170     GstCaps *caps;
1171     GstStructure *s;
1172     gboolean res;
1173     gint width, height;
1174     GdkPixbuf *pixbuf;
1175     GError *error = NULL;
1176     guint8 *data;
1177     gsize size;
1178
1179     /* get the snapshot buffer format now. We set the caps on the appsink so
1180      * that it can only be an rgb buffer. The only thing we have not specified
1181      * on the caps is the height, which is dependant on the pixel-aspect-ratio
1182      * of the source material */
1183 #if 0
1184     caps = GST_BUFFER_CAPS (buffer);
1185 #endif
1186     /* FIXME, need to get the caps of the buffer somehow */
1187     caps = NULL;
1188     if (!caps) {
1189       g_warning ("could not get snapshot format\n");
1190       goto done;
1191     }
1192     s = gst_caps_get_structure (caps, 0);
1193
1194     /* we need to get the final caps on the buffer to get the size */
1195     res = gst_structure_get_int (s, "width", &width);
1196     res |= gst_structure_get_int (s, "height", &height);
1197     if (!res) {
1198       g_warning ("could not get snapshot dimension\n");
1199       goto done;
1200     }
1201
1202     /* create pixmap from buffer and save, gstreamer video buffers have a stride
1203      * that is rounded up to the nearest multiple of 4 */
1204     data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
1205     pixbuf = gdk_pixbuf_new_from_data (data,
1206         GDK_COLORSPACE_RGB, FALSE, 8, width, height,
1207         GST_ROUND_UP_4 (width * 3), NULL, NULL);
1208
1209     /* save the pixbuf */
1210     gdk_pixbuf_save (pixbuf, "snapshot.png", "png", &error, NULL);
1211     gst_buffer_unmap (buffer, data, size);
1212
1213   done:
1214     gst_buffer_unref (buffer);
1215   }
1216 }
1217
1218 /* called when the Step button is pressed */
1219 static void
1220 step_cb (GtkButton * button, gpointer data)
1221 {
1222   GstEvent *event;
1223   GstFormat format;
1224   guint64 amount;
1225   gdouble rate;
1226   gboolean flush, res;
1227   gint active;
1228
1229   active = gtk_combo_box_get_active (GTK_COMBO_BOX (format_combo));
1230   amount =
1231       gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
1232       (step_amount_spinbutton));
1233   rate = gtk_spin_button_get_value (GTK_SPIN_BUTTON (step_rate_spinbutton));
1234   flush = TRUE;
1235
1236   switch (active) {
1237     case 0:
1238       format = GST_FORMAT_BUFFERS;
1239       break;
1240     case 1:
1241       format = GST_FORMAT_TIME;
1242       amount *= GST_MSECOND;
1243       break;
1244     default:
1245       format = GST_FORMAT_UNDEFINED;
1246       break;
1247   }
1248
1249   event = gst_event_new_step (format, amount, rate, flush, FALSE);
1250
1251   res = send_event (event);
1252
1253   if (!res) {
1254     g_print ("Sending step event failed\n");
1255   }
1256 }
1257
1258 static void
1259 message_received (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
1260 {
1261   const GstStructure *s;
1262
1263   s = gst_message_get_structure (message);
1264   g_print ("message from \"%s\" (%s): ",
1265       GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))),
1266       gst_message_type_get_name (GST_MESSAGE_TYPE (message)));
1267   if (s) {
1268     gchar *sstr;
1269
1270     sstr = gst_structure_to_string (s);
1271     g_print ("%s\n", sstr);
1272     g_free (sstr);
1273   } else {
1274     g_print ("no message details\n");
1275   }
1276 }
1277
1278 static gboolean shuttling = FALSE;
1279 static gdouble shuttle_rate = 0.0;
1280 static gdouble play_rate = 1.0;
1281
1282 static void
1283 do_shuttle (GstElement * element)
1284 {
1285   guint64 duration;
1286
1287   if (shuttling)
1288     duration = 40 * GST_MSECOND;
1289   else
1290     duration = -1;
1291
1292   gst_element_send_event (element,
1293       gst_event_new_step (GST_FORMAT_TIME, duration, shuttle_rate, FALSE,
1294           FALSE));
1295 }
1296
1297 static void
1298 msg_sync_step_done (GstBus * bus, GstMessage * message, GstElement * element)
1299 {
1300   GstFormat format;
1301   guint64 amount;
1302   gdouble rate;
1303   gboolean flush;
1304   gboolean intermediate;
1305   guint64 duration;
1306   gboolean eos;
1307
1308   gst_message_parse_step_done (message, &format, &amount, &rate, &flush,
1309       &intermediate, &duration, &eos);
1310
1311   if (eos) {
1312     g_print ("stepped till EOS\n");
1313     return;
1314   }
1315
1316   if (g_static_mutex_trylock (&state_mutex)) {
1317     if (shuttling)
1318       do_shuttle (element);
1319     g_static_mutex_unlock (&state_mutex);
1320   } else {
1321     /* ignore step messages that come while we are doing a state change */
1322     g_print ("state change is busy\n");
1323   }
1324 }
1325
1326 static void
1327 shuttle_toggled (GtkToggleButton * button, GstElement * element)
1328 {
1329   gboolean active;
1330
1331   active = gtk_toggle_button_get_active (button);
1332
1333   if (active != shuttling) {
1334     shuttling = active;
1335     g_print ("shuttling %s\n", shuttling ? "active" : "inactive");
1336     if (active) {
1337       shuttle_rate = 0.0;
1338       play_rate = 1.0;
1339       pause_cb (NULL, NULL);
1340       gst_element_get_state (element, NULL, NULL, -1);
1341     }
1342   }
1343 }
1344
1345 static void
1346 shuttle_rate_switch (GstElement * element)
1347 {
1348   GstSeekFlags flags;
1349   GstEvent *s_event;
1350   gboolean res;
1351
1352   if (state == GST_STATE_PLAYING) {
1353     /* pause when we need to */
1354     pause_cb (NULL, NULL);
1355     gst_element_get_state (element, NULL, NULL, -1);
1356   }
1357
1358   if (play_rate == 1.0)
1359     play_rate = -1.0;
1360   else
1361     play_rate = 1.0;
1362
1363   g_print ("rate changed to %lf %" GST_TIME_FORMAT "\n", play_rate,
1364       GST_TIME_ARGS (position));
1365
1366   flags = GST_SEEK_FLAG_FLUSH;
1367   flags |= GST_SEEK_FLAG_ACCURATE;
1368
1369   if (play_rate >= 0.0) {
1370     s_event = gst_event_new_seek (play_rate,
1371         GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, position,
1372         GST_SEEK_TYPE_SET, GST_CLOCK_TIME_NONE);
1373   } else {
1374     s_event = gst_event_new_seek (play_rate,
1375         GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, G_GINT64_CONSTANT (0),
1376         GST_SEEK_TYPE_SET, position);
1377   }
1378   res = send_event (s_event);
1379   if (res) {
1380     gst_element_get_state (element, NULL, NULL, SEEK_TIMEOUT);
1381   } else {
1382     g_print ("seek failed\n");
1383   }
1384 }
1385
1386 static void
1387 shuttle_value_changed (GtkRange * range, GstElement * element)
1388 {
1389   gdouble rate;
1390
1391   rate = gtk_adjustment_get_value (shuttle_adjustment);
1392
1393   if (rate == 0.0) {
1394     g_print ("rate 0.0, pause\n");
1395     pause_cb (NULL, NULL);
1396     gst_element_get_state (element, NULL, NULL, -1);
1397   } else {
1398     g_print ("rate changed %0.3g\n", rate);
1399
1400     if ((rate < 0.0 && play_rate > 0.0) || (rate > 0.0 && play_rate < 0.0)) {
1401       shuttle_rate_switch (element);
1402     }
1403
1404     shuttle_rate = ABS (rate);
1405     if (state != GST_STATE_PLAYING) {
1406       do_shuttle (element);
1407       play_cb (NULL, NULL);
1408     }
1409   }
1410 }
1411
1412 static void
1413 msg_async_done (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
1414 {
1415   GST_DEBUG ("async done");
1416   /* when we get ASYNC_DONE we can query position, duration and other
1417    * properties */
1418   update_scale (pipeline);
1419
1420   /* update the available streams */
1421   update_streams (pipeline);
1422 }
1423
1424 static void
1425 msg_state_changed (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
1426 {
1427   const GstStructure *s;
1428
1429   s = gst_message_get_structure (message);
1430
1431   /* We only care about state changed on the pipeline */
1432   if (s && GST_MESSAGE_SRC (message) == GST_OBJECT_CAST (pipeline)) {
1433     GstState old, new, pending;
1434
1435     gst_message_parse_state_changed (message, &old, &new, &pending);
1436
1437     /* When state of the pipeline changes to paused or playing we start updating scale */
1438     if (new == GST_STATE_PLAYING) {
1439       set_update_scale (TRUE);
1440     } else {
1441       set_update_scale (FALSE);
1442     }
1443   }
1444 }
1445
1446 static void
1447 msg_segment_done (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
1448 {
1449   GstEvent *s_event;
1450   GstSeekFlags flags;
1451   gboolean res;
1452   GstFormat format;
1453
1454   GST_DEBUG ("position is %" GST_TIME_FORMAT, GST_TIME_ARGS (position));
1455   gst_message_parse_segment_done (message, &format, &position);
1456   GST_DEBUG ("end of segment at %" GST_TIME_FORMAT, GST_TIME_ARGS (position));
1457
1458   flags = 0;
1459   /* in the segment-done callback we never flush as this would not make sense
1460    * for seamless playback. */
1461   if (loop_seek)
1462     flags |= GST_SEEK_FLAG_SEGMENT;
1463   if (skip_seek)
1464     flags |= GST_SEEK_FLAG_SKIP;
1465
1466   s_event = gst_event_new_seek (rate,
1467       GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, G_GINT64_CONSTANT (0),
1468       GST_SEEK_TYPE_SET, duration);
1469
1470   GST_DEBUG ("restart loop with rate %lf to 0 / %" GST_TIME_FORMAT,
1471       rate, GST_TIME_ARGS (duration));
1472
1473   res = send_event (s_event);
1474   if (!res)
1475     g_print ("segment seek failed\n");
1476 }
1477
1478 /* in stream buffering mode we PAUSE the pipeline until we receive a 100%
1479  * message */
1480 static void
1481 do_stream_buffering (gint percent)
1482 {
1483   gchar *bufstr;
1484
1485   gtk_statusbar_pop (GTK_STATUSBAR (statusbar), status_id);
1486   bufstr = g_strdup_printf ("Buffering...%d", percent);
1487   gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, bufstr);
1488   g_free (bufstr);
1489
1490   if (percent == 100) {
1491     /* a 100% message means buffering is done */
1492     buffering = FALSE;
1493     /* if the desired state is playing, go back */
1494     if (state == GST_STATE_PLAYING) {
1495       /* no state management needed for live pipelines */
1496       if (!is_live) {
1497         fprintf (stderr, "Done buffering, setting pipeline to PLAYING ...\n");
1498         gst_element_set_state (pipeline, GST_STATE_PLAYING);
1499       }
1500       gtk_statusbar_pop (GTK_STATUSBAR (statusbar), status_id);
1501       gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Playing");
1502     }
1503   } else {
1504     /* buffering busy */
1505     if (buffering == FALSE && state == GST_STATE_PLAYING) {
1506       /* we were not buffering but PLAYING, PAUSE  the pipeline. */
1507       if (!is_live) {
1508         fprintf (stderr, "Buffering, setting pipeline to PAUSED ...\n");
1509         gst_element_set_state (pipeline, GST_STATE_PAUSED);
1510       }
1511     }
1512     buffering = TRUE;
1513   }
1514 }
1515
1516 static void
1517 do_download_buffering (gint percent)
1518 {
1519   if (!buffering && percent < 100) {
1520     gchar *bufstr;
1521
1522     buffering = TRUE;
1523
1524     bufstr = g_strdup_printf ("Downloading...");
1525     gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, bufstr);
1526     g_free (bufstr);
1527
1528     /* once we get a buffering message, we'll do the fill update */
1529     set_update_fill (TRUE);
1530
1531     if (state == GST_STATE_PLAYING && !is_live) {
1532       fprintf (stderr, "Downloading, setting pipeline to PAUSED ...\n");
1533       gst_element_set_state (pipeline, GST_STATE_PAUSED);
1534       /* user has to manually start the playback */
1535       state = GST_STATE_PAUSED;
1536     }
1537   }
1538 }
1539
1540 static void
1541 msg_buffering (GstBus * bus, GstMessage * message, GstPipeline * data)
1542 {
1543   gint percent;
1544
1545   gst_message_parse_buffering (message, &percent);
1546
1547   /* get more stats */
1548   gst_message_parse_buffering_stats (message, &mode, NULL, NULL,
1549       &buffering_left);
1550
1551   switch (mode) {
1552     case GST_BUFFERING_DOWNLOAD:
1553       do_download_buffering (percent);
1554       break;
1555     case GST_BUFFERING_LIVE:
1556       is_live = TRUE;
1557     case GST_BUFFERING_TIMESHIFT:
1558     case GST_BUFFERING_STREAM:
1559       do_stream_buffering (percent);
1560       break;
1561   }
1562 }
1563
1564 static void
1565 msg_clock_lost (GstBus * bus, GstMessage * message, GstPipeline * data)
1566 {
1567   g_print ("clock lost! PAUSE and PLAY to select a new clock\n");
1568   if (state == GST_STATE_PLAYING) {
1569     gst_element_set_state (pipeline, GST_STATE_PAUSED);
1570     gst_element_set_state (pipeline, GST_STATE_PLAYING);
1571   }
1572 }
1573
1574 #if defined (GDK_WINDOWING_X11) || defined (GDK_WINDOWING_WIN32)
1575
1576 static gulong embed_xid = 0;
1577
1578 /* We set the xid here in response to the prepare-window-handle message via a
1579  * bus sync handler because we don't know the actual videosink used from the
1580  * start (as we don't know the pipeline, or bin elements such as autovideosink
1581  * or gconfvideosink may be used which create the actual videosink only once
1582  * the pipeline is started) */
1583 static GstBusSyncReply
1584 bus_sync_handler (GstBus * bus, GstMessage * message, GstPipeline * data)
1585 {
1586   GstElement *element;
1587
1588   if (!gst_is_video_overlay_prepare_window_handle_message (message))
1589     return GST_BUS_PASS;
1590
1591   element = GST_ELEMENT (GST_MESSAGE_SRC (message));
1592
1593   g_print ("got prepare-window-handle, setting XID %lu\n", embed_xid);
1594
1595   if (g_object_class_find_property (G_OBJECT_GET_CLASS (element),
1596           "force-aspect-ratio")) {
1597     g_object_set (element, "force-aspect-ratio", TRUE, NULL);
1598   }
1599
1600   /* Should have been initialised from main thread before (can't use
1601    * GDK_WINDOW_XID here with Gtk+ >= 2.18, because the sync handler will
1602    * be called from a streaming thread and GDK_WINDOW_XID maps to more than
1603    * a simple structure lookup with Gtk+ >= 2.18, where 'more' is stuff that
1604    * shouldn't be done from a non-GUI thread without explicit locking).  */
1605   g_assert (embed_xid != 0);
1606
1607   gst_video_overlay_set_window_handle (GST_VIDEO_OVERLAY (element), embed_xid);
1608   return GST_BUS_PASS;
1609 }
1610 #endif
1611
1612 static gboolean
1613 handle_expose_cb (GtkWidget * widget, GdkEventExpose * event, gpointer data)
1614 {
1615   if (state < GST_STATE_PAUSED) {
1616     GtkAllocation allocation;
1617     GdkWindow *window = gtk_widget_get_window (widget);
1618     cairo_t *cr;
1619
1620     gtk_widget_get_allocation (widget, &allocation);
1621     cr = gdk_cairo_create (window);
1622     cairo_set_source_rgb (cr, 0, 0, 0);
1623     cairo_rectangle (cr, 0, 0, allocation.width, allocation.height);
1624     cairo_fill (cr);
1625     cairo_destroy (cr);
1626   }
1627   return FALSE;
1628 }
1629
1630 static void
1631 realize_cb (GtkWidget * widget, gpointer data)
1632 {
1633 #if GTK_CHECK_VERSION(2,18,0)
1634   {
1635     GdkWindow *window = gtk_widget_get_window (widget);
1636
1637     /* This is here just for pedagogical purposes, GDK_WINDOW_XID will call it
1638      * as well */
1639     if (!gdk_window_ensure_native (window))
1640       g_error ("Couldn't create native window needed for GstVideoOverlay!");
1641   }
1642 #endif
1643
1644 #if defined (GDK_WINDOWING_X11) || defined (GDK_WINDOWING_WIN32)
1645   {
1646     GdkWindow *window = gtk_widget_get_window (video_window);
1647
1648 #if defined (GDK_WINDOWING_WIN32)
1649     embed_xid = GDK_WINDOW_HWND (window);
1650 #else
1651     embed_xid = GDK_WINDOW_XID (window);
1652 #endif
1653     g_print ("Window realize: video window XID = %lu\n", embed_xid);
1654   }
1655 #endif
1656 }
1657
1658 static void
1659 msg_eos (GstBus * bus, GstMessage * message, GstPipeline * data)
1660 {
1661   message_received (bus, message, data);
1662
1663   /* Set new uri for playerbins and continue playback */
1664   if (l && (pipeline_type == 14 || pipeline_type == 16)) {
1665     stop_cb (NULL, NULL);
1666     l = g_list_next (l);
1667     if (l) {
1668       playerbin_set_uri (GST_ELEMENT (data), l->data);
1669       play_cb (NULL, NULL);
1670     }
1671   }
1672 }
1673
1674 static void
1675 msg_step_done (GstBus * bus, GstMessage * message, GstPipeline * data)
1676 {
1677   if (!shuttling)
1678     message_received (bus, message, data);
1679 }
1680
1681 static void
1682 connect_bus_signals (GstElement * pipeline)
1683 {
1684   GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
1685
1686 #if defined (GDK_WINDOWING_X11) || defined (GDK_WINDOWING_WIN32)
1687   /* handle prepare-window-handle element message synchronously */
1688   gst_bus_set_sync_handler (bus, (GstBusSyncHandler) bus_sync_handler,
1689       pipeline);
1690 #endif
1691
1692   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
1693   gst_bus_enable_sync_message_emission (bus);
1694
1695   g_signal_connect (bus, "message::state-changed",
1696       (GCallback) msg_state_changed, pipeline);
1697   g_signal_connect (bus, "message::segment-done", (GCallback) msg_segment_done,
1698       pipeline);
1699   g_signal_connect (bus, "message::async-done", (GCallback) msg_async_done,
1700       pipeline);
1701
1702   g_signal_connect (bus, "message::new-clock", (GCallback) message_received,
1703       pipeline);
1704   g_signal_connect (bus, "message::clock-lost", (GCallback) msg_clock_lost,
1705       pipeline);
1706   g_signal_connect (bus, "message::error", (GCallback) message_received,
1707       pipeline);
1708   g_signal_connect (bus, "message::warning", (GCallback) message_received,
1709       pipeline);
1710   g_signal_connect (bus, "message::eos", (GCallback) msg_eos, pipeline);
1711   g_signal_connect (bus, "message::tag", (GCallback) message_received,
1712       pipeline);
1713   g_signal_connect (bus, "message::element", (GCallback) message_received,
1714       pipeline);
1715   g_signal_connect (bus, "message::segment-done", (GCallback) message_received,
1716       pipeline);
1717   g_signal_connect (bus, "message::buffering", (GCallback) msg_buffering,
1718       pipeline);
1719 //  g_signal_connect (bus, "message::step-done", (GCallback) msg_step_done,
1720 //      pipeline);
1721   g_signal_connect (bus, "message::step-start", (GCallback) msg_step_done,
1722       pipeline);
1723   g_signal_connect (bus, "sync-message::step-done",
1724       (GCallback) msg_sync_step_done, pipeline);
1725
1726   gst_object_unref (bus);
1727 }
1728
1729 /* Return GList of paths described in location string */
1730 static GList *
1731 handle_wildcards (const gchar * location)
1732 {
1733   GList *res = NULL;
1734   gchar *path = g_path_get_dirname (location);
1735   gchar *pattern = g_path_get_basename (location);
1736   GPatternSpec *pspec = g_pattern_spec_new (pattern);
1737   GDir *dir = g_dir_open (path, 0, NULL);
1738   const gchar *name;
1739
1740   g_print ("matching %s from %s\n", pattern, path);
1741
1742   if (!dir) {
1743     g_print ("opening directory %s failed\n", path);
1744     goto out;
1745   }
1746
1747   while ((name = g_dir_read_name (dir)) != NULL) {
1748     if (g_pattern_match_string (pspec, name)) {
1749       res = g_list_append (res, g_strjoin ("/", path, name, NULL));
1750       g_print ("  found clip %s\n", name);
1751     }
1752   }
1753
1754   g_dir_close (dir);
1755 out:
1756   g_pattern_spec_free (pspec);
1757   g_free (pattern);
1758   g_free (path);
1759
1760   return res;
1761 }
1762
1763 static void
1764 delete_event_cb (void)
1765 {
1766   stop_cb (NULL, NULL);
1767   gtk_main_quit ();
1768 }
1769
1770 static void
1771 print_usage (int argc, char **argv)
1772 {
1773   gint i;
1774
1775   g_print ("usage: %s <type> <filename>\n", argv[0]);
1776   g_print ("   possible types:\n");
1777
1778   for (i = 0; i < NUM_TYPES; i++) {
1779     g_print ("     %d = %s\n", i, pipelines[i].name);
1780   }
1781 }
1782
1783 int
1784 main (int argc, char **argv)
1785 {
1786   GtkWidget *window, *hbox, *vbox, *panel, *expander, *pb2vbox, *boxes,
1787       *flagtable, *boxes2, *step;
1788   GtkWidget *play_button, *pause_button, *stop_button, *shot_button;
1789   GtkWidget *accurate_checkbox, *key_checkbox, *loop_checkbox, *flush_checkbox;
1790   GtkWidget *scrub_checkbox, *play_scrub_checkbox;
1791   GtkWidget *rate_label, *volume_label;
1792   GOptionEntry options[] = {
1793     {"audiosink", '\0', 0, G_OPTION_ARG_STRING, &opt_audiosink_str,
1794         "audio sink to use (default: " DEFAULT_AUDIOSINK ")", NULL},
1795     {"stats", 's', 0, G_OPTION_ARG_NONE, &stats,
1796         "Show pad stats", NULL},
1797     {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
1798         "Verbose properties", NULL},
1799     {"videosink", '\0', 0, G_OPTION_ARG_STRING, &opt_videosink_str,
1800         "video sink to use (default: " DEFAULT_VIDEOSINK ")", NULL},
1801     {NULL}
1802   };
1803   GOptionContext *ctx;
1804   GError *err = NULL;
1805
1806   if (!g_thread_supported ())
1807     g_thread_init (NULL);
1808
1809   ctx = g_option_context_new ("- test seeking in gsteamer");
1810   g_option_context_add_main_entries (ctx, options, NULL);
1811   g_option_context_add_group (ctx, gst_init_get_option_group ());
1812   g_option_context_add_group (ctx, gtk_get_option_group (TRUE));
1813
1814   if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
1815     g_print ("Error initializing: %s\n", err->message);
1816     exit (1);
1817   }
1818
1819   if (opt_audiosink_str == NULL)
1820     opt_audiosink_str = g_strdup (DEFAULT_AUDIOSINK);
1821
1822   if (opt_videosink_str == NULL)
1823     opt_videosink_str = g_strdup (DEFAULT_VIDEOSINK);
1824
1825   GST_DEBUG_CATEGORY_INIT (seek_debug, "seek", 0, "seek example");
1826
1827   if (argc != 3) {
1828     print_usage (argc, argv);
1829     exit (-1);
1830   }
1831
1832   pipeline_type = atoi (argv[1]);
1833
1834   if (pipeline_type < 0 || pipeline_type >= NUM_TYPES) {
1835     print_usage (argc, argv);
1836     exit (-1);
1837   }
1838
1839   pipeline_spec = argv[2];
1840
1841   if (g_path_is_absolute (pipeline_spec) &&
1842       (g_strrstr (pipeline_spec, "*") != NULL ||
1843           g_strrstr (pipeline_spec, "?") != NULL)) {
1844     paths = handle_wildcards (pipeline_spec);
1845   } else {
1846     paths = g_list_prepend (paths, g_strdup (pipeline_spec));
1847   }
1848
1849   if (!paths) {
1850     g_print ("opening %s failed\n", pipeline_spec);
1851     exit (-1);
1852   }
1853
1854   l = paths;
1855
1856   pipeline = pipelines[pipeline_type].func ((gchar *) l->data);
1857   g_assert (pipeline);
1858
1859   /* initialize gui elements ... */
1860   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1861   video_window = gtk_drawing_area_new ();
1862   g_signal_connect (video_window, "expose-event",
1863       G_CALLBACK (handle_expose_cb), NULL);
1864   g_signal_connect (video_window, "realize", G_CALLBACK (realize_cb), NULL);
1865   gtk_widget_set_double_buffered (video_window, FALSE);
1866
1867   statusbar = gtk_statusbar_new ();
1868   status_id = gtk_statusbar_get_context_id (GTK_STATUSBAR (statusbar), "seek");
1869   gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Stopped");
1870   hbox = gtk_hbox_new (FALSE, 0);
1871   vbox = gtk_vbox_new (FALSE, 0);
1872   flagtable = gtk_table_new (4, 2, FALSE);
1873   gtk_container_set_border_width (GTK_CONTAINER (vbox), 3);
1874
1875   /* media controls */
1876   play_button = gtk_button_new_from_stock (GTK_STOCK_MEDIA_PLAY);
1877   pause_button = gtk_button_new_from_stock (GTK_STOCK_MEDIA_PAUSE);
1878   stop_button = gtk_button_new_from_stock (GTK_STOCK_MEDIA_STOP);
1879
1880   /* seek flags */
1881   accurate_checkbox = gtk_check_button_new_with_label ("Accurate Seek");
1882   key_checkbox = gtk_check_button_new_with_label ("Key-unit Seek");
1883   loop_checkbox = gtk_check_button_new_with_label ("Loop");
1884   flush_checkbox = gtk_check_button_new_with_label ("Flush");
1885   scrub_checkbox = gtk_check_button_new_with_label ("Scrub");
1886   play_scrub_checkbox = gtk_check_button_new_with_label ("Play Scrub");
1887   skip_checkbox = gtk_check_button_new_with_label ("Play Skip");
1888   rate_spinbutton = gtk_spin_button_new_with_range (-100, 100, 0.1);
1889   gtk_spin_button_set_digits (GTK_SPIN_BUTTON (rate_spinbutton), 3);
1890   rate_label = gtk_label_new ("Rate");
1891
1892   gtk_widget_set_tooltip_text (accurate_checkbox,
1893       "accurate position is requested, this might be considerably slower for some formats");
1894   gtk_widget_set_tooltip_text (key_checkbox,
1895       "seek to the nearest keyframe. This might be faster but less accurate");
1896   gtk_widget_set_tooltip_text (loop_checkbox, "loop playback");
1897   gtk_widget_set_tooltip_text (flush_checkbox, "flush pipeline after seeking");
1898   gtk_widget_set_tooltip_text (rate_spinbutton, "define the playback rate, "
1899       "negative value trigger reverse playback");
1900   gtk_widget_set_tooltip_text (scrub_checkbox, "show images while seeking");
1901   gtk_widget_set_tooltip_text (play_scrub_checkbox, "play video while seeking");
1902   gtk_widget_set_tooltip_text (skip_checkbox,
1903       "Skip frames while playing at high frame rates");
1904
1905   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (flush_checkbox), TRUE);
1906   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (scrub_checkbox), TRUE);
1907
1908   gtk_spin_button_set_value (GTK_SPIN_BUTTON (rate_spinbutton), rate);
1909
1910   /* step expander */
1911   {
1912     GtkWidget *hbox;
1913
1914     step = gtk_expander_new ("step options");
1915     hbox = gtk_hbox_new (FALSE, 0);
1916
1917     format_combo = gtk_combo_box_text_new ();
1918     gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (format_combo),
1919         "frames");
1920     gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (format_combo),
1921         "time (ms)");
1922     gtk_combo_box_set_active (GTK_COMBO_BOX (format_combo), 0);
1923     gtk_box_pack_start (GTK_BOX (hbox), format_combo, FALSE, FALSE, 2);
1924
1925     step_amount_spinbutton = gtk_spin_button_new_with_range (1, 1000, 1);
1926     gtk_spin_button_set_digits (GTK_SPIN_BUTTON (step_amount_spinbutton), 0);
1927     gtk_spin_button_set_value (GTK_SPIN_BUTTON (step_amount_spinbutton), 1.0);
1928     gtk_box_pack_start (GTK_BOX (hbox), step_amount_spinbutton, FALSE, FALSE,
1929         2);
1930
1931     step_rate_spinbutton = gtk_spin_button_new_with_range (0.0, 100, 0.1);
1932     gtk_spin_button_set_digits (GTK_SPIN_BUTTON (step_rate_spinbutton), 3);
1933     gtk_spin_button_set_value (GTK_SPIN_BUTTON (step_rate_spinbutton), 1.0);
1934     gtk_box_pack_start (GTK_BOX (hbox), step_rate_spinbutton, FALSE, FALSE, 2);
1935
1936     step_button = gtk_button_new_from_stock (GTK_STOCK_MEDIA_FORWARD);
1937     gtk_button_set_label (GTK_BUTTON (step_button), "Step");
1938     gtk_box_pack_start (GTK_BOX (hbox), step_button, FALSE, FALSE, 2);
1939
1940     g_signal_connect (G_OBJECT (step_button), "clicked", G_CALLBACK (step_cb),
1941         pipeline);
1942
1943     /* shuttle scale */
1944     shuttle_checkbox = gtk_check_button_new_with_label ("Shuttle");
1945     gtk_box_pack_start (GTK_BOX (hbox), shuttle_checkbox, FALSE, FALSE, 2);
1946     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (shuttle_checkbox), FALSE);
1947     g_signal_connect (shuttle_checkbox, "toggled", G_CALLBACK (shuttle_toggled),
1948         pipeline);
1949
1950     shuttle_adjustment =
1951         GTK_ADJUSTMENT (gtk_adjustment_new (0.0, -3.00, 4.0, 0.1, 1.0, 1.0));
1952     shuttle_hscale = gtk_hscale_new (shuttle_adjustment);
1953     gtk_scale_set_digits (GTK_SCALE (shuttle_hscale), 2);
1954     gtk_scale_set_value_pos (GTK_SCALE (shuttle_hscale), GTK_POS_TOP);
1955     g_signal_connect (shuttle_hscale, "value_changed",
1956         G_CALLBACK (shuttle_value_changed), pipeline);
1957     g_signal_connect (shuttle_hscale, "format_value",
1958         G_CALLBACK (shuttle_format_value), pipeline);
1959
1960     gtk_box_pack_start (GTK_BOX (hbox), shuttle_hscale, TRUE, TRUE, 2);
1961
1962     gtk_container_add (GTK_CONTAINER (step), hbox);
1963   }
1964
1965   /* seek bar */
1966   adjustment =
1967       GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.00, N_GRAD, 0.1, 1.0, 1.0));
1968   hscale = gtk_hscale_new (adjustment);
1969   gtk_scale_set_digits (GTK_SCALE (hscale), 2);
1970   gtk_scale_set_value_pos (GTK_SCALE (hscale), GTK_POS_RIGHT);
1971   gtk_range_set_show_fill_level (GTK_RANGE (hscale), TRUE);
1972   gtk_range_set_fill_level (GTK_RANGE (hscale), N_GRAD);
1973
1974   g_signal_connect (hscale, "button_press_event", G_CALLBACK (start_seek),
1975       pipeline);
1976   g_signal_connect (hscale, "button_release_event", G_CALLBACK (stop_seek),
1977       pipeline);
1978   g_signal_connect (hscale, "format_value", G_CALLBACK (format_value),
1979       pipeline);
1980
1981   if (pipeline_type == 0) {
1982     /* the playbin2 panel controls for the video/audio/subtitle tracks */
1983     panel = gtk_hbox_new (FALSE, 0);
1984     video_combo = gtk_combo_box_text_new ();
1985     audio_combo = gtk_combo_box_text_new ();
1986     text_combo = gtk_combo_box_text_new ();
1987     gtk_widget_set_sensitive (video_combo, FALSE);
1988     gtk_widget_set_sensitive (audio_combo, FALSE);
1989     gtk_widget_set_sensitive (text_combo, FALSE);
1990     gtk_box_pack_start (GTK_BOX (panel), video_combo, TRUE, TRUE, 2);
1991     gtk_box_pack_start (GTK_BOX (panel), audio_combo, TRUE, TRUE, 2);
1992     gtk_box_pack_start (GTK_BOX (panel), text_combo, TRUE, TRUE, 2);
1993     g_signal_connect (G_OBJECT (video_combo), "changed",
1994         G_CALLBACK (video_combo_cb), pipeline);
1995     g_signal_connect (G_OBJECT (audio_combo), "changed",
1996         G_CALLBACK (audio_combo_cb), pipeline);
1997     g_signal_connect (G_OBJECT (text_combo), "changed",
1998         G_CALLBACK (text_combo_cb), pipeline);
1999     /* playbin2 panel for flag checkboxes and volume/mute */
2000     boxes = gtk_hbox_new (FALSE, 0);
2001     vis_checkbox = gtk_check_button_new_with_label ("Vis");
2002     video_checkbox = gtk_check_button_new_with_label ("Video");
2003     audio_checkbox = gtk_check_button_new_with_label ("Audio");
2004     text_checkbox = gtk_check_button_new_with_label ("Text");
2005     mute_checkbox = gtk_check_button_new_with_label ("Mute");
2006     download_checkbox = gtk_check_button_new_with_label ("Download");
2007     buffer_checkbox = gtk_check_button_new_with_label ("Buffer");
2008     volume_label = gtk_label_new ("Volume");
2009     volume_spinbutton = gtk_spin_button_new_with_range (0, 10.0, 0.1);
2010     gtk_spin_button_set_value (GTK_SPIN_BUTTON (volume_spinbutton), 1.0);
2011     gtk_box_pack_start (GTK_BOX (boxes), video_checkbox, TRUE, TRUE, 2);
2012     gtk_box_pack_start (GTK_BOX (boxes), audio_checkbox, TRUE, TRUE, 2);
2013     gtk_box_pack_start (GTK_BOX (boxes), text_checkbox, TRUE, TRUE, 2);
2014     gtk_box_pack_start (GTK_BOX (boxes), vis_checkbox, TRUE, TRUE, 2);
2015     gtk_box_pack_start (GTK_BOX (boxes), mute_checkbox, TRUE, TRUE, 2);
2016     gtk_box_pack_start (GTK_BOX (boxes), download_checkbox, TRUE, TRUE, 2);
2017     gtk_box_pack_start (GTK_BOX (boxes), buffer_checkbox, TRUE, TRUE, 2);
2018     gtk_box_pack_start (GTK_BOX (boxes), volume_label, TRUE, TRUE, 2);
2019     gtk_box_pack_start (GTK_BOX (boxes), volume_spinbutton, TRUE, TRUE, 2);
2020     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (vis_checkbox), FALSE);
2021     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (audio_checkbox), TRUE);
2022     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (video_checkbox), TRUE);
2023     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (text_checkbox), TRUE);
2024     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (mute_checkbox), FALSE);
2025     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (download_checkbox), FALSE);
2026     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (buffer_checkbox), FALSE);
2027     g_signal_connect (G_OBJECT (vis_checkbox), "toggled",
2028         G_CALLBACK (vis_toggle_cb), pipeline);
2029     g_signal_connect (G_OBJECT (audio_checkbox), "toggled",
2030         G_CALLBACK (audio_toggle_cb), pipeline);
2031     g_signal_connect (G_OBJECT (video_checkbox), "toggled",
2032         G_CALLBACK (video_toggle_cb), pipeline);
2033     g_signal_connect (G_OBJECT (text_checkbox), "toggled",
2034         G_CALLBACK (text_toggle_cb), pipeline);
2035     g_signal_connect (G_OBJECT (mute_checkbox), "toggled",
2036         G_CALLBACK (mute_toggle_cb), pipeline);
2037     g_signal_connect (G_OBJECT (download_checkbox), "toggled",
2038         G_CALLBACK (download_toggle_cb), pipeline);
2039     g_signal_connect (G_OBJECT (buffer_checkbox), "toggled",
2040         G_CALLBACK (buffer_toggle_cb), pipeline);
2041     g_signal_connect (G_OBJECT (volume_spinbutton), "value_changed",
2042         G_CALLBACK (volume_spinbutton_changed_cb), pipeline);
2043     /* playbin2 panel for snapshot */
2044     boxes2 = gtk_hbox_new (FALSE, 0);
2045     shot_button = gtk_button_new_from_stock (GTK_STOCK_SAVE);
2046     gtk_widget_set_tooltip_text (shot_button,
2047         "save a screenshot .png in the current directory");
2048     g_signal_connect (G_OBJECT (shot_button), "clicked", G_CALLBACK (shot_cb),
2049         pipeline);
2050     vis_combo = gtk_combo_box_text_new ();
2051     g_signal_connect (G_OBJECT (vis_combo), "changed",
2052         G_CALLBACK (vis_combo_cb), pipeline);
2053     gtk_widget_set_sensitive (vis_combo, FALSE);
2054     gtk_box_pack_start (GTK_BOX (boxes2), shot_button, TRUE, TRUE, 2);
2055     gtk_box_pack_start (GTK_BOX (boxes2), vis_combo, TRUE, TRUE, 2);
2056
2057     /* fill the vis combo box and the array of factories */
2058     init_visualization_features ();
2059   } else {
2060     panel = boxes = boxes2 = NULL;
2061   }
2062
2063   /* do the packing stuff ... */
2064   gtk_window_set_default_size (GTK_WINDOW (window), 250, 96);
2065   /* FIXME: can we avoid this for audio only? */
2066   gtk_widget_set_size_request (GTK_WIDGET (video_window), -1,
2067       DEFAULT_VIDEO_HEIGHT);
2068   gtk_container_add (GTK_CONTAINER (window), vbox);
2069   gtk_box_pack_start (GTK_BOX (vbox), video_window, TRUE, TRUE, 2);
2070   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 2);
2071   gtk_box_pack_start (GTK_BOX (hbox), play_button, FALSE, FALSE, 2);
2072   gtk_box_pack_start (GTK_BOX (hbox), pause_button, FALSE, FALSE, 2);
2073   gtk_box_pack_start (GTK_BOX (hbox), stop_button, FALSE, FALSE, 2);
2074   gtk_box_pack_start (GTK_BOX (hbox), flagtable, FALSE, FALSE, 2);
2075   gtk_table_attach_defaults (GTK_TABLE (flagtable), accurate_checkbox, 0, 1, 0,
2076       1);
2077   gtk_table_attach_defaults (GTK_TABLE (flagtable), flush_checkbox, 1, 2, 0, 1);
2078   gtk_table_attach_defaults (GTK_TABLE (flagtable), loop_checkbox, 2, 3, 0, 1);
2079   gtk_table_attach_defaults (GTK_TABLE (flagtable), key_checkbox, 0, 1, 1, 2);
2080   gtk_table_attach_defaults (GTK_TABLE (flagtable), scrub_checkbox, 1, 2, 1, 2);
2081   gtk_table_attach_defaults (GTK_TABLE (flagtable), play_scrub_checkbox, 2, 3,
2082       1, 2);
2083   gtk_table_attach_defaults (GTK_TABLE (flagtable), skip_checkbox, 3, 4, 0, 1);
2084   gtk_table_attach_defaults (GTK_TABLE (flagtable), rate_label, 4, 5, 0, 1);
2085   gtk_table_attach_defaults (GTK_TABLE (flagtable), rate_spinbutton, 4, 5, 1,
2086       2);
2087   if (panel && boxes && boxes2) {
2088     expander = gtk_expander_new ("playbin2 options");
2089     pb2vbox = gtk_vbox_new (FALSE, 0);
2090     gtk_box_pack_start (GTK_BOX (pb2vbox), panel, FALSE, FALSE, 2);
2091     gtk_box_pack_start (GTK_BOX (pb2vbox), boxes, FALSE, FALSE, 2);
2092     gtk_box_pack_start (GTK_BOX (pb2vbox), boxes2, FALSE, FALSE, 2);
2093     gtk_container_add (GTK_CONTAINER (expander), pb2vbox);
2094     gtk_box_pack_start (GTK_BOX (vbox), expander, FALSE, FALSE, 2);
2095   }
2096   gtk_box_pack_start (GTK_BOX (vbox), step, FALSE, FALSE, 2);
2097   gtk_box_pack_start (GTK_BOX (vbox), hscale, FALSE, FALSE, 2);
2098   gtk_box_pack_start (GTK_BOX (vbox), statusbar, FALSE, FALSE, 2);
2099
2100   /* connect things ... */
2101   g_signal_connect (G_OBJECT (play_button), "clicked", G_CALLBACK (play_cb),
2102       pipeline);
2103   g_signal_connect (G_OBJECT (pause_button), "clicked", G_CALLBACK (pause_cb),
2104       pipeline);
2105   g_signal_connect (G_OBJECT (stop_button), "clicked", G_CALLBACK (stop_cb),
2106       pipeline);
2107   g_signal_connect (G_OBJECT (accurate_checkbox), "toggled",
2108       G_CALLBACK (accurate_toggle_cb), pipeline);
2109   g_signal_connect (G_OBJECT (key_checkbox), "toggled",
2110       G_CALLBACK (key_toggle_cb), pipeline);
2111   g_signal_connect (G_OBJECT (loop_checkbox), "toggled",
2112       G_CALLBACK (loop_toggle_cb), pipeline);
2113   g_signal_connect (G_OBJECT (flush_checkbox), "toggled",
2114       G_CALLBACK (flush_toggle_cb), pipeline);
2115   g_signal_connect (G_OBJECT (scrub_checkbox), "toggled",
2116       G_CALLBACK (scrub_toggle_cb), pipeline);
2117   g_signal_connect (G_OBJECT (play_scrub_checkbox), "toggled",
2118       G_CALLBACK (play_scrub_toggle_cb), pipeline);
2119   g_signal_connect (G_OBJECT (skip_checkbox), "toggled",
2120       G_CALLBACK (skip_toggle_cb), pipeline);
2121   g_signal_connect (G_OBJECT (rate_spinbutton), "value_changed",
2122       G_CALLBACK (rate_spinbutton_changed_cb), pipeline);
2123
2124   g_signal_connect (G_OBJECT (window), "delete-event", delete_event_cb, NULL);
2125
2126   /* show the gui. */
2127   gtk_widget_show_all (window);
2128
2129   /* realize window now so that the video window gets created and we can
2130    * obtain its XID before the pipeline is started up and the videosink
2131    * asks for the XID of the window to render onto */
2132   gtk_widget_realize (window);
2133
2134 #if defined (GDK_WINDOWING_X11) || defined (GDK_WINDOWING_WIN32)
2135   /* we should have the XID now */
2136   g_assert (embed_xid != 0);
2137 #endif
2138
2139   if (verbose) {
2140     g_signal_connect (pipeline, "deep_notify",
2141         G_CALLBACK (gst_object_default_deep_notify), NULL);
2142   }
2143
2144   connect_bus_signals (pipeline);
2145   gtk_main ();
2146
2147   g_print ("NULL pipeline\n");
2148   gst_element_set_state (pipeline, GST_STATE_NULL);
2149
2150   g_print ("free pipeline\n");
2151   gst_object_unref (pipeline);
2152
2153   g_list_foreach (paths, (GFunc) g_free, NULL);
2154   g_list_free (paths);
2155
2156   return 0;
2157 }