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