seek: Add support for changing the remaining playbin2 properties
[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  *               2012 Collabora Ltd.
8  *                 Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public
21  * License along with this library; if not, write to the
22  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23  * Boston, MA 02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29 /* FIXME 0.11: suppress warnings for deprecated API such as GStaticRecMutex
30  * with newer GTK versions (>= 3.3.0) */
31 #define GDK_DISABLE_DEPRECATION_WARNINGS
32 #define GLIB_DISABLE_DEPRECATION_WARNINGS
33
34 #include <stdlib.h>
35 #include <math.h>
36 #include <glib.h>
37 #include <gtk/gtk.h>
38 #include <gst/gst.h>
39 #include <string.h>
40
41 #include <gdk/gdk.h>
42 #if defined (GDK_WINDOWING_X11)
43 #include <gdk/gdkx.h>
44 #elif defined (GDK_WINDOWING_WIN32)
45 #include <gdk/gdkwin32.h>
46 #elif defined (GDK_WINDOWING_QUARTZ)
47 #include <gdk/gdkquartzwindow.h>
48 #endif
49
50 #include <gst/interfaces/xoverlay.h>
51 #include <gst/interfaces/navigation.h>
52 #include <gst/interfaces/colorbalance.h>
53
54 GST_DEBUG_CATEGORY_STATIC (seek_debug);
55 #define GST_CAT_DEFAULT (seek_debug)
56
57 /* Copied from gst-plugins-base/gst/playback/gstplay-enum.h */
58 typedef enum
59 {
60   GST_PLAY_FLAG_VIDEO = (1 << 0),
61   GST_PLAY_FLAG_AUDIO = (1 << 1),
62   GST_PLAY_FLAG_TEXT = (1 << 2),
63   GST_PLAY_FLAG_VIS = (1 << 3),
64   GST_PLAY_FLAG_SOFT_VOLUME = (1 << 4),
65   GST_PLAY_FLAG_NATIVE_AUDIO = (1 << 5),
66   GST_PLAY_FLAG_NATIVE_VIDEO = (1 << 6),
67   GST_PLAY_FLAG_DOWNLOAD = (1 << 7),
68   GST_PLAY_FLAG_BUFFERING = (1 << 8),
69   GST_PLAY_FLAG_DEINTERLACE = (1 << 9),
70   GST_PLAY_FLAG_SOFT_COLORBALANCE = (1 << 10)
71 } GstPlayFlags;
72
73 /* configuration */
74
75 #define FILL_INTERVAL 100
76 //#define UPDATE_INTERVAL 500
77 //#define UPDATE_INTERVAL 100
78 #define UPDATE_INTERVAL 40
79
80 /* number of milliseconds to play for after a seek */
81 #define SCRUB_TIME 100
82
83 /* timeout for gst_element_get_state() after a seek */
84 #define SEEK_TIMEOUT 40 * GST_MSECOND
85
86 #define DEFAULT_VIDEO_HEIGHT 300
87
88 /* the state to go to when stop is pressed */
89 #define STOP_STATE      GST_STATE_READY
90
91 #define N_GRAD 1000.0
92
93 /* we keep an array of the visualisation entries so that we can easily switch
94  * with the combo box index. */
95 typedef struct
96 {
97   GstElementFactory *factory;
98 } VisEntry;
99
100 typedef struct
101 {
102   /* GTK widgets */
103   GtkWidget *window;
104   GtkWidget *video_combo, *audio_combo, *text_combo, *vis_combo;
105   GtkWidget *video_window;
106
107   GtkWidget *vis_checkbox, *video_checkbox, *audio_checkbox;
108   GtkWidget *text_checkbox, *mute_checkbox, *volume_spinbutton;
109   GtkWidget *soft_volume_checkbox, *native_audio_checkbox,
110       *native_video_checkbox;
111   GtkWidget *download_checkbox, *buffering_checkbox, *deinterlace_checkbox;
112   GtkWidget *soft_colorbalance_checkbox;
113   GtkWidget *video_sink_entry, *audio_sink_entry, *text_sink_entry;
114   GtkWidget *buffer_size_entry, *buffer_duration_entry;
115   GtkWidget *ringbuffer_maxsize_entry, *connection_speed_entry;
116   GtkWidget *av_offset_entry, *subtitle_encoding_entry;
117   GtkWidget *subtitle_fontdesc_button;
118
119   GtkWidget *seek_format_combo, *seek_position_label, *seek_duration_label;
120   GtkWidget *seek_entry;
121
122   GtkWidget *seek_scale, *statusbar;
123   guint status_id;
124
125   GtkWidget *step_format_combo, *step_amount_spinbutton, *step_rate_spinbutton;
126   GtkWidget *shuttle_scale;
127
128   GtkWidget *contrast_scale, *brightness_scale, *hue_scale, *saturation_scale;
129
130   struct
131   {
132     GstNavigationCommand cmd;
133     GtkWidget *button;
134   } navigation_buttons[14];
135
136   guintptr embed_xid;
137
138   /* GStreamer pipeline */
139   GstElement *pipeline;
140
141   GstElement *navigation_element;
142   GstElement *colorbalance_element;
143   GstElement *xoverlay_element;
144
145   /* Settings */
146   gboolean accurate_seek;
147   gboolean keyframe_seek;
148   gboolean loop_seek;
149   gboolean flush_seek;
150   gboolean scrub;
151   gboolean play_scrub;
152   gboolean skip_seek;
153   gdouble rate;
154
155   /* From commandline parameters */
156   gboolean stats;
157   gboolean verbose;
158   const gchar *pipeline_spec;
159   gint pipeline_type;
160   GList *paths, *current_path;
161
162   gchar *audiosink_str, *videosink_str;
163
164   /* Internal state */
165   gint64 position, duration;
166
167   gboolean is_live;
168   gboolean buffering;
169   GstBufferingMode mode;
170   gint64 buffering_left;
171   GstState state;
172   guint update_id;
173   guint seek_timeout_id;
174   gulong changed_id;
175   guint fill_id;
176
177   gboolean need_streams;
178   gint n_video, n_audio, n_text;
179
180   GStaticMutex state_mutex;
181
182   GArray *vis_entries;          /* Array of VisEntry structs */
183
184   gboolean shuttling;
185   gdouble shuttle_rate;
186   gdouble play_rate;
187
188   const GstFormatDefinition *seek_format;
189   GList *formats;
190 } SeekApp;
191
192 static void clear_streams (SeekApp * app);
193 static void find_interface_elements (SeekApp * app);
194 static void volume_notify_cb (GstElement * pipeline, GParamSpec * arg,
195     SeekApp * app);
196 static void mute_notify_cb (GstElement * pipeline, GParamSpec * arg,
197     SeekApp * app);
198
199 static void video_sink_activate_cb (GtkEntry * entry, SeekApp * app);
200 static void text_sink_activate_cb (GtkEntry * entry, SeekApp * app);
201 static void audio_sink_activate_cb (GtkEntry * entry, SeekApp * app);
202 static void buffer_size_activate_cb (GtkEntry * entry, SeekApp * app);
203 static void buffer_duration_activate_cb (GtkEntry * entry, SeekApp * app);
204 static void ringbuffer_maxsize_activate_cb (GtkEntry * entry, SeekApp * app);
205 static void connection_speed_activate_cb (GtkEntry * entry, SeekApp * app);
206 static void av_offset_activate_cb (GtkEntry * entry, SeekApp * app);
207 static void subtitle_encoding_activate_cb (GtkEntry * entry, SeekApp * app);
208
209 /* pipeline construction */
210
211 static GstElement *
212 gst_element_factory_make_or_warn (const gchar * type, const gchar * name)
213 {
214   GstElement *element = gst_element_factory_make (type, name);
215
216 #ifndef GST_DISABLE_PARSE
217   if (!element) {
218     /* Try parsing it as a pipeline description */
219     element = gst_parse_bin_from_description (type, TRUE, NULL);
220     if (element) {
221       gst_element_set_name (element, name);
222     }
223   }
224 #endif
225
226   if (!element) {
227     g_warning ("Failed to create element %s of type %s", name, type);
228   }
229
230   return element;
231 }
232
233 static void
234 playbin_set_uri (GstElement * playbin, const gchar * location)
235 {
236   gchar *uri;
237
238   /* Add "file://" prefix for convenience */
239   if (g_str_has_prefix (location, "/") || !gst_uri_is_valid (location)) {
240     uri = gst_filename_to_uri (location, NULL);
241     g_print ("Setting URI: %s\n", uri);
242     g_object_set (G_OBJECT (playbin), "uri", uri, NULL);
243     g_free (uri);
244   } else {
245     g_print ("Setting URI: %s\n", location);
246     g_object_set (G_OBJECT (playbin), "uri", location, NULL);
247   }
248 }
249
250 static void
251 make_playbin2_pipeline (SeekApp * app, const gchar * location)
252 {
253   GstElement *pipeline;
254
255   app->pipeline = pipeline = gst_element_factory_make ("playbin2", "playbin2");
256   g_assert (pipeline);
257
258   playbin_set_uri (pipeline, location);
259
260   g_signal_connect (pipeline, "notify::volume", G_CALLBACK (volume_notify_cb),
261       app);
262   g_signal_connect (pipeline, "notify::mute", G_CALLBACK (mute_notify_cb), app);
263
264   app->navigation_element = GST_ELEMENT (gst_object_ref (pipeline));
265   app->colorbalance_element = GST_ELEMENT (gst_object_ref (pipeline));
266 }
267
268 #ifndef GST_DISABLE_PARSE
269 static void
270 make_parselaunch_pipeline (SeekApp * app, const gchar * description)
271 {
272   app->pipeline = gst_parse_launch (description, NULL);
273 }
274 #endif
275
276 typedef struct
277 {
278   const gchar *name;
279   void (*func) (SeekApp * app, const gchar * location);
280 }
281 Pipeline;
282
283 static const Pipeline pipelines[] = {
284   {"playbin2", make_playbin2_pipeline},
285 #ifndef GST_DISABLE_PARSE
286   {"parse-launch", make_parselaunch_pipeline},
287 #endif
288 };
289
290 /* ui callbacks and helpers */
291
292 static gchar *
293 format_value (GtkScale * scale, gdouble value, SeekApp * app)
294 {
295   gint64 real;
296   gint64 seconds;
297   gint64 subseconds;
298
299   real = value * app->duration / N_GRAD;
300   seconds = (gint64) real / GST_SECOND;
301   subseconds = (gint64) real / (GST_SECOND / N_GRAD);
302
303   return g_strdup_printf ("%02" G_GINT64_FORMAT ":%02" G_GINT64_FORMAT ":%02"
304       G_GINT64_FORMAT, seconds / 60, seconds % 60, subseconds % 100);
305 }
306
307 static gchar *
308 shuttle_format_value (GtkScale * scale, gdouble value)
309 {
310   return g_strdup_printf ("%0.*g", gtk_scale_get_digits (scale), value);
311 }
312
313 typedef struct
314 {
315   const gchar *name;
316   const GstFormat format;
317 }
318 seek_format;
319
320 static seek_format seek_formats[] = {
321   {"tim", GST_FORMAT_TIME},
322   {"byt", GST_FORMAT_BYTES},
323   {"buf", GST_FORMAT_BUFFERS},
324   {"def", GST_FORMAT_DEFAULT},
325   {NULL, 0},
326 };
327
328 static void
329 query_positions (SeekApp * app)
330 {
331   gint i = 0;
332
333   g_print ("positions %8.8s: ", GST_ELEMENT_NAME (app->pipeline));
334   while (seek_formats[i].name) {
335     gint64 position, total;
336     GstFormat format;
337
338     format = seek_formats[i].format;
339
340     if (gst_element_query_position (app->pipeline, &format, &position) &&
341         gst_element_query_duration (app->pipeline, &format, &total)) {
342       g_print ("%s %13" G_GINT64_FORMAT " / %13" G_GINT64_FORMAT " | ",
343           seek_formats[i].name, position, total);
344     } else {
345       g_print ("%s %13.13s / %13.13s | ", seek_formats[i].name, "*NA*", "*NA*");
346     }
347     i++;
348   }
349   g_print (" %s\n", GST_ELEMENT_NAME (app->pipeline));
350 }
351
352 static gboolean start_seek (GtkRange * range, GdkEventButton * event,
353     SeekApp * app);
354 static gboolean stop_seek (GtkRange * range, GdkEventButton * event,
355     SeekApp * app);
356 static void seek_cb (GtkRange * range, SeekApp * app);
357
358 static void
359 set_scale (SeekApp * app, gdouble value)
360 {
361   g_signal_handlers_block_by_func (app->seek_scale, start_seek, app);
362   g_signal_handlers_block_by_func (app->seek_scale, stop_seek, app);
363   g_signal_handlers_block_by_func (app->seek_scale, seek_cb, app);
364   gtk_range_set_value (GTK_RANGE (app->seek_scale), value);
365   g_signal_handlers_unblock_by_func (app->seek_scale, start_seek, app);
366   g_signal_handlers_unblock_by_func (app->seek_scale, stop_seek, app);
367   g_signal_handlers_unblock_by_func (app->seek_scale, seek_cb, app);
368   gtk_widget_queue_draw (app->seek_scale);
369 }
370
371 static gboolean
372 update_fill (SeekApp * app)
373 {
374   GstQuery *query;
375
376   query = gst_query_new_buffering (GST_FORMAT_PERCENT);
377   if (gst_element_query (app->pipeline, query)) {
378     gint64 start, stop, buffering_total;
379     GstFormat format;
380     gdouble fill;
381     gboolean busy;
382     gint percent;
383     GstBufferingMode mode;
384     gint avg_in, avg_out;
385     gint64 buffering_left;
386
387     gst_query_parse_buffering_percent (query, &busy, &percent);
388     gst_query_parse_buffering_range (query, &format, &start, &stop,
389         &buffering_total);
390     gst_query_parse_buffering_stats (query, &mode, &avg_in, &avg_out,
391         &buffering_left);
392
393     /* note that we could start the playback when buffering_left < remaining
394      * playback time */
395     GST_DEBUG ("buffering total %" G_GINT64_FORMAT " ms, left %"
396         G_GINT64_FORMAT " ms", buffering_total, buffering_left);
397     GST_DEBUG ("start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT,
398         start, stop);
399
400     if (stop != -1)
401       fill = N_GRAD * stop / GST_FORMAT_PERCENT_MAX;
402     else
403       fill = N_GRAD;
404
405     gtk_range_set_fill_level (GTK_RANGE (app->seek_scale), fill);
406   }
407   gst_query_unref (query);
408
409   return TRUE;
410 }
411
412 static gboolean
413 update_scale (SeekApp * app)
414 {
415   GstFormat format = GST_FORMAT_TIME;
416   gint64 seek_pos, seek_dur;
417   gchar *str;
418
419   //position = 0;
420   //duration = 0;
421
422   gst_element_query_position (app->pipeline, &format, &app->position);
423   gst_element_query_duration (app->pipeline, &format, &app->duration);
424
425   if (app->stats)
426     query_positions (app);
427
428   if (app->position >= app->duration)
429     app->duration = app->position;
430
431   if (app->duration > 0) {
432     set_scale (app, app->position * N_GRAD / app->duration);
433   }
434
435   if (app->seek_format) {
436     format = app->seek_format->value;
437     seek_pos = seek_dur = -1;
438     gst_element_query_position (app->pipeline, &format, &seek_pos);
439     gst_element_query_duration (app->pipeline, &format, &seek_dur);
440
441     str = g_strdup_printf ("%" G_GINT64_FORMAT, seek_pos);
442     gtk_label_set_text (GTK_LABEL (app->seek_position_label), str);
443     g_free (str);
444
445     str = g_strdup_printf ("%" G_GINT64_FORMAT, seek_dur);
446     gtk_label_set_text (GTK_LABEL (app->seek_duration_label), str);
447     g_free (str);
448   }
449
450   return TRUE;
451 }
452
453 static void set_update_scale (SeekApp * app, gboolean active);
454 static void set_update_fill (SeekApp * app, gboolean active);
455
456 static gboolean
457 end_scrub (SeekApp * app)
458 {
459   GST_DEBUG ("end scrub, PAUSE");
460   gst_element_set_state (app->pipeline, GST_STATE_PAUSED);
461   app->seek_timeout_id = 0;
462
463   return FALSE;
464 }
465
466 static gboolean
467 send_event (SeekApp * app, GstEvent * event)
468 {
469   gboolean res = FALSE;
470
471   GST_DEBUG ("send event on element %s", GST_ELEMENT_NAME (app->pipeline));
472   res = gst_element_send_event (app->pipeline, event);
473
474   return res;
475 }
476
477 static void
478 do_seek (SeekApp * app, GstFormat format, gint64 position)
479 {
480   gboolean res = FALSE;
481   GstEvent *s_event;
482   GstSeekFlags flags;
483
484   flags = 0;
485   if (app->flush_seek)
486     flags |= GST_SEEK_FLAG_FLUSH;
487   if (app->accurate_seek)
488     flags |= GST_SEEK_FLAG_ACCURATE;
489   if (app->keyframe_seek)
490     flags |= GST_SEEK_FLAG_KEY_UNIT;
491   if (app->loop_seek)
492     flags |= GST_SEEK_FLAG_SEGMENT;
493   if (app->skip_seek)
494     flags |= GST_SEEK_FLAG_SKIP;
495
496   if (app->rate >= 0) {
497     s_event = gst_event_new_seek (app->rate,
498         format, flags, GST_SEEK_TYPE_SET, position, GST_SEEK_TYPE_SET,
499         GST_CLOCK_TIME_NONE);
500     GST_DEBUG ("seek with rate %lf to %" GST_TIME_FORMAT " / %" GST_TIME_FORMAT,
501         app->rate, GST_TIME_ARGS (position), GST_TIME_ARGS (app->duration));
502   } else {
503     s_event = gst_event_new_seek (app->rate,
504         format, flags, GST_SEEK_TYPE_SET, G_GINT64_CONSTANT (0),
505         GST_SEEK_TYPE_SET, position);
506     GST_DEBUG ("seek with rate %lf to %" GST_TIME_FORMAT " / %" GST_TIME_FORMAT,
507         app->rate, GST_TIME_ARGS (0), GST_TIME_ARGS (position));
508   }
509
510   res = send_event (app, s_event);
511
512   if (res) {
513     if (app->flush_seek) {
514       gst_element_get_state (GST_ELEMENT (app->pipeline), NULL, NULL,
515           SEEK_TIMEOUT);
516     } else {
517       set_update_scale (app, TRUE);
518     }
519   } else {
520     g_print ("seek failed\n");
521     set_update_scale (app, TRUE);
522   }
523 }
524
525 static void
526 seek_cb (GtkRange * range, SeekApp * app)
527 {
528   gint64 real;
529   /* If the timer hasn't expired yet, then the pipeline is running */
530   if (app->play_scrub && app->seek_timeout_id != 0) {
531     GST_DEBUG ("do scrub seek, PAUSED");
532     gst_element_set_state (app->pipeline, GST_STATE_PAUSED);
533   }
534
535   real =
536       gtk_range_get_value (GTK_RANGE (app->seek_scale)) * app->duration /
537       N_GRAD;
538
539   GST_DEBUG ("value=%f, real=%" G_GINT64_FORMAT,
540       gtk_range_get_value (GTK_RANGE (app->seek_scale)), real);
541
542   GST_DEBUG ("do seek");
543   do_seek (app, GST_FORMAT_TIME, real);
544
545   if (app->play_scrub) {
546     GST_DEBUG ("do scrub seek, PLAYING");
547     gst_element_set_state (app->pipeline, GST_STATE_PLAYING);
548
549     if (app->seek_timeout_id == 0) {
550       app->seek_timeout_id =
551           g_timeout_add (SCRUB_TIME, (GSourceFunc) end_scrub, app);
552     }
553   }
554 }
555
556 static void
557 advanced_seek_button_cb (GtkButton * button, SeekApp * app)
558 {
559   GstFormat fmt;
560   gint64 pos;
561   const gchar *text;
562   gchar *endptr;
563
564   fmt = app->seek_format->value;
565
566   text = gtk_entry_get_text (GTK_ENTRY (app->seek_entry));
567
568   pos = g_ascii_strtoll (text, &endptr, 10);
569   if (endptr != text && pos != G_MAXINT64 && pos != G_MININT64) {
570     do_seek (app, fmt, pos);
571   }
572 }
573
574 static void
575 set_update_fill (SeekApp * app, gboolean active)
576 {
577   GST_DEBUG ("fill scale is %d", active);
578
579   if (active) {
580     if (app->fill_id == 0) {
581       app->fill_id =
582           g_timeout_add (FILL_INTERVAL, (GSourceFunc) update_fill, app);
583     }
584   } else {
585     if (app->fill_id) {
586       g_source_remove (app->fill_id);
587       app->fill_id = 0;
588     }
589   }
590 }
591
592 static void
593 set_update_scale (SeekApp * app, gboolean active)
594 {
595   GST_DEBUG ("update scale is %d", active);
596
597   if (active) {
598     if (app->update_id == 0) {
599       app->update_id =
600           g_timeout_add (UPDATE_INTERVAL, (GSourceFunc) update_scale, app);
601     }
602   } else {
603     if (app->update_id) {
604       g_source_remove (app->update_id);
605       app->update_id = 0;
606     }
607   }
608 }
609
610 static gboolean
611 start_seek (GtkRange * range, GdkEventButton * event, SeekApp * app)
612 {
613   if (event->type != GDK_BUTTON_PRESS)
614     return FALSE;
615
616   set_update_scale (app, FALSE);
617
618   if (app->state == GST_STATE_PLAYING && app->flush_seek && app->scrub) {
619     GST_DEBUG ("start scrub seek, PAUSE");
620     gst_element_set_state (app->pipeline, GST_STATE_PAUSED);
621   }
622
623   if (app->changed_id == 0 && app->flush_seek && app->scrub) {
624     app->changed_id =
625         g_signal_connect (app->seek_scale, "value-changed",
626         G_CALLBACK (seek_cb), app);
627   }
628
629   return FALSE;
630 }
631
632 static gboolean
633 stop_seek (GtkRange * range, GdkEventButton * event, SeekApp * app)
634 {
635   if (app->changed_id) {
636     g_signal_handler_disconnect (app->seek_scale, app->changed_id);
637     app->changed_id = 0;
638   }
639
640   if (!app->flush_seek || !app->scrub) {
641     gint64 real;
642
643     GST_DEBUG ("do final seek");
644     real =
645         gtk_range_get_value (GTK_RANGE (app->seek_scale)) * app->duration /
646         N_GRAD;
647     do_seek (app, GST_FORMAT_TIME, real);
648   }
649
650   if (app->seek_timeout_id != 0) {
651     g_source_remove (app->seek_timeout_id);
652     app->seek_timeout_id = 0;
653     /* Still scrubbing, so the pipeline is playing, see if we need PAUSED
654      * instead. */
655     if (app->state == GST_STATE_PAUSED) {
656       GST_DEBUG ("stop scrub seek, PAUSED");
657       gst_element_set_state (app->pipeline, GST_STATE_PAUSED);
658     }
659   } else {
660     if (app->state == GST_STATE_PLAYING) {
661       GST_DEBUG ("stop scrub seek, PLAYING");
662       gst_element_set_state (app->pipeline, GST_STATE_PLAYING);
663     }
664   }
665
666   return FALSE;
667 }
668
669 static void
670 play_cb (GtkButton * button, SeekApp * app)
671 {
672   GstStateChangeReturn ret;
673
674   if (app->state != GST_STATE_PLAYING) {
675     g_print ("PLAY pipeline\n");
676     gtk_statusbar_pop (GTK_STATUSBAR (app->statusbar), app->status_id);
677
678     if (app->pipeline_type == 0) {
679       video_sink_activate_cb (GTK_ENTRY (app->video_sink_entry), app);
680       audio_sink_activate_cb (GTK_ENTRY (app->audio_sink_entry), app);
681       text_sink_activate_cb (GTK_ENTRY (app->text_sink_entry), app);
682       buffer_size_activate_cb (GTK_ENTRY (app->buffer_size_entry), app);
683       buffer_duration_activate_cb (GTK_ENTRY (app->buffer_duration_entry), app);
684       ringbuffer_maxsize_activate_cb (GTK_ENTRY (app->ringbuffer_maxsize_entry),
685           app);
686       connection_speed_activate_cb (GTK_ENTRY (app->connection_speed_entry),
687           app);
688       av_offset_activate_cb (GTK_ENTRY (app->av_offset_entry), app);
689       subtitle_encoding_activate_cb (GTK_ENTRY (app->subtitle_encoding_entry),
690           app);
691     }
692
693     ret = gst_element_set_state (app->pipeline, GST_STATE_PLAYING);
694     switch (ret) {
695       case GST_STATE_CHANGE_FAILURE:
696         goto failed;
697       case GST_STATE_CHANGE_NO_PREROLL:
698         app->is_live = TRUE;
699         break;
700       default:
701         break;
702     }
703     app->state = GST_STATE_PLAYING;
704     gtk_statusbar_push (GTK_STATUSBAR (app->statusbar), app->status_id,
705         "Playing");
706   }
707
708   return;
709
710 failed:
711   {
712     g_print ("PLAY failed\n");
713     gtk_statusbar_push (GTK_STATUSBAR (app->statusbar), app->status_id,
714         "Play failed");
715   }
716 }
717
718 static void
719 pause_cb (GtkButton * button, SeekApp * app)
720 {
721   g_static_mutex_lock (&app->state_mutex);
722   if (app->state != GST_STATE_PAUSED) {
723     GstStateChangeReturn ret;
724
725     gtk_statusbar_pop (GTK_STATUSBAR (app->statusbar), app->status_id);
726     g_print ("PAUSE pipeline\n");
727     ret = gst_element_set_state (app->pipeline, GST_STATE_PAUSED);
728     switch (ret) {
729       case GST_STATE_CHANGE_FAILURE:
730         goto failed;
731       case GST_STATE_CHANGE_NO_PREROLL:
732         app->is_live = TRUE;
733         break;
734       default:
735         break;
736     }
737
738     app->state = GST_STATE_PAUSED;
739     gtk_statusbar_push (GTK_STATUSBAR (app->statusbar), app->status_id,
740         "Paused");
741   }
742   g_static_mutex_unlock (&app->state_mutex);
743
744   return;
745
746 failed:
747   {
748     g_static_mutex_unlock (&app->state_mutex);
749     g_print ("PAUSE failed\n");
750     gtk_statusbar_push (GTK_STATUSBAR (app->statusbar), app->status_id,
751         "Pause failed");
752   }
753 }
754
755 static void
756 stop_cb (GtkButton * button, SeekApp * app)
757 {
758   if (app->state != STOP_STATE) {
759     GstStateChangeReturn ret;
760     gint i;
761
762     g_print ("READY pipeline\n");
763     gtk_statusbar_pop (GTK_STATUSBAR (app->statusbar), app->status_id);
764
765     g_static_mutex_lock (&app->state_mutex);
766     ret = gst_element_set_state (app->pipeline, STOP_STATE);
767     if (ret == GST_STATE_CHANGE_FAILURE)
768       goto failed;
769
770     app->state = STOP_STATE;
771     gtk_statusbar_push (GTK_STATUSBAR (app->statusbar), app->status_id,
772         "Stopped");
773     gtk_widget_queue_draw (app->video_window);
774
775     app->is_live = FALSE;
776     app->buffering = FALSE;
777     set_update_scale (app, FALSE);
778     set_scale (app, 0.0);
779     set_update_fill (app, FALSE);
780
781     if (app->pipeline_type == 0)
782       clear_streams (app);
783     g_static_mutex_unlock (&app->state_mutex);
784
785     gtk_widget_set_sensitive (GTK_WIDGET (app->seek_scale), TRUE);
786     for (i = 0; i < G_N_ELEMENTS (app->navigation_buttons); i++)
787       gtk_widget_set_sensitive (app->navigation_buttons[i].button, FALSE);
788   }
789   return;
790
791 failed:
792   {
793     g_static_mutex_unlock (&app->state_mutex);
794     g_print ("STOP failed\n");
795     gtk_statusbar_push (GTK_STATUSBAR (app->statusbar), app->status_id,
796         "Stop failed");
797   }
798 }
799
800 static void
801 accurate_toggle_cb (GtkToggleButton * button, SeekApp * app)
802 {
803   app->accurate_seek = gtk_toggle_button_get_active (button);
804 }
805
806 static void
807 key_toggle_cb (GtkToggleButton * button, SeekApp * app)
808 {
809   app->keyframe_seek = gtk_toggle_button_get_active (button);
810 }
811
812 static void
813 loop_toggle_cb (GtkToggleButton * button, SeekApp * app)
814 {
815   app->loop_seek = gtk_toggle_button_get_active (button);
816   if (app->state == GST_STATE_PLAYING) {
817     gint64 real;
818
819     real =
820         gtk_range_get_value (GTK_RANGE (app->seek_scale)) * app->duration /
821         N_GRAD;
822     do_seek (app, GST_FORMAT_TIME, real);
823   }
824 }
825
826 static void
827 flush_toggle_cb (GtkToggleButton * button, SeekApp * app)
828 {
829   app->flush_seek = gtk_toggle_button_get_active (button);
830 }
831
832 static void
833 scrub_toggle_cb (GtkToggleButton * button, SeekApp * app)
834 {
835   app->scrub = gtk_toggle_button_get_active (button);
836 }
837
838 static void
839 play_scrub_toggle_cb (GtkToggleButton * button, SeekApp * app)
840 {
841   app->play_scrub = gtk_toggle_button_get_active (button);
842 }
843
844 static void
845 skip_toggle_cb (GtkToggleButton * button, SeekApp * app)
846 {
847   app->skip_seek = gtk_toggle_button_get_active (button);
848   if (app->state == GST_STATE_PLAYING) {
849     gint64 real;
850
851     real =
852         gtk_range_get_value (GTK_RANGE (app->seek_scale)) * app->duration /
853         N_GRAD;
854     do_seek (app, GST_FORMAT_TIME, real);
855   }
856 }
857
858 static void
859 rate_spinbutton_changed_cb (GtkSpinButton * button, SeekApp * app)
860 {
861   gboolean res = FALSE;
862   GstEvent *s_event;
863   GstSeekFlags flags;
864
865   app->rate = gtk_spin_button_get_value (button);
866
867   GST_DEBUG ("rate changed to %lf", app->rate);
868
869   flags = 0;
870   if (app->flush_seek)
871     flags |= GST_SEEK_FLAG_FLUSH;
872   if (app->loop_seek)
873     flags |= GST_SEEK_FLAG_SEGMENT;
874   if (app->accurate_seek)
875     flags |= GST_SEEK_FLAG_ACCURATE;
876   if (app->keyframe_seek)
877     flags |= GST_SEEK_FLAG_KEY_UNIT;
878   if (app->skip_seek)
879     flags |= GST_SEEK_FLAG_SKIP;
880
881   if (app->rate >= 0.0) {
882     s_event = gst_event_new_seek (app->rate,
883         GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, app->position,
884         GST_SEEK_TYPE_SET, GST_CLOCK_TIME_NONE);
885   } else {
886     s_event = gst_event_new_seek (app->rate,
887         GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, G_GINT64_CONSTANT (0),
888         GST_SEEK_TYPE_SET, app->position);
889   }
890
891   res = send_event (app, s_event);
892
893   if (res) {
894     if (app->flush_seek) {
895       gst_element_get_state (GST_ELEMENT (app->pipeline), NULL, NULL,
896           SEEK_TIMEOUT);
897     }
898   } else
899     g_print ("seek failed\n");
900 }
901
902 static void
903 update_flag (GstElement * pipeline, GstPlayFlags flag, gboolean state)
904 {
905   gint flags;
906
907   g_print ("%ssetting flag 0x%08x\n", (state ? "" : "un"), flag);
908
909   g_object_get (pipeline, "flags", &flags, NULL);
910   if (state)
911     flags |= flag;
912   else
913     flags &= ~(flag);
914   g_object_set (pipeline, "flags", flags, NULL);
915 }
916
917 static void
918 vis_toggle_cb (GtkToggleButton * button, SeekApp * app)
919 {
920   gboolean state;
921
922   state = gtk_toggle_button_get_active (button);
923   update_flag (app->pipeline, GST_PLAY_FLAG_VIS, state);
924   gtk_widget_set_sensitive (app->vis_combo, state);
925 }
926
927 static void
928 audio_toggle_cb (GtkToggleButton * button, SeekApp * app)
929 {
930   gboolean state;
931
932   state = gtk_toggle_button_get_active (button);
933   update_flag (app->pipeline, GST_PLAY_FLAG_AUDIO, state);
934   gtk_widget_set_sensitive (app->audio_combo, state);
935 }
936
937 static void
938 video_toggle_cb (GtkToggleButton * button, SeekApp * app)
939 {
940   gboolean state;
941
942   state = gtk_toggle_button_get_active (button);
943   update_flag (app->pipeline, GST_PLAY_FLAG_VIDEO, state);
944   gtk_widget_set_sensitive (app->video_combo, state);
945 }
946
947 static void
948 text_toggle_cb (GtkToggleButton * button, SeekApp * app)
949 {
950   gboolean state;
951
952   state = gtk_toggle_button_get_active (button);
953   update_flag (app->pipeline, GST_PLAY_FLAG_TEXT, state);
954   gtk_widget_set_sensitive (app->text_combo, state);
955 }
956
957 static void
958 mute_toggle_cb (GtkToggleButton * button, SeekApp * app)
959 {
960   gboolean mute;
961
962   mute = gtk_toggle_button_get_active (button);
963   g_object_set (app->pipeline, "mute", mute, NULL);
964 }
965
966 static void
967 download_toggle_cb (GtkToggleButton * button, SeekApp * app)
968 {
969   gboolean state;
970
971   state = gtk_toggle_button_get_active (button);
972   update_flag (app->pipeline, GST_PLAY_FLAG_DOWNLOAD, state);
973 }
974
975 static void
976 buffering_toggle_cb (GtkToggleButton * button, SeekApp * app)
977 {
978   gboolean state;
979
980   state = gtk_toggle_button_get_active (button);
981   update_flag (app->pipeline, GST_PLAY_FLAG_BUFFERING, state);
982 }
983
984 static void
985 soft_volume_toggle_cb (GtkToggleButton * button, SeekApp * app)
986 {
987   gboolean state;
988
989   state = gtk_toggle_button_get_active (button);
990   update_flag (app->pipeline, GST_PLAY_FLAG_SOFT_VOLUME, state);
991 }
992
993 static void
994 native_audio_toggle_cb (GtkToggleButton * button, SeekApp * app)
995 {
996   gboolean state;
997
998   state = gtk_toggle_button_get_active (button);
999   update_flag (app->pipeline, GST_PLAY_FLAG_NATIVE_AUDIO, state);
1000 }
1001
1002 static void
1003 native_video_toggle_cb (GtkToggleButton * button, SeekApp * app)
1004 {
1005   gboolean state;
1006
1007   state = gtk_toggle_button_get_active (button);
1008   update_flag (app->pipeline, GST_PLAY_FLAG_NATIVE_VIDEO, state);
1009 }
1010
1011 static void
1012 deinterlace_toggle_cb (GtkToggleButton * button, SeekApp * app)
1013 {
1014   gboolean state;
1015
1016   state = gtk_toggle_button_get_active (button);
1017   update_flag (app->pipeline, GST_PLAY_FLAG_DEINTERLACE, state);
1018 }
1019
1020 static void
1021 soft_colorbalance_toggle_cb (GtkToggleButton * button, SeekApp * app)
1022 {
1023   gboolean state;
1024
1025   state = gtk_toggle_button_get_active (button);
1026   update_flag (app->pipeline, GST_PLAY_FLAG_SOFT_COLORBALANCE, state);
1027 }
1028
1029 static void
1030 clear_streams (SeekApp * app)
1031 {
1032   gint i;
1033
1034   /* remove previous info */
1035   for (i = 0; i < app->n_video; i++)
1036     gtk_combo_box_text_remove (GTK_COMBO_BOX_TEXT (app->video_combo), 0);
1037   for (i = 0; i < app->n_audio; i++)
1038     gtk_combo_box_text_remove (GTK_COMBO_BOX_TEXT (app->audio_combo), 0);
1039   for (i = 0; i < app->n_text; i++)
1040     gtk_combo_box_text_remove (GTK_COMBO_BOX_TEXT (app->text_combo), 0);
1041
1042   app->n_audio = app->n_video = app->n_text = 0;
1043   gtk_widget_set_sensitive (app->video_combo, FALSE);
1044   gtk_widget_set_sensitive (app->audio_combo, FALSE);
1045   gtk_widget_set_sensitive (app->text_combo, FALSE);
1046
1047   app->need_streams = TRUE;
1048 }
1049
1050 static void
1051 update_streams (SeekApp * app)
1052 {
1053   gint i;
1054
1055   if (app->pipeline_type == 0 && app->need_streams) {
1056     GstTagList *tags;
1057     gchar *name, *str;
1058     gint active_idx;
1059     gboolean state;
1060
1061     /* remove previous info */
1062     clear_streams (app);
1063
1064     /* here we get and update the different streams detected by playbin2 */
1065     g_object_get (app->pipeline, "n-video", &app->n_video, NULL);
1066     g_object_get (app->pipeline, "n-audio", &app->n_audio, NULL);
1067     g_object_get (app->pipeline, "n-text", &app->n_text, NULL);
1068
1069     g_print ("video %d, audio %d, text %d\n", app->n_video, app->n_audio,
1070         app->n_text);
1071
1072     active_idx = 0;
1073     for (i = 0; i < app->n_video; i++) {
1074       g_signal_emit_by_name (app->pipeline, "get-video-tags", i, &tags);
1075       if (tags) {
1076         str = gst_structure_to_string ((GstStructure *) tags);
1077         g_print ("video %d: %s\n", i, str);
1078         g_free (str);
1079       }
1080       /* find good name for the label */
1081       name = g_strdup_printf ("video %d", i + 1);
1082       gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (app->video_combo),
1083           name);
1084       g_free (name);
1085     }
1086     state =
1087         gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (app->video_checkbox));
1088     gtk_widget_set_sensitive (app->video_combo, state && app->n_video > 0);
1089     gtk_combo_box_set_active (GTK_COMBO_BOX (app->video_combo), active_idx);
1090
1091     active_idx = 0;
1092     for (i = 0; i < app->n_audio; i++) {
1093       g_signal_emit_by_name (app->pipeline, "get-audio-tags", i, &tags);
1094       if (tags) {
1095         str = gst_structure_to_string ((GstStructure *) tags);
1096         g_print ("audio %d: %s\n", i, str);
1097         g_free (str);
1098       }
1099       /* find good name for the label */
1100       name = g_strdup_printf ("audio %d", i + 1);
1101       gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (app->audio_combo),
1102           name);
1103       g_free (name);
1104     }
1105     state =
1106         gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (app->audio_checkbox));
1107     gtk_widget_set_sensitive (app->audio_combo, state && app->n_audio > 0);
1108     gtk_combo_box_set_active (GTK_COMBO_BOX (app->audio_combo), active_idx);
1109
1110     active_idx = 0;
1111     for (i = 0; i < app->n_text; i++) {
1112       g_signal_emit_by_name (app->pipeline, "get-text-tags", i, &tags);
1113
1114       name = NULL;
1115       if (tags) {
1116         const GValue *value;
1117
1118         str = gst_structure_to_string ((GstStructure *) tags);
1119         g_print ("text %d: %s\n", i, str);
1120         g_free (str);
1121
1122         /* get the language code if we can */
1123         value = gst_tag_list_get_value_index (tags, GST_TAG_LANGUAGE_CODE, 0);
1124         if (value && G_VALUE_HOLDS_STRING (value)) {
1125           name = g_strdup_printf ("text %s", g_value_get_string (value));
1126         }
1127       }
1128       /* find good name for the label if we didn't use a tag */
1129       if (name == NULL)
1130         name = g_strdup_printf ("text %d", i + 1);
1131
1132       gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (app->text_combo),
1133           name);
1134       g_free (name);
1135     }
1136     state =
1137         gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (app->text_checkbox));
1138     gtk_widget_set_sensitive (app->text_combo, state && app->n_text > 0);
1139     gtk_combo_box_set_active (GTK_COMBO_BOX (app->text_combo), active_idx);
1140
1141     app->need_streams = FALSE;
1142   }
1143 }
1144
1145 static void
1146 video_combo_cb (GtkComboBox * combo, SeekApp * app)
1147 {
1148   gint active;
1149
1150   active = gtk_combo_box_get_active (combo);
1151
1152   g_print ("setting current video track %d\n", active);
1153   g_object_set (app->pipeline, "current-video", active, NULL);
1154 }
1155
1156 static void
1157 audio_combo_cb (GtkComboBox * combo, SeekApp * app)
1158 {
1159   gint active;
1160
1161   active = gtk_combo_box_get_active (combo);
1162
1163   g_print ("setting current audio track %d\n", active);
1164   g_object_set (app->pipeline, "current-audio", active, NULL);
1165 }
1166
1167 static void
1168 text_combo_cb (GtkComboBox * combo, SeekApp * app)
1169 {
1170   gint active;
1171
1172   active = gtk_combo_box_get_active (combo);
1173
1174   g_print ("setting current text track %d\n", active);
1175   g_object_set (app->pipeline, "current-text", active, NULL);
1176 }
1177
1178 static gboolean
1179 filter_vis_features (GstPluginFeature * feature, gpointer data)
1180 {
1181   GstElementFactory *f;
1182
1183   if (!GST_IS_ELEMENT_FACTORY (feature))
1184     return FALSE;
1185   f = GST_ELEMENT_FACTORY (feature);
1186   if (!g_strrstr (gst_element_factory_get_klass (f), "Visualization"))
1187     return FALSE;
1188
1189   return TRUE;
1190 }
1191
1192 static void
1193 init_visualization_features (SeekApp * app)
1194 {
1195   GList *list, *walk;
1196
1197   app->vis_entries = g_array_new (FALSE, FALSE, sizeof (VisEntry));
1198
1199   list = gst_registry_feature_filter (gst_registry_get_default (),
1200       filter_vis_features, FALSE, NULL);
1201
1202   for (walk = list; walk; walk = g_list_next (walk)) {
1203     VisEntry entry;
1204     const gchar *name;
1205
1206     entry.factory = GST_ELEMENT_FACTORY (walk->data);
1207     name = gst_element_factory_get_longname (entry.factory);
1208
1209     g_array_append_val (app->vis_entries, entry);
1210     gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (app->vis_combo), name);
1211   }
1212   gtk_combo_box_set_active (GTK_COMBO_BOX (app->vis_combo), 0);
1213 }
1214
1215 static void
1216 vis_combo_cb (GtkComboBox * combo, SeekApp * app)
1217 {
1218   guint index;
1219   VisEntry *entry;
1220   GstElement *element;
1221
1222   /* get the selected index and get the factory for this index */
1223   index = gtk_combo_box_get_active (GTK_COMBO_BOX (app->vis_combo));
1224   if (app->vis_entries->len > 0) {
1225     entry = &g_array_index (app->vis_entries, VisEntry, index);
1226
1227     /* create an instance of the element from the factory */
1228     element = gst_element_factory_create (entry->factory, NULL);
1229     if (!element)
1230       return;
1231
1232     /* set vis plugin for playbin2 */
1233     g_object_set (app->pipeline, "vis-plugin", element, NULL);
1234   }
1235 }
1236
1237 static void
1238 volume_spinbutton_changed_cb (GtkSpinButton * button, SeekApp * app)
1239 {
1240   gdouble volume;
1241
1242   volume = gtk_spin_button_get_value (button);
1243
1244   g_object_set (app->pipeline, "volume", volume, NULL);
1245 }
1246
1247 static gboolean
1248 volume_notify_idle_cb (SeekApp * app)
1249 {
1250   gdouble cur_volume, new_volume;
1251
1252   g_object_get (app->pipeline, "volume", &new_volume, NULL);
1253   cur_volume =
1254       gtk_spin_button_get_value (GTK_SPIN_BUTTON (app->volume_spinbutton));
1255   if (fabs (cur_volume - new_volume) > 0.001) {
1256     g_signal_handlers_block_by_func (app->volume_spinbutton,
1257         volume_spinbutton_changed_cb, app);
1258     gtk_spin_button_set_value (GTK_SPIN_BUTTON (app->volume_spinbutton),
1259         new_volume);
1260     g_signal_handlers_unblock_by_func (app->volume_spinbutton,
1261         volume_spinbutton_changed_cb, app);
1262   }
1263
1264   return FALSE;
1265 }
1266
1267 static void
1268 volume_notify_cb (GstElement * pipeline, GParamSpec * arg, SeekApp * app)
1269 {
1270   /* Do this from the main thread */
1271   g_idle_add ((GSourceFunc) volume_notify_idle_cb, app);
1272 }
1273
1274 static gboolean
1275 mute_notify_idle_cb (SeekApp * app)
1276 {
1277   gboolean cur_mute, new_mute;
1278
1279   g_object_get (app->pipeline, "mute", &new_mute, NULL);
1280   cur_mute =
1281       gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (app->mute_checkbox));
1282   if (cur_mute != new_mute) {
1283     g_signal_handlers_block_by_func (app->mute_checkbox, mute_toggle_cb, app);
1284     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (app->mute_checkbox),
1285         new_mute);
1286     g_signal_handlers_unblock_by_func (app->mute_checkbox, mute_toggle_cb, app);
1287   }
1288
1289   return FALSE;
1290 }
1291
1292 static void
1293 mute_notify_cb (GstElement * pipeline, GParamSpec * arg, SeekApp * app)
1294 {
1295   /* Do this from the main thread */
1296   g_idle_add ((GSourceFunc) mute_notify_idle_cb, app);
1297 }
1298
1299 static void
1300 shot_cb (GtkButton * button, SeekApp * app)
1301 {
1302   GstBuffer *buffer;
1303   GstCaps *caps;
1304
1305   /* convert to our desired format (RGB24) */
1306   caps = gst_caps_new_simple ("video/x-raw-rgb",
1307       "bpp", G_TYPE_INT, 24, "depth", G_TYPE_INT, 24,
1308       /* Note: we don't ask for a specific width/height here, so that
1309        * videoscale can adjust dimensions from a non-1/1 pixel aspect
1310        * ratio to a 1/1 pixel-aspect-ratio */
1311       "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
1312       "endianness", G_TYPE_INT, G_BIG_ENDIAN,
1313       "red_mask", G_TYPE_INT, 0xff0000,
1314       "green_mask", G_TYPE_INT, 0x00ff00,
1315       "blue_mask", G_TYPE_INT, 0x0000ff, NULL);
1316
1317   /* convert the latest frame to the requested format */
1318   g_signal_emit_by_name (app->pipeline, "convert-frame", caps, &buffer);
1319   gst_caps_unref (caps);
1320
1321   if (buffer) {
1322     GstCaps *caps;
1323     GstStructure *s;
1324     gboolean res;
1325     gint width, height;
1326     GdkPixbuf *pixbuf;
1327     GError *error = NULL;
1328
1329     /* get the snapshot buffer format now. We set the caps on the appsink so
1330      * that it can only be an rgb buffer. The only thing we have not specified
1331      * on the caps is the height, which is dependant on the pixel-aspect-ratio
1332      * of the source material */
1333     caps = GST_BUFFER_CAPS (buffer);
1334     if (!caps) {
1335       g_warning ("could not get snapshot format\n");
1336       goto done;
1337     }
1338     s = gst_caps_get_structure (caps, 0);
1339
1340     /* we need to get the final caps on the buffer to get the size */
1341     res = gst_structure_get_int (s, "width", &width);
1342     res |= gst_structure_get_int (s, "height", &height);
1343     if (!res) {
1344       g_warning ("could not get snapshot dimension\n");
1345       goto done;
1346     }
1347
1348     /* create pixmap from buffer and save, gstreamer video buffers have a stride
1349      * that is rounded up to the nearest multiple of 4 */
1350     pixbuf = gdk_pixbuf_new_from_data (GST_BUFFER_DATA (buffer),
1351         GDK_COLORSPACE_RGB, FALSE, 8, width, height,
1352         GST_ROUND_UP_4 (width * 3), NULL, NULL);
1353
1354     /* save the pixbuf */
1355     gdk_pixbuf_save (pixbuf, "snapshot.png", "png", &error, NULL);
1356
1357   done:
1358     gst_buffer_unref (buffer);
1359   }
1360 }
1361
1362 /* called when the Step button is pressed */
1363 static void
1364 step_cb (GtkButton * button, SeekApp * app)
1365 {
1366   GstEvent *event;
1367   GstFormat format;
1368   guint64 amount;
1369   gdouble rate;
1370   gboolean flush, res;
1371   gint active;
1372
1373   active = gtk_combo_box_get_active (GTK_COMBO_BOX (app->step_format_combo));
1374   amount =
1375       gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
1376       (app->step_amount_spinbutton));
1377   rate =
1378       gtk_spin_button_get_value (GTK_SPIN_BUTTON (app->step_rate_spinbutton));
1379   flush = TRUE;
1380
1381   switch (active) {
1382     case 0:
1383       format = GST_FORMAT_BUFFERS;
1384       break;
1385     case 1:
1386       format = GST_FORMAT_TIME;
1387       amount *= GST_MSECOND;
1388       break;
1389     default:
1390       format = GST_FORMAT_UNDEFINED;
1391       break;
1392   }
1393
1394   event = gst_event_new_step (format, amount, rate, flush, FALSE);
1395
1396   res = send_event (app, event);
1397
1398   if (!res) {
1399     g_print ("Sending step event failed\n");
1400   }
1401 }
1402
1403 static void
1404 message_received (GstBus * bus, GstMessage * message, SeekApp * app)
1405 {
1406   const GstStructure *s;
1407
1408   switch (GST_MESSAGE_TYPE (message)) {
1409     case GST_MESSAGE_ERROR:
1410       GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (app->pipeline),
1411           GST_DEBUG_GRAPH_SHOW_ALL, "seek.error");
1412       break;
1413     case GST_MESSAGE_WARNING:
1414       GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (app->pipeline),
1415           GST_DEBUG_GRAPH_SHOW_ALL, "seek.warning");
1416       break;
1417     default:
1418       break;
1419   }
1420
1421   s = gst_message_get_structure (message);
1422   g_print ("message from \"%s\" (%s): ",
1423       GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))),
1424       gst_message_type_get_name (GST_MESSAGE_TYPE (message)));
1425   if (s) {
1426     gchar *sstr;
1427
1428     sstr = gst_structure_to_string (s);
1429     g_print ("%s\n", sstr);
1430     g_free (sstr);
1431   } else {
1432     g_print ("no message details\n");
1433   }
1434 }
1435
1436 static void
1437 do_shuttle (SeekApp * app)
1438 {
1439   guint64 duration;
1440
1441   if (app->shuttling)
1442     duration = 40 * GST_MSECOND;
1443   else
1444     duration = -1;
1445
1446   gst_element_send_event (app->pipeline,
1447       gst_event_new_step (GST_FORMAT_TIME, duration, app->shuttle_rate, FALSE,
1448           FALSE));
1449 }
1450
1451 static void
1452 msg_sync_step_done (GstBus * bus, GstMessage * message, SeekApp * app)
1453 {
1454   GstFormat format;
1455   guint64 amount;
1456   gdouble rate;
1457   gboolean flush;
1458   gboolean intermediate;
1459   guint64 duration;
1460   gboolean eos;
1461
1462   gst_message_parse_step_done (message, &format, &amount, &rate, &flush,
1463       &intermediate, &duration, &eos);
1464
1465   if (eos) {
1466     g_print ("stepped till EOS\n");
1467     return;
1468   }
1469
1470   if (g_static_mutex_trylock (&app->state_mutex)) {
1471     if (app->shuttling)
1472       do_shuttle (app);
1473     g_static_mutex_unlock (&app->state_mutex);
1474   } else {
1475     /* ignore step messages that come while we are doing a state change */
1476     g_print ("state change is busy\n");
1477   }
1478 }
1479
1480 static void
1481 shuttle_toggled (GtkToggleButton * button, SeekApp * app)
1482 {
1483   gboolean active;
1484
1485   active = gtk_toggle_button_get_active (button);
1486
1487   if (active != app->shuttling) {
1488     app->shuttling = active;
1489     g_print ("shuttling %s\n", app->shuttling ? "active" : "inactive");
1490     if (active) {
1491       app->shuttle_rate = 0.0;
1492       app->play_rate = 1.0;
1493       pause_cb (NULL, app);
1494       gst_element_get_state (app->pipeline, NULL, NULL, -1);
1495     }
1496   }
1497 }
1498
1499 static void
1500 shuttle_rate_switch (SeekApp * app)
1501 {
1502   GstSeekFlags flags;
1503   GstEvent *s_event;
1504   gboolean res;
1505
1506   if (app->state == GST_STATE_PLAYING) {
1507     /* pause when we need to */
1508     pause_cb (NULL, app);
1509     gst_element_get_state (app->pipeline, NULL, NULL, -1);
1510   }
1511
1512   if (app->play_rate == 1.0)
1513     app->play_rate = -1.0;
1514   else
1515     app->play_rate = 1.0;
1516
1517   g_print ("rate changed to %lf %" GST_TIME_FORMAT "\n", app->play_rate,
1518       GST_TIME_ARGS (app->position));
1519
1520   flags = GST_SEEK_FLAG_FLUSH;
1521   flags |= GST_SEEK_FLAG_ACCURATE;
1522
1523   if (app->play_rate >= 0.0) {
1524     s_event = gst_event_new_seek (app->play_rate,
1525         GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, app->position,
1526         GST_SEEK_TYPE_SET, GST_CLOCK_TIME_NONE);
1527   } else {
1528     s_event = gst_event_new_seek (app->play_rate,
1529         GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, G_GINT64_CONSTANT (0),
1530         GST_SEEK_TYPE_SET, app->position);
1531   }
1532   res = send_event (app, s_event);
1533   if (res) {
1534     gst_element_get_state (app->pipeline, NULL, NULL, SEEK_TIMEOUT);
1535   } else {
1536     g_print ("seek failed\n");
1537   }
1538 }
1539
1540 static void
1541 shuttle_value_changed (GtkRange * range, SeekApp * app)
1542 {
1543   gdouble rate;
1544
1545   rate = gtk_range_get_value (range);
1546
1547   if (rate == 0.0) {
1548     g_print ("rate 0.0, pause\n");
1549     pause_cb (NULL, app);
1550     gst_element_get_state (app->pipeline, NULL, NULL, -1);
1551   } else {
1552     g_print ("rate changed %0.3g\n", rate);
1553
1554     if ((rate < 0.0 && app->play_rate > 0.0) || (rate > 0.0
1555             && app->play_rate < 0.0)) {
1556       shuttle_rate_switch (app);
1557     }
1558
1559     app->shuttle_rate = ABS (rate);
1560     if (app->state != GST_STATE_PLAYING) {
1561       do_shuttle (app);
1562       play_cb (NULL, app);
1563     }
1564   }
1565 }
1566
1567 static void
1568 colorbalance_value_changed (GtkRange * range, SeekApp * app)
1569 {
1570   const gchar *label;
1571   gdouble val;
1572   gint ival;
1573   GstColorBalanceChannel *channel = NULL;
1574   const GList *channels, *l;
1575
1576   if (range == GTK_RANGE (app->contrast_scale))
1577     label = "CONTRAST";
1578   else if (range == GTK_RANGE (app->brightness_scale))
1579     label = "BRIGHTNESS";
1580   else if (range == GTK_RANGE (app->hue_scale))
1581     label = "HUE";
1582   else if (range == GTK_RANGE (app->saturation_scale))
1583     label = "SATURATION";
1584   else
1585     g_assert_not_reached ();
1586
1587   val = gtk_range_get_value (range);
1588
1589   g_print ("colorbalance %s value changed %lf\n", label, val / N_GRAD);
1590
1591   if (!app->colorbalance_element) {
1592     find_interface_elements (app);
1593     if (!app->colorbalance_element)
1594       return;
1595   }
1596
1597   channels =
1598       gst_color_balance_list_channels (GST_COLOR_BALANCE
1599       (app->colorbalance_element));
1600   for (l = channels; l; l = l->next) {
1601     GstColorBalanceChannel *tmp = l->data;
1602
1603     if (g_strrstr (tmp->label, label)) {
1604       channel = tmp;
1605       break;
1606     }
1607   }
1608
1609   if (!channel)
1610     return;
1611
1612   ival =
1613       (gint) (0.5 + channel->min_value +
1614       (val / N_GRAD) * ((gdouble) channel->max_value -
1615           (gdouble) channel->min_value));
1616   gst_color_balance_set_value (GST_COLOR_BALANCE (app->colorbalance_element),
1617       channel, ival);
1618 }
1619
1620 static void
1621 seek_format_changed_cb (GtkComboBox * box, SeekApp * app)
1622 {
1623   gchar *format_str;
1624   GList *l;
1625   const GstFormatDefinition *format = NULL;
1626
1627   format_str = gtk_combo_box_text_get_active_text (GTK_COMBO_BOX_TEXT (box));
1628
1629   for (l = app->formats; l; l = l->next) {
1630     const GstFormatDefinition *tmp = l->data;
1631
1632     if (g_strcmp0 (tmp->nick, format_str) == 0) {
1633       format = tmp;
1634       break;
1635     }
1636   }
1637
1638   if (!format)
1639     goto done;
1640
1641   app->seek_format = format;
1642   update_scale (app);
1643
1644 done:
1645   g_free (format_str);
1646 }
1647
1648 static void
1649 update_formats (SeekApp * app)
1650 {
1651   GstIterator *it;
1652   gboolean done;
1653   GList *l;
1654   gpointer item;
1655   gchar *selected;
1656   gint selected_idx = 0, i;
1657
1658   selected =
1659       gtk_combo_box_text_get_active_text (GTK_COMBO_BOX_TEXT
1660       (app->seek_format_combo));
1661   if (selected == NULL)
1662     selected = g_strdup ("time");
1663
1664   it = gst_format_iterate_definitions ();
1665   done = FALSE;
1666
1667   g_list_free (app->formats);
1668   app->formats = NULL;
1669
1670   while (!done) {
1671     switch (gst_iterator_next (it, &item)) {
1672       case GST_ITERATOR_OK:
1673         app->formats = g_list_prepend (app->formats, item);
1674         break;
1675       case GST_ITERATOR_RESYNC:
1676         g_list_free (app->formats);
1677         app->formats = NULL;
1678         gst_iterator_resync (it);
1679         break;
1680       case GST_ITERATOR_ERROR:
1681       case GST_ITERATOR_DONE:
1682       default:
1683         done = TRUE;
1684         break;
1685     }
1686   }
1687
1688   app->formats = g_list_reverse (app->formats);
1689   gst_iterator_free (it);
1690
1691   g_signal_handlers_block_by_func (app->seek_format_combo,
1692       seek_format_changed_cb, app);
1693   gtk_combo_box_text_remove_all (GTK_COMBO_BOX_TEXT (app->seek_format_combo));
1694
1695   for (i = 0, l = app->formats; l; l = l->next, i++) {
1696     const GstFormatDefinition *def = l->data;
1697
1698     gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (app->seek_format_combo),
1699         def->nick);
1700     if (g_strcmp0 (def->nick, selected) == 0)
1701       selected_idx = i;
1702   }
1703   g_signal_handlers_unblock_by_func (app->seek_format_combo,
1704       seek_format_changed_cb, app);
1705
1706   gtk_combo_box_set_active (GTK_COMBO_BOX (app->seek_format_combo),
1707       selected_idx);
1708
1709   g_free (selected);
1710 }
1711
1712 static void
1713 msg_async_done (GstBus * bus, GstMessage * message, SeekApp * app)
1714 {
1715   GST_DEBUG ("async done");
1716
1717   /* Now query all available GstFormats */
1718   update_formats (app);
1719
1720   /* when we get ASYNC_DONE we can query position, duration and other
1721    * properties */
1722   update_scale (app);
1723
1724   /* update the available streams */
1725   update_streams (app);
1726
1727   find_interface_elements (app);
1728 }
1729
1730 static void
1731 msg_state_changed (GstBus * bus, GstMessage * message, SeekApp * app)
1732 {
1733   const GstStructure *s;
1734
1735   s = gst_message_get_structure (message);
1736
1737   /* We only care about state changed on the pipeline */
1738   if (s && GST_MESSAGE_SRC (message) == GST_OBJECT_CAST (app->pipeline)) {
1739     GstState old, new, pending;
1740
1741     gst_message_parse_state_changed (message, &old, &new, &pending);
1742
1743     /* When state of the pipeline changes to paused or playing we start updating scale */
1744     if (new == GST_STATE_PLAYING) {
1745       set_update_scale (app, TRUE);
1746     } else {
1747       set_update_scale (app, FALSE);
1748     }
1749
1750     /* dump graph for (some) pipeline state changes */
1751     {
1752       gchar *dump_name;
1753
1754       dump_name = g_strdup_printf ("seek.%s_%s",
1755           gst_element_state_get_name (old), gst_element_state_get_name (new));
1756
1757       GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (app->pipeline),
1758           GST_DEBUG_GRAPH_SHOW_ALL, dump_name);
1759
1760       g_free (dump_name);
1761     }
1762   }
1763 }
1764
1765 static void
1766 msg_segment_done (GstBus * bus, GstMessage * message, SeekApp * app)
1767 {
1768   GstEvent *s_event;
1769   GstSeekFlags flags;
1770   gboolean res;
1771   GstFormat format;
1772
1773   GST_DEBUG ("position is %" GST_TIME_FORMAT, GST_TIME_ARGS (app->position));
1774   gst_message_parse_segment_done (message, &format, &app->position);
1775   GST_DEBUG ("end of segment at %" GST_TIME_FORMAT,
1776       GST_TIME_ARGS (app->position));
1777
1778   flags = 0;
1779   /* in the segment-done callback we never flush as this would not make sense
1780    * for seamless playback. */
1781   if (app->loop_seek)
1782     flags |= GST_SEEK_FLAG_SEGMENT;
1783   if (app->skip_seek)
1784     flags |= GST_SEEK_FLAG_SKIP;
1785
1786   s_event = gst_event_new_seek (app->rate,
1787       GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, G_GINT64_CONSTANT (0),
1788       GST_SEEK_TYPE_SET, app->duration);
1789
1790   GST_DEBUG ("restart loop with rate %lf to 0 / %" GST_TIME_FORMAT,
1791       app->rate, GST_TIME_ARGS (app->duration));
1792
1793   res = send_event (app, s_event);
1794   if (!res)
1795     g_print ("segment seek failed\n");
1796 }
1797
1798 /* in stream buffering mode we PAUSE the pipeline until we receive a 100%
1799  * message */
1800 static void
1801 do_stream_buffering (SeekApp * app, gint percent)
1802 {
1803   gchar *bufstr;
1804
1805   gtk_statusbar_pop (GTK_STATUSBAR (app->statusbar), app->status_id);
1806   bufstr = g_strdup_printf ("Buffering...%d", percent);
1807   gtk_statusbar_push (GTK_STATUSBAR (app->statusbar), app->status_id, bufstr);
1808   g_free (bufstr);
1809
1810   if (percent == 100) {
1811     /* a 100% message means buffering is done */
1812     app->buffering = FALSE;
1813     /* if the desired state is playing, go back */
1814     if (app->state == GST_STATE_PLAYING) {
1815       /* no state management needed for live pipelines */
1816       if (!app->is_live) {
1817         fprintf (stderr, "Done buffering, setting pipeline to PLAYING ...\n");
1818         gst_element_set_state (app->pipeline, GST_STATE_PLAYING);
1819       }
1820       gtk_statusbar_pop (GTK_STATUSBAR (app->statusbar), app->status_id);
1821       gtk_statusbar_push (GTK_STATUSBAR (app->statusbar), app->status_id,
1822           "Playing");
1823     }
1824   } else {
1825     /* buffering busy */
1826     if (app->buffering == FALSE && app->state == GST_STATE_PLAYING) {
1827       /* we were not buffering but PLAYING, PAUSE  the pipeline. */
1828       if (!app->is_live) {
1829         fprintf (stderr, "Buffering, setting pipeline to PAUSED ...\n");
1830         gst_element_set_state (app->pipeline, GST_STATE_PAUSED);
1831       }
1832     }
1833     app->buffering = TRUE;
1834   }
1835 }
1836
1837 static void
1838 do_download_buffering (SeekApp * app, gint percent)
1839 {
1840   if (!app->buffering && percent < 100) {
1841     gchar *bufstr;
1842
1843     app->buffering = TRUE;
1844
1845     bufstr = g_strdup_printf ("Downloading...");
1846     gtk_statusbar_push (GTK_STATUSBAR (app->statusbar), app->status_id, bufstr);
1847     g_free (bufstr);
1848
1849     /* once we get a buffering message, we'll do the fill update */
1850     set_update_fill (app, TRUE);
1851
1852     if (app->state == GST_STATE_PLAYING && !app->is_live) {
1853       fprintf (stderr, "Downloading, setting pipeline to PAUSED ...\n");
1854       gst_element_set_state (app->pipeline, GST_STATE_PAUSED);
1855       /* user has to manually start the playback */
1856       app->state = GST_STATE_PAUSED;
1857     }
1858   }
1859 }
1860
1861 static void
1862 msg_buffering (GstBus * bus, GstMessage * message, SeekApp * app)
1863 {
1864   gint percent;
1865
1866   gst_message_parse_buffering (message, &percent);
1867
1868   /* get more stats */
1869   gst_message_parse_buffering_stats (message, &app->mode, NULL, NULL,
1870       &app->buffering_left);
1871
1872   switch (app->mode) {
1873     case GST_BUFFERING_DOWNLOAD:
1874       do_download_buffering (app, percent);
1875       break;
1876     case GST_BUFFERING_LIVE:
1877       app->is_live = TRUE;
1878     case GST_BUFFERING_TIMESHIFT:
1879     case GST_BUFFERING_STREAM:
1880       do_stream_buffering (app, percent);
1881       break;
1882   }
1883 }
1884
1885 static void
1886 msg_clock_lost (GstBus * bus, GstMessage * message, SeekApp * app)
1887 {
1888   g_print ("clock lost! PAUSE and PLAY to select a new clock\n");
1889   if (app->state == GST_STATE_PLAYING) {
1890     gst_element_set_state (app->pipeline, GST_STATE_PAUSED);
1891     gst_element_set_state (app->pipeline, GST_STATE_PLAYING);
1892   }
1893 }
1894
1895 static gboolean
1896 is_valid_color_balance_element (GstElement * element)
1897 {
1898   GstColorBalance *bal = GST_COLOR_BALANCE (element);
1899   gboolean have_brightness = FALSE;
1900   gboolean have_contrast = FALSE;
1901   gboolean have_hue = FALSE;
1902   gboolean have_saturation = FALSE;
1903   const GList *channels, *l;
1904
1905   channels = gst_color_balance_list_channels (bal);
1906   for (l = channels; l; l = l->next) {
1907     GstColorBalanceChannel *ch = l->data;
1908
1909     if (g_strrstr (ch->label, "BRIGHTNESS"))
1910       have_brightness = TRUE;
1911     else if (g_strrstr (ch->label, "CONTRAST"))
1912       have_contrast = TRUE;
1913     else if (g_strrstr (ch->label, "HUE"))
1914       have_hue = TRUE;
1915     else if (g_strrstr (ch->label, "SATURATION"))
1916       have_saturation = TRUE;
1917   }
1918
1919   return have_brightness && have_contrast && have_hue && have_saturation;
1920 }
1921
1922 static void
1923 find_interface_elements (SeekApp * app)
1924 {
1925   GstIterator *it;
1926   gpointer item;
1927   gboolean done = FALSE, hardware = FALSE;
1928
1929   if (app->pipeline_type == 0)
1930     return;
1931
1932   if (app->navigation_element)
1933     gst_object_unref (app->navigation_element);
1934   app->navigation_element = NULL;
1935
1936   if (app->colorbalance_element)
1937     gst_object_unref (app->colorbalance_element);
1938   app->colorbalance_element = NULL;
1939
1940   app->navigation_element =
1941       gst_bin_get_by_interface (GST_BIN (app->pipeline), GST_TYPE_NAVIGATION);
1942
1943   it = gst_bin_iterate_all_by_interface (GST_BIN (app->pipeline),
1944       GST_TYPE_COLOR_BALANCE);
1945   while (!done) {
1946     switch (gst_iterator_next (it, &item)) {
1947       case GST_ITERATOR_OK:{
1948         GstElement *element = GST_ELEMENT (item);
1949
1950         if (is_valid_color_balance_element (element)) {
1951           if (!app->colorbalance_element) {
1952             app->colorbalance_element =
1953                 GST_ELEMENT_CAST (gst_object_ref (element));
1954             hardware =
1955                 (gst_color_balance_get_balance_type (GST_COLOR_BALANCE
1956                     (element)) == GST_COLOR_BALANCE_HARDWARE);
1957           } else if (!hardware) {
1958             gboolean tmp =
1959                 (gst_color_balance_get_balance_type (GST_COLOR_BALANCE
1960                     (element)) == GST_COLOR_BALANCE_HARDWARE);
1961
1962             if (tmp) {
1963               if (app->colorbalance_element)
1964                 gst_object_unref (app->colorbalance_element);
1965               app->colorbalance_element =
1966                   GST_ELEMENT_CAST (gst_object_ref (element));
1967               hardware = TRUE;
1968             }
1969           }
1970         }
1971
1972         gst_object_unref (element);
1973
1974         if (hardware && app->colorbalance_element)
1975           done = TRUE;
1976         break;
1977       }
1978       case GST_ITERATOR_RESYNC:
1979         gst_iterator_resync (it);
1980         done = FALSE;
1981         hardware = FALSE;
1982         if (app->colorbalance_element)
1983           gst_object_unref (app->colorbalance_element);
1984         app->colorbalance_element = NULL;
1985         break;
1986       case GST_ITERATOR_DONE:
1987       case GST_ITERATOR_ERROR:
1988       default:
1989         done = TRUE;
1990     }
1991   }
1992
1993   gst_iterator_free (it);
1994 }
1995
1996 /* called when Navigation command button is pressed */
1997 static void
1998 navigation_cmd_cb (GtkButton * button, SeekApp * app)
1999 {
2000   GstNavigationCommand cmd = GST_NAVIGATION_COMMAND_INVALID;
2001   gint i;
2002
2003   if (!app->navigation_element) {
2004     find_interface_elements (app);
2005     if (!app->navigation_element)
2006       return;
2007   }
2008
2009   for (i = 0; i < G_N_ELEMENTS (app->navigation_buttons); i++) {
2010     if (app->navigation_buttons[i].button == GTK_WIDGET (button)) {
2011       cmd = app->navigation_buttons[i].cmd;
2012       break;
2013     }
2014   }
2015
2016   if (cmd != GST_NAVIGATION_COMMAND_INVALID)
2017     gst_navigation_send_command (GST_NAVIGATION (app->navigation_element), cmd);
2018 }
2019
2020 #if defined (GDK_WINDOWING_X11) || defined (GDK_WINDOWING_WIN32) || defined (GDK_WINDOWING_QUARTZ)
2021 /* We set the xid here in response to the prepare-xwindow-id message via a
2022  * bus sync handler because we don't know the actual videosink used from the
2023  * start (as we don't know the pipeline, or bin elements such as autovideosink
2024  * or gconfvideosink may be used which create the actual videosink only once
2025  * the pipeline is started) */
2026 static GstBusSyncReply
2027 bus_sync_handler (GstBus * bus, GstMessage * message, SeekApp * app)
2028 {
2029   if ((GST_MESSAGE_TYPE (message) == GST_MESSAGE_ELEMENT) &&
2030       gst_structure_has_name (message->structure, "prepare-xwindow-id")) {
2031     GstElement *element = GST_ELEMENT (GST_MESSAGE_SRC (message));
2032
2033     if (app->xoverlay_element)
2034       gst_object_unref (app->xoverlay_element);
2035     app->xoverlay_element = GST_ELEMENT (gst_object_ref (element));
2036
2037     g_print ("got prepare-xwindow-id, setting XID %" G_GUINTPTR_FORMAT "\n",
2038         app->embed_xid);
2039
2040     if (g_object_class_find_property (G_OBJECT_GET_CLASS (element),
2041             "force-aspect-ratio")) {
2042       g_object_set (element, "force-aspect-ratio", TRUE, NULL);
2043     }
2044
2045     /* Should have been initialised from main thread before (can't use
2046      * GDK_WINDOW_XID here with Gtk+ >= 2.18, because the sync handler will
2047      * be called from a streaming thread and GDK_WINDOW_XID maps to more than
2048      * a simple structure lookup with Gtk+ >= 2.18, where 'more' is stuff that
2049      * shouldn't be done from a non-GUI thread without explicit locking).  */
2050     g_assert (app->embed_xid != 0);
2051
2052     gst_x_overlay_set_window_handle (GST_X_OVERLAY (element), app->embed_xid);
2053
2054     find_interface_elements (app);
2055   }
2056   return GST_BUS_PASS;
2057 }
2058 #endif
2059
2060 static gboolean
2061 draw_cb (GtkWidget * widget, cairo_t * cr, SeekApp * app)
2062 {
2063   if (app->state < GST_STATE_PAUSED) {
2064     int width, height;
2065
2066     width = gtk_widget_get_allocated_width (widget);
2067     height = gtk_widget_get_allocated_height (widget);
2068     cairo_set_source_rgb (cr, 0, 0, 0);
2069     cairo_rectangle (cr, 0, 0, width, height);
2070     cairo_fill (cr);
2071     return TRUE;
2072   }
2073
2074   if (app->xoverlay_element)
2075     gst_x_overlay_expose (GST_X_OVERLAY (app->xoverlay_element));
2076
2077   return FALSE;
2078 }
2079
2080 static void
2081 realize_cb (GtkWidget * widget, SeekApp * app)
2082 {
2083   GdkWindow *window = gtk_widget_get_window (widget);
2084
2085   /* This is here just for pedagogical purposes, GDK_WINDOW_XID will call it
2086    * as well */
2087   if (!gdk_window_ensure_native (window))
2088     g_error ("Couldn't create native window needed for GstXOverlay!");
2089
2090 #if defined (GDK_WINDOWING_WIN32)
2091   app->embed_xid = GDK_WINDOW_HWND (window);
2092   g_print ("Window realize: video window HWND = %lu\n", app->embed_xid);
2093 #elif defined (GDK_WINDOWING_QUARTZ)
2094   app->embed_xid = gdk_quartz_window_get_nsview (window);
2095   g_print ("Window realize: video window NSView = %p\n", app->embed_xid);
2096 #elif defined (GDK_WINDOWING_X11)
2097   app->embed_xid = GDK_WINDOW_XID (window);
2098   g_print ("Window realize: video window XID = %" G_GUINTPTR_FORMAT "\n",
2099       app->embed_xid);
2100 #endif
2101 }
2102
2103 static gboolean
2104 button_press_cb (GtkWidget * widget, GdkEventButton * event, SeekApp * app)
2105 {
2106   gtk_widget_grab_focus (widget);
2107
2108   if (app->navigation_element)
2109     gst_navigation_send_mouse_event (GST_NAVIGATION (app->navigation_element),
2110         "mouse-button-press", event->button, event->x, event->y);
2111
2112   return FALSE;
2113 }
2114
2115 static gboolean
2116 button_release_cb (GtkWidget * widget, GdkEventButton * event, SeekApp * app)
2117 {
2118   if (app->navigation_element)
2119     gst_navigation_send_mouse_event (GST_NAVIGATION (app->navigation_element),
2120         "mouse-button-release", event->button, event->x, event->y);
2121
2122   return FALSE;
2123 }
2124
2125 static gboolean
2126 key_press_cb (GtkWidget * widget, GdkEventKey * event, SeekApp * app)
2127 {
2128   if (app->navigation_element)
2129     gst_navigation_send_key_event (GST_NAVIGATION (app->navigation_element),
2130         "key-press", gdk_keyval_name (event->keyval));
2131
2132   return FALSE;
2133 }
2134
2135 static gboolean
2136 key_release_cb (GtkWidget * widget, GdkEventKey * event, SeekApp * app)
2137 {
2138   if (app->navigation_element)
2139     gst_navigation_send_key_event (GST_NAVIGATION (app->navigation_element),
2140         "key-release", gdk_keyval_name (event->keyval));
2141
2142   return FALSE;
2143 }
2144
2145 static gboolean
2146 motion_notify_cb (GtkWidget * widget, GdkEventMotion * event, SeekApp * app)
2147 {
2148   if (app->navigation_element)
2149     gst_navigation_send_mouse_event (GST_NAVIGATION (app->navigation_element),
2150         "mouse-move", 0, event->x, event->y);
2151
2152   return FALSE;
2153 }
2154
2155 static void
2156 msg_eos (GstBus * bus, GstMessage * message, SeekApp * app)
2157 {
2158   message_received (bus, message, app);
2159
2160   /* Set new uri for playerbins and continue playback */
2161   if (app->current_path && app->pipeline_type == 0) {
2162     stop_cb (NULL, app);
2163     app->current_path = g_list_next (app->current_path);
2164     if (app->current_path) {
2165       playbin_set_uri (app->pipeline, app->current_path->data);
2166       play_cb (NULL, app);
2167     }
2168   }
2169 }
2170
2171 static void
2172 msg_step_done (GstBus * bus, GstMessage * message, SeekApp * app)
2173 {
2174   if (!app->shuttling)
2175     message_received (bus, message, app);
2176 }
2177
2178 static void
2179 msg (GstBus * bus, GstMessage * message, SeekApp * app)
2180 {
2181   GstNavigationMessageType nav_type;
2182
2183   nav_type = gst_navigation_message_get_type (message);
2184   switch (nav_type) {
2185     case GST_NAVIGATION_MESSAGE_COMMANDS_CHANGED:{
2186       GstQuery *query;
2187       gboolean res, j;
2188
2189       /* Heuristic to detect if we're dealing with a DVD menu */
2190       query = gst_navigation_query_new_commands ();
2191       res = gst_element_query (GST_ELEMENT (GST_MESSAGE_SRC (message)), query);
2192
2193       for (j = 0; j < G_N_ELEMENTS (app->navigation_buttons); j++)
2194         gtk_widget_set_sensitive (app->navigation_buttons[j].button, FALSE);
2195
2196       if (res) {
2197         gboolean is_menu = FALSE;
2198         guint i, n;
2199
2200         if (gst_navigation_query_parse_commands_length (query, &n)) {
2201           for (i = 0; i < n; i++) {
2202             GstNavigationCommand cmd;
2203
2204             if (!gst_navigation_query_parse_commands_nth (query, i, &cmd))
2205               break;
2206
2207             is_menu |= (cmd == GST_NAVIGATION_COMMAND_ACTIVATE);
2208             is_menu |= (cmd == GST_NAVIGATION_COMMAND_LEFT);
2209             is_menu |= (cmd == GST_NAVIGATION_COMMAND_RIGHT);
2210             is_menu |= (cmd == GST_NAVIGATION_COMMAND_UP);
2211             is_menu |= (cmd == GST_NAVIGATION_COMMAND_DOWN);
2212
2213             for (j = 0; j < G_N_ELEMENTS (app->navigation_buttons); j++) {
2214               if (app->navigation_buttons[j].cmd != cmd)
2215                 continue;
2216
2217               gtk_widget_set_sensitive (app->navigation_buttons[j].button,
2218                   TRUE);
2219             }
2220           }
2221         }
2222
2223         gtk_widget_set_sensitive (GTK_WIDGET (app->seek_scale), !is_menu);
2224       } else {
2225         g_assert_not_reached ();
2226       }
2227
2228       gst_query_unref (query);
2229       message_received (bus, message, app);
2230       break;
2231     }
2232     default:
2233       break;
2234   }
2235 }
2236
2237 static void
2238 connect_bus_signals (SeekApp * app)
2239 {
2240   GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (app->pipeline));
2241
2242 #if defined (GDK_WINDOWING_X11) || defined (GDK_WINDOWING_WIN32) || defined (GDK_WINDOWING_QUARTZ)
2243   if (app->pipeline_type != 0) {
2244     /* handle prepare-xwindow-id element message synchronously, but only for non-playbin2 */
2245     gst_bus_set_sync_handler (bus, (GstBusSyncHandler) bus_sync_handler, app);
2246   }
2247 #endif
2248
2249   gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
2250   gst_bus_enable_sync_message_emission (bus);
2251
2252   g_signal_connect (bus, "message::state-changed",
2253       G_CALLBACK (msg_state_changed), app);
2254   g_signal_connect (bus, "message::segment-done", G_CALLBACK (msg_segment_done),
2255       app);
2256   g_signal_connect (bus, "message::async-done", G_CALLBACK (msg_async_done),
2257       app);
2258
2259   g_signal_connect (bus, "message::new-clock", G_CALLBACK (message_received),
2260       app);
2261   g_signal_connect (bus, "message::clock-lost", G_CALLBACK (msg_clock_lost),
2262       app);
2263   g_signal_connect (bus, "message::error", G_CALLBACK (message_received), app);
2264   g_signal_connect (bus, "message::warning", G_CALLBACK (message_received),
2265       app);
2266   g_signal_connect (bus, "message::eos", G_CALLBACK (msg_eos), app);
2267   g_signal_connect (bus, "message::tag", G_CALLBACK (message_received), app);
2268   g_signal_connect (bus, "message::element", G_CALLBACK (message_received),
2269       app);
2270   g_signal_connect (bus, "message::segment-done", G_CALLBACK (message_received),
2271       app);
2272   g_signal_connect (bus, "message::buffering", G_CALLBACK (msg_buffering), app);
2273 //  g_signal_connect (bus, "message::step-done", G_CALLBACK (msg_step_done),
2274 //      app);
2275   g_signal_connect (bus, "message::step-start", G_CALLBACK (msg_step_done),
2276       app);
2277   g_signal_connect (bus, "sync-message::step-done",
2278       G_CALLBACK (msg_sync_step_done), app);
2279   g_signal_connect (bus, "message", G_CALLBACK (msg), app);
2280
2281   gst_object_unref (bus);
2282 }
2283
2284 /* Return GList of paths described in location string */
2285 static GList *
2286 handle_wildcards (const gchar * location)
2287 {
2288   GList *res = NULL;
2289   gchar *path = g_path_get_dirname (location);
2290   gchar *pattern = g_path_get_basename (location);
2291   GPatternSpec *pspec = g_pattern_spec_new (pattern);
2292   GDir *dir = g_dir_open (path, 0, NULL);
2293   const gchar *name;
2294
2295   g_print ("matching %s from %s\n", pattern, path);
2296
2297   if (!dir) {
2298     g_print ("opening directory %s failed\n", path);
2299     goto out;
2300   }
2301
2302   while ((name = g_dir_read_name (dir)) != NULL) {
2303     if (g_pattern_match_string (pspec, name)) {
2304       res = g_list_append (res, g_strjoin ("/", path, name, NULL));
2305       g_print ("  found clip %s\n", name);
2306     }
2307   }
2308
2309   g_dir_close (dir);
2310 out:
2311   g_pattern_spec_free (pspec);
2312   g_free (pattern);
2313   g_free (path);
2314
2315   return res;
2316 }
2317
2318 static void
2319 delete_event_cb (GtkWidget * widget, GdkEvent * event, SeekApp * app)
2320 {
2321   stop_cb (NULL, app);
2322   gtk_main_quit ();
2323 }
2324
2325 static void
2326 video_sink_activate_cb (GtkEntry * entry, SeekApp * app)
2327 {
2328   GstElement *sink = NULL;
2329   const gchar *text;
2330
2331   text = gtk_entry_get_text (entry);
2332   if (text != NULL && *text != '\0') {
2333     sink = gst_element_factory_make_or_warn (text, NULL);
2334   }
2335
2336   g_object_set (app->pipeline, "video-sink", sink, NULL);
2337 }
2338
2339 static void
2340 audio_sink_activate_cb (GtkEntry * entry, SeekApp * app)
2341 {
2342   GstElement *sink = NULL;
2343   const gchar *text;
2344
2345   text = gtk_entry_get_text (entry);
2346   if (text != NULL && *text != '\0') {
2347     sink = gst_element_factory_make_or_warn (text, NULL);
2348   }
2349
2350   g_object_set (app->pipeline, "audio-sink", sink, NULL);
2351 }
2352
2353 static void
2354 text_sink_activate_cb (GtkEntry * entry, SeekApp * app)
2355 {
2356   GstElement *sink = NULL;
2357   const gchar *text;
2358
2359   text = gtk_entry_get_text (entry);
2360   if (text != NULL && *text != '\0') {
2361     sink = gst_element_factory_make_or_warn (text, NULL);
2362   }
2363
2364   g_object_set (app->pipeline, "text-sink", sink, NULL);
2365 }
2366
2367 static void
2368 buffer_size_activate_cb (GtkEntry * entry, SeekApp * app)
2369 {
2370   const gchar *text;
2371
2372   text = gtk_entry_get_text (entry);
2373   if (text != NULL && *text != '\0') {
2374     gint v;
2375     gchar *endptr;
2376
2377     v = g_ascii_strtoll (text, &endptr, 10);
2378     if (endptr != text && v != G_MAXINT64 && v != G_MININT64) {
2379       g_object_set (app->pipeline, "buffer-size", v, NULL);
2380     }
2381   }
2382 }
2383
2384 static void
2385 buffer_duration_activate_cb (GtkEntry * entry, SeekApp * app)
2386 {
2387   const gchar *text;
2388
2389   text = gtk_entry_get_text (entry);
2390   if (text != NULL && *text != '\0') {
2391     gint64 v;
2392     gchar *endptr;
2393
2394     v = g_ascii_strtoll (text, &endptr, 10);
2395     if (endptr != text && v != G_MAXINT64 && v != G_MININT64) {
2396       g_object_set (app->pipeline, "buffer-duration", v, NULL);
2397     }
2398   }
2399 }
2400
2401 static void
2402 ringbuffer_maxsize_activate_cb (GtkEntry * entry, SeekApp * app)
2403 {
2404   const gchar *text;
2405
2406   text = gtk_entry_get_text (entry);
2407   if (text != NULL && *text != '\0') {
2408     guint64 v;
2409     gchar *endptr;
2410
2411     v = g_ascii_strtoull (text, &endptr, 10);
2412     if (endptr != text && v != G_MAXUINT64) {
2413       g_object_set (app->pipeline, "ring-buffer-max-size", v, NULL);
2414     }
2415   }
2416 }
2417
2418 static void
2419 connection_speed_activate_cb (GtkEntry * entry, SeekApp * app)
2420 {
2421   const gchar *text;
2422
2423   text = gtk_entry_get_text (entry);
2424   if (text != NULL && *text != '\0') {
2425     guint v;
2426     gchar *endptr;
2427
2428     v = g_ascii_strtoll (text, &endptr, 10);
2429     if (endptr != text && v != G_MAXINT64 && v != G_MININT64) {
2430       g_object_set (app->pipeline, "connection-speed", v, NULL);
2431     }
2432   }
2433 }
2434
2435 static void
2436 subtitle_encoding_activate_cb (GtkEntry * entry, SeekApp * app)
2437 {
2438   const gchar *text;
2439
2440   text = gtk_entry_get_text (entry);
2441   g_object_set (app->pipeline, "subtitle-encoding", text, NULL);
2442 }
2443
2444 static void
2445 subtitle_fontdesc_cb (GtkFontButton * button, SeekApp * app)
2446 {
2447   const gchar *text;
2448
2449   text = gtk_font_button_get_font_name (button);
2450   g_object_set (app->pipeline, "subtitle-font-desc", text, NULL);
2451 }
2452
2453 static void
2454 av_offset_activate_cb (GtkEntry * entry, SeekApp * app)
2455 {
2456   const gchar *text;
2457
2458   text = gtk_entry_get_text (entry);
2459   if (text != NULL && *text != '\0') {
2460     gint64 v;
2461     gchar *endptr;
2462
2463     v = g_ascii_strtoll (text, &endptr, 10);
2464     if (endptr != text && v != G_MAXINT64 && v != G_MININT64) {
2465       g_object_set (app->pipeline, "av-offset", v, NULL);
2466     }
2467   }
2468 }
2469
2470 static void
2471 print_usage (int argc, char **argv)
2472 {
2473   gint i;
2474
2475   g_print ("usage: %s <type> <filename>\n", argv[0]);
2476   g_print ("   possible types:\n");
2477
2478   for (i = 0; i < G_N_ELEMENTS (pipelines); i++) {
2479     g_print ("     %d = %s\n", i, pipelines[i].name);
2480   }
2481 }
2482
2483 static void
2484 create_ui (SeekApp * app)
2485 {
2486   GtkWidget *hbox, *vbox, *seek, *playbin, *step, *navigation, *colorbalance;
2487   GtkWidget *play_button, *pause_button, *stop_button;
2488   GtkAdjustment *adjustment;
2489
2490   /* initialize gui elements ... */
2491   app->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
2492   app->video_window = gtk_drawing_area_new ();
2493   g_signal_connect (app->video_window, "draw", G_CALLBACK (draw_cb), app);
2494   g_signal_connect (app->video_window, "realize", G_CALLBACK (realize_cb), app);
2495   g_signal_connect (app->video_window, "button-press-event",
2496       G_CALLBACK (button_press_cb), app);
2497   g_signal_connect (app->video_window, "button-release-event",
2498       G_CALLBACK (button_release_cb), app);
2499   g_signal_connect (app->video_window, "key-press-event",
2500       G_CALLBACK (key_press_cb), app);
2501   g_signal_connect (app->video_window, "key-release-event",
2502       G_CALLBACK (key_release_cb), app);
2503   g_signal_connect (app->video_window, "motion-notify-event",
2504       G_CALLBACK (motion_notify_cb), app);
2505   gtk_widget_set_can_focus (app->video_window, TRUE);
2506   gtk_widget_set_double_buffered (app->video_window, FALSE);
2507   gtk_widget_add_events (app->video_window,
2508       GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
2509       | GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK);
2510
2511   app->statusbar = gtk_statusbar_new ();
2512   app->status_id =
2513       gtk_statusbar_get_context_id (GTK_STATUSBAR (app->statusbar), "seek");
2514   gtk_statusbar_push (GTK_STATUSBAR (app->statusbar), app->status_id,
2515       "Stopped");
2516   hbox = gtk_hbox_new (FALSE, 0);
2517   vbox = gtk_vbox_new (FALSE, 0);
2518   gtk_container_set_border_width (GTK_CONTAINER (vbox), 3);
2519
2520   /* media controls */
2521   play_button = gtk_button_new_from_stock (GTK_STOCK_MEDIA_PLAY);
2522   pause_button = gtk_button_new_from_stock (GTK_STOCK_MEDIA_PAUSE);
2523   stop_button = gtk_button_new_from_stock (GTK_STOCK_MEDIA_STOP);
2524
2525   /* seek expander */
2526   {
2527     GtkWidget *accurate_checkbox, *key_checkbox, *loop_checkbox,
2528         *flush_checkbox;
2529     GtkWidget *scrub_checkbox, *play_scrub_checkbox, *rate_label;
2530     GtkWidget *skip_checkbox, *rate_spinbutton;
2531     GtkWidget *flagtable, *advanced_seek, *advanced_seek_grid;
2532     GtkWidget *duration_label, *position_label, *seek_button;
2533
2534     seek = gtk_expander_new ("seek options");
2535     flagtable = gtk_grid_new ();
2536     gtk_grid_set_row_spacing (GTK_GRID (flagtable), 2);
2537     gtk_grid_set_row_homogeneous (GTK_GRID (flagtable), FALSE);
2538     gtk_grid_set_column_spacing (GTK_GRID (flagtable), 2);
2539     gtk_grid_set_column_homogeneous (GTK_GRID (flagtable), TRUE);
2540
2541     accurate_checkbox = gtk_check_button_new_with_label ("Accurate Seek");
2542     key_checkbox = gtk_check_button_new_with_label ("Key-unit Seek");
2543     loop_checkbox = gtk_check_button_new_with_label ("Loop");
2544     flush_checkbox = gtk_check_button_new_with_label ("Flush");
2545     scrub_checkbox = gtk_check_button_new_with_label ("Scrub");
2546     play_scrub_checkbox = gtk_check_button_new_with_label ("Play Scrub");
2547     skip_checkbox = gtk_check_button_new_with_label ("Play Skip");
2548     rate_spinbutton = gtk_spin_button_new_with_range (-100, 100, 0.1);
2549     gtk_spin_button_set_digits (GTK_SPIN_BUTTON (rate_spinbutton), 3);
2550     rate_label = gtk_label_new ("Rate");
2551
2552     gtk_widget_set_tooltip_text (accurate_checkbox,
2553         "accurate position is requested, this might be considerably slower for some formats");
2554     gtk_widget_set_tooltip_text (key_checkbox,
2555         "seek to the nearest keyframe. This might be faster but less accurate");
2556     gtk_widget_set_tooltip_text (loop_checkbox, "loop playback");
2557     gtk_widget_set_tooltip_text (flush_checkbox,
2558         "flush pipeline after seeking");
2559     gtk_widget_set_tooltip_text (rate_spinbutton,
2560         "define the playback rate, " "negative value trigger reverse playback");
2561     gtk_widget_set_tooltip_text (scrub_checkbox, "show images while seeking");
2562     gtk_widget_set_tooltip_text (play_scrub_checkbox,
2563         "play video while seeking");
2564     gtk_widget_set_tooltip_text (skip_checkbox,
2565         "Skip frames while playing at high frame rates");
2566
2567     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (flush_checkbox), TRUE);
2568     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (scrub_checkbox), TRUE);
2569
2570     gtk_spin_button_set_value (GTK_SPIN_BUTTON (rate_spinbutton), app->rate);
2571
2572     g_signal_connect (G_OBJECT (accurate_checkbox), "toggled",
2573         G_CALLBACK (accurate_toggle_cb), app);
2574     g_signal_connect (G_OBJECT (key_checkbox), "toggled",
2575         G_CALLBACK (key_toggle_cb), app);
2576     g_signal_connect (G_OBJECT (loop_checkbox), "toggled",
2577         G_CALLBACK (loop_toggle_cb), app);
2578     g_signal_connect (G_OBJECT (flush_checkbox), "toggled",
2579         G_CALLBACK (flush_toggle_cb), app);
2580     g_signal_connect (G_OBJECT (scrub_checkbox), "toggled",
2581         G_CALLBACK (scrub_toggle_cb), app);
2582     g_signal_connect (G_OBJECT (play_scrub_checkbox), "toggled",
2583         G_CALLBACK (play_scrub_toggle_cb), app);
2584     g_signal_connect (G_OBJECT (skip_checkbox), "toggled",
2585         G_CALLBACK (skip_toggle_cb), app);
2586     g_signal_connect (G_OBJECT (rate_spinbutton), "value-changed",
2587         G_CALLBACK (rate_spinbutton_changed_cb), app);
2588
2589     gtk_grid_attach (GTK_GRID (flagtable), accurate_checkbox, 0, 0, 1, 1);
2590     gtk_grid_attach (GTK_GRID (flagtable), flush_checkbox, 1, 0, 1, 1);
2591     gtk_grid_attach (GTK_GRID (flagtable), loop_checkbox, 2, 0, 1, 1);
2592     gtk_grid_attach (GTK_GRID (flagtable), key_checkbox, 0, 1, 1, 1);
2593     gtk_grid_attach (GTK_GRID (flagtable), scrub_checkbox, 1, 1, 1, 1);
2594     gtk_grid_attach (GTK_GRID (flagtable), play_scrub_checkbox, 2, 1, 1, 1);
2595     gtk_grid_attach (GTK_GRID (flagtable), skip_checkbox, 3, 0, 1, 1);
2596     gtk_grid_attach (GTK_GRID (flagtable), rate_label, 4, 0, 1, 1);
2597     gtk_grid_attach (GTK_GRID (flagtable), rate_spinbutton, 4, 1, 1, 1);
2598
2599     advanced_seek = gtk_frame_new ("Advanced Seek");
2600     advanced_seek_grid = gtk_grid_new ();
2601     gtk_grid_set_row_spacing (GTK_GRID (advanced_seek_grid), 2);
2602     gtk_grid_set_row_homogeneous (GTK_GRID (advanced_seek_grid), FALSE);
2603     gtk_grid_set_column_spacing (GTK_GRID (advanced_seek_grid), 5);
2604     gtk_grid_set_column_homogeneous (GTK_GRID (advanced_seek_grid), FALSE);
2605
2606     app->seek_format_combo = gtk_combo_box_text_new ();
2607     g_signal_connect (app->seek_format_combo, "changed",
2608         G_CALLBACK (seek_format_changed_cb), app);
2609     gtk_grid_attach (GTK_GRID (advanced_seek_grid), app->seek_format_combo, 0,
2610         0, 1, 1);
2611
2612     app->seek_entry = gtk_entry_new ();
2613     gtk_entry_set_width_chars (GTK_ENTRY (app->seek_entry), 12);
2614     gtk_grid_attach (GTK_GRID (advanced_seek_grid), app->seek_entry, 0, 1, 1,
2615         1);
2616
2617     seek_button = gtk_button_new_with_label ("Seek");
2618     g_signal_connect (G_OBJECT (seek_button), "clicked",
2619         G_CALLBACK (advanced_seek_button_cb), app);
2620     gtk_grid_attach (GTK_GRID (advanced_seek_grid), seek_button, 1, 0, 1, 1);
2621
2622     position_label = gtk_label_new ("Position:");
2623     gtk_grid_attach (GTK_GRID (advanced_seek_grid), position_label, 2, 0, 1, 1);
2624     duration_label = gtk_label_new ("Duration:");
2625     gtk_grid_attach (GTK_GRID (advanced_seek_grid), duration_label, 2, 1, 1, 1);
2626
2627     app->seek_position_label = gtk_label_new ("-1");
2628     gtk_grid_attach (GTK_GRID (advanced_seek_grid), app->seek_position_label, 3,
2629         0, 1, 1);
2630     app->seek_duration_label = gtk_label_new ("-1");
2631     gtk_grid_attach (GTK_GRID (advanced_seek_grid), app->seek_duration_label, 3,
2632         1, 1, 1);
2633
2634     gtk_container_add (GTK_CONTAINER (advanced_seek), advanced_seek_grid);
2635     gtk_grid_attach (GTK_GRID (flagtable), advanced_seek, 0, 2, 3, 2);
2636     gtk_container_add (GTK_CONTAINER (seek), flagtable);
2637   }
2638
2639   /* step expander */
2640   {
2641     GtkWidget *hbox;
2642     GtkWidget *step_button, *shuttle_checkbox;
2643
2644     step = gtk_expander_new ("step options");
2645     hbox = gtk_hbox_new (FALSE, 0);
2646
2647     app->step_format_combo = gtk_combo_box_text_new ();
2648     gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (app->step_format_combo),
2649         "frames");
2650     gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (app->step_format_combo),
2651         "time (ms)");
2652     gtk_combo_box_set_active (GTK_COMBO_BOX (app->step_format_combo), 0);
2653     gtk_box_pack_start (GTK_BOX (hbox), app->step_format_combo, FALSE, FALSE,
2654         2);
2655
2656     app->step_amount_spinbutton = gtk_spin_button_new_with_range (1, 1000, 1);
2657     gtk_spin_button_set_digits (GTK_SPIN_BUTTON (app->step_amount_spinbutton),
2658         0);
2659     gtk_spin_button_set_value (GTK_SPIN_BUTTON (app->step_amount_spinbutton),
2660         1.0);
2661     gtk_box_pack_start (GTK_BOX (hbox), app->step_amount_spinbutton, FALSE,
2662         FALSE, 2);
2663
2664     app->step_rate_spinbutton = gtk_spin_button_new_with_range (0.0, 100, 0.1);
2665     gtk_spin_button_set_digits (GTK_SPIN_BUTTON (app->step_rate_spinbutton), 3);
2666     gtk_spin_button_set_value (GTK_SPIN_BUTTON (app->step_rate_spinbutton),
2667         1.0);
2668     gtk_box_pack_start (GTK_BOX (hbox), app->step_rate_spinbutton, FALSE, FALSE,
2669         2);
2670
2671     step_button = gtk_button_new_from_stock (GTK_STOCK_MEDIA_FORWARD);
2672     gtk_button_set_label (GTK_BUTTON (step_button), "Step");
2673     gtk_box_pack_start (GTK_BOX (hbox), step_button, FALSE, FALSE, 2);
2674
2675     g_signal_connect (G_OBJECT (step_button), "clicked", G_CALLBACK (step_cb),
2676         app);
2677
2678     /* shuttle scale */
2679     shuttle_checkbox = gtk_check_button_new_with_label ("Shuttle");
2680     gtk_box_pack_start (GTK_BOX (hbox), shuttle_checkbox, FALSE, FALSE, 2);
2681     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (shuttle_checkbox), FALSE);
2682     g_signal_connect (shuttle_checkbox, "toggled", G_CALLBACK (shuttle_toggled),
2683         app);
2684
2685     adjustment =
2686         GTK_ADJUSTMENT (gtk_adjustment_new (0.0, -3.00, 4.0, 0.1, 1.0, 1.0));
2687     app->shuttle_scale = gtk_hscale_new (adjustment);
2688     gtk_scale_set_digits (GTK_SCALE (app->shuttle_scale), 2);
2689     gtk_scale_set_value_pos (GTK_SCALE (app->shuttle_scale), GTK_POS_TOP);
2690     g_signal_connect (app->shuttle_scale, "value-changed",
2691         G_CALLBACK (shuttle_value_changed), app);
2692     g_signal_connect (app->shuttle_scale, "format_value",
2693         G_CALLBACK (shuttle_format_value), app);
2694
2695     gtk_box_pack_start (GTK_BOX (hbox), app->shuttle_scale, TRUE, TRUE, 2);
2696
2697     gtk_container_add (GTK_CONTAINER (step), hbox);
2698   }
2699
2700   /* navigation command expander */
2701   {
2702     GtkWidget *navigation_button;
2703     GtkWidget *grid;
2704     gint i = 0;
2705
2706     navigation = gtk_expander_new ("navigation commands");
2707     grid = gtk_grid_new ();
2708     gtk_grid_set_row_spacing (GTK_GRID (grid), 2);
2709     gtk_grid_set_row_homogeneous (GTK_GRID (grid), FALSE);
2710     gtk_grid_set_column_spacing (GTK_GRID (grid), 2);
2711     gtk_grid_set_column_homogeneous (GTK_GRID (grid), TRUE);
2712
2713     navigation_button = gtk_button_new_with_label ("Menu 1");
2714     g_signal_connect (G_OBJECT (navigation_button), "clicked",
2715         G_CALLBACK (navigation_cmd_cb), app);
2716     gtk_grid_attach (GTK_GRID (grid), navigation_button, i, 0, 1, 1);
2717     gtk_widget_set_sensitive (navigation_button, FALSE);
2718     gtk_widget_set_tooltip_text (navigation_button, "DVD Menu");
2719     app->navigation_buttons[i].button = navigation_button;
2720     app->navigation_buttons[i++].cmd = GST_NAVIGATION_COMMAND_MENU1;
2721
2722     navigation_button = gtk_button_new_with_label ("Menu 2");
2723     g_signal_connect (G_OBJECT (navigation_button), "clicked",
2724         G_CALLBACK (navigation_cmd_cb), app);
2725     gtk_grid_attach (GTK_GRID (grid), navigation_button, i, 0, 1, 1);
2726     gtk_widget_set_sensitive (navigation_button, FALSE);
2727     gtk_widget_set_tooltip_text (navigation_button, "DVD Title Menu");
2728     app->navigation_buttons[i].button = navigation_button;
2729     app->navigation_buttons[i++].cmd = GST_NAVIGATION_COMMAND_MENU2;
2730
2731     navigation_button = gtk_button_new_with_label ("Menu 3");
2732     g_signal_connect (G_OBJECT (navigation_button), "clicked",
2733         G_CALLBACK (navigation_cmd_cb), app);
2734     gtk_grid_attach (GTK_GRID (grid), navigation_button, i, 0, 1, 1);
2735     gtk_widget_set_sensitive (navigation_button, FALSE);
2736     gtk_widget_set_tooltip_text (navigation_button, "DVD Root Menu");
2737     app->navigation_buttons[i].button = navigation_button;
2738     app->navigation_buttons[i++].cmd = GST_NAVIGATION_COMMAND_MENU3;
2739
2740     navigation_button = gtk_button_new_with_label ("Menu 4");
2741     g_signal_connect (G_OBJECT (navigation_button), "clicked",
2742         G_CALLBACK (navigation_cmd_cb), app);
2743     gtk_grid_attach (GTK_GRID (grid), navigation_button, i, 0, 1, 1);
2744     gtk_widget_set_sensitive (navigation_button, FALSE);
2745     gtk_widget_set_tooltip_text (navigation_button, "DVD Subpicture Menu");
2746     app->navigation_buttons[i].button = navigation_button;
2747     app->navigation_buttons[i++].cmd = GST_NAVIGATION_COMMAND_MENU4;
2748
2749     navigation_button = gtk_button_new_with_label ("Menu 5");
2750     g_signal_connect (G_OBJECT (navigation_button), "clicked",
2751         G_CALLBACK (navigation_cmd_cb), app);
2752     gtk_grid_attach (GTK_GRID (grid), navigation_button, i, 0, 1, 1);
2753     gtk_widget_set_sensitive (navigation_button, FALSE);
2754     gtk_widget_set_tooltip_text (navigation_button, "DVD Audio Menu");
2755     app->navigation_buttons[i].button = navigation_button;
2756     app->navigation_buttons[i++].cmd = GST_NAVIGATION_COMMAND_MENU5;
2757
2758     navigation_button = gtk_button_new_with_label ("Menu 6");
2759     g_signal_connect (G_OBJECT (navigation_button), "clicked",
2760         G_CALLBACK (navigation_cmd_cb), app);
2761     gtk_grid_attach (GTK_GRID (grid), navigation_button, i, 0, 1, 1);
2762     gtk_widget_set_sensitive (navigation_button, FALSE);
2763     gtk_widget_set_tooltip_text (navigation_button, "DVD Angle Menu");
2764     app->navigation_buttons[i].button = navigation_button;
2765     app->navigation_buttons[i++].cmd = GST_NAVIGATION_COMMAND_MENU6;
2766
2767     navigation_button = gtk_button_new_with_label ("Menu 7");
2768     g_signal_connect (G_OBJECT (navigation_button), "clicked",
2769         G_CALLBACK (navigation_cmd_cb), app);
2770     gtk_grid_attach (GTK_GRID (grid), navigation_button, i, 0, 1, 1);
2771     gtk_widget_set_sensitive (navigation_button, FALSE);
2772     gtk_widget_set_tooltip_text (navigation_button, "DVD Chapter Menu");
2773     app->navigation_buttons[i].button = navigation_button;
2774     app->navigation_buttons[i++].cmd = GST_NAVIGATION_COMMAND_MENU7;
2775
2776     navigation_button = gtk_button_new_with_label ("Left");
2777     g_signal_connect (G_OBJECT (navigation_button), "clicked",
2778         G_CALLBACK (navigation_cmd_cb), app);
2779     gtk_grid_attach (GTK_GRID (grid), navigation_button, i - 7, 1, 1, 1);
2780     gtk_widget_set_sensitive (navigation_button, FALSE);
2781     app->navigation_buttons[i].button = navigation_button;
2782     app->navigation_buttons[i++].cmd = GST_NAVIGATION_COMMAND_LEFT;
2783
2784     navigation_button = gtk_button_new_with_label ("Right");
2785     g_signal_connect (G_OBJECT (navigation_button), "clicked",
2786         G_CALLBACK (navigation_cmd_cb), app);
2787     gtk_grid_attach (GTK_GRID (grid), navigation_button, i - 7, 1, 1, 1);
2788     gtk_widget_set_sensitive (navigation_button, FALSE);
2789     app->navigation_buttons[i].button = navigation_button;
2790     app->navigation_buttons[i++].cmd = GST_NAVIGATION_COMMAND_RIGHT;
2791
2792     navigation_button = gtk_button_new_with_label ("Up");
2793     g_signal_connect (G_OBJECT (navigation_button), "clicked",
2794         G_CALLBACK (navigation_cmd_cb), app);
2795     gtk_grid_attach (GTK_GRID (grid), navigation_button, i - 7, 1, 1, 1);
2796     gtk_widget_set_sensitive (navigation_button, FALSE);
2797     app->navigation_buttons[i].button = navigation_button;
2798     app->navigation_buttons[i++].cmd = GST_NAVIGATION_COMMAND_UP;
2799
2800     navigation_button = gtk_button_new_with_label ("Down");
2801     g_signal_connect (G_OBJECT (navigation_button), "clicked",
2802         G_CALLBACK (navigation_cmd_cb), app);
2803     gtk_grid_attach (GTK_GRID (grid), navigation_button, i - 7, 1, 1, 1);
2804     gtk_widget_set_sensitive (navigation_button, FALSE);
2805     app->navigation_buttons[i].button = navigation_button;
2806     app->navigation_buttons[i++].cmd = GST_NAVIGATION_COMMAND_DOWN;
2807
2808     navigation_button = gtk_button_new_with_label ("Activate");
2809     g_signal_connect (G_OBJECT (navigation_button), "clicked",
2810         G_CALLBACK (navigation_cmd_cb), app);
2811     gtk_grid_attach (GTK_GRID (grid), navigation_button, i - 7, 1, 1, 1);
2812     gtk_widget_set_sensitive (navigation_button, FALSE);
2813     app->navigation_buttons[i].button = navigation_button;
2814     app->navigation_buttons[i++].cmd = GST_NAVIGATION_COMMAND_ACTIVATE;
2815
2816     navigation_button = gtk_button_new_with_label ("Prev. Angle");
2817     g_signal_connect (G_OBJECT (navigation_button), "clicked",
2818         G_CALLBACK (navigation_cmd_cb), app);
2819     gtk_grid_attach (GTK_GRID (grid), navigation_button, i - 7, 1, 1, 1);
2820     gtk_widget_set_sensitive (navigation_button, FALSE);
2821     app->navigation_buttons[i].button = navigation_button;
2822     app->navigation_buttons[i++].cmd = GST_NAVIGATION_COMMAND_PREV_ANGLE;
2823
2824     navigation_button = gtk_button_new_with_label ("Next. Angle");
2825     g_signal_connect (G_OBJECT (navigation_button), "clicked",
2826         G_CALLBACK (navigation_cmd_cb), app);
2827     gtk_grid_attach (GTK_GRID (grid), navigation_button, i - 7, 1, 1, 1);
2828     gtk_widget_set_sensitive (navigation_button, FALSE);
2829     app->navigation_buttons[i].button = navigation_button;
2830     app->navigation_buttons[i++].cmd = GST_NAVIGATION_COMMAND_NEXT_ANGLE;
2831
2832     gtk_container_add (GTK_CONTAINER (navigation), grid);
2833   }
2834
2835   /* colorbalance expander */
2836   {
2837     GtkWidget *vbox, *frame;
2838
2839     colorbalance = gtk_expander_new ("color balance options");
2840     vbox = gtk_vbox_new (FALSE, 0);
2841
2842     /* contrast scale */
2843     frame = gtk_frame_new ("Contrast");
2844     adjustment =
2845         GTK_ADJUSTMENT (gtk_adjustment_new (N_GRAD / 2.0, 0.00, N_GRAD, 0.1,
2846             1.0, 1.0));
2847     app->contrast_scale = gtk_hscale_new (adjustment);
2848     gtk_scale_set_draw_value (GTK_SCALE (app->contrast_scale), FALSE);
2849     g_signal_connect (app->contrast_scale, "value-changed",
2850         G_CALLBACK (colorbalance_value_changed), app);
2851     gtk_container_add (GTK_CONTAINER (frame), app->contrast_scale);
2852     gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 2);
2853
2854     /* brightness scale */
2855     frame = gtk_frame_new ("Brightness");
2856     adjustment =
2857         GTK_ADJUSTMENT (gtk_adjustment_new (N_GRAD / 2.0, 0.00, N_GRAD, 0.1,
2858             1.0, 1.0));
2859     app->brightness_scale = gtk_hscale_new (adjustment);
2860     gtk_scale_set_draw_value (GTK_SCALE (app->brightness_scale), FALSE);
2861     g_signal_connect (app->brightness_scale, "value-changed",
2862         G_CALLBACK (colorbalance_value_changed), app);
2863     gtk_container_add (GTK_CONTAINER (frame), app->brightness_scale);
2864     gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 2);
2865
2866     /* hue scale */
2867     frame = gtk_frame_new ("Hue");
2868     adjustment =
2869         GTK_ADJUSTMENT (gtk_adjustment_new (N_GRAD / 2.0, 0.00, N_GRAD, 0.1,
2870             1.0, 1.0));
2871     app->hue_scale = gtk_hscale_new (adjustment);
2872     gtk_scale_set_draw_value (GTK_SCALE (app->hue_scale), FALSE);
2873     g_signal_connect (app->hue_scale, "value-changed",
2874         G_CALLBACK (colorbalance_value_changed), app);
2875     gtk_container_add (GTK_CONTAINER (frame), app->hue_scale);
2876     gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 2);
2877
2878     /* saturation scale */
2879     frame = gtk_frame_new ("Saturation");
2880     adjustment =
2881         GTK_ADJUSTMENT (gtk_adjustment_new (N_GRAD / 2.0, 0.00, N_GRAD, 0.1,
2882             1.0, 1.0));
2883     app->saturation_scale = gtk_hscale_new (adjustment);
2884     gtk_scale_set_draw_value (GTK_SCALE (app->saturation_scale), FALSE);
2885     g_signal_connect (app->saturation_scale, "value-changed",
2886         G_CALLBACK (colorbalance_value_changed), app);
2887     gtk_container_add (GTK_CONTAINER (frame), app->saturation_scale);
2888     gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 2);
2889
2890     gtk_container_add (GTK_CONTAINER (colorbalance), vbox);
2891   }
2892
2893   /* seek bar */
2894   adjustment =
2895       GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.00, N_GRAD, 0.1, 1.0, 1.0));
2896   app->seek_scale = gtk_hscale_new (adjustment);
2897   gtk_scale_set_digits (GTK_SCALE (app->seek_scale), 2);
2898   gtk_scale_set_value_pos (GTK_SCALE (app->seek_scale), GTK_POS_RIGHT);
2899   gtk_range_set_show_fill_level (GTK_RANGE (app->seek_scale), TRUE);
2900   gtk_range_set_fill_level (GTK_RANGE (app->seek_scale), N_GRAD);
2901
2902   g_signal_connect (app->seek_scale, "button_press_event",
2903       G_CALLBACK (start_seek), app);
2904   g_signal_connect (app->seek_scale, "button_release_event",
2905       G_CALLBACK (stop_seek), app);
2906   g_signal_connect (app->seek_scale, "format_value", G_CALLBACK (format_value),
2907       app);
2908
2909   if (app->pipeline_type == 0) {
2910     GtkWidget *pb2vbox, *boxes, *boxes2, *panel, *boxes3;
2911     GtkWidget *volume_label, *shot_button;
2912     GtkWidget *label;
2913
2914     playbin = gtk_expander_new ("playbin2 options");
2915     /* the playbin2 panel controls for the video/audio/subtitle tracks */
2916     panel = gtk_hbox_new (FALSE, 0);
2917     app->video_combo = gtk_combo_box_text_new ();
2918     app->audio_combo = gtk_combo_box_text_new ();
2919     app->text_combo = gtk_combo_box_text_new ();
2920     gtk_widget_set_sensitive (app->video_combo, FALSE);
2921     gtk_widget_set_sensitive (app->audio_combo, FALSE);
2922     gtk_widget_set_sensitive (app->text_combo, FALSE);
2923     gtk_box_pack_start (GTK_BOX (panel), app->video_combo, TRUE, TRUE, 2);
2924     gtk_box_pack_start (GTK_BOX (panel), app->audio_combo, TRUE, TRUE, 2);
2925     gtk_box_pack_start (GTK_BOX (panel), app->text_combo, TRUE, TRUE, 2);
2926     g_signal_connect (G_OBJECT (app->video_combo), "changed",
2927         G_CALLBACK (video_combo_cb), app);
2928     g_signal_connect (G_OBJECT (app->audio_combo), "changed",
2929         G_CALLBACK (audio_combo_cb), app);
2930     g_signal_connect (G_OBJECT (app->text_combo), "changed",
2931         G_CALLBACK (text_combo_cb), app);
2932     /* playbin2 panel for flag checkboxes and volume/mute */
2933     boxes = gtk_grid_new ();
2934     gtk_grid_set_row_spacing (GTK_GRID (boxes), 2);
2935     gtk_grid_set_row_homogeneous (GTK_GRID (boxes), FALSE);
2936     gtk_grid_set_column_spacing (GTK_GRID (boxes), 2);
2937     gtk_grid_set_column_homogeneous (GTK_GRID (boxes), TRUE);
2938
2939     app->video_checkbox = gtk_check_button_new_with_label ("Video");
2940     app->audio_checkbox = gtk_check_button_new_with_label ("Audio");
2941     app->text_checkbox = gtk_check_button_new_with_label ("Text");
2942     app->vis_checkbox = gtk_check_button_new_with_label ("Vis");
2943     app->soft_volume_checkbox = gtk_check_button_new_with_label ("Soft Volume");
2944     app->native_audio_checkbox =
2945         gtk_check_button_new_with_label ("Native Audio");
2946     app->native_video_checkbox =
2947         gtk_check_button_new_with_label ("Native Video");
2948     app->download_checkbox = gtk_check_button_new_with_label ("Download");
2949     app->buffering_checkbox = gtk_check_button_new_with_label ("Buffering");
2950     app->deinterlace_checkbox = gtk_check_button_new_with_label ("Deinterlace");
2951     app->soft_colorbalance_checkbox =
2952         gtk_check_button_new_with_label ("Soft Colorbalance");
2953     app->mute_checkbox = gtk_check_button_new_with_label ("Mute");
2954     volume_label = gtk_label_new ("Volume");
2955     app->volume_spinbutton = gtk_spin_button_new_with_range (0, 10.0, 0.1);
2956
2957     gtk_grid_attach (GTK_GRID (boxes), app->video_checkbox, 0, 0, 1, 1);
2958     gtk_grid_attach (GTK_GRID (boxes), app->audio_checkbox, 1, 0, 1, 1);
2959     gtk_grid_attach (GTK_GRID (boxes), app->text_checkbox, 2, 0, 1, 1);
2960     gtk_grid_attach (GTK_GRID (boxes), app->vis_checkbox, 3, 0, 1, 1);
2961     gtk_grid_attach (GTK_GRID (boxes), app->soft_volume_checkbox, 4, 0, 1, 1);
2962     gtk_grid_attach (GTK_GRID (boxes), app->native_audio_checkbox, 5, 0, 1, 1);
2963     gtk_grid_attach (GTK_GRID (boxes), app->native_video_checkbox, 0, 1, 1, 1);
2964     gtk_grid_attach (GTK_GRID (boxes), app->download_checkbox, 1, 1, 1, 1);
2965     gtk_grid_attach (GTK_GRID (boxes), app->buffering_checkbox, 2, 1, 1, 1);
2966     gtk_grid_attach (GTK_GRID (boxes), app->deinterlace_checkbox, 3, 1, 1, 1);
2967     gtk_grid_attach (GTK_GRID (boxes), app->soft_colorbalance_checkbox, 4, 1, 1,
2968         1);
2969
2970     gtk_grid_attach (GTK_GRID (boxes), app->mute_checkbox, 7, 0, 2, 1);
2971     gtk_grid_attach (GTK_GRID (boxes), volume_label, 6, 1, 1, 1);
2972     gtk_grid_attach (GTK_GRID (boxes), app->volume_spinbutton, 7, 1, 1, 1);
2973
2974     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (app->video_checkbox),
2975         TRUE);
2976     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (app->audio_checkbox),
2977         TRUE);
2978     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (app->text_checkbox), TRUE);
2979     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (app->vis_checkbox), FALSE);
2980     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (app->soft_volume_checkbox),
2981         TRUE);
2982     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
2983         (app->native_audio_checkbox), FALSE);
2984     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
2985         (app->native_video_checkbox), FALSE);
2986     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (app->download_checkbox),
2987         FALSE);
2988     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (app->buffering_checkbox),
2989         FALSE);
2990     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (app->deinterlace_checkbox),
2991         FALSE);
2992     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
2993         (app->soft_colorbalance_checkbox), TRUE);
2994     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (app->mute_checkbox),
2995         FALSE);
2996     gtk_spin_button_set_value (GTK_SPIN_BUTTON (app->volume_spinbutton), 1.0);
2997
2998     g_signal_connect (G_OBJECT (app->video_checkbox), "toggled",
2999         G_CALLBACK (video_toggle_cb), app);
3000     g_signal_connect (G_OBJECT (app->audio_checkbox), "toggled",
3001         G_CALLBACK (audio_toggle_cb), app);
3002     g_signal_connect (G_OBJECT (app->text_checkbox), "toggled",
3003         G_CALLBACK (text_toggle_cb), app);
3004     g_signal_connect (G_OBJECT (app->vis_checkbox), "toggled",
3005         G_CALLBACK (vis_toggle_cb), app);
3006     g_signal_connect (G_OBJECT (app->soft_volume_checkbox), "toggled",
3007         G_CALLBACK (soft_volume_toggle_cb), app);
3008     g_signal_connect (G_OBJECT (app->native_audio_checkbox), "toggled",
3009         G_CALLBACK (native_audio_toggle_cb), app);
3010     g_signal_connect (G_OBJECT (app->native_video_checkbox), "toggled",
3011         G_CALLBACK (native_video_toggle_cb), app);
3012     g_signal_connect (G_OBJECT (app->download_checkbox), "toggled",
3013         G_CALLBACK (download_toggle_cb), app);
3014     g_signal_connect (G_OBJECT (app->buffering_checkbox), "toggled",
3015         G_CALLBACK (buffering_toggle_cb), app);
3016     g_signal_connect (G_OBJECT (app->deinterlace_checkbox), "toggled",
3017         G_CALLBACK (deinterlace_toggle_cb), app);
3018     g_signal_connect (G_OBJECT (app->soft_colorbalance_checkbox), "toggled",
3019         G_CALLBACK (soft_colorbalance_toggle_cb), app);
3020     g_signal_connect (G_OBJECT (app->mute_checkbox), "toggled",
3021         G_CALLBACK (mute_toggle_cb), app);
3022     g_signal_connect (G_OBJECT (app->volume_spinbutton), "value-changed",
3023         G_CALLBACK (volume_spinbutton_changed_cb), app);
3024     /* playbin2 panel for snapshot */
3025     boxes2 = gtk_hbox_new (FALSE, 0);
3026     shot_button = gtk_button_new_from_stock (GTK_STOCK_SAVE);
3027     gtk_widget_set_tooltip_text (shot_button,
3028         "save a screenshot .png in the current directory");
3029     g_signal_connect (G_OBJECT (shot_button), "clicked", G_CALLBACK (shot_cb),
3030         app);
3031     app->vis_combo = gtk_combo_box_text_new ();
3032     g_signal_connect (G_OBJECT (app->vis_combo), "changed",
3033         G_CALLBACK (vis_combo_cb), app);
3034     gtk_widget_set_sensitive (app->vis_combo, FALSE);
3035     gtk_box_pack_start (GTK_BOX (boxes2), shot_button, TRUE, TRUE, 2);
3036     gtk_box_pack_start (GTK_BOX (boxes2), app->vis_combo, TRUE, TRUE, 2);
3037
3038     /* fill the vis combo box and the array of factories */
3039     init_visualization_features (app);
3040
3041     /* Grid with other properties */
3042     boxes3 = gtk_grid_new ();
3043     gtk_grid_set_row_spacing (GTK_GRID (boxes3), 2);
3044     gtk_grid_set_row_homogeneous (GTK_GRID (boxes3), FALSE);
3045     gtk_grid_set_column_spacing (GTK_GRID (boxes3), 2);
3046     gtk_grid_set_column_homogeneous (GTK_GRID (boxes3), TRUE);
3047
3048     label = gtk_label_new ("Video sink");
3049     gtk_grid_attach (GTK_GRID (boxes3), label, 0, 0, 1, 1);
3050     app->video_sink_entry = gtk_entry_new ();
3051     g_signal_connect (app->video_sink_entry, "activate",
3052         G_CALLBACK (video_sink_activate_cb), app);
3053     gtk_grid_attach (GTK_GRID (boxes3), app->video_sink_entry, 0, 1, 1, 1);
3054
3055     label = gtk_label_new ("Audio sink");
3056     gtk_grid_attach (GTK_GRID (boxes3), label, 1, 0, 1, 1);
3057     app->audio_sink_entry = gtk_entry_new ();
3058     g_signal_connect (app->audio_sink_entry, "activate",
3059         G_CALLBACK (audio_sink_activate_cb), app);
3060     gtk_grid_attach (GTK_GRID (boxes3), app->audio_sink_entry, 1, 1, 1, 1);
3061
3062     label = gtk_label_new ("Text sink");
3063     gtk_grid_attach (GTK_GRID (boxes3), label, 2, 0, 1, 1);
3064     app->text_sink_entry = gtk_entry_new ();
3065     g_signal_connect (app->text_sink_entry, "activate",
3066         G_CALLBACK (text_sink_activate_cb), app);
3067     gtk_grid_attach (GTK_GRID (boxes3), app->text_sink_entry, 2, 1, 1, 1);
3068
3069     label = gtk_label_new ("Buffer Size");
3070     gtk_grid_attach (GTK_GRID (boxes3), label, 0, 2, 1, 1);
3071     app->buffer_size_entry = gtk_entry_new ();
3072     gtk_entry_set_text (GTK_ENTRY (app->buffer_size_entry), "-1");
3073     g_signal_connect (app->buffer_size_entry, "activate",
3074         G_CALLBACK (buffer_size_activate_cb), app);
3075     gtk_grid_attach (GTK_GRID (boxes3), app->buffer_size_entry, 0, 3, 1, 1);
3076
3077     label = gtk_label_new ("Buffer Duration");
3078     gtk_grid_attach (GTK_GRID (boxes3), label, 1, 2, 1, 1);
3079     app->buffer_duration_entry = gtk_entry_new ();
3080     gtk_entry_set_text (GTK_ENTRY (app->buffer_duration_entry), "-1");
3081     g_signal_connect (app->buffer_duration_entry, "activate",
3082         G_CALLBACK (buffer_duration_activate_cb), app);
3083     gtk_grid_attach (GTK_GRID (boxes3), app->buffer_duration_entry, 1, 3, 1, 1);
3084
3085     label = gtk_label_new ("Ringbuffer Max Size");
3086     gtk_grid_attach (GTK_GRID (boxes3), label, 2, 2, 1, 1);
3087     app->ringbuffer_maxsize_entry = gtk_entry_new ();
3088     gtk_entry_set_text (GTK_ENTRY (app->ringbuffer_maxsize_entry), "0");
3089     g_signal_connect (app->ringbuffer_maxsize_entry, "activate",
3090         G_CALLBACK (ringbuffer_maxsize_activate_cb), app);
3091     gtk_grid_attach (GTK_GRID (boxes3), app->ringbuffer_maxsize_entry, 2, 3, 1,
3092         1);
3093
3094     label = gtk_label_new ("Connection Speed");
3095     gtk_grid_attach (GTK_GRID (boxes3), label, 3, 2, 1, 1);
3096     app->connection_speed_entry = gtk_entry_new ();
3097     gtk_entry_set_text (GTK_ENTRY (app->connection_speed_entry), "0");
3098     g_signal_connect (app->connection_speed_entry, "activate",
3099         G_CALLBACK (connection_speed_activate_cb), app);
3100     gtk_grid_attach (GTK_GRID (boxes3), app->connection_speed_entry, 3, 3, 1,
3101         1);
3102
3103     label = gtk_label_new ("A/V offset");
3104     gtk_grid_attach (GTK_GRID (boxes3), label, 4, 2, 1, 1);
3105     app->av_offset_entry = gtk_entry_new ();
3106     g_signal_connect (app->av_offset_entry, "activate",
3107         G_CALLBACK (av_offset_activate_cb), app);
3108     gtk_entry_set_text (GTK_ENTRY (app->av_offset_entry), "0");
3109     g_signal_connect (app->av_offset_entry, "activate",
3110         G_CALLBACK (av_offset_activate_cb), app);
3111     gtk_grid_attach (GTK_GRID (boxes3), app->av_offset_entry, 4, 3, 1, 1);
3112
3113     label = gtk_label_new ("Subtitle Encoding");
3114     gtk_grid_attach (GTK_GRID (boxes3), label, 0, 4, 1, 1);
3115     app->subtitle_encoding_entry = gtk_entry_new ();
3116     g_signal_connect (app->subtitle_encoding_entry, "activate",
3117         G_CALLBACK (subtitle_encoding_activate_cb), app);
3118     gtk_grid_attach (GTK_GRID (boxes3), app->subtitle_encoding_entry, 0, 5, 1,
3119         1);
3120
3121     label = gtk_label_new ("Subtitle Fontdesc");
3122     gtk_grid_attach (GTK_GRID (boxes3), label, 1, 4, 1, 1);
3123     app->subtitle_fontdesc_button = gtk_font_button_new ();
3124     g_signal_connect (app->subtitle_fontdesc_button, "font-set",
3125         G_CALLBACK (subtitle_fontdesc_cb), app);
3126     gtk_grid_attach (GTK_GRID (boxes3), app->subtitle_fontdesc_button, 1, 5, 1,
3127         1);
3128
3129     pb2vbox = gtk_vbox_new (FALSE, 0);
3130     gtk_box_pack_start (GTK_BOX (pb2vbox), panel, FALSE, FALSE, 2);
3131     gtk_box_pack_start (GTK_BOX (pb2vbox), boxes, FALSE, FALSE, 2);
3132     gtk_box_pack_start (GTK_BOX (pb2vbox), boxes2, FALSE, FALSE, 2);
3133     gtk_box_pack_start (GTK_BOX (pb2vbox), boxes3, FALSE, FALSE, 2);
3134     gtk_container_add (GTK_CONTAINER (playbin), pb2vbox);
3135   } else {
3136     playbin = NULL;
3137   }
3138
3139   /* do the packing stuff ... */
3140   gtk_window_set_default_size (GTK_WINDOW (app->window), 250, 96);
3141   /* FIXME: can we avoid this for audio only? */
3142   gtk_widget_set_size_request (GTK_WIDGET (app->video_window), -1,
3143       DEFAULT_VIDEO_HEIGHT);
3144   gtk_container_add (GTK_CONTAINER (app->window), vbox);
3145   gtk_box_pack_start (GTK_BOX (vbox), app->video_window, TRUE, TRUE, 2);
3146   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 2);
3147   gtk_box_pack_start (GTK_BOX (hbox), play_button, FALSE, FALSE, 2);
3148   gtk_box_pack_start (GTK_BOX (hbox), pause_button, FALSE, FALSE, 2);
3149   gtk_box_pack_start (GTK_BOX (hbox), stop_button, FALSE, FALSE, 2);
3150
3151   gtk_box_pack_start (GTK_BOX (vbox), seek, FALSE, FALSE, 2);
3152   if (playbin)
3153     gtk_box_pack_start (GTK_BOX (vbox), playbin, FALSE, FALSE, 2);
3154   gtk_box_pack_start (GTK_BOX (vbox), step, FALSE, FALSE, 2);
3155   gtk_box_pack_start (GTK_BOX (vbox), navigation, FALSE, FALSE, 2);
3156   gtk_box_pack_start (GTK_BOX (vbox), colorbalance, FALSE, FALSE, 2);
3157   gtk_box_pack_start (GTK_BOX (vbox), gtk_hseparator_new (), FALSE, FALSE, 2);
3158   gtk_box_pack_start (GTK_BOX (vbox), app->seek_scale, FALSE, FALSE, 2);
3159   gtk_box_pack_start (GTK_BOX (vbox), app->statusbar, FALSE, FALSE, 2);
3160
3161   /* connect things ... */
3162   g_signal_connect (G_OBJECT (play_button), "clicked", G_CALLBACK (play_cb),
3163       app);
3164   g_signal_connect (G_OBJECT (pause_button), "clicked", G_CALLBACK (pause_cb),
3165       app);
3166   g_signal_connect (G_OBJECT (stop_button), "clicked", G_CALLBACK (stop_cb),
3167       app);
3168
3169   g_signal_connect (G_OBJECT (app->window), "delete-event",
3170       G_CALLBACK (delete_event_cb), app);
3171 }
3172
3173 static void
3174 set_defaults (SeekApp * app)
3175 {
3176   memset (app, 0, sizeof (SeekApp));
3177
3178   app->flush_seek = TRUE;
3179   app->scrub = TRUE;
3180   app->rate = 1.0;
3181
3182   app->position = app->duration = -1;
3183   app->state = GST_STATE_NULL;
3184
3185   app->need_streams = TRUE;
3186
3187   g_static_mutex_init (&app->state_mutex);
3188
3189   app->play_rate = 1.0;
3190 }
3191
3192 static void
3193 reset_app (SeekApp * app)
3194 {
3195   g_free (app->audiosink_str);
3196   g_free (app->videosink_str);
3197
3198   g_list_free (app->formats);
3199
3200   g_static_mutex_free (&app->state_mutex);
3201
3202   if (app->xoverlay_element)
3203     gst_object_unref (app->xoverlay_element);
3204   if (app->navigation_element)
3205     gst_object_unref (app->navigation_element);
3206
3207   g_list_foreach (app->paths, (GFunc) g_free, NULL);
3208   g_list_free (app->paths);
3209
3210   g_print ("free pipeline\n");
3211   gst_object_unref (app->pipeline);
3212 }
3213
3214 int
3215 main (int argc, char **argv)
3216 {
3217   SeekApp app;
3218   GOptionEntry options[] = {
3219     {"stats", 's', 0, G_OPTION_ARG_NONE, &app.stats,
3220         "Show pad stats", NULL},
3221     {"verbose", 'v', 0, G_OPTION_ARG_NONE, &app.verbose,
3222         "Verbose properties", NULL},
3223     {NULL}
3224   };
3225   GOptionContext *ctx;
3226   GError *err = NULL;
3227
3228   set_defaults (&app);
3229
3230 #if !GLIB_CHECK_VERSION (2, 31, 0)
3231   if (!g_thread_supported ())
3232     g_thread_init (NULL);
3233 #endif
3234
3235   ctx = g_option_context_new ("- test seeking in gsteamer");
3236   g_option_context_add_main_entries (ctx, options, NULL);
3237   g_option_context_add_group (ctx, gst_init_get_option_group ());
3238   g_option_context_add_group (ctx, gtk_get_option_group (TRUE));
3239
3240   if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
3241     g_print ("Error initializing: %s\n", err->message);
3242     exit (1);
3243   }
3244
3245   GST_DEBUG_CATEGORY_INIT (seek_debug, "seek", 0, "seek example");
3246
3247   if (argc != 3) {
3248     print_usage (argc, argv);
3249     exit (-1);
3250   }
3251
3252   app.pipeline_type = atoi (argv[1]);
3253
3254   if (app.pipeline_type < 0 || app.pipeline_type >= G_N_ELEMENTS (pipelines)) {
3255     print_usage (argc, argv);
3256     exit (-1);
3257   }
3258
3259   app.pipeline_spec = argv[2];
3260
3261   if (g_path_is_absolute (app.pipeline_spec) &&
3262       (g_strrstr (app.pipeline_spec, "*") != NULL ||
3263           g_strrstr (app.pipeline_spec, "?") != NULL)) {
3264     app.paths = handle_wildcards (app.pipeline_spec);
3265   } else {
3266     app.paths = g_list_prepend (app.paths, g_strdup (app.pipeline_spec));
3267   }
3268
3269   if (!app.paths) {
3270     g_print ("opening %s failed\n", app.pipeline_spec);
3271     exit (-1);
3272   }
3273
3274   app.current_path = app.paths;
3275
3276   pipelines[app.pipeline_type].func (&app, app.current_path->data);
3277   g_assert (app.pipeline);
3278
3279   create_ui (&app);
3280
3281   /* show the gui. */
3282   gtk_widget_show_all (app.window);
3283
3284   /* realize window now so that the video window gets created and we can
3285    * obtain its XID before the pipeline is started up and the videosink
3286    * asks for the XID of the window to render onto */
3287   gtk_widget_realize (app.window);
3288
3289 #if defined (GDK_WINDOWING_X11) || defined (GDK_WINDOWING_WIN32) || defined (GDK_WINDOWING_QUARTZ)
3290   /* we should have the XID now */
3291   g_assert (app.embed_xid != 0);
3292
3293   if (app.pipeline_type == 0) {
3294     gst_x_overlay_set_window_handle (GST_X_OVERLAY (app.pipeline),
3295         app.embed_xid);
3296   }
3297 #endif
3298
3299   if (app.verbose) {
3300     g_signal_connect (app.pipeline, "deep_notify",
3301         G_CALLBACK (gst_object_default_deep_notify), NULL);
3302   }
3303
3304   connect_bus_signals (&app);
3305
3306   gtk_main ();
3307
3308   g_print ("NULL pipeline\n");
3309   gst_element_set_state (app.pipeline, GST_STATE_NULL);
3310
3311   reset_app (&app);
3312
3313   return 0;
3314 }