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