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/interfaces/videooverlay.h>
44 #if (!GTK_CHECK_VERSION(2, 23, 0) || GTK_CHECK_VERSION(2, 90, 0)) && !GTK_CHECK_VERSION(2, 91, 1)
45 #define gtk_combo_box_text_new gtk_combo_box_new_text
46 #define gtk_combo_box_text_append_text gtk_combo_box_append_text
47 #define gtk_combo_box_text_remove gtk_combo_box_remove_text
48 #define GTK_COMBO_BOX_TEXT GTK_COMBO_BOX
51 GST_DEBUG_CATEGORY_STATIC (seek_debug);
52 #define GST_CAT_DEFAULT (seek_debug)
54 #if !GTK_CHECK_VERSION (2, 17, 7)
56 gtk_widget_get_allocation (GtkWidget * w, GtkAllocation * a)
64 #define SOURCE "filesrc"
66 static gchar *opt_audiosink_str; /* NULL */
67 static gchar *opt_videosink_str; /* NULL */
69 #define FILL_INTERVAL 100
70 //#define UPDATE_INTERVAL 500
71 //#define UPDATE_INTERVAL 100
72 #define UPDATE_INTERVAL 40
74 /* number of milliseconds to play for after a seek */
75 #define SCRUB_TIME 100
77 /* timeout for gst_element_get_state() after a seek */
78 #define SEEK_TIMEOUT 40 * GST_MSECOND
80 #define DEFAULT_VIDEO_HEIGHT 300
82 /* the state to go to when stop is pressed */
83 #define STOP_STATE GST_STATE_READY
87 static GList *seekable_elements = NULL;
89 static gboolean accurate_seek = FALSE;
90 static gboolean keyframe_seek = FALSE;
91 static gboolean loop_seek = FALSE;
92 static gboolean flush_seek = TRUE;
93 static gboolean scrub = TRUE;
94 static gboolean play_scrub = FALSE;
95 static gboolean skip_seek = FALSE;
96 static gdouble rate = 1.0;
98 static GstElement *pipeline;
99 static gint pipeline_type;
100 static const gchar *pipeline_spec;
101 static gint64 position = -1;
102 static gint64 duration = -1;
103 static GtkAdjustment *adjustment;
104 static GtkWidget *hscale, *statusbar;
105 static guint status_id = 0;
106 static gboolean stats = FALSE;
107 static gboolean verbose = FALSE;
109 static gboolean is_live = FALSE;
110 static gboolean buffering = FALSE;
111 static GstBufferingMode mode;
112 static gint64 buffering_left;
113 static GstState state = GST_STATE_NULL;
114 static guint update_id = 0;
115 static guint seek_timeout_id = 0;
116 static gulong changed_id;
117 static guint fill_id = 0;
119 static gint n_video = 0, n_audio = 0, n_text = 0;
120 static gboolean need_streams = TRUE;
121 static GtkWidget *video_combo, *audio_combo, *text_combo, *vis_combo;
122 static GtkWidget *vis_checkbox, *video_checkbox, *audio_checkbox;
123 static GtkWidget *text_checkbox, *mute_checkbox, *volume_spinbutton;
124 static GtkWidget *skip_checkbox, *video_window, *download_checkbox;
125 static GtkWidget *buffer_checkbox, *rate_spinbutton;
127 static GStaticMutex state_mutex = G_STATIC_MUTEX_INIT;
129 static GtkWidget *format_combo, *step_amount_spinbutton, *step_rate_spinbutton;
130 static GtkWidget *shuttle_checkbox, *step_button;
131 static GtkWidget *shuttle_hscale;
132 static GtkAdjustment *shuttle_adjustment;
134 static GList *paths = NULL, *l = NULL;
136 /* we keep an array of the visualisation entries so that we can easily switch
137 * with the combo box index. */
140 GstElementFactory *factory;
143 static GArray *vis_entries;
145 static void clear_streams (GstElement * pipeline);
146 static void volume_notify_cb (GstElement * pipeline, GParamSpec * arg,
149 /* pipeline construction */
153 const gchar *padname;
160 gst_element_factory_make_or_warn (const gchar * type, const gchar * name)
162 GstElement *element = gst_element_factory_make (type, name);
165 g_warning ("Failed to create element %s of type %s", name, type);
172 playerbin_set_uri (GstElement * player, const gchar * location)
176 /* Add "file://" prefix for convenience */
177 if (g_str_has_prefix (location, "/") || !gst_uri_is_valid (location)) {
178 uri = gst_filename_to_uri (location, NULL);
179 g_print ("Setting URI: %s\n", uri);
180 g_object_set (G_OBJECT (player), "uri", uri, NULL);
183 g_print ("Setting URI: %s\n", location);
184 g_object_set (G_OBJECT (player), "uri", location, NULL);
189 construct_playbin (const gchar * name, const gchar * location)
194 player = gst_element_factory_make (name, "player");
197 playerbin_set_uri (player, location);
199 seekable_elements = g_list_prepend (seekable_elements, player);
201 avsink = gst_element_factory_make_or_warn (opt_audiosink_str, "a_sink");
203 g_object_set (player, "audio-sink", avsink, NULL);
205 avsink = gst_element_factory_make_or_warn (opt_videosink_str, "v_sink");
207 g_object_set (player, "video-sink", avsink, NULL);
213 make_playbin_pipeline (const gchar * location)
215 GstElement *pipeline = construct_playbin ("playbin", location);
217 /* FIXME: this is not triggered, playbin is not forwarding it from the sink */
218 g_signal_connect (pipeline, "notify::volume", G_CALLBACK (volume_notify_cb),
223 #ifndef GST_DISABLE_PARSE
225 make_parselaunch_pipeline (const gchar * description)
227 GstElement *pipeline;
228 GError *error = NULL;
230 pipeline = gst_parse_launch (description, &error);
232 seekable_elements = g_list_prepend (seekable_elements, pipeline);
241 GstElement *(*func) (const gchar * location);
245 static Pipeline pipelines[] = {
246 {"playbin", make_playbin_pipeline},
247 #ifndef GST_DISABLE_PARSE
248 {"parse-launch", make_parselaunch_pipeline},
253 #define NUM_TYPES ((sizeof (pipelines) / sizeof (Pipeline)) - 1)
255 /* ui callbacks and helpers */
258 format_value (GtkScale * scale, gdouble value)
264 real = value * duration / N_GRAD;
265 seconds = (gint64) real / GST_SECOND;
266 subseconds = (gint64) real / (GST_SECOND / N_GRAD);
268 return g_strdup_printf ("%02" G_GINT64_FORMAT ":%02" G_GINT64_FORMAT ":%02"
269 G_GINT64_FORMAT, seconds / 60, seconds % 60, subseconds % 100);
274 shuttle_format_value (GtkScale * scale, gdouble value)
276 return g_strdup_printf ("%0.*g", gtk_scale_get_digits (scale), value);
282 const GstFormat format;
286 static seek_format seek_formats[] = {
287 {"tim", GST_FORMAT_TIME},
288 {"byt", GST_FORMAT_BYTES},
289 {"buf", GST_FORMAT_BUFFERS},
290 {"def", GST_FORMAT_DEFAULT},
294 G_GNUC_UNUSED static void
295 query_positions_elems (void)
297 GList *walk = seekable_elements;
300 GstElement *element = GST_ELEMENT (walk->data);
303 g_print ("positions %8.8s: ", GST_ELEMENT_NAME (element));
304 while (seek_formats[i].name) {
305 gint64 position, total;
308 format = seek_formats[i].format;
310 if (gst_element_query_position (element, format, &position) &&
311 gst_element_query_duration (element, format, &total)) {
312 g_print ("%s %13" G_GINT64_FORMAT " / %13" G_GINT64_FORMAT " | ",
313 seek_formats[i].name, position, total);
315 g_print ("%s %13.13s / %13.13s | ", seek_formats[i].name, "*NA*",
320 g_print (" %s\n", GST_ELEMENT_NAME (element));
322 walk = g_list_next (walk);
326 static gboolean start_seek (GtkWidget * widget, GdkEventButton * event,
328 static gboolean stop_seek (GtkWidget * widget, GdkEventButton * event,
330 static void seek_cb (GtkWidget * widget);
333 set_scale (gdouble value)
335 g_signal_handlers_block_by_func (hscale, (void *) start_seek,
337 g_signal_handlers_block_by_func (hscale, (void *) stop_seek,
339 g_signal_handlers_block_by_func (hscale, (void *) seek_cb, (void *) pipeline);
340 gtk_adjustment_set_value (adjustment, value);
341 g_signal_handlers_unblock_by_func (hscale, (void *) start_seek,
343 g_signal_handlers_unblock_by_func (hscale, (void *) stop_seek,
345 g_signal_handlers_unblock_by_func (hscale, (void *) seek_cb,
347 gtk_widget_queue_draw (hscale);
351 update_fill (gpointer data)
353 if (seekable_elements) {
354 GstElement *element = GST_ELEMENT (seekable_elements->data);
357 query = gst_query_new_buffering (GST_FORMAT_PERCENT);
358 if (gst_element_query (element, query)) {
359 gint64 start, stop, buffering_total;
364 GstBufferingMode mode;
365 gint avg_in, avg_out;
366 gint64 buffering_left;
368 gst_query_parse_buffering_percent (query, &busy, &percent);
369 gst_query_parse_buffering_range (query, &format, &start, &stop,
371 gst_query_parse_buffering_stats (query, &mode, &avg_in, &avg_out,
374 /* note that we could start the playback when buffering_left < remaining
376 GST_DEBUG ("buffering total %" G_GINT64_FORMAT " ms, left %"
377 G_GINT64_FORMAT " ms", buffering_total, buffering_left);
378 GST_DEBUG ("start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT,
382 fill = N_GRAD * stop / GST_FORMAT_PERCENT_MAX;
386 gtk_range_set_fill_level (GTK_RANGE (hscale), fill);
388 gst_query_unref (query);
394 update_scale (gpointer data)
396 if (seekable_elements) {
397 GstElement *element = GST_ELEMENT (seekable_elements->data);
399 gst_element_query_position (element, GST_FORMAT_TIME, &position);
400 gst_element_query_duration (element, GST_FORMAT_TIME, &duration);
404 query_positions_elems ();
407 if (position >= duration)
411 set_scale (position * N_GRAD / duration);
414 /* FIXME: see make_playerbin2_pipeline() and volume_notify_cb() */
415 if (pipeline_type == 16) {
416 g_object_notify (G_OBJECT (pipeline), "volume");
422 static void do_seek (GtkWidget * widget);
423 static void connect_bus_signals (GstElement * pipeline);
424 static void set_update_scale (gboolean active);
425 static void set_update_fill (gboolean active);
428 end_scrub (GtkWidget * widget)
430 GST_DEBUG ("end scrub, PAUSE");
431 gst_element_set_state (pipeline, GST_STATE_PAUSED);
438 send_event (GstEvent * event)
440 gboolean res = FALSE;
441 GList *walk = seekable_elements;
444 GstElement *seekable = GST_ELEMENT (walk->data);
446 GST_DEBUG ("send event on element %s", GST_ELEMENT_NAME (seekable));
448 gst_event_ref (event);
449 res = gst_element_send_event (seekable, event);
451 walk = g_list_next (walk);
453 gst_event_unref (event);
458 do_seek (GtkWidget * widget)
461 gboolean res = FALSE;
465 real = gtk_range_get_value (GTK_RANGE (widget)) * duration / N_GRAD;
467 GST_DEBUG ("value=%f, real=%" G_GINT64_FORMAT,
468 gtk_range_get_value (GTK_RANGE (widget)), real);
472 flags |= GST_SEEK_FLAG_FLUSH;
474 flags |= GST_SEEK_FLAG_ACCURATE;
476 flags |= GST_SEEK_FLAG_KEY_UNIT;
478 flags |= GST_SEEK_FLAG_SEGMENT;
480 flags |= GST_SEEK_FLAG_SKIP;
483 s_event = gst_event_new_seek (rate,
484 GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, real, GST_SEEK_TYPE_SET,
485 GST_CLOCK_TIME_NONE);
486 GST_DEBUG ("seek with rate %lf to %" GST_TIME_FORMAT " / %" GST_TIME_FORMAT,
487 rate, GST_TIME_ARGS (real), GST_TIME_ARGS (duration));
489 s_event = gst_event_new_seek (rate,
490 GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, G_GINT64_CONSTANT (0),
491 GST_SEEK_TYPE_SET, real);
492 GST_DEBUG ("seek with rate %lf to %" GST_TIME_FORMAT " / %" GST_TIME_FORMAT,
493 rate, GST_TIME_ARGS (0), GST_TIME_ARGS (real));
496 res = send_event (s_event);
500 gst_element_get_state (GST_ELEMENT (pipeline), NULL, NULL, SEEK_TIMEOUT);
502 set_update_scale (TRUE);
505 g_print ("seek failed\n");
506 set_update_scale (TRUE);
511 seek_cb (GtkWidget * widget)
513 /* If the timer hasn't expired yet, then the pipeline is running */
514 if (play_scrub && seek_timeout_id != 0) {
515 GST_DEBUG ("do scrub seek, PAUSED");
516 gst_element_set_state (pipeline, GST_STATE_PAUSED);
519 GST_DEBUG ("do seek");
523 GST_DEBUG ("do scrub seek, PLAYING");
524 gst_element_set_state (pipeline, GST_STATE_PLAYING);
526 if (seek_timeout_id == 0) {
528 g_timeout_add (SCRUB_TIME, (GSourceFunc) end_scrub, widget);
534 set_update_fill (gboolean active)
536 GST_DEBUG ("fill scale is %d", active);
541 g_timeout_add (FILL_INTERVAL, (GSourceFunc) update_fill, pipeline);
545 g_source_remove (fill_id);
552 set_update_scale (gboolean active)
555 GST_DEBUG ("update scale is %d", active);
558 if (update_id == 0) {
560 g_timeout_add (UPDATE_INTERVAL, (GSourceFunc) update_scale, pipeline);
564 g_source_remove (update_id);
571 start_seek (GtkWidget * widget, GdkEventButton * event, gpointer user_data)
573 if (event->type != GDK_BUTTON_PRESS)
576 set_update_scale (FALSE);
578 if (state == GST_STATE_PLAYING && flush_seek && scrub) {
579 GST_DEBUG ("start scrub seek, PAUSE");
580 gst_element_set_state (pipeline, GST_STATE_PAUSED);
583 if (changed_id == 0 && flush_seek && scrub) {
585 g_signal_connect (hscale, "value_changed", G_CALLBACK (seek_cb),
593 stop_seek (GtkWidget * widget, GdkEventButton * event, gpointer user_data)
596 g_signal_handler_disconnect (hscale, changed_id);
600 if (!flush_seek || !scrub) {
601 GST_DEBUG ("do final seek");
605 if (seek_timeout_id != 0) {
606 g_source_remove (seek_timeout_id);
608 /* Still scrubbing, so the pipeline is playing, see if we need PAUSED
610 if (state == GST_STATE_PAUSED) {
611 GST_DEBUG ("stop scrub seek, PAUSED");
612 gst_element_set_state (pipeline, GST_STATE_PAUSED);
615 if (state == GST_STATE_PLAYING) {
616 GST_DEBUG ("stop scrub seek, PLAYING");
617 gst_element_set_state (pipeline, GST_STATE_PLAYING);
625 play_cb (GtkButton * button, gpointer data)
627 GstStateChangeReturn ret;
629 if (state != GST_STATE_PLAYING) {
630 g_print ("PLAY pipeline\n");
631 gtk_statusbar_pop (GTK_STATUSBAR (statusbar), status_id);
633 ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
635 case GST_STATE_CHANGE_FAILURE:
637 case GST_STATE_CHANGE_NO_PREROLL:
643 state = GST_STATE_PLAYING;
644 gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Playing");
651 g_print ("PLAY failed\n");
652 gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Play failed");
657 pause_cb (GtkButton * button, gpointer data)
659 g_static_mutex_lock (&state_mutex);
660 if (state != GST_STATE_PAUSED) {
661 GstStateChangeReturn ret;
663 gtk_statusbar_pop (GTK_STATUSBAR (statusbar), status_id);
664 g_print ("PAUSE pipeline\n");
665 ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
667 case GST_STATE_CHANGE_FAILURE:
669 case GST_STATE_CHANGE_NO_PREROLL:
676 state = GST_STATE_PAUSED;
677 gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Paused");
679 g_static_mutex_unlock (&state_mutex);
685 g_static_mutex_unlock (&state_mutex);
686 g_print ("PAUSE failed\n");
687 gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Pause failed");
692 stop_cb (GtkButton * button, gpointer data)
694 if (state != STOP_STATE) {
695 GstStateChangeReturn ret;
697 g_print ("READY pipeline\n");
698 gtk_statusbar_pop (GTK_STATUSBAR (statusbar), status_id);
700 g_static_mutex_lock (&state_mutex);
701 ret = gst_element_set_state (pipeline, STOP_STATE);
702 if (ret == GST_STATE_CHANGE_FAILURE)
706 gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Stopped");
710 set_update_scale (FALSE);
712 set_update_fill (FALSE);
714 if (pipeline_type == 16)
715 clear_streams (pipeline);
716 g_static_mutex_unlock (&state_mutex);
719 /* if one uses parse_launch, play, stop and play again it fails as all the
720 * pads after the demuxer can't be reconnected
722 if (!strcmp (pipelines[pipeline_type].name, "parse-launch")) {
723 gst_element_set_state (pipeline, GST_STATE_NULL);
724 gst_object_unref (pipeline);
726 g_list_free (seekable_elements);
727 seekable_elements = NULL;
729 pipeline = pipelines[pipeline_type].func (pipeline_spec);
731 gst_element_set_state (pipeline, STOP_STATE);
732 connect_bus_signals (pipeline);
740 g_static_mutex_unlock (&state_mutex);
741 g_print ("STOP failed\n");
742 gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Stop failed");
747 accurate_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
749 accurate_seek = gtk_toggle_button_get_active (button);
753 key_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
755 keyframe_seek = gtk_toggle_button_get_active (button);
759 loop_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
761 loop_seek = gtk_toggle_button_get_active (button);
762 if (state == GST_STATE_PLAYING) {
768 flush_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
770 flush_seek = gtk_toggle_button_get_active (button);
774 scrub_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
776 scrub = gtk_toggle_button_get_active (button);
780 play_scrub_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
782 play_scrub = gtk_toggle_button_get_active (button);
786 skip_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
788 skip_seek = gtk_toggle_button_get_active (button);
789 if (state == GST_STATE_PLAYING) {
795 rate_spinbutton_changed_cb (GtkSpinButton * button, GstPipeline * pipeline)
797 gboolean res = FALSE;
801 rate = gtk_spin_button_get_value (button);
803 GST_DEBUG ("rate changed to %lf", rate);
807 flags |= GST_SEEK_FLAG_FLUSH;
809 flags |= GST_SEEK_FLAG_SEGMENT;
811 flags |= GST_SEEK_FLAG_ACCURATE;
813 flags |= GST_SEEK_FLAG_KEY_UNIT;
815 flags |= GST_SEEK_FLAG_SKIP;
818 s_event = gst_event_new_seek (rate,
819 GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, position,
820 GST_SEEK_TYPE_SET, GST_CLOCK_TIME_NONE);
822 s_event = gst_event_new_seek (rate,
823 GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, G_GINT64_CONSTANT (0),
824 GST_SEEK_TYPE_SET, position);
827 res = send_event (s_event);
831 gst_element_get_state (GST_ELEMENT (pipeline), NULL, NULL, SEEK_TIMEOUT);
834 g_print ("seek failed\n");
838 update_flag (GstPipeline * pipeline, gint num, gboolean state)
842 g_object_get (pipeline, "flags", &flags, NULL);
846 flags &= ~(1 << num);
847 g_object_set (pipeline, "flags", flags, NULL);
851 vis_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
855 state = gtk_toggle_button_get_active (button);
856 update_flag (pipeline, 3, state);
857 gtk_widget_set_sensitive (vis_combo, state);
861 audio_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
865 state = gtk_toggle_button_get_active (button);
866 update_flag (pipeline, 1, state);
867 gtk_widget_set_sensitive (audio_combo, state);
871 video_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
875 state = gtk_toggle_button_get_active (button);
876 update_flag (pipeline, 0, state);
877 gtk_widget_set_sensitive (video_combo, state);
881 text_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
885 state = gtk_toggle_button_get_active (button);
886 update_flag (pipeline, 2, state);
887 gtk_widget_set_sensitive (text_combo, state);
891 mute_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
895 mute = gtk_toggle_button_get_active (button);
896 g_object_set (pipeline, "mute", mute, NULL);
900 download_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
904 state = gtk_toggle_button_get_active (button);
905 update_flag (pipeline, 7, state);
909 buffer_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
913 state = gtk_toggle_button_get_active (button);
914 update_flag (pipeline, 8, state);
918 clear_streams (GstElement * pipeline)
922 /* remove previous info */
923 for (i = 0; i < n_video; i++)
924 gtk_combo_box_text_remove (GTK_COMBO_BOX_TEXT (video_combo), 0);
925 for (i = 0; i < n_audio; i++)
926 gtk_combo_box_text_remove (GTK_COMBO_BOX_TEXT (audio_combo), 0);
927 for (i = 0; i < n_text; i++)
928 gtk_combo_box_text_remove (GTK_COMBO_BOX_TEXT (text_combo), 0);
930 n_audio = n_video = n_text = 0;
931 gtk_widget_set_sensitive (video_combo, FALSE);
932 gtk_widget_set_sensitive (audio_combo, FALSE);
933 gtk_widget_set_sensitive (text_combo, FALSE);
939 update_streams (GstPipeline * pipeline)
943 if (pipeline_type == 16 && need_streams) {
949 /* remove previous info */
950 clear_streams (GST_ELEMENT_CAST (pipeline));
952 /* here we get and update the different streams detected by playbin2 */
953 g_object_get (pipeline, "n-video", &n_video, NULL);
954 g_object_get (pipeline, "n-audio", &n_audio, NULL);
955 g_object_get (pipeline, "n-text", &n_text, NULL);
957 g_print ("video %d, audio %d, text %d\n", n_video, n_audio, n_text);
960 for (i = 0; i < n_video; i++) {
961 g_signal_emit_by_name (pipeline, "get-video-tags", i, &tags);
963 str = gst_structure_to_string ((GstStructure *) tags);
964 g_print ("video %d: %s\n", i, str);
967 /* find good name for the label */
968 name = g_strdup_printf ("video %d", i + 1);
969 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (video_combo), name);
972 state = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (video_checkbox));
973 gtk_widget_set_sensitive (video_combo, state && n_video > 0);
974 gtk_combo_box_set_active (GTK_COMBO_BOX (video_combo), active_idx);
977 for (i = 0; i < n_audio; i++) {
978 g_signal_emit_by_name (pipeline, "get-audio-tags", i, &tags);
980 str = gst_structure_to_string ((GstStructure *) tags);
981 g_print ("audio %d: %s\n", i, str);
984 /* find good name for the label */
985 name = g_strdup_printf ("audio %d", i + 1);
986 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (audio_combo), name);
989 state = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (audio_checkbox));
990 gtk_widget_set_sensitive (audio_combo, state && n_audio > 0);
991 gtk_combo_box_set_active (GTK_COMBO_BOX (audio_combo), active_idx);
994 for (i = 0; i < n_text; i++) {
995 g_signal_emit_by_name (pipeline, "get-text-tags", i, &tags);
1001 str = gst_structure_to_string ((GstStructure *) tags);
1002 g_print ("text %d: %s\n", i, str);
1005 /* get the language code if we can */
1006 value = gst_tag_list_get_value_index (tags, GST_TAG_LANGUAGE_CODE, 0);
1007 if (value && G_VALUE_HOLDS_STRING (value)) {
1008 name = g_strdup_printf ("text %s", g_value_get_string (value));
1011 /* find good name for the label if we didn't use a tag */
1013 name = g_strdup_printf ("text %d", i + 1);
1015 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (text_combo), name);
1018 state = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (text_checkbox));
1019 gtk_widget_set_sensitive (text_combo, state && n_text > 0);
1020 gtk_combo_box_set_active (GTK_COMBO_BOX (text_combo), active_idx);
1022 need_streams = FALSE;
1027 video_combo_cb (GtkComboBox * combo, GstPipeline * pipeline)
1031 active = gtk_combo_box_get_active (combo);
1033 g_print ("setting current video track %d\n", active);
1034 g_object_set (pipeline, "current-video", active, NULL);
1038 audio_combo_cb (GtkComboBox * combo, GstPipeline * pipeline)
1042 active = gtk_combo_box_get_active (combo);
1044 g_print ("setting current audio track %d\n", active);
1045 g_object_set (pipeline, "current-audio", active, NULL);
1049 text_combo_cb (GtkComboBox * combo, GstPipeline * pipeline)
1053 active = gtk_combo_box_get_active (combo);
1055 g_print ("setting current text track %d\n", active);
1056 g_object_set (pipeline, "current-text", active, NULL);
1060 filter_features (GstPluginFeature * feature, gpointer data)
1062 GstElementFactory *f;
1064 if (!GST_IS_ELEMENT_FACTORY (feature))
1066 f = GST_ELEMENT_FACTORY (feature);
1067 if (!g_strrstr (gst_element_factory_get_klass (f), "Visualization"))
1074 init_visualization_features (void)
1078 vis_entries = g_array_new (FALSE, FALSE, sizeof (VisEntry));
1080 list = gst_registry_feature_filter (gst_registry_get_default (),
1081 filter_features, FALSE, NULL);
1083 for (walk = list; walk; walk = g_list_next (walk)) {
1087 entry.factory = GST_ELEMENT_FACTORY (walk->data);
1088 name = gst_element_factory_get_longname (entry.factory);
1090 g_array_append_val (vis_entries, entry);
1091 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (vis_combo), name);
1093 gtk_combo_box_set_active (GTK_COMBO_BOX (vis_combo), 0);
1097 vis_combo_cb (GtkComboBox * combo, GstPipeline * pipeline)
1101 GstElement *element;
1103 /* get the selected index and get the factory for this index */
1104 index = gtk_combo_box_get_active (GTK_COMBO_BOX (vis_combo));
1105 if (vis_entries->len > 0) {
1106 entry = &g_array_index (vis_entries, VisEntry, index);
1108 /* create an instance of the element from the factory */
1109 element = gst_element_factory_create (entry->factory, NULL);
1113 /* set vis plugin for playbin2 */
1114 g_object_set (pipeline, "vis-plugin", element, NULL);
1119 volume_spinbutton_changed_cb (GtkSpinButton * button, GstPipeline * pipeline)
1123 volume = gtk_spin_button_get_value (button);
1125 g_object_set (pipeline, "volume", volume, NULL);
1129 volume_notify_cb (GstElement * pipeline, GParamSpec * arg, gpointer user_dat)
1131 gdouble cur_volume, new_volume;
1133 if (volume_spinbutton == NULL)
1136 g_object_get (pipeline, "volume", &new_volume, NULL);
1137 cur_volume = gtk_spin_button_get_value (GTK_SPIN_BUTTON (volume_spinbutton));
1138 if (fabs (cur_volume - new_volume) > 0.001) {
1139 g_signal_handlers_block_by_func (volume_spinbutton,
1140 volume_spinbutton_changed_cb, pipeline);
1141 gtk_spin_button_set_value (GTK_SPIN_BUTTON (volume_spinbutton), new_volume);
1142 g_signal_handlers_unblock_by_func (volume_spinbutton,
1143 volume_spinbutton_changed_cb, pipeline);
1148 shot_cb (GtkButton * button, gpointer data)
1153 /* convert to our desired format (RGB24) */
1154 caps = gst_caps_new_simple ("video/x-raw-rgb",
1155 "bpp", G_TYPE_INT, 24, "depth", G_TYPE_INT, 24,
1156 /* Note: we don't ask for a specific width/height here, so that
1157 * videoscale can adjust dimensions from a non-1/1 pixel aspect
1158 * ratio to a 1/1 pixel-aspect-ratio */
1159 "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
1160 "endianness", G_TYPE_INT, G_BIG_ENDIAN,
1161 "red_mask", G_TYPE_INT, 0xff0000,
1162 "green_mask", G_TYPE_INT, 0x00ff00,
1163 "blue_mask", G_TYPE_INT, 0x0000ff, NULL);
1165 /* convert the latest frame to the requested format */
1166 g_signal_emit_by_name (pipeline, "convert-frame", caps, &buffer);
1167 gst_caps_unref (caps);
1175 GError *error = NULL;
1179 /* get the snapshot buffer format now. We set the caps on the appsink so
1180 * that it can only be an rgb buffer. The only thing we have not specified
1181 * on the caps is the height, which is dependant on the pixel-aspect-ratio
1182 * of the source material */
1184 caps = GST_BUFFER_CAPS (buffer);
1186 /* FIXME, need to get the caps of the buffer somehow */
1189 g_warning ("could not get snapshot format\n");
1192 s = gst_caps_get_structure (caps, 0);
1194 /* we need to get the final caps on the buffer to get the size */
1195 res = gst_structure_get_int (s, "width", &width);
1196 res |= gst_structure_get_int (s, "height", &height);
1198 g_warning ("could not get snapshot dimension\n");
1202 /* create pixmap from buffer and save, gstreamer video buffers have a stride
1203 * that is rounded up to the nearest multiple of 4 */
1204 data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
1205 pixbuf = gdk_pixbuf_new_from_data (data,
1206 GDK_COLORSPACE_RGB, FALSE, 8, width, height,
1207 GST_ROUND_UP_4 (width * 3), NULL, NULL);
1209 /* save the pixbuf */
1210 gdk_pixbuf_save (pixbuf, "snapshot.png", "png", &error, NULL);
1211 gst_buffer_unmap (buffer, data, size);
1214 gst_buffer_unref (buffer);
1218 /* called when the Step button is pressed */
1220 step_cb (GtkButton * button, gpointer data)
1226 gboolean flush, res;
1229 active = gtk_combo_box_get_active (GTK_COMBO_BOX (format_combo));
1231 gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
1232 (step_amount_spinbutton));
1233 rate = gtk_spin_button_get_value (GTK_SPIN_BUTTON (step_rate_spinbutton));
1238 format = GST_FORMAT_BUFFERS;
1241 format = GST_FORMAT_TIME;
1242 amount *= GST_MSECOND;
1245 format = GST_FORMAT_UNDEFINED;
1249 event = gst_event_new_step (format, amount, rate, flush, FALSE);
1251 res = send_event (event);
1254 g_print ("Sending step event failed\n");
1259 message_received (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
1261 const GstStructure *s;
1263 s = gst_message_get_structure (message);
1264 g_print ("message from \"%s\" (%s): ",
1265 GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))),
1266 gst_message_type_get_name (GST_MESSAGE_TYPE (message)));
1270 sstr = gst_structure_to_string (s);
1271 g_print ("%s\n", sstr);
1274 g_print ("no message details\n");
1278 static gboolean shuttling = FALSE;
1279 static gdouble shuttle_rate = 0.0;
1280 static gdouble play_rate = 1.0;
1283 do_shuttle (GstElement * element)
1288 duration = 40 * GST_MSECOND;
1292 gst_element_send_event (element,
1293 gst_event_new_step (GST_FORMAT_TIME, duration, shuttle_rate, FALSE,
1298 msg_sync_step_done (GstBus * bus, GstMessage * message, GstElement * element)
1304 gboolean intermediate;
1308 gst_message_parse_step_done (message, &format, &amount, &rate, &flush,
1309 &intermediate, &duration, &eos);
1312 g_print ("stepped till EOS\n");
1316 if (g_static_mutex_trylock (&state_mutex)) {
1318 do_shuttle (element);
1319 g_static_mutex_unlock (&state_mutex);
1321 /* ignore step messages that come while we are doing a state change */
1322 g_print ("state change is busy\n");
1327 shuttle_toggled (GtkToggleButton * button, GstElement * element)
1331 active = gtk_toggle_button_get_active (button);
1333 if (active != shuttling) {
1335 g_print ("shuttling %s\n", shuttling ? "active" : "inactive");
1339 pause_cb (NULL, NULL);
1340 gst_element_get_state (element, NULL, NULL, -1);
1346 shuttle_rate_switch (GstElement * element)
1352 if (state == GST_STATE_PLAYING) {
1353 /* pause when we need to */
1354 pause_cb (NULL, NULL);
1355 gst_element_get_state (element, NULL, NULL, -1);
1358 if (play_rate == 1.0)
1363 g_print ("rate changed to %lf %" GST_TIME_FORMAT "\n", play_rate,
1364 GST_TIME_ARGS (position));
1366 flags = GST_SEEK_FLAG_FLUSH;
1367 flags |= GST_SEEK_FLAG_ACCURATE;
1369 if (play_rate >= 0.0) {
1370 s_event = gst_event_new_seek (play_rate,
1371 GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, position,
1372 GST_SEEK_TYPE_SET, GST_CLOCK_TIME_NONE);
1374 s_event = gst_event_new_seek (play_rate,
1375 GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, G_GINT64_CONSTANT (0),
1376 GST_SEEK_TYPE_SET, position);
1378 res = send_event (s_event);
1380 gst_element_get_state (element, NULL, NULL, SEEK_TIMEOUT);
1382 g_print ("seek failed\n");
1387 shuttle_value_changed (GtkRange * range, GstElement * element)
1391 rate = gtk_adjustment_get_value (shuttle_adjustment);
1394 g_print ("rate 0.0, pause\n");
1395 pause_cb (NULL, NULL);
1396 gst_element_get_state (element, NULL, NULL, -1);
1398 g_print ("rate changed %0.3g\n", rate);
1400 if ((rate < 0.0 && play_rate > 0.0) || (rate > 0.0 && play_rate < 0.0)) {
1401 shuttle_rate_switch (element);
1404 shuttle_rate = ABS (rate);
1405 if (state != GST_STATE_PLAYING) {
1406 do_shuttle (element);
1407 play_cb (NULL, NULL);
1413 msg_async_done (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
1415 GST_DEBUG ("async done");
1416 /* when we get ASYNC_DONE we can query position, duration and other
1418 update_scale (pipeline);
1420 /* update the available streams */
1421 update_streams (pipeline);
1425 msg_state_changed (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
1427 const GstStructure *s;
1429 s = gst_message_get_structure (message);
1431 /* We only care about state changed on the pipeline */
1432 if (s && GST_MESSAGE_SRC (message) == GST_OBJECT_CAST (pipeline)) {
1433 GstState old, new, pending;
1435 gst_message_parse_state_changed (message, &old, &new, &pending);
1437 /* When state of the pipeline changes to paused or playing we start updating scale */
1438 if (new == GST_STATE_PLAYING) {
1439 set_update_scale (TRUE);
1441 set_update_scale (FALSE);
1447 msg_segment_done (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
1454 GST_DEBUG ("position is %" GST_TIME_FORMAT, GST_TIME_ARGS (position));
1455 gst_message_parse_segment_done (message, &format, &position);
1456 GST_DEBUG ("end of segment at %" GST_TIME_FORMAT, GST_TIME_ARGS (position));
1459 /* in the segment-done callback we never flush as this would not make sense
1460 * for seamless playback. */
1462 flags |= GST_SEEK_FLAG_SEGMENT;
1464 flags |= GST_SEEK_FLAG_SKIP;
1466 s_event = gst_event_new_seek (rate,
1467 GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, G_GINT64_CONSTANT (0),
1468 GST_SEEK_TYPE_SET, duration);
1470 GST_DEBUG ("restart loop with rate %lf to 0 / %" GST_TIME_FORMAT,
1471 rate, GST_TIME_ARGS (duration));
1473 res = send_event (s_event);
1475 g_print ("segment seek failed\n");
1478 /* in stream buffering mode we PAUSE the pipeline until we receive a 100%
1481 do_stream_buffering (gint percent)
1485 gtk_statusbar_pop (GTK_STATUSBAR (statusbar), status_id);
1486 bufstr = g_strdup_printf ("Buffering...%d", percent);
1487 gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, bufstr);
1490 if (percent == 100) {
1491 /* a 100% message means buffering is done */
1493 /* if the desired state is playing, go back */
1494 if (state == GST_STATE_PLAYING) {
1495 /* no state management needed for live pipelines */
1497 fprintf (stderr, "Done buffering, setting pipeline to PLAYING ...\n");
1498 gst_element_set_state (pipeline, GST_STATE_PLAYING);
1500 gtk_statusbar_pop (GTK_STATUSBAR (statusbar), status_id);
1501 gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Playing");
1504 /* buffering busy */
1505 if (buffering == FALSE && state == GST_STATE_PLAYING) {
1506 /* we were not buffering but PLAYING, PAUSE the pipeline. */
1508 fprintf (stderr, "Buffering, setting pipeline to PAUSED ...\n");
1509 gst_element_set_state (pipeline, GST_STATE_PAUSED);
1517 do_download_buffering (gint percent)
1519 if (!buffering && percent < 100) {
1524 bufstr = g_strdup_printf ("Downloading...");
1525 gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, bufstr);
1528 /* once we get a buffering message, we'll do the fill update */
1529 set_update_fill (TRUE);
1531 if (state == GST_STATE_PLAYING && !is_live) {
1532 fprintf (stderr, "Downloading, setting pipeline to PAUSED ...\n");
1533 gst_element_set_state (pipeline, GST_STATE_PAUSED);
1534 /* user has to manually start the playback */
1535 state = GST_STATE_PAUSED;
1541 msg_buffering (GstBus * bus, GstMessage * message, GstPipeline * data)
1545 gst_message_parse_buffering (message, &percent);
1547 /* get more stats */
1548 gst_message_parse_buffering_stats (message, &mode, NULL, NULL,
1552 case GST_BUFFERING_DOWNLOAD:
1553 do_download_buffering (percent);
1555 case GST_BUFFERING_LIVE:
1557 case GST_BUFFERING_TIMESHIFT:
1558 case GST_BUFFERING_STREAM:
1559 do_stream_buffering (percent);
1565 msg_clock_lost (GstBus * bus, GstMessage * message, GstPipeline * data)
1567 g_print ("clock lost! PAUSE and PLAY to select a new clock\n");
1568 if (state == GST_STATE_PLAYING) {
1569 gst_element_set_state (pipeline, GST_STATE_PAUSED);
1570 gst_element_set_state (pipeline, GST_STATE_PLAYING);
1574 #if defined (GDK_WINDOWING_X11) || defined (GDK_WINDOWING_WIN32)
1576 static gulong embed_xid = 0;
1578 /* We set the xid here in response to the prepare-window-handle message via a
1579 * bus sync handler because we don't know the actual videosink used from the
1580 * start (as we don't know the pipeline, or bin elements such as autovideosink
1581 * or gconfvideosink may be used which create the actual videosink only once
1582 * the pipeline is started) */
1583 static GstBusSyncReply
1584 bus_sync_handler (GstBus * bus, GstMessage * message, GstPipeline * data)
1586 GstElement *element;
1588 if (!gst_is_video_overlay_prepare_window_handle_message (message))
1589 return GST_BUS_PASS;
1591 element = GST_ELEMENT (GST_MESSAGE_SRC (message));
1593 g_print ("got prepare-window-handle, setting XID %lu\n", embed_xid);
1595 if (g_object_class_find_property (G_OBJECT_GET_CLASS (element),
1596 "force-aspect-ratio")) {
1597 g_object_set (element, "force-aspect-ratio", TRUE, NULL);
1600 /* Should have been initialised from main thread before (can't use
1601 * GDK_WINDOW_XID here with Gtk+ >= 2.18, because the sync handler will
1602 * be called from a streaming thread and GDK_WINDOW_XID maps to more than
1603 * a simple structure lookup with Gtk+ >= 2.18, where 'more' is stuff that
1604 * shouldn't be done from a non-GUI thread without explicit locking). */
1605 g_assert (embed_xid != 0);
1607 gst_video_overlay_set_window_handle (GST_VIDEO_OVERLAY (element), embed_xid);
1608 return GST_BUS_PASS;
1613 handle_expose_cb (GtkWidget * widget, GdkEventExpose * event, gpointer data)
1615 if (state < GST_STATE_PAUSED) {
1616 GtkAllocation allocation;
1617 GdkWindow *window = gtk_widget_get_window (widget);
1620 gtk_widget_get_allocation (widget, &allocation);
1621 cr = gdk_cairo_create (window);
1622 cairo_set_source_rgb (cr, 0, 0, 0);
1623 cairo_rectangle (cr, 0, 0, allocation.width, allocation.height);
1631 realize_cb (GtkWidget * widget, gpointer data)
1633 #if GTK_CHECK_VERSION(2,18,0)
1635 GdkWindow *window = gtk_widget_get_window (widget);
1637 /* This is here just for pedagogical purposes, GDK_WINDOW_XID will call it
1639 if (!gdk_window_ensure_native (window))
1640 g_error ("Couldn't create native window needed for GstVideoOverlay!");
1644 #if defined (GDK_WINDOWING_X11) || defined (GDK_WINDOWING_WIN32)
1646 GdkWindow *window = gtk_widget_get_window (video_window);
1648 #if defined (GDK_WINDOWING_WIN32)
1649 embed_xid = GDK_WINDOW_HWND (window);
1651 embed_xid = GDK_WINDOW_XID (window);
1653 g_print ("Window realize: video window XID = %lu\n", embed_xid);
1659 msg_eos (GstBus * bus, GstMessage * message, GstPipeline * data)
1661 message_received (bus, message, data);
1663 /* Set new uri for playerbins and continue playback */
1664 if (l && (pipeline_type == 14 || pipeline_type == 16)) {
1665 stop_cb (NULL, NULL);
1666 l = g_list_next (l);
1668 playerbin_set_uri (GST_ELEMENT (data), l->data);
1669 play_cb (NULL, NULL);
1675 msg_step_done (GstBus * bus, GstMessage * message, GstPipeline * data)
1678 message_received (bus, message, data);
1682 connect_bus_signals (GstElement * pipeline)
1684 GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
1686 #if defined (GDK_WINDOWING_X11) || defined (GDK_WINDOWING_WIN32)
1687 /* handle prepare-window-handle element message synchronously */
1688 gst_bus_set_sync_handler (bus, (GstBusSyncHandler) bus_sync_handler,
1692 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
1693 gst_bus_enable_sync_message_emission (bus);
1695 g_signal_connect (bus, "message::state-changed",
1696 (GCallback) msg_state_changed, pipeline);
1697 g_signal_connect (bus, "message::segment-done", (GCallback) msg_segment_done,
1699 g_signal_connect (bus, "message::async-done", (GCallback) msg_async_done,
1702 g_signal_connect (bus, "message::new-clock", (GCallback) message_received,
1704 g_signal_connect (bus, "message::clock-lost", (GCallback) msg_clock_lost,
1706 g_signal_connect (bus, "message::error", (GCallback) message_received,
1708 g_signal_connect (bus, "message::warning", (GCallback) message_received,
1710 g_signal_connect (bus, "message::eos", (GCallback) msg_eos, pipeline);
1711 g_signal_connect (bus, "message::tag", (GCallback) message_received,
1713 g_signal_connect (bus, "message::element", (GCallback) message_received,
1715 g_signal_connect (bus, "message::segment-done", (GCallback) message_received,
1717 g_signal_connect (bus, "message::buffering", (GCallback) msg_buffering,
1719 // g_signal_connect (bus, "message::step-done", (GCallback) msg_step_done,
1721 g_signal_connect (bus, "message::step-start", (GCallback) msg_step_done,
1723 g_signal_connect (bus, "sync-message::step-done",
1724 (GCallback) msg_sync_step_done, pipeline);
1726 gst_object_unref (bus);
1729 /* Return GList of paths described in location string */
1731 handle_wildcards (const gchar * location)
1734 gchar *path = g_path_get_dirname (location);
1735 gchar *pattern = g_path_get_basename (location);
1736 GPatternSpec *pspec = g_pattern_spec_new (pattern);
1737 GDir *dir = g_dir_open (path, 0, NULL);
1740 g_print ("matching %s from %s\n", pattern, path);
1743 g_print ("opening directory %s failed\n", path);
1747 while ((name = g_dir_read_name (dir)) != NULL) {
1748 if (g_pattern_match_string (pspec, name)) {
1749 res = g_list_append (res, g_strjoin ("/", path, name, NULL));
1750 g_print (" found clip %s\n", name);
1756 g_pattern_spec_free (pspec);
1764 delete_event_cb (void)
1766 stop_cb (NULL, NULL);
1771 print_usage (int argc, char **argv)
1775 g_print ("usage: %s <type> <filename>\n", argv[0]);
1776 g_print (" possible types:\n");
1778 for (i = 0; i < NUM_TYPES; i++) {
1779 g_print (" %d = %s\n", i, pipelines[i].name);
1784 main (int argc, char **argv)
1786 GtkWidget *window, *hbox, *vbox, *panel, *expander, *pb2vbox, *boxes,
1787 *flagtable, *boxes2, *step;
1788 GtkWidget *play_button, *pause_button, *stop_button, *shot_button;
1789 GtkWidget *accurate_checkbox, *key_checkbox, *loop_checkbox, *flush_checkbox;
1790 GtkWidget *scrub_checkbox, *play_scrub_checkbox;
1791 GtkWidget *rate_label, *volume_label;
1792 GOptionEntry options[] = {
1793 {"audiosink", '\0', 0, G_OPTION_ARG_STRING, &opt_audiosink_str,
1794 "audio sink to use (default: " DEFAULT_AUDIOSINK ")", NULL},
1795 {"stats", 's', 0, G_OPTION_ARG_NONE, &stats,
1796 "Show pad stats", NULL},
1797 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
1798 "Verbose properties", NULL},
1799 {"videosink", '\0', 0, G_OPTION_ARG_STRING, &opt_videosink_str,
1800 "video sink to use (default: " DEFAULT_VIDEOSINK ")", NULL},
1803 GOptionContext *ctx;
1806 if (!g_thread_supported ())
1807 g_thread_init (NULL);
1809 ctx = g_option_context_new ("- test seeking in gsteamer");
1810 g_option_context_add_main_entries (ctx, options, NULL);
1811 g_option_context_add_group (ctx, gst_init_get_option_group ());
1812 g_option_context_add_group (ctx, gtk_get_option_group (TRUE));
1814 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
1815 g_print ("Error initializing: %s\n", err->message);
1819 if (opt_audiosink_str == NULL)
1820 opt_audiosink_str = g_strdup (DEFAULT_AUDIOSINK);
1822 if (opt_videosink_str == NULL)
1823 opt_videosink_str = g_strdup (DEFAULT_VIDEOSINK);
1825 GST_DEBUG_CATEGORY_INIT (seek_debug, "seek", 0, "seek example");
1828 print_usage (argc, argv);
1832 pipeline_type = atoi (argv[1]);
1834 if (pipeline_type < 0 || pipeline_type >= NUM_TYPES) {
1835 print_usage (argc, argv);
1839 pipeline_spec = argv[2];
1841 if (g_path_is_absolute (pipeline_spec) &&
1842 (g_strrstr (pipeline_spec, "*") != NULL ||
1843 g_strrstr (pipeline_spec, "?") != NULL)) {
1844 paths = handle_wildcards (pipeline_spec);
1846 paths = g_list_prepend (paths, g_strdup (pipeline_spec));
1850 g_print ("opening %s failed\n", pipeline_spec);
1856 pipeline = pipelines[pipeline_type].func ((gchar *) l->data);
1857 g_assert (pipeline);
1859 /* initialize gui elements ... */
1860 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1861 video_window = gtk_drawing_area_new ();
1862 g_signal_connect (video_window, "expose-event",
1863 G_CALLBACK (handle_expose_cb), NULL);
1864 g_signal_connect (video_window, "realize", G_CALLBACK (realize_cb), NULL);
1865 gtk_widget_set_double_buffered (video_window, FALSE);
1867 statusbar = gtk_statusbar_new ();
1868 status_id = gtk_statusbar_get_context_id (GTK_STATUSBAR (statusbar), "seek");
1869 gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Stopped");
1870 hbox = gtk_hbox_new (FALSE, 0);
1871 vbox = gtk_vbox_new (FALSE, 0);
1872 flagtable = gtk_table_new (4, 2, FALSE);
1873 gtk_container_set_border_width (GTK_CONTAINER (vbox), 3);
1875 /* media controls */
1876 play_button = gtk_button_new_from_stock (GTK_STOCK_MEDIA_PLAY);
1877 pause_button = gtk_button_new_from_stock (GTK_STOCK_MEDIA_PAUSE);
1878 stop_button = gtk_button_new_from_stock (GTK_STOCK_MEDIA_STOP);
1881 accurate_checkbox = gtk_check_button_new_with_label ("Accurate Seek");
1882 key_checkbox = gtk_check_button_new_with_label ("Key-unit Seek");
1883 loop_checkbox = gtk_check_button_new_with_label ("Loop");
1884 flush_checkbox = gtk_check_button_new_with_label ("Flush");
1885 scrub_checkbox = gtk_check_button_new_with_label ("Scrub");
1886 play_scrub_checkbox = gtk_check_button_new_with_label ("Play Scrub");
1887 skip_checkbox = gtk_check_button_new_with_label ("Play Skip");
1888 rate_spinbutton = gtk_spin_button_new_with_range (-100, 100, 0.1);
1889 gtk_spin_button_set_digits (GTK_SPIN_BUTTON (rate_spinbutton), 3);
1890 rate_label = gtk_label_new ("Rate");
1892 gtk_widget_set_tooltip_text (accurate_checkbox,
1893 "accurate position is requested, this might be considerably slower for some formats");
1894 gtk_widget_set_tooltip_text (key_checkbox,
1895 "seek to the nearest keyframe. This might be faster but less accurate");
1896 gtk_widget_set_tooltip_text (loop_checkbox, "loop playback");
1897 gtk_widget_set_tooltip_text (flush_checkbox, "flush pipeline after seeking");
1898 gtk_widget_set_tooltip_text (rate_spinbutton, "define the playback rate, "
1899 "negative value trigger reverse playback");
1900 gtk_widget_set_tooltip_text (scrub_checkbox, "show images while seeking");
1901 gtk_widget_set_tooltip_text (play_scrub_checkbox, "play video while seeking");
1902 gtk_widget_set_tooltip_text (skip_checkbox,
1903 "Skip frames while playing at high frame rates");
1905 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (flush_checkbox), TRUE);
1906 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (scrub_checkbox), TRUE);
1908 gtk_spin_button_set_value (GTK_SPIN_BUTTON (rate_spinbutton), rate);
1914 step = gtk_expander_new ("step options");
1915 hbox = gtk_hbox_new (FALSE, 0);
1917 format_combo = gtk_combo_box_text_new ();
1918 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (format_combo),
1920 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (format_combo),
1922 gtk_combo_box_set_active (GTK_COMBO_BOX (format_combo), 0);
1923 gtk_box_pack_start (GTK_BOX (hbox), format_combo, FALSE, FALSE, 2);
1925 step_amount_spinbutton = gtk_spin_button_new_with_range (1, 1000, 1);
1926 gtk_spin_button_set_digits (GTK_SPIN_BUTTON (step_amount_spinbutton), 0);
1927 gtk_spin_button_set_value (GTK_SPIN_BUTTON (step_amount_spinbutton), 1.0);
1928 gtk_box_pack_start (GTK_BOX (hbox), step_amount_spinbutton, FALSE, FALSE,
1931 step_rate_spinbutton = gtk_spin_button_new_with_range (0.0, 100, 0.1);
1932 gtk_spin_button_set_digits (GTK_SPIN_BUTTON (step_rate_spinbutton), 3);
1933 gtk_spin_button_set_value (GTK_SPIN_BUTTON (step_rate_spinbutton), 1.0);
1934 gtk_box_pack_start (GTK_BOX (hbox), step_rate_spinbutton, FALSE, FALSE, 2);
1936 step_button = gtk_button_new_from_stock (GTK_STOCK_MEDIA_FORWARD);
1937 gtk_button_set_label (GTK_BUTTON (step_button), "Step");
1938 gtk_box_pack_start (GTK_BOX (hbox), step_button, FALSE, FALSE, 2);
1940 g_signal_connect (G_OBJECT (step_button), "clicked", G_CALLBACK (step_cb),
1944 shuttle_checkbox = gtk_check_button_new_with_label ("Shuttle");
1945 gtk_box_pack_start (GTK_BOX (hbox), shuttle_checkbox, FALSE, FALSE, 2);
1946 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (shuttle_checkbox), FALSE);
1947 g_signal_connect (shuttle_checkbox, "toggled", G_CALLBACK (shuttle_toggled),
1950 shuttle_adjustment =
1951 GTK_ADJUSTMENT (gtk_adjustment_new (0.0, -3.00, 4.0, 0.1, 1.0, 1.0));
1952 shuttle_hscale = gtk_hscale_new (shuttle_adjustment);
1953 gtk_scale_set_digits (GTK_SCALE (shuttle_hscale), 2);
1954 gtk_scale_set_value_pos (GTK_SCALE (shuttle_hscale), GTK_POS_TOP);
1955 g_signal_connect (shuttle_hscale, "value_changed",
1956 G_CALLBACK (shuttle_value_changed), pipeline);
1957 g_signal_connect (shuttle_hscale, "format_value",
1958 G_CALLBACK (shuttle_format_value), pipeline);
1960 gtk_box_pack_start (GTK_BOX (hbox), shuttle_hscale, TRUE, TRUE, 2);
1962 gtk_container_add (GTK_CONTAINER (step), hbox);
1967 GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.00, N_GRAD, 0.1, 1.0, 1.0));
1968 hscale = gtk_hscale_new (adjustment);
1969 gtk_scale_set_digits (GTK_SCALE (hscale), 2);
1970 gtk_scale_set_value_pos (GTK_SCALE (hscale), GTK_POS_RIGHT);
1971 gtk_range_set_show_fill_level (GTK_RANGE (hscale), TRUE);
1972 gtk_range_set_fill_level (GTK_RANGE (hscale), N_GRAD);
1974 g_signal_connect (hscale, "button_press_event", G_CALLBACK (start_seek),
1976 g_signal_connect (hscale, "button_release_event", G_CALLBACK (stop_seek),
1978 g_signal_connect (hscale, "format_value", G_CALLBACK (format_value),
1981 if (pipeline_type == 0) {
1982 /* the playbin2 panel controls for the video/audio/subtitle tracks */
1983 panel = gtk_hbox_new (FALSE, 0);
1984 video_combo = gtk_combo_box_text_new ();
1985 audio_combo = gtk_combo_box_text_new ();
1986 text_combo = gtk_combo_box_text_new ();
1987 gtk_widget_set_sensitive (video_combo, FALSE);
1988 gtk_widget_set_sensitive (audio_combo, FALSE);
1989 gtk_widget_set_sensitive (text_combo, FALSE);
1990 gtk_box_pack_start (GTK_BOX (panel), video_combo, TRUE, TRUE, 2);
1991 gtk_box_pack_start (GTK_BOX (panel), audio_combo, TRUE, TRUE, 2);
1992 gtk_box_pack_start (GTK_BOX (panel), text_combo, TRUE, TRUE, 2);
1993 g_signal_connect (G_OBJECT (video_combo), "changed",
1994 G_CALLBACK (video_combo_cb), pipeline);
1995 g_signal_connect (G_OBJECT (audio_combo), "changed",
1996 G_CALLBACK (audio_combo_cb), pipeline);
1997 g_signal_connect (G_OBJECT (text_combo), "changed",
1998 G_CALLBACK (text_combo_cb), pipeline);
1999 /* playbin2 panel for flag checkboxes and volume/mute */
2000 boxes = gtk_hbox_new (FALSE, 0);
2001 vis_checkbox = gtk_check_button_new_with_label ("Vis");
2002 video_checkbox = gtk_check_button_new_with_label ("Video");
2003 audio_checkbox = gtk_check_button_new_with_label ("Audio");
2004 text_checkbox = gtk_check_button_new_with_label ("Text");
2005 mute_checkbox = gtk_check_button_new_with_label ("Mute");
2006 download_checkbox = gtk_check_button_new_with_label ("Download");
2007 buffer_checkbox = gtk_check_button_new_with_label ("Buffer");
2008 volume_label = gtk_label_new ("Volume");
2009 volume_spinbutton = gtk_spin_button_new_with_range (0, 10.0, 0.1);
2010 gtk_spin_button_set_value (GTK_SPIN_BUTTON (volume_spinbutton), 1.0);
2011 gtk_box_pack_start (GTK_BOX (boxes), video_checkbox, TRUE, TRUE, 2);
2012 gtk_box_pack_start (GTK_BOX (boxes), audio_checkbox, TRUE, TRUE, 2);
2013 gtk_box_pack_start (GTK_BOX (boxes), text_checkbox, TRUE, TRUE, 2);
2014 gtk_box_pack_start (GTK_BOX (boxes), vis_checkbox, TRUE, TRUE, 2);
2015 gtk_box_pack_start (GTK_BOX (boxes), mute_checkbox, TRUE, TRUE, 2);
2016 gtk_box_pack_start (GTK_BOX (boxes), download_checkbox, TRUE, TRUE, 2);
2017 gtk_box_pack_start (GTK_BOX (boxes), buffer_checkbox, TRUE, TRUE, 2);
2018 gtk_box_pack_start (GTK_BOX (boxes), volume_label, TRUE, TRUE, 2);
2019 gtk_box_pack_start (GTK_BOX (boxes), volume_spinbutton, TRUE, TRUE, 2);
2020 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (vis_checkbox), FALSE);
2021 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (audio_checkbox), TRUE);
2022 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (video_checkbox), TRUE);
2023 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (text_checkbox), TRUE);
2024 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (mute_checkbox), FALSE);
2025 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (download_checkbox), FALSE);
2026 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (buffer_checkbox), FALSE);
2027 g_signal_connect (G_OBJECT (vis_checkbox), "toggled",
2028 G_CALLBACK (vis_toggle_cb), pipeline);
2029 g_signal_connect (G_OBJECT (audio_checkbox), "toggled",
2030 G_CALLBACK (audio_toggle_cb), pipeline);
2031 g_signal_connect (G_OBJECT (video_checkbox), "toggled",
2032 G_CALLBACK (video_toggle_cb), pipeline);
2033 g_signal_connect (G_OBJECT (text_checkbox), "toggled",
2034 G_CALLBACK (text_toggle_cb), pipeline);
2035 g_signal_connect (G_OBJECT (mute_checkbox), "toggled",
2036 G_CALLBACK (mute_toggle_cb), pipeline);
2037 g_signal_connect (G_OBJECT (download_checkbox), "toggled",
2038 G_CALLBACK (download_toggle_cb), pipeline);
2039 g_signal_connect (G_OBJECT (buffer_checkbox), "toggled",
2040 G_CALLBACK (buffer_toggle_cb), pipeline);
2041 g_signal_connect (G_OBJECT (volume_spinbutton), "value_changed",
2042 G_CALLBACK (volume_spinbutton_changed_cb), pipeline);
2043 /* playbin2 panel for snapshot */
2044 boxes2 = gtk_hbox_new (FALSE, 0);
2045 shot_button = gtk_button_new_from_stock (GTK_STOCK_SAVE);
2046 gtk_widget_set_tooltip_text (shot_button,
2047 "save a screenshot .png in the current directory");
2048 g_signal_connect (G_OBJECT (shot_button), "clicked", G_CALLBACK (shot_cb),
2050 vis_combo = gtk_combo_box_text_new ();
2051 g_signal_connect (G_OBJECT (vis_combo), "changed",
2052 G_CALLBACK (vis_combo_cb), pipeline);
2053 gtk_widget_set_sensitive (vis_combo, FALSE);
2054 gtk_box_pack_start (GTK_BOX (boxes2), shot_button, TRUE, TRUE, 2);
2055 gtk_box_pack_start (GTK_BOX (boxes2), vis_combo, TRUE, TRUE, 2);
2057 /* fill the vis combo box and the array of factories */
2058 init_visualization_features ();
2060 panel = boxes = boxes2 = NULL;
2063 /* do the packing stuff ... */
2064 gtk_window_set_default_size (GTK_WINDOW (window), 250, 96);
2065 /* FIXME: can we avoid this for audio only? */
2066 gtk_widget_set_size_request (GTK_WIDGET (video_window), -1,
2067 DEFAULT_VIDEO_HEIGHT);
2068 gtk_container_add (GTK_CONTAINER (window), vbox);
2069 gtk_box_pack_start (GTK_BOX (vbox), video_window, TRUE, TRUE, 2);
2070 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 2);
2071 gtk_box_pack_start (GTK_BOX (hbox), play_button, FALSE, FALSE, 2);
2072 gtk_box_pack_start (GTK_BOX (hbox), pause_button, FALSE, FALSE, 2);
2073 gtk_box_pack_start (GTK_BOX (hbox), stop_button, FALSE, FALSE, 2);
2074 gtk_box_pack_start (GTK_BOX (hbox), flagtable, FALSE, FALSE, 2);
2075 gtk_table_attach_defaults (GTK_TABLE (flagtable), accurate_checkbox, 0, 1, 0,
2077 gtk_table_attach_defaults (GTK_TABLE (flagtable), flush_checkbox, 1, 2, 0, 1);
2078 gtk_table_attach_defaults (GTK_TABLE (flagtable), loop_checkbox, 2, 3, 0, 1);
2079 gtk_table_attach_defaults (GTK_TABLE (flagtable), key_checkbox, 0, 1, 1, 2);
2080 gtk_table_attach_defaults (GTK_TABLE (flagtable), scrub_checkbox, 1, 2, 1, 2);
2081 gtk_table_attach_defaults (GTK_TABLE (flagtable), play_scrub_checkbox, 2, 3,
2083 gtk_table_attach_defaults (GTK_TABLE (flagtable), skip_checkbox, 3, 4, 0, 1);
2084 gtk_table_attach_defaults (GTK_TABLE (flagtable), rate_label, 4, 5, 0, 1);
2085 gtk_table_attach_defaults (GTK_TABLE (flagtable), rate_spinbutton, 4, 5, 1,
2087 if (panel && boxes && boxes2) {
2088 expander = gtk_expander_new ("playbin2 options");
2089 pb2vbox = gtk_vbox_new (FALSE, 0);
2090 gtk_box_pack_start (GTK_BOX (pb2vbox), panel, FALSE, FALSE, 2);
2091 gtk_box_pack_start (GTK_BOX (pb2vbox), boxes, FALSE, FALSE, 2);
2092 gtk_box_pack_start (GTK_BOX (pb2vbox), boxes2, FALSE, FALSE, 2);
2093 gtk_container_add (GTK_CONTAINER (expander), pb2vbox);
2094 gtk_box_pack_start (GTK_BOX (vbox), expander, FALSE, FALSE, 2);
2096 gtk_box_pack_start (GTK_BOX (vbox), step, FALSE, FALSE, 2);
2097 gtk_box_pack_start (GTK_BOX (vbox), hscale, FALSE, FALSE, 2);
2098 gtk_box_pack_start (GTK_BOX (vbox), statusbar, FALSE, FALSE, 2);
2100 /* connect things ... */
2101 g_signal_connect (G_OBJECT (play_button), "clicked", G_CALLBACK (play_cb),
2103 g_signal_connect (G_OBJECT (pause_button), "clicked", G_CALLBACK (pause_cb),
2105 g_signal_connect (G_OBJECT (stop_button), "clicked", G_CALLBACK (stop_cb),
2107 g_signal_connect (G_OBJECT (accurate_checkbox), "toggled",
2108 G_CALLBACK (accurate_toggle_cb), pipeline);
2109 g_signal_connect (G_OBJECT (key_checkbox), "toggled",
2110 G_CALLBACK (key_toggle_cb), pipeline);
2111 g_signal_connect (G_OBJECT (loop_checkbox), "toggled",
2112 G_CALLBACK (loop_toggle_cb), pipeline);
2113 g_signal_connect (G_OBJECT (flush_checkbox), "toggled",
2114 G_CALLBACK (flush_toggle_cb), pipeline);
2115 g_signal_connect (G_OBJECT (scrub_checkbox), "toggled",
2116 G_CALLBACK (scrub_toggle_cb), pipeline);
2117 g_signal_connect (G_OBJECT (play_scrub_checkbox), "toggled",
2118 G_CALLBACK (play_scrub_toggle_cb), pipeline);
2119 g_signal_connect (G_OBJECT (skip_checkbox), "toggled",
2120 G_CALLBACK (skip_toggle_cb), pipeline);
2121 g_signal_connect (G_OBJECT (rate_spinbutton), "value_changed",
2122 G_CALLBACK (rate_spinbutton_changed_cb), pipeline);
2124 g_signal_connect (G_OBJECT (window), "delete-event", delete_event_cb, NULL);
2127 gtk_widget_show_all (window);
2129 /* realize window now so that the video window gets created and we can
2130 * obtain its XID before the pipeline is started up and the videosink
2131 * asks for the XID of the window to render onto */
2132 gtk_widget_realize (window);
2134 #if defined (GDK_WINDOWING_X11) || defined (GDK_WINDOWING_WIN32)
2135 /* we should have the XID now */
2136 g_assert (embed_xid != 0);
2140 g_signal_connect (pipeline, "deep_notify",
2141 G_CALLBACK (gst_object_default_deep_notify), NULL);
2144 connect_bus_signals (pipeline);
2147 g_print ("NULL pipeline\n");
2148 gst_element_set_state (pipeline, GST_STATE_NULL);
2150 g_print ("free pipeline\n");
2151 gst_object_unref (pipeline);
2153 g_list_foreach (paths, (GFunc) g_free, NULL);
2154 g_list_free (paths);