3 * seek.c: seeking sample application
5 * Copyright (C) 2005 Wim Taymans <wim@fluendo.com>
6 * 2006 Stefan Kost <ensonic@users.sf.net>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
36 #if defined (GDK_WINDOWING_X11)
38 #elif defined (GDK_WINDOWING_WIN32)
39 #include <gdk/gdkwin32.h>
42 #include <gst/video/videooverlay.h>
44 GST_DEBUG_CATEGORY_STATIC (seek_debug);
45 #define GST_CAT_DEFAULT (seek_debug)
49 #define SOURCE "filesrc"
51 static gchar *opt_audiosink_str; /* NULL */
52 static gchar *opt_videosink_str; /* NULL */
54 #define FILL_INTERVAL 100
55 //#define UPDATE_INTERVAL 500
56 //#define UPDATE_INTERVAL 100
57 #define UPDATE_INTERVAL 40
59 /* number of milliseconds to play for after a seek */
60 #define SCRUB_TIME 100
62 /* timeout for gst_element_get_state() after a seek */
63 #define SEEK_TIMEOUT 40 * GST_MSECOND
65 #define DEFAULT_VIDEO_HEIGHT 300
67 /* the state to go to when stop is pressed */
68 #define STOP_STATE GST_STATE_READY
72 static GList *seekable_elements = NULL;
74 static gboolean accurate_seek = FALSE;
75 static gboolean keyframe_seek = FALSE;
76 static gboolean loop_seek = FALSE;
77 static gboolean flush_seek = TRUE;
78 static gboolean scrub = TRUE;
79 static gboolean play_scrub = FALSE;
80 static gboolean skip_seek = FALSE;
81 static gdouble rate = 1.0;
83 static GstElement *pipeline;
84 static gint pipeline_type;
85 static const gchar *pipeline_spec;
86 static gint64 position = -1;
87 static gint64 duration = -1;
88 static GtkAdjustment *adjustment;
89 static GtkWidget *hscale, *statusbar;
90 static guint status_id = 0;
91 static gboolean stats = FALSE;
92 static gboolean verbose = FALSE;
94 static gboolean is_live = FALSE;
95 static gboolean buffering = FALSE;
96 static GstBufferingMode mode;
97 static gint64 buffering_left;
98 static GstState state = GST_STATE_NULL;
99 static guint update_id = 0;
100 static guint seek_timeout_id = 0;
101 static gulong changed_id;
102 static guint fill_id = 0;
104 static gint n_video = 0, n_audio = 0, n_text = 0;
105 static gboolean need_streams = TRUE;
106 static GtkWidget *video_combo, *audio_combo, *text_combo, *vis_combo;
107 static GtkWidget *vis_checkbox, *video_checkbox, *audio_checkbox;
108 static GtkWidget *text_checkbox, *mute_checkbox, *volume_spinbutton;
109 static GtkWidget *skip_checkbox, *video_window, *download_checkbox;
110 static GtkWidget *buffer_checkbox, *rate_spinbutton;
112 static GStaticMutex state_mutex = G_STATIC_MUTEX_INIT;
114 static GtkWidget *format_combo, *step_amount_spinbutton, *step_rate_spinbutton;
115 static GtkWidget *shuttle_checkbox, *step_button;
116 static GtkWidget *shuttle_hscale;
117 static GtkAdjustment *shuttle_adjustment;
119 static GList *paths = NULL, *l = NULL;
121 /* we keep an array of the visualisation entries so that we can easily switch
122 * with the combo box index. */
125 GstElementFactory *factory;
128 static GArray *vis_entries;
130 static void clear_streams (GstElement * pipeline);
131 static void volume_notify_cb (GstElement * pipeline, GParamSpec * arg,
134 /* pipeline construction */
138 const gchar *padname;
145 gst_element_factory_make_or_warn (const gchar * type, const gchar * name)
147 GstElement *element = gst_element_factory_make (type, name);
149 #ifndef GST_DISABLE_PARSE
151 /* Try parsing it as a pipeline description */
152 element = gst_parse_bin_from_description (type, TRUE, NULL);
154 gst_element_set_name (element, name);
160 g_warning ("Failed to create element %s of type %s", name, type);
167 playerbin_set_uri (GstElement * player, const gchar * location)
171 /* Add "file://" prefix for convenience */
172 if (g_str_has_prefix (location, "/") || !gst_uri_is_valid (location)) {
173 uri = gst_filename_to_uri (location, NULL);
174 g_print ("Setting URI: %s\n", uri);
175 g_object_set (G_OBJECT (player), "uri", uri, NULL);
178 g_print ("Setting URI: %s\n", location);
179 g_object_set (G_OBJECT (player), "uri", location, NULL);
184 construct_playbin (const gchar * name, const gchar * location)
189 player = gst_element_factory_make (name, "player");
192 playerbin_set_uri (player, location);
194 seekable_elements = g_list_prepend (seekable_elements, player);
196 avsink = gst_element_factory_make_or_warn (opt_audiosink_str, "a_sink");
198 g_object_set (player, "audio-sink", avsink, NULL);
200 avsink = gst_element_factory_make_or_warn (opt_videosink_str, "v_sink");
202 g_object_set (player, "video-sink", avsink, NULL);
208 make_playbin_pipeline (const gchar * location)
210 GstElement *pipeline = construct_playbin ("playbin", location);
212 /* FIXME: this is not triggered, playbin is not forwarding it from the sink */
213 g_signal_connect (pipeline, "notify::volume", G_CALLBACK (volume_notify_cb),
218 #ifndef GST_DISABLE_PARSE
220 make_parselaunch_pipeline (const gchar * description)
222 GstElement *pipeline;
223 GError *error = NULL;
225 pipeline = gst_parse_launch (description, &error);
227 seekable_elements = g_list_prepend (seekable_elements, pipeline);
236 GstElement *(*func) (const gchar * location);
240 static Pipeline pipelines[] = {
241 {"playbin", make_playbin_pipeline},
242 #ifndef GST_DISABLE_PARSE
243 {"parse-launch", make_parselaunch_pipeline},
248 #define NUM_TYPES ((sizeof (pipelines) / sizeof (Pipeline)) - 1)
250 /* ui callbacks and helpers */
253 format_value (GtkScale * scale, gdouble value)
259 real = value * duration / N_GRAD;
260 seconds = (gint64) real / GST_SECOND;
261 subseconds = (gint64) real / (GST_SECOND / N_GRAD);
263 return g_strdup_printf ("%02" G_GINT64_FORMAT ":%02" G_GINT64_FORMAT ":%02"
264 G_GINT64_FORMAT, seconds / 60, seconds % 60, subseconds % 100);
269 shuttle_format_value (GtkScale * scale, gdouble value)
271 return g_strdup_printf ("%0.*g", gtk_scale_get_digits (scale), value);
277 const GstFormat format;
281 static seek_format seek_formats[] = {
282 {"tim", GST_FORMAT_TIME},
283 {"byt", GST_FORMAT_BYTES},
284 {"buf", GST_FORMAT_BUFFERS},
285 {"def", GST_FORMAT_DEFAULT},
289 G_GNUC_UNUSED static void
290 query_positions_elems (void)
292 GList *walk = seekable_elements;
295 GstElement *element = GST_ELEMENT (walk->data);
298 g_print ("positions %8.8s: ", GST_ELEMENT_NAME (element));
299 while (seek_formats[i].name) {
300 gint64 position, total;
303 format = seek_formats[i].format;
305 if (gst_element_query_position (element, format, &position) &&
306 gst_element_query_duration (element, format, &total)) {
307 g_print ("%s %13" G_GINT64_FORMAT " / %13" G_GINT64_FORMAT " | ",
308 seek_formats[i].name, position, total);
310 g_print ("%s %13.13s / %13.13s | ", seek_formats[i].name, "*NA*",
315 g_print (" %s\n", GST_ELEMENT_NAME (element));
317 walk = g_list_next (walk);
321 static gboolean start_seek (GtkWidget * widget, GdkEventButton * event,
323 static gboolean stop_seek (GtkWidget * widget, GdkEventButton * event,
325 static void seek_cb (GtkWidget * widget);
328 set_scale (gdouble value)
330 g_signal_handlers_block_by_func (hscale, (void *) start_seek,
332 g_signal_handlers_block_by_func (hscale, (void *) stop_seek,
334 g_signal_handlers_block_by_func (hscale, (void *) seek_cb, (void *) pipeline);
335 gtk_adjustment_set_value (adjustment, value);
336 g_signal_handlers_unblock_by_func (hscale, (void *) start_seek,
338 g_signal_handlers_unblock_by_func (hscale, (void *) stop_seek,
340 g_signal_handlers_unblock_by_func (hscale, (void *) seek_cb,
342 gtk_widget_queue_draw (hscale);
346 update_fill (gpointer data)
348 if (seekable_elements) {
349 GstElement *element = GST_ELEMENT (seekable_elements->data);
352 query = gst_query_new_buffering (GST_FORMAT_PERCENT);
353 if (gst_element_query (element, query)) {
354 gint64 start, stop, buffering_total;
359 GstBufferingMode mode;
360 gint avg_in, avg_out;
361 gint64 buffering_left;
363 gst_query_parse_buffering_percent (query, &busy, &percent);
364 gst_query_parse_buffering_range (query, &format, &start, &stop,
366 gst_query_parse_buffering_stats (query, &mode, &avg_in, &avg_out,
369 /* note that we could start the playback when buffering_left < remaining
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,
377 fill = N_GRAD * stop / GST_FORMAT_PERCENT_MAX;
381 gtk_range_set_fill_level (GTK_RANGE (hscale), fill);
383 gst_query_unref (query);
389 update_scale (gpointer data)
391 if (seekable_elements) {
392 GstElement *element = GST_ELEMENT (seekable_elements->data);
394 gst_element_query_position (element, GST_FORMAT_TIME, &position);
395 gst_element_query_duration (element, GST_FORMAT_TIME, &duration);
399 query_positions_elems ();
402 if (position >= duration)
406 set_scale (position * N_GRAD / duration);
409 /* FIXME: see make_playerbin2_pipeline() and volume_notify_cb() */
410 if (pipeline_type == 16) {
411 g_object_notify (G_OBJECT (pipeline), "volume");
417 static void do_seek (GtkWidget * widget);
418 static void connect_bus_signals (GstElement * pipeline);
419 static void set_update_scale (gboolean active);
420 static void set_update_fill (gboolean active);
423 end_scrub (GtkWidget * widget)
425 GST_DEBUG ("end scrub, PAUSE");
426 gst_element_set_state (pipeline, GST_STATE_PAUSED);
433 send_event (GstEvent * event)
435 gboolean res = FALSE;
436 GList *walk = seekable_elements;
439 GstElement *seekable = GST_ELEMENT (walk->data);
441 GST_DEBUG ("send event on element %s", GST_ELEMENT_NAME (seekable));
443 gst_event_ref (event);
444 res = gst_element_send_event (seekable, event);
446 walk = g_list_next (walk);
448 gst_event_unref (event);
453 do_seek (GtkWidget * widget)
456 gboolean res = FALSE;
460 real = gtk_range_get_value (GTK_RANGE (widget)) * duration / N_GRAD;
462 GST_DEBUG ("value=%f, real=%" G_GINT64_FORMAT,
463 gtk_range_get_value (GTK_RANGE (widget)), real);
467 flags |= GST_SEEK_FLAG_FLUSH;
469 flags |= GST_SEEK_FLAG_ACCURATE;
471 flags |= GST_SEEK_FLAG_KEY_UNIT;
473 flags |= GST_SEEK_FLAG_SEGMENT;
475 flags |= GST_SEEK_FLAG_SKIP;
478 s_event = gst_event_new_seek (rate,
479 GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, real, GST_SEEK_TYPE_SET,
480 GST_CLOCK_TIME_NONE);
481 GST_DEBUG ("seek with rate %lf to %" GST_TIME_FORMAT " / %" GST_TIME_FORMAT,
482 rate, GST_TIME_ARGS (real), GST_TIME_ARGS (duration));
484 s_event = gst_event_new_seek (rate,
485 GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, G_GINT64_CONSTANT (0),
486 GST_SEEK_TYPE_SET, real);
487 GST_DEBUG ("seek with rate %lf to %" GST_TIME_FORMAT " / %" GST_TIME_FORMAT,
488 rate, GST_TIME_ARGS (0), GST_TIME_ARGS (real));
491 res = send_event (s_event);
495 gst_element_get_state (GST_ELEMENT (pipeline), NULL, NULL, SEEK_TIMEOUT);
497 set_update_scale (TRUE);
500 g_print ("seek failed\n");
501 set_update_scale (TRUE);
506 seek_cb (GtkWidget * widget)
508 /* If the timer hasn't expired yet, then the pipeline is running */
509 if (play_scrub && seek_timeout_id != 0) {
510 GST_DEBUG ("do scrub seek, PAUSED");
511 gst_element_set_state (pipeline, GST_STATE_PAUSED);
514 GST_DEBUG ("do seek");
518 GST_DEBUG ("do scrub seek, PLAYING");
519 gst_element_set_state (pipeline, GST_STATE_PLAYING);
521 if (seek_timeout_id == 0) {
523 g_timeout_add (SCRUB_TIME, (GSourceFunc) end_scrub, widget);
529 set_update_fill (gboolean active)
531 GST_DEBUG ("fill scale is %d", active);
536 g_timeout_add (FILL_INTERVAL, (GSourceFunc) update_fill, pipeline);
540 g_source_remove (fill_id);
547 set_update_scale (gboolean active)
550 GST_DEBUG ("update scale is %d", active);
553 if (update_id == 0) {
555 g_timeout_add (UPDATE_INTERVAL, (GSourceFunc) update_scale, pipeline);
559 g_source_remove (update_id);
566 start_seek (GtkWidget * widget, GdkEventButton * event, gpointer user_data)
568 if (event->type != GDK_BUTTON_PRESS)
571 set_update_scale (FALSE);
573 if (state == GST_STATE_PLAYING && flush_seek && scrub) {
574 GST_DEBUG ("start scrub seek, PAUSE");
575 gst_element_set_state (pipeline, GST_STATE_PAUSED);
578 if (changed_id == 0 && flush_seek && scrub) {
580 g_signal_connect (hscale, "value_changed", G_CALLBACK (seek_cb),
588 stop_seek (GtkWidget * widget, GdkEventButton * event, gpointer user_data)
591 g_signal_handler_disconnect (hscale, changed_id);
595 if (!flush_seek || !scrub) {
596 GST_DEBUG ("do final seek");
600 if (seek_timeout_id != 0) {
601 g_source_remove (seek_timeout_id);
603 /* Still scrubbing, so the pipeline is playing, see if we need PAUSED
605 if (state == GST_STATE_PAUSED) {
606 GST_DEBUG ("stop scrub seek, PAUSED");
607 gst_element_set_state (pipeline, GST_STATE_PAUSED);
610 if (state == GST_STATE_PLAYING) {
611 GST_DEBUG ("stop scrub seek, PLAYING");
612 gst_element_set_state (pipeline, GST_STATE_PLAYING);
620 play_cb (GtkButton * button, gpointer data)
622 GstStateChangeReturn ret;
624 if (state != GST_STATE_PLAYING) {
625 g_print ("PLAY pipeline\n");
626 gtk_statusbar_pop (GTK_STATUSBAR (statusbar), status_id);
628 ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
630 case GST_STATE_CHANGE_FAILURE:
632 case GST_STATE_CHANGE_NO_PREROLL:
638 state = GST_STATE_PLAYING;
639 gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Playing");
646 g_print ("PLAY failed\n");
647 gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Play failed");
652 pause_cb (GtkButton * button, gpointer data)
654 g_static_mutex_lock (&state_mutex);
655 if (state != GST_STATE_PAUSED) {
656 GstStateChangeReturn ret;
658 gtk_statusbar_pop (GTK_STATUSBAR (statusbar), status_id);
659 g_print ("PAUSE pipeline\n");
660 ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
662 case GST_STATE_CHANGE_FAILURE:
664 case GST_STATE_CHANGE_NO_PREROLL:
671 state = GST_STATE_PAUSED;
672 gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Paused");
674 g_static_mutex_unlock (&state_mutex);
680 g_static_mutex_unlock (&state_mutex);
681 g_print ("PAUSE failed\n");
682 gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Pause failed");
687 stop_cb (GtkButton * button, gpointer data)
689 if (state != STOP_STATE) {
690 GstStateChangeReturn ret;
692 g_print ("READY pipeline\n");
693 gtk_statusbar_pop (GTK_STATUSBAR (statusbar), status_id);
695 g_static_mutex_lock (&state_mutex);
696 ret = gst_element_set_state (pipeline, STOP_STATE);
697 if (ret == GST_STATE_CHANGE_FAILURE)
701 gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Stopped");
702 gtk_widget_queue_draw (video_window);
706 set_update_scale (FALSE);
708 set_update_fill (FALSE);
710 if (pipeline_type == 16)
711 clear_streams (pipeline);
712 g_static_mutex_unlock (&state_mutex);
715 /* if one uses parse_launch, play, stop and play again it fails as all the
716 * pads after the demuxer can't be reconnected
718 if (!strcmp (pipelines[pipeline_type].name, "parse-launch")) {
719 gst_element_set_state (pipeline, GST_STATE_NULL);
720 gst_object_unref (pipeline);
722 g_list_free (seekable_elements);
723 seekable_elements = NULL;
725 pipeline = pipelines[pipeline_type].func (pipeline_spec);
727 gst_element_set_state (pipeline, STOP_STATE);
728 connect_bus_signals (pipeline);
736 g_static_mutex_unlock (&state_mutex);
737 g_print ("STOP failed\n");
738 gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Stop failed");
743 accurate_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
745 accurate_seek = gtk_toggle_button_get_active (button);
749 key_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
751 keyframe_seek = gtk_toggle_button_get_active (button);
755 loop_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
757 loop_seek = gtk_toggle_button_get_active (button);
758 if (state == GST_STATE_PLAYING) {
764 flush_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
766 flush_seek = gtk_toggle_button_get_active (button);
770 scrub_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
772 scrub = gtk_toggle_button_get_active (button);
776 play_scrub_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
778 play_scrub = gtk_toggle_button_get_active (button);
782 skip_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
784 skip_seek = gtk_toggle_button_get_active (button);
785 if (state == GST_STATE_PLAYING) {
791 rate_spinbutton_changed_cb (GtkSpinButton * button, GstPipeline * pipeline)
793 gboolean res = FALSE;
797 rate = gtk_spin_button_get_value (button);
799 GST_DEBUG ("rate changed to %lf", rate);
803 flags |= GST_SEEK_FLAG_FLUSH;
805 flags |= GST_SEEK_FLAG_SEGMENT;
807 flags |= GST_SEEK_FLAG_ACCURATE;
809 flags |= GST_SEEK_FLAG_KEY_UNIT;
811 flags |= GST_SEEK_FLAG_SKIP;
814 s_event = gst_event_new_seek (rate,
815 GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, position,
816 GST_SEEK_TYPE_SET, GST_CLOCK_TIME_NONE);
818 s_event = gst_event_new_seek (rate,
819 GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, G_GINT64_CONSTANT (0),
820 GST_SEEK_TYPE_SET, position);
823 res = send_event (s_event);
827 gst_element_get_state (GST_ELEMENT (pipeline), NULL, NULL, SEEK_TIMEOUT);
830 g_print ("seek failed\n");
834 update_flag (GstPipeline * pipeline, gint num, gboolean state)
838 g_object_get (pipeline, "flags", &flags, NULL);
842 flags &= ~(1 << num);
843 g_object_set (pipeline, "flags", flags, NULL);
847 vis_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
851 state = gtk_toggle_button_get_active (button);
852 update_flag (pipeline, 3, state);
853 gtk_widget_set_sensitive (vis_combo, state);
857 audio_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
861 state = gtk_toggle_button_get_active (button);
862 update_flag (pipeline, 1, state);
863 gtk_widget_set_sensitive (audio_combo, state);
867 video_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
871 state = gtk_toggle_button_get_active (button);
872 update_flag (pipeline, 0, state);
873 gtk_widget_set_sensitive (video_combo, state);
877 text_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
881 state = gtk_toggle_button_get_active (button);
882 update_flag (pipeline, 2, state);
883 gtk_widget_set_sensitive (text_combo, state);
887 mute_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
891 mute = gtk_toggle_button_get_active (button);
892 g_object_set (pipeline, "mute", mute, NULL);
896 download_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
900 state = gtk_toggle_button_get_active (button);
901 update_flag (pipeline, 7, state);
905 buffer_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
909 state = gtk_toggle_button_get_active (button);
910 update_flag (pipeline, 8, state);
914 clear_streams (GstElement * pipeline)
918 /* remove previous info */
919 for (i = 0; i < n_video; i++)
920 gtk_combo_box_text_remove (GTK_COMBO_BOX_TEXT (video_combo), 0);
921 for (i = 0; i < n_audio; i++)
922 gtk_combo_box_text_remove (GTK_COMBO_BOX_TEXT (audio_combo), 0);
923 for (i = 0; i < n_text; i++)
924 gtk_combo_box_text_remove (GTK_COMBO_BOX_TEXT (text_combo), 0);
926 n_audio = n_video = n_text = 0;
927 gtk_widget_set_sensitive (video_combo, FALSE);
928 gtk_widget_set_sensitive (audio_combo, FALSE);
929 gtk_widget_set_sensitive (text_combo, FALSE);
935 update_streams (GstPipeline * pipeline)
939 if (pipeline_type == 16 && need_streams) {
945 /* remove previous info */
946 clear_streams (GST_ELEMENT_CAST (pipeline));
948 /* here we get and update the different streams detected by playbin */
949 g_object_get (pipeline, "n-video", &n_video, NULL);
950 g_object_get (pipeline, "n-audio", &n_audio, NULL);
951 g_object_get (pipeline, "n-text", &n_text, NULL);
953 g_print ("video %d, audio %d, text %d\n", n_video, n_audio, n_text);
956 for (i = 0; i < n_video; i++) {
957 g_signal_emit_by_name (pipeline, "get-video-tags", i, &tags);
959 str = gst_structure_to_string ((GstStructure *) tags);
960 g_print ("video %d: %s\n", i, str);
963 /* find good name for the label */
964 name = g_strdup_printf ("video %d", i + 1);
965 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (video_combo), name);
968 state = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (video_checkbox));
969 gtk_widget_set_sensitive (video_combo, state && n_video > 0);
970 gtk_combo_box_set_active (GTK_COMBO_BOX (video_combo), active_idx);
973 for (i = 0; i < n_audio; i++) {
974 g_signal_emit_by_name (pipeline, "get-audio-tags", i, &tags);
976 str = gst_structure_to_string ((GstStructure *) tags);
977 g_print ("audio %d: %s\n", i, str);
980 /* find good name for the label */
981 name = g_strdup_printf ("audio %d", i + 1);
982 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (audio_combo), name);
985 state = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (audio_checkbox));
986 gtk_widget_set_sensitive (audio_combo, state && n_audio > 0);
987 gtk_combo_box_set_active (GTK_COMBO_BOX (audio_combo), active_idx);
990 for (i = 0; i < n_text; i++) {
991 g_signal_emit_by_name (pipeline, "get-text-tags", i, &tags);
997 str = gst_structure_to_string ((GstStructure *) tags);
998 g_print ("text %d: %s\n", i, str);
1001 /* get the language code if we can */
1002 value = gst_tag_list_get_value_index (tags, GST_TAG_LANGUAGE_CODE, 0);
1003 if (value && G_VALUE_HOLDS_STRING (value)) {
1004 name = g_strdup_printf ("text %s", g_value_get_string (value));
1007 /* find good name for the label if we didn't use a tag */
1009 name = g_strdup_printf ("text %d", i + 1);
1011 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (text_combo), name);
1014 state = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (text_checkbox));
1015 gtk_widget_set_sensitive (text_combo, state && n_text > 0);
1016 gtk_combo_box_set_active (GTK_COMBO_BOX (text_combo), active_idx);
1018 need_streams = FALSE;
1023 video_combo_cb (GtkComboBox * combo, GstPipeline * pipeline)
1027 active = gtk_combo_box_get_active (combo);
1029 g_print ("setting current video track %d\n", active);
1030 g_object_set (pipeline, "current-video", active, NULL);
1034 audio_combo_cb (GtkComboBox * combo, GstPipeline * pipeline)
1038 active = gtk_combo_box_get_active (combo);
1040 g_print ("setting current audio track %d\n", active);
1041 g_object_set (pipeline, "current-audio", active, NULL);
1045 text_combo_cb (GtkComboBox * combo, GstPipeline * pipeline)
1049 active = gtk_combo_box_get_active (combo);
1051 g_print ("setting current text track %d\n", active);
1052 g_object_set (pipeline, "current-text", active, NULL);
1056 filter_features (GstPluginFeature * feature, gpointer data)
1058 GstElementFactory *f;
1060 if (!GST_IS_ELEMENT_FACTORY (feature))
1062 f = GST_ELEMENT_FACTORY (feature);
1063 if (!g_strrstr (gst_element_factory_get_klass (f), "Visualization"))
1070 init_visualization_features (void)
1074 vis_entries = g_array_new (FALSE, FALSE, sizeof (VisEntry));
1076 list = gst_registry_feature_filter (gst_registry_get (),
1077 filter_features, FALSE, NULL);
1079 for (walk = list; walk; walk = g_list_next (walk)) {
1083 entry.factory = GST_ELEMENT_FACTORY (walk->data);
1084 name = gst_element_factory_get_longname (entry.factory);
1086 g_array_append_val (vis_entries, entry);
1087 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (vis_combo), name);
1089 gtk_combo_box_set_active (GTK_COMBO_BOX (vis_combo), 0);
1093 vis_combo_cb (GtkComboBox * combo, GstPipeline * pipeline)
1097 GstElement *element;
1099 /* get the selected index and get the factory for this index */
1100 index = gtk_combo_box_get_active (GTK_COMBO_BOX (vis_combo));
1101 if (vis_entries->len > 0) {
1102 entry = &g_array_index (vis_entries, VisEntry, index);
1104 /* create an instance of the element from the factory */
1105 element = gst_element_factory_create (entry->factory, NULL);
1109 /* set vis plugin for playbin */
1110 g_object_set (pipeline, "vis-plugin", element, NULL);
1115 volume_spinbutton_changed_cb (GtkSpinButton * button, GstPipeline * pipeline)
1119 volume = gtk_spin_button_get_value (button);
1121 g_object_set (pipeline, "volume", volume, NULL);
1125 volume_notify_cb (GstElement * pipeline, GParamSpec * arg, gpointer user_dat)
1127 gdouble cur_volume, new_volume;
1129 if (volume_spinbutton == NULL)
1132 g_object_get (pipeline, "volume", &new_volume, NULL);
1133 cur_volume = gtk_spin_button_get_value (GTK_SPIN_BUTTON (volume_spinbutton));
1134 if (fabs (cur_volume - new_volume) > 0.001) {
1135 g_signal_handlers_block_by_func (volume_spinbutton,
1136 volume_spinbutton_changed_cb, pipeline);
1137 gtk_spin_button_set_value (GTK_SPIN_BUTTON (volume_spinbutton), new_volume);
1138 g_signal_handlers_unblock_by_func (volume_spinbutton,
1139 volume_spinbutton_changed_cb, pipeline);
1144 shot_cb (GtkButton * button, gpointer data)
1146 GstSample *sample = NULL;
1149 GST_DEBUG ("taking snapshot");
1151 /* convert to our desired format (RGB24) */
1152 caps = gst_caps_new_simple ("video/x-raw", "format", G_TYPE_STRING, "RGB",
1153 /* Note: we don't ask for a specific width/height here, so that
1154 * videoscale can adjust dimensions from a non-1/1 pixel aspect
1155 * ratio to a 1/1 pixel-aspect-ratio */
1156 "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1, NULL);
1158 /* convert the latest sample to the requested format */
1159 g_signal_emit_by_name (pipeline, "convert-sample", caps, &sample);
1160 gst_caps_unref (caps);
1169 GError *error = NULL;
1173 /* get the snapshot buffer format now. We set the caps on the appsink so
1174 * that it can only be an rgb buffer. The only thing we have not specified
1175 * on the caps is the height, which is dependant on the pixel-aspect-ratio
1176 * of the source material */
1177 caps = gst_sample_get_caps (sample);
1179 g_warning ("could not get snapshot format\n");
1182 s = gst_caps_get_structure (caps, 0);
1184 /* we need to get the final caps on the buffer to get the size */
1185 res = gst_structure_get_int (s, "width", &width);
1186 res |= gst_structure_get_int (s, "height", &height);
1188 g_warning ("could not get snapshot dimension\n");
1192 /* create pixmap from buffer and save, gstreamer video buffers have a stride
1193 * that is rounded up to the nearest multiple of 4 */
1194 buffer = gst_sample_get_buffer (sample);
1195 data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
1196 pixbuf = gdk_pixbuf_new_from_data (data,
1197 GDK_COLORSPACE_RGB, FALSE, 8, width, height,
1198 GST_ROUND_UP_4 (width * 3), NULL, NULL);
1200 /* save the pixbuf */
1201 gdk_pixbuf_save (pixbuf, "snapshot.png", "png", &error, NULL);
1202 gst_buffer_unmap (buffer, data, size);
1205 gst_sample_unref (sample);
1209 /* called when the Step button is pressed */
1211 step_cb (GtkButton * button, gpointer data)
1217 gboolean flush, res;
1220 active = gtk_combo_box_get_active (GTK_COMBO_BOX (format_combo));
1222 gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
1223 (step_amount_spinbutton));
1224 rate = gtk_spin_button_get_value (GTK_SPIN_BUTTON (step_rate_spinbutton));
1229 format = GST_FORMAT_BUFFERS;
1232 format = GST_FORMAT_TIME;
1233 amount *= GST_MSECOND;
1236 format = GST_FORMAT_UNDEFINED;
1240 event = gst_event_new_step (format, amount, rate, flush, FALSE);
1242 res = send_event (event);
1245 g_print ("Sending step event failed\n");
1250 message_received (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
1252 const GstStructure *s;
1254 switch (GST_MESSAGE_TYPE (message)) {
1255 case GST_MESSAGE_ERROR:
1256 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
1257 GST_DEBUG_GRAPH_SHOW_ALL, "seek.error");
1259 case GST_MESSAGE_WARNING:
1260 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
1261 GST_DEBUG_GRAPH_SHOW_ALL, "seek.warning");
1267 s = gst_message_get_structure (message);
1268 g_print ("message from \"%s\" (%s): ",
1269 GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))),
1270 gst_message_type_get_name (GST_MESSAGE_TYPE (message)));
1274 sstr = gst_structure_to_string (s);
1275 g_print ("%s\n", sstr);
1278 g_print ("no message details\n");
1282 static gboolean shuttling = FALSE;
1283 static gdouble shuttle_rate = 0.0;
1284 static gdouble play_rate = 1.0;
1287 do_shuttle (GstElement * element)
1292 duration = 40 * GST_MSECOND;
1296 gst_element_send_event (element,
1297 gst_event_new_step (GST_FORMAT_TIME, duration, shuttle_rate, FALSE,
1302 msg_sync_step_done (GstBus * bus, GstMessage * message, GstElement * element)
1308 gboolean intermediate;
1312 gst_message_parse_step_done (message, &format, &amount, &rate, &flush,
1313 &intermediate, &duration, &eos);
1316 g_print ("stepped till EOS\n");
1320 if (g_static_mutex_trylock (&state_mutex)) {
1322 do_shuttle (element);
1323 g_static_mutex_unlock (&state_mutex);
1325 /* ignore step messages that come while we are doing a state change */
1326 g_print ("state change is busy\n");
1331 shuttle_toggled (GtkToggleButton * button, GstElement * element)
1335 active = gtk_toggle_button_get_active (button);
1337 if (active != shuttling) {
1339 g_print ("shuttling %s\n", shuttling ? "active" : "inactive");
1343 pause_cb (NULL, NULL);
1344 gst_element_get_state (element, NULL, NULL, -1);
1350 shuttle_rate_switch (GstElement * element)
1356 if (state == GST_STATE_PLAYING) {
1357 /* pause when we need to */
1358 pause_cb (NULL, NULL);
1359 gst_element_get_state (element, NULL, NULL, -1);
1362 if (play_rate == 1.0)
1367 g_print ("rate changed to %lf %" GST_TIME_FORMAT "\n", play_rate,
1368 GST_TIME_ARGS (position));
1370 flags = GST_SEEK_FLAG_FLUSH;
1371 flags |= GST_SEEK_FLAG_ACCURATE;
1373 if (play_rate >= 0.0) {
1374 s_event = gst_event_new_seek (play_rate,
1375 GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, position,
1376 GST_SEEK_TYPE_SET, GST_CLOCK_TIME_NONE);
1378 s_event = gst_event_new_seek (play_rate,
1379 GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, G_GINT64_CONSTANT (0),
1380 GST_SEEK_TYPE_SET, position);
1382 res = send_event (s_event);
1384 gst_element_get_state (element, NULL, NULL, SEEK_TIMEOUT);
1386 g_print ("seek failed\n");
1391 shuttle_value_changed (GtkRange * range, GstElement * element)
1395 rate = gtk_adjustment_get_value (shuttle_adjustment);
1398 g_print ("rate 0.0, pause\n");
1399 pause_cb (NULL, NULL);
1400 gst_element_get_state (element, NULL, NULL, -1);
1402 g_print ("rate changed %0.3g\n", rate);
1404 if ((rate < 0.0 && play_rate > 0.0) || (rate > 0.0 && play_rate < 0.0)) {
1405 shuttle_rate_switch (element);
1408 shuttle_rate = ABS (rate);
1409 if (state != GST_STATE_PLAYING) {
1410 do_shuttle (element);
1411 play_cb (NULL, NULL);
1417 msg_async_done (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
1419 GST_DEBUG ("async done");
1420 /* when we get ASYNC_DONE we can query position, duration and other
1422 update_scale (pipeline);
1424 /* update the available streams */
1425 update_streams (pipeline);
1429 msg_state_changed (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
1431 const GstStructure *s;
1433 s = gst_message_get_structure (message);
1435 /* We only care about state changed on the pipeline */
1436 if (s && GST_MESSAGE_SRC (message) == GST_OBJECT_CAST (pipeline)) {
1437 GstState old, new, pending;
1439 gst_message_parse_state_changed (message, &old, &new, &pending);
1441 /* When state of the pipeline changes to paused or playing we start updating scale */
1442 if (new == GST_STATE_PLAYING) {
1443 set_update_scale (TRUE);
1445 set_update_scale (FALSE);
1448 /* dump graph for (some) pipeline state changes */
1452 dump_name = g_strdup_printf ("seek.%s_%s",
1453 gst_element_state_get_name (old), gst_element_state_get_name (new));
1455 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
1456 GST_DEBUG_GRAPH_SHOW_ALL, dump_name);
1464 msg_segment_done (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
1471 GST_DEBUG ("position is %" GST_TIME_FORMAT, GST_TIME_ARGS (position));
1472 gst_message_parse_segment_done (message, &format, &position);
1473 GST_DEBUG ("end of segment at %" GST_TIME_FORMAT, GST_TIME_ARGS (position));
1476 /* in the segment-done callback we never flush as this would not make sense
1477 * for seamless playback. */
1479 flags |= GST_SEEK_FLAG_SEGMENT;
1481 flags |= GST_SEEK_FLAG_SKIP;
1483 s_event = gst_event_new_seek (rate,
1484 GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, G_GINT64_CONSTANT (0),
1485 GST_SEEK_TYPE_SET, duration);
1487 GST_DEBUG ("restart loop with rate %lf to 0 / %" GST_TIME_FORMAT,
1488 rate, GST_TIME_ARGS (duration));
1490 res = send_event (s_event);
1492 g_print ("segment seek failed\n");
1495 /* in stream buffering mode we PAUSE the pipeline until we receive a 100%
1498 do_stream_buffering (gint percent)
1502 gtk_statusbar_pop (GTK_STATUSBAR (statusbar), status_id);
1503 bufstr = g_strdup_printf ("Buffering...%d", percent);
1504 gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, bufstr);
1507 if (percent == 100) {
1508 /* a 100% message means buffering is done */
1510 /* if the desired state is playing, go back */
1511 if (state == GST_STATE_PLAYING) {
1512 /* no state management needed for live pipelines */
1514 fprintf (stderr, "Done buffering, setting pipeline to PLAYING ...\n");
1515 gst_element_set_state (pipeline, GST_STATE_PLAYING);
1517 gtk_statusbar_pop (GTK_STATUSBAR (statusbar), status_id);
1518 gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Playing");
1521 /* buffering busy */
1522 if (buffering == FALSE && state == GST_STATE_PLAYING) {
1523 /* we were not buffering but PLAYING, PAUSE the pipeline. */
1525 fprintf (stderr, "Buffering, setting pipeline to PAUSED ...\n");
1526 gst_element_set_state (pipeline, GST_STATE_PAUSED);
1534 do_download_buffering (gint percent)
1536 if (!buffering && percent < 100) {
1541 bufstr = g_strdup_printf ("Downloading...");
1542 gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, bufstr);
1545 /* once we get a buffering message, we'll do the fill update */
1546 set_update_fill (TRUE);
1548 if (state == GST_STATE_PLAYING && !is_live) {
1549 fprintf (stderr, "Downloading, setting pipeline to PAUSED ...\n");
1550 gst_element_set_state (pipeline, GST_STATE_PAUSED);
1551 /* user has to manually start the playback */
1552 state = GST_STATE_PAUSED;
1558 msg_buffering (GstBus * bus, GstMessage * message, GstPipeline * data)
1562 gst_message_parse_buffering (message, &percent);
1564 /* get more stats */
1565 gst_message_parse_buffering_stats (message, &mode, NULL, NULL,
1569 case GST_BUFFERING_DOWNLOAD:
1570 do_download_buffering (percent);
1572 case GST_BUFFERING_LIVE:
1574 case GST_BUFFERING_TIMESHIFT:
1575 case GST_BUFFERING_STREAM:
1576 do_stream_buffering (percent);
1582 msg_clock_lost (GstBus * bus, GstMessage * message, GstPipeline * data)
1584 g_print ("clock lost! PAUSE and PLAY to select a new clock\n");
1585 if (state == GST_STATE_PLAYING) {
1586 gst_element_set_state (pipeline, GST_STATE_PAUSED);
1587 gst_element_set_state (pipeline, GST_STATE_PLAYING);
1591 #if defined (GDK_WINDOWING_X11) || defined (GDK_WINDOWING_WIN32)
1593 static gulong embed_xid = 0;
1595 /* We set the xid here in response to the prepare-window-handle message via a
1596 * bus sync handler because we don't know the actual videosink used from the
1597 * start (as we don't know the pipeline, or bin elements such as autovideosink
1598 * or gconfvideosink may be used which create the actual videosink only once
1599 * the pipeline is started) */
1600 static GstBusSyncReply
1601 bus_sync_handler (GstBus * bus, GstMessage * message, GstPipeline * data)
1603 GstElement *element;
1605 if (!gst_is_video_overlay_prepare_window_handle_message (message))
1606 return GST_BUS_PASS;
1608 element = GST_ELEMENT (GST_MESSAGE_SRC (message));
1610 g_print ("got prepare-window-handle, setting XID %lu\n", embed_xid);
1612 if (g_object_class_find_property (G_OBJECT_GET_CLASS (element),
1613 "force-aspect-ratio")) {
1614 g_object_set (element, "force-aspect-ratio", TRUE, NULL);
1617 /* Should have been initialised from main thread before (can't use
1618 * GDK_WINDOW_XID here with Gtk+ >= 2.18, because the sync handler will
1619 * be called from a streaming thread and GDK_WINDOW_XID maps to more than
1620 * a simple structure lookup with Gtk+ >= 2.18, where 'more' is stuff that
1621 * shouldn't be done from a non-GUI thread without explicit locking). */
1622 g_assert (embed_xid != 0);
1624 gst_video_overlay_set_window_handle (GST_VIDEO_OVERLAY (element), embed_xid);
1625 return GST_BUS_PASS;
1630 draw_cb (GtkWidget * widget, cairo_t * cr, gpointer data)
1632 if (state < GST_STATE_PAUSED) {
1635 width = gtk_widget_get_allocated_width (widget);
1636 height = gtk_widget_get_allocated_height (widget);
1637 cairo_set_source_rgb (cr, 0, 0, 0);
1638 cairo_rectangle (cr, 0, 0, width, height);
1646 realize_cb (GtkWidget * widget, gpointer data)
1648 GdkWindow *window = gtk_widget_get_window (widget);
1650 /* This is here just for pedagogical purposes, GDK_WINDOW_XID will call it
1652 if (!gdk_window_ensure_native (window))
1653 g_error ("Couldn't create native window needed for GstXOverlay!");
1655 #if defined (GDK_WINDOWING_WIN32)
1656 embed_xid = GDK_WINDOW_HWND (window);
1657 g_print ("Window realize: video window HWND = %lu\n", embed_xid);
1659 embed_xid = GDK_WINDOW_XID (window);
1660 g_print ("Window realize: video window XID = %lu\n", embed_xid);
1665 msg_eos (GstBus * bus, GstMessage * message, GstPipeline * data)
1667 message_received (bus, message, data);
1669 /* Set new uri for playerbins and continue playback */
1670 if (l && (pipeline_type == 14 || pipeline_type == 16)) {
1671 stop_cb (NULL, NULL);
1672 l = g_list_next (l);
1674 playerbin_set_uri (GST_ELEMENT (data), l->data);
1675 play_cb (NULL, NULL);
1681 msg_step_done (GstBus * bus, GstMessage * message, GstPipeline * data)
1684 message_received (bus, message, data);
1688 connect_bus_signals (GstElement * pipeline)
1690 GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
1692 #if defined (GDK_WINDOWING_X11) || defined (GDK_WINDOWING_WIN32)
1693 /* handle prepare-window-handle element message synchronously */
1694 gst_bus_set_sync_handler (bus, (GstBusSyncHandler) bus_sync_handler,
1698 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
1699 gst_bus_enable_sync_message_emission (bus);
1701 g_signal_connect (bus, "message::state-changed",
1702 (GCallback) msg_state_changed, pipeline);
1703 g_signal_connect (bus, "message::segment-done", (GCallback) msg_segment_done,
1705 g_signal_connect (bus, "message::async-done", (GCallback) msg_async_done,
1708 g_signal_connect (bus, "message::new-clock", (GCallback) message_received,
1710 g_signal_connect (bus, "message::clock-lost", (GCallback) msg_clock_lost,
1712 g_signal_connect (bus, "message::error", (GCallback) message_received,
1714 g_signal_connect (bus, "message::warning", (GCallback) message_received,
1716 g_signal_connect (bus, "message::eos", (GCallback) msg_eos, pipeline);
1717 g_signal_connect (bus, "message::tag", (GCallback) message_received,
1719 g_signal_connect (bus, "message::element", (GCallback) message_received,
1721 g_signal_connect (bus, "message::segment-done", (GCallback) message_received,
1723 g_signal_connect (bus, "message::buffering", (GCallback) msg_buffering,
1725 // g_signal_connect (bus, "message::step-done", (GCallback) msg_step_done,
1727 g_signal_connect (bus, "message::step-start", (GCallback) msg_step_done,
1729 g_signal_connect (bus, "sync-message::step-done",
1730 (GCallback) msg_sync_step_done, pipeline);
1732 gst_object_unref (bus);
1735 /* Return GList of paths described in location string */
1737 handle_wildcards (const gchar * location)
1740 gchar *path = g_path_get_dirname (location);
1741 gchar *pattern = g_path_get_basename (location);
1742 GPatternSpec *pspec = g_pattern_spec_new (pattern);
1743 GDir *dir = g_dir_open (path, 0, NULL);
1746 g_print ("matching %s from %s\n", pattern, path);
1749 g_print ("opening directory %s failed\n", path);
1753 while ((name = g_dir_read_name (dir)) != NULL) {
1754 if (g_pattern_match_string (pspec, name)) {
1755 res = g_list_append (res, g_strjoin ("/", path, name, NULL));
1756 g_print (" found clip %s\n", name);
1762 g_pattern_spec_free (pspec);
1770 delete_event_cb (void)
1772 stop_cb (NULL, NULL);
1777 print_usage (int argc, char **argv)
1781 g_print ("usage: %s <type> <filename>\n", argv[0]);
1782 g_print (" possible types:\n");
1784 for (i = 0; i < NUM_TYPES; i++) {
1785 g_print (" %d = %s\n", i, pipelines[i].name);
1790 main (int argc, char **argv)
1792 GtkWidget *window, *hbox, *vbox, *panel, *expander, *pb2vbox, *boxes,
1793 *flagtable, *boxes2, *step;
1794 GtkWidget *play_button, *pause_button, *stop_button, *shot_button;
1795 GtkWidget *accurate_checkbox, *key_checkbox, *loop_checkbox, *flush_checkbox;
1796 GtkWidget *scrub_checkbox, *play_scrub_checkbox;
1797 GtkWidget *rate_label, *volume_label;
1798 GOptionEntry options[] = {
1799 {"audiosink", '\0', 0, G_OPTION_ARG_STRING, &opt_audiosink_str,
1800 "audio sink to use (default: " DEFAULT_AUDIOSINK ")", NULL},
1801 {"stats", 's', 0, G_OPTION_ARG_NONE, &stats,
1802 "Show pad stats", NULL},
1803 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
1804 "Verbose properties", NULL},
1805 {"videosink", '\0', 0, G_OPTION_ARG_STRING, &opt_videosink_str,
1806 "video sink to use (default: " DEFAULT_VIDEOSINK ")", NULL},
1809 GOptionContext *ctx;
1812 #if !GLIB_CHECK_VERSION (2, 31, 0)
1813 if (!g_thread_supported ())
1814 g_thread_init (NULL);
1817 ctx = g_option_context_new ("- test seeking in gsteamer");
1818 g_option_context_add_main_entries (ctx, options, NULL);
1819 g_option_context_add_group (ctx, gst_init_get_option_group ());
1820 g_option_context_add_group (ctx, gtk_get_option_group (TRUE));
1822 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
1823 g_print ("Error initializing: %s\n", err->message);
1827 if (opt_audiosink_str == NULL)
1828 opt_audiosink_str = g_strdup (DEFAULT_AUDIOSINK);
1830 if (opt_videosink_str == NULL)
1831 opt_videosink_str = g_strdup (DEFAULT_VIDEOSINK);
1833 GST_DEBUG_CATEGORY_INIT (seek_debug, "seek", 0, "seek example");
1836 print_usage (argc, argv);
1840 pipeline_type = atoi (argv[1]);
1842 if (pipeline_type < 0 || pipeline_type >= NUM_TYPES) {
1843 print_usage (argc, argv);
1847 pipeline_spec = argv[2];
1849 if (g_path_is_absolute (pipeline_spec) &&
1850 (g_strrstr (pipeline_spec, "*") != NULL ||
1851 g_strrstr (pipeline_spec, "?") != NULL)) {
1852 paths = handle_wildcards (pipeline_spec);
1854 paths = g_list_prepend (paths, g_strdup (pipeline_spec));
1858 g_print ("opening %s failed\n", pipeline_spec);
1864 pipeline = pipelines[pipeline_type].func ((gchar *) l->data);
1865 g_assert (pipeline);
1867 /* initialize gui elements ... */
1868 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1869 video_window = gtk_drawing_area_new ();
1870 g_signal_connect (video_window, "draw", G_CALLBACK (draw_cb), NULL);
1871 g_signal_connect (video_window, "realize", G_CALLBACK (realize_cb), NULL);
1872 gtk_widget_set_double_buffered (video_window, FALSE);
1874 statusbar = gtk_statusbar_new ();
1875 status_id = gtk_statusbar_get_context_id (GTK_STATUSBAR (statusbar), "seek");
1876 gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Stopped");
1877 hbox = gtk_hbox_new (FALSE, 0);
1878 vbox = gtk_vbox_new (FALSE, 0);
1879 flagtable = gtk_table_new (4, 2, FALSE);
1880 gtk_container_set_border_width (GTK_CONTAINER (vbox), 3);
1882 /* media controls */
1883 play_button = gtk_button_new_from_stock (GTK_STOCK_MEDIA_PLAY);
1884 pause_button = gtk_button_new_from_stock (GTK_STOCK_MEDIA_PAUSE);
1885 stop_button = gtk_button_new_from_stock (GTK_STOCK_MEDIA_STOP);
1888 accurate_checkbox = gtk_check_button_new_with_label ("Accurate Seek");
1889 key_checkbox = gtk_check_button_new_with_label ("Key-unit Seek");
1890 loop_checkbox = gtk_check_button_new_with_label ("Loop");
1891 flush_checkbox = gtk_check_button_new_with_label ("Flush");
1892 scrub_checkbox = gtk_check_button_new_with_label ("Scrub");
1893 play_scrub_checkbox = gtk_check_button_new_with_label ("Play Scrub");
1894 skip_checkbox = gtk_check_button_new_with_label ("Play Skip");
1895 rate_spinbutton = gtk_spin_button_new_with_range (-100, 100, 0.1);
1896 gtk_spin_button_set_digits (GTK_SPIN_BUTTON (rate_spinbutton), 3);
1897 rate_label = gtk_label_new ("Rate");
1899 gtk_widget_set_tooltip_text (accurate_checkbox,
1900 "accurate position is requested, this might be considerably slower for some formats");
1901 gtk_widget_set_tooltip_text (key_checkbox,
1902 "seek to the nearest keyframe. This might be faster but less accurate");
1903 gtk_widget_set_tooltip_text (loop_checkbox, "loop playback");
1904 gtk_widget_set_tooltip_text (flush_checkbox, "flush pipeline after seeking");
1905 gtk_widget_set_tooltip_text (rate_spinbutton, "define the playback rate, "
1906 "negative value trigger reverse playback");
1907 gtk_widget_set_tooltip_text (scrub_checkbox, "show images while seeking");
1908 gtk_widget_set_tooltip_text (play_scrub_checkbox, "play video while seeking");
1909 gtk_widget_set_tooltip_text (skip_checkbox,
1910 "Skip frames while playing at high frame rates");
1912 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (flush_checkbox), TRUE);
1913 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (scrub_checkbox), TRUE);
1915 gtk_spin_button_set_value (GTK_SPIN_BUTTON (rate_spinbutton), rate);
1921 step = gtk_expander_new ("step options");
1922 hbox = gtk_hbox_new (FALSE, 0);
1924 format_combo = gtk_combo_box_text_new ();
1925 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (format_combo),
1927 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (format_combo),
1929 gtk_combo_box_set_active (GTK_COMBO_BOX (format_combo), 0);
1930 gtk_box_pack_start (GTK_BOX (hbox), format_combo, FALSE, FALSE, 2);
1932 step_amount_spinbutton = gtk_spin_button_new_with_range (1, 1000, 1);
1933 gtk_spin_button_set_digits (GTK_SPIN_BUTTON (step_amount_spinbutton), 0);
1934 gtk_spin_button_set_value (GTK_SPIN_BUTTON (step_amount_spinbutton), 1.0);
1935 gtk_box_pack_start (GTK_BOX (hbox), step_amount_spinbutton, FALSE, FALSE,
1938 step_rate_spinbutton = gtk_spin_button_new_with_range (0.0, 100, 0.1);
1939 gtk_spin_button_set_digits (GTK_SPIN_BUTTON (step_rate_spinbutton), 3);
1940 gtk_spin_button_set_value (GTK_SPIN_BUTTON (step_rate_spinbutton), 1.0);
1941 gtk_box_pack_start (GTK_BOX (hbox), step_rate_spinbutton, FALSE, FALSE, 2);
1943 step_button = gtk_button_new_from_stock (GTK_STOCK_MEDIA_FORWARD);
1944 gtk_button_set_label (GTK_BUTTON (step_button), "Step");
1945 gtk_box_pack_start (GTK_BOX (hbox), step_button, FALSE, FALSE, 2);
1947 g_signal_connect (G_OBJECT (step_button), "clicked", G_CALLBACK (step_cb),
1951 shuttle_checkbox = gtk_check_button_new_with_label ("Shuttle");
1952 gtk_box_pack_start (GTK_BOX (hbox), shuttle_checkbox, FALSE, FALSE, 2);
1953 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (shuttle_checkbox), FALSE);
1954 g_signal_connect (shuttle_checkbox, "toggled", G_CALLBACK (shuttle_toggled),
1957 shuttle_adjustment =
1958 GTK_ADJUSTMENT (gtk_adjustment_new (0.0, -3.00, 4.0, 0.1, 1.0, 1.0));
1959 shuttle_hscale = gtk_hscale_new (shuttle_adjustment);
1960 gtk_scale_set_digits (GTK_SCALE (shuttle_hscale), 2);
1961 gtk_scale_set_value_pos (GTK_SCALE (shuttle_hscale), GTK_POS_TOP);
1962 g_signal_connect (shuttle_hscale, "value_changed",
1963 G_CALLBACK (shuttle_value_changed), pipeline);
1964 g_signal_connect (shuttle_hscale, "format_value",
1965 G_CALLBACK (shuttle_format_value), pipeline);
1967 gtk_box_pack_start (GTK_BOX (hbox), shuttle_hscale, TRUE, TRUE, 2);
1969 gtk_container_add (GTK_CONTAINER (step), hbox);
1974 GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.00, N_GRAD, 0.1, 1.0, 1.0));
1975 hscale = gtk_hscale_new (adjustment);
1976 gtk_scale_set_digits (GTK_SCALE (hscale), 2);
1977 gtk_scale_set_value_pos (GTK_SCALE (hscale), GTK_POS_RIGHT);
1978 gtk_range_set_show_fill_level (GTK_RANGE (hscale), TRUE);
1979 gtk_range_set_fill_level (GTK_RANGE (hscale), N_GRAD);
1981 g_signal_connect (hscale, "button_press_event", G_CALLBACK (start_seek),
1983 g_signal_connect (hscale, "button_release_event", G_CALLBACK (stop_seek),
1985 g_signal_connect (hscale, "format_value", G_CALLBACK (format_value),
1988 if (pipeline_type == 0) {
1989 /* the playbin panel controls for the video/audio/subtitle tracks */
1990 panel = gtk_hbox_new (FALSE, 0);
1991 video_combo = gtk_combo_box_text_new ();
1992 audio_combo = gtk_combo_box_text_new ();
1993 text_combo = gtk_combo_box_text_new ();
1994 gtk_widget_set_sensitive (video_combo, FALSE);
1995 gtk_widget_set_sensitive (audio_combo, FALSE);
1996 gtk_widget_set_sensitive (text_combo, FALSE);
1997 gtk_box_pack_start (GTK_BOX (panel), video_combo, TRUE, TRUE, 2);
1998 gtk_box_pack_start (GTK_BOX (panel), audio_combo, TRUE, TRUE, 2);
1999 gtk_box_pack_start (GTK_BOX (panel), text_combo, TRUE, TRUE, 2);
2000 g_signal_connect (G_OBJECT (video_combo), "changed",
2001 G_CALLBACK (video_combo_cb), pipeline);
2002 g_signal_connect (G_OBJECT (audio_combo), "changed",
2003 G_CALLBACK (audio_combo_cb), pipeline);
2004 g_signal_connect (G_OBJECT (text_combo), "changed",
2005 G_CALLBACK (text_combo_cb), pipeline);
2006 /* playbin panel for flag checkboxes and volume/mute */
2007 boxes = gtk_hbox_new (FALSE, 0);
2008 vis_checkbox = gtk_check_button_new_with_label ("Vis");
2009 video_checkbox = gtk_check_button_new_with_label ("Video");
2010 audio_checkbox = gtk_check_button_new_with_label ("Audio");
2011 text_checkbox = gtk_check_button_new_with_label ("Text");
2012 mute_checkbox = gtk_check_button_new_with_label ("Mute");
2013 download_checkbox = gtk_check_button_new_with_label ("Download");
2014 buffer_checkbox = gtk_check_button_new_with_label ("Buffer");
2015 volume_label = gtk_label_new ("Volume");
2016 volume_spinbutton = gtk_spin_button_new_with_range (0, 10.0, 0.1);
2017 gtk_spin_button_set_value (GTK_SPIN_BUTTON (volume_spinbutton), 1.0);
2018 gtk_box_pack_start (GTK_BOX (boxes), video_checkbox, TRUE, TRUE, 2);
2019 gtk_box_pack_start (GTK_BOX (boxes), audio_checkbox, TRUE, TRUE, 2);
2020 gtk_box_pack_start (GTK_BOX (boxes), text_checkbox, TRUE, TRUE, 2);
2021 gtk_box_pack_start (GTK_BOX (boxes), vis_checkbox, TRUE, TRUE, 2);
2022 gtk_box_pack_start (GTK_BOX (boxes), mute_checkbox, TRUE, TRUE, 2);
2023 gtk_box_pack_start (GTK_BOX (boxes), download_checkbox, TRUE, TRUE, 2);
2024 gtk_box_pack_start (GTK_BOX (boxes), buffer_checkbox, TRUE, TRUE, 2);
2025 gtk_box_pack_start (GTK_BOX (boxes), volume_label, TRUE, TRUE, 2);
2026 gtk_box_pack_start (GTK_BOX (boxes), volume_spinbutton, TRUE, TRUE, 2);
2027 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (vis_checkbox), FALSE);
2028 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (audio_checkbox), TRUE);
2029 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (video_checkbox), TRUE);
2030 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (text_checkbox), TRUE);
2031 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (mute_checkbox), FALSE);
2032 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (download_checkbox), FALSE);
2033 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (buffer_checkbox), FALSE);
2034 g_signal_connect (G_OBJECT (vis_checkbox), "toggled",
2035 G_CALLBACK (vis_toggle_cb), pipeline);
2036 g_signal_connect (G_OBJECT (audio_checkbox), "toggled",
2037 G_CALLBACK (audio_toggle_cb), pipeline);
2038 g_signal_connect (G_OBJECT (video_checkbox), "toggled",
2039 G_CALLBACK (video_toggle_cb), pipeline);
2040 g_signal_connect (G_OBJECT (text_checkbox), "toggled",
2041 G_CALLBACK (text_toggle_cb), pipeline);
2042 g_signal_connect (G_OBJECT (mute_checkbox), "toggled",
2043 G_CALLBACK (mute_toggle_cb), pipeline);
2044 g_signal_connect (G_OBJECT (download_checkbox), "toggled",
2045 G_CALLBACK (download_toggle_cb), pipeline);
2046 g_signal_connect (G_OBJECT (buffer_checkbox), "toggled",
2047 G_CALLBACK (buffer_toggle_cb), pipeline);
2048 g_signal_connect (G_OBJECT (volume_spinbutton), "value_changed",
2049 G_CALLBACK (volume_spinbutton_changed_cb), pipeline);
2050 /* playbin panel for snapshot */
2051 boxes2 = gtk_hbox_new (FALSE, 0);
2052 shot_button = gtk_button_new_from_stock (GTK_STOCK_SAVE);
2053 gtk_widget_set_tooltip_text (shot_button,
2054 "save a screenshot .png in the current directory");
2055 g_signal_connect (G_OBJECT (shot_button), "clicked", G_CALLBACK (shot_cb),
2057 vis_combo = gtk_combo_box_text_new ();
2058 g_signal_connect (G_OBJECT (vis_combo), "changed",
2059 G_CALLBACK (vis_combo_cb), pipeline);
2060 gtk_widget_set_sensitive (vis_combo, FALSE);
2061 gtk_box_pack_start (GTK_BOX (boxes2), shot_button, TRUE, TRUE, 2);
2062 gtk_box_pack_start (GTK_BOX (boxes2), vis_combo, TRUE, TRUE, 2);
2064 /* fill the vis combo box and the array of factories */
2065 init_visualization_features ();
2067 panel = boxes = boxes2 = NULL;
2070 /* do the packing stuff ... */
2071 gtk_window_set_default_size (GTK_WINDOW (window), 250, 96);
2072 /* FIXME: can we avoid this for audio only? */
2073 gtk_widget_set_size_request (GTK_WIDGET (video_window), -1,
2074 DEFAULT_VIDEO_HEIGHT);
2075 gtk_container_add (GTK_CONTAINER (window), vbox);
2076 gtk_box_pack_start (GTK_BOX (vbox), video_window, TRUE, TRUE, 2);
2077 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 2);
2078 gtk_box_pack_start (GTK_BOX (hbox), play_button, FALSE, FALSE, 2);
2079 gtk_box_pack_start (GTK_BOX (hbox), pause_button, FALSE, FALSE, 2);
2080 gtk_box_pack_start (GTK_BOX (hbox), stop_button, FALSE, FALSE, 2);
2081 gtk_box_pack_start (GTK_BOX (hbox), flagtable, FALSE, FALSE, 2);
2082 gtk_table_attach_defaults (GTK_TABLE (flagtable), accurate_checkbox, 0, 1, 0,
2084 gtk_table_attach_defaults (GTK_TABLE (flagtable), flush_checkbox, 1, 2, 0, 1);
2085 gtk_table_attach_defaults (GTK_TABLE (flagtable), loop_checkbox, 2, 3, 0, 1);
2086 gtk_table_attach_defaults (GTK_TABLE (flagtable), key_checkbox, 0, 1, 1, 2);
2087 gtk_table_attach_defaults (GTK_TABLE (flagtable), scrub_checkbox, 1, 2, 1, 2);
2088 gtk_table_attach_defaults (GTK_TABLE (flagtable), play_scrub_checkbox, 2, 3,
2090 gtk_table_attach_defaults (GTK_TABLE (flagtable), skip_checkbox, 3, 4, 0, 1);
2091 gtk_table_attach_defaults (GTK_TABLE (flagtable), rate_label, 4, 5, 0, 1);
2092 gtk_table_attach_defaults (GTK_TABLE (flagtable), rate_spinbutton, 4, 5, 1,
2094 if (panel && boxes && boxes2) {
2095 expander = gtk_expander_new ("playbin options");
2096 pb2vbox = gtk_vbox_new (FALSE, 0);
2097 gtk_box_pack_start (GTK_BOX (pb2vbox), panel, FALSE, FALSE, 2);
2098 gtk_box_pack_start (GTK_BOX (pb2vbox), boxes, FALSE, FALSE, 2);
2099 gtk_box_pack_start (GTK_BOX (pb2vbox), boxes2, FALSE, FALSE, 2);
2100 gtk_container_add (GTK_CONTAINER (expander), pb2vbox);
2101 gtk_box_pack_start (GTK_BOX (vbox), expander, FALSE, FALSE, 2);
2103 gtk_box_pack_start (GTK_BOX (vbox), step, FALSE, FALSE, 2);
2104 gtk_box_pack_start (GTK_BOX (vbox), hscale, FALSE, FALSE, 2);
2105 gtk_box_pack_start (GTK_BOX (vbox), statusbar, FALSE, FALSE, 2);
2107 /* connect things ... */
2108 g_signal_connect (G_OBJECT (play_button), "clicked", G_CALLBACK (play_cb),
2110 g_signal_connect (G_OBJECT (pause_button), "clicked", G_CALLBACK (pause_cb),
2112 g_signal_connect (G_OBJECT (stop_button), "clicked", G_CALLBACK (stop_cb),
2114 g_signal_connect (G_OBJECT (accurate_checkbox), "toggled",
2115 G_CALLBACK (accurate_toggle_cb), pipeline);
2116 g_signal_connect (G_OBJECT (key_checkbox), "toggled",
2117 G_CALLBACK (key_toggle_cb), pipeline);
2118 g_signal_connect (G_OBJECT (loop_checkbox), "toggled",
2119 G_CALLBACK (loop_toggle_cb), pipeline);
2120 g_signal_connect (G_OBJECT (flush_checkbox), "toggled",
2121 G_CALLBACK (flush_toggle_cb), pipeline);
2122 g_signal_connect (G_OBJECT (scrub_checkbox), "toggled",
2123 G_CALLBACK (scrub_toggle_cb), pipeline);
2124 g_signal_connect (G_OBJECT (play_scrub_checkbox), "toggled",
2125 G_CALLBACK (play_scrub_toggle_cb), pipeline);
2126 g_signal_connect (G_OBJECT (skip_checkbox), "toggled",
2127 G_CALLBACK (skip_toggle_cb), pipeline);
2128 g_signal_connect (G_OBJECT (rate_spinbutton), "value_changed",
2129 G_CALLBACK (rate_spinbutton_changed_cb), pipeline);
2131 g_signal_connect (G_OBJECT (window), "delete-event", delete_event_cb, NULL);
2134 gtk_widget_show_all (window);
2136 /* realize window now so that the video window gets created and we can
2137 * obtain its XID before the pipeline is started up and the videosink
2138 * asks for the XID of the window to render onto */
2139 gtk_widget_realize (window);
2141 #if defined (GDK_WINDOWING_X11) || defined (GDK_WINDOWING_WIN32)
2142 /* we should have the XID now */
2143 g_assert (embed_xid != 0);
2147 g_signal_connect (pipeline, "deep_notify",
2148 G_CALLBACK (gst_object_default_deep_notify), NULL);
2151 connect_bus_signals (pipeline);
2154 g_print ("NULL pipeline\n");
2155 gst_element_set_state (pipeline, GST_STATE_NULL);
2157 g_print ("free pipeline\n");
2158 gst_object_unref (pipeline);
2160 g_list_foreach (paths, (GFunc) g_free, NULL);
2161 g_list_free (paths);