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