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);
164 #ifndef GST_DISABLE_PARSE
166 /* Try parsing it as a pipeline description */
167 element = gst_parse_bin_from_description (type, TRUE, NULL);
169 gst_element_set_name (element, name);
175 g_warning ("Failed to create element %s of type %s", name, type);
182 playerbin_set_uri (GstElement * player, const gchar * location)
186 /* Add "file://" prefix for convenience */
187 if (g_str_has_prefix (location, "/") || !gst_uri_is_valid (location)) {
188 uri = gst_filename_to_uri (location, NULL);
189 g_print ("Setting URI: %s\n", uri);
190 g_object_set (G_OBJECT (player), "uri", uri, NULL);
193 g_print ("Setting URI: %s\n", location);
194 g_object_set (G_OBJECT (player), "uri", location, NULL);
199 construct_playbin (const gchar * name, const gchar * location)
204 player = gst_element_factory_make (name, "player");
207 playerbin_set_uri (player, location);
209 seekable_elements = g_list_prepend (seekable_elements, player);
211 avsink = gst_element_factory_make_or_warn (opt_audiosink_str, "a_sink");
213 g_object_set (player, "audio-sink", avsink, NULL);
215 avsink = gst_element_factory_make_or_warn (opt_videosink_str, "v_sink");
217 g_object_set (player, "video-sink", avsink, NULL);
223 make_playbin_pipeline (const gchar * location)
225 GstElement *pipeline = construct_playbin ("playbin", location);
227 /* FIXME: this is not triggered, playbin is not forwarding it from the sink */
228 g_signal_connect (pipeline, "notify::volume", G_CALLBACK (volume_notify_cb),
233 #ifndef GST_DISABLE_PARSE
235 make_parselaunch_pipeline (const gchar * description)
237 GstElement *pipeline;
238 GError *error = NULL;
240 pipeline = gst_parse_launch (description, &error);
242 seekable_elements = g_list_prepend (seekable_elements, pipeline);
251 GstElement *(*func) (const gchar * location);
255 static Pipeline pipelines[] = {
256 {"playbin", make_playbin_pipeline},
257 #ifndef GST_DISABLE_PARSE
258 {"parse-launch", make_parselaunch_pipeline},
263 #define NUM_TYPES ((sizeof (pipelines) / sizeof (Pipeline)) - 1)
265 /* ui callbacks and helpers */
268 format_value (GtkScale * scale, gdouble value)
274 real = value * duration / N_GRAD;
275 seconds = (gint64) real / GST_SECOND;
276 subseconds = (gint64) real / (GST_SECOND / N_GRAD);
278 return g_strdup_printf ("%02" G_GINT64_FORMAT ":%02" G_GINT64_FORMAT ":%02"
279 G_GINT64_FORMAT, seconds / 60, seconds % 60, subseconds % 100);
284 shuttle_format_value (GtkScale * scale, gdouble value)
286 return g_strdup_printf ("%0.*g", gtk_scale_get_digits (scale), value);
292 const GstFormat format;
296 static seek_format seek_formats[] = {
297 {"tim", GST_FORMAT_TIME},
298 {"byt", GST_FORMAT_BYTES},
299 {"buf", GST_FORMAT_BUFFERS},
300 {"def", GST_FORMAT_DEFAULT},
304 G_GNUC_UNUSED static void
305 query_positions_elems (void)
307 GList *walk = seekable_elements;
310 GstElement *element = GST_ELEMENT (walk->data);
313 g_print ("positions %8.8s: ", GST_ELEMENT_NAME (element));
314 while (seek_formats[i].name) {
315 gint64 position, total;
318 format = seek_formats[i].format;
320 if (gst_element_query_position (element, format, &position) &&
321 gst_element_query_duration (element, format, &total)) {
322 g_print ("%s %13" G_GINT64_FORMAT " / %13" G_GINT64_FORMAT " | ",
323 seek_formats[i].name, position, total);
325 g_print ("%s %13.13s / %13.13s | ", seek_formats[i].name, "*NA*",
330 g_print (" %s\n", GST_ELEMENT_NAME (element));
332 walk = g_list_next (walk);
336 static gboolean start_seek (GtkWidget * widget, GdkEventButton * event,
338 static gboolean stop_seek (GtkWidget * widget, GdkEventButton * event,
340 static void seek_cb (GtkWidget * widget);
343 set_scale (gdouble value)
345 g_signal_handlers_block_by_func (hscale, (void *) start_seek,
347 g_signal_handlers_block_by_func (hscale, (void *) stop_seek,
349 g_signal_handlers_block_by_func (hscale, (void *) seek_cb, (void *) pipeline);
350 gtk_adjustment_set_value (adjustment, value);
351 g_signal_handlers_unblock_by_func (hscale, (void *) start_seek,
353 g_signal_handlers_unblock_by_func (hscale, (void *) stop_seek,
355 g_signal_handlers_unblock_by_func (hscale, (void *) seek_cb,
357 gtk_widget_queue_draw (hscale);
361 update_fill (gpointer data)
363 if (seekable_elements) {
364 GstElement *element = GST_ELEMENT (seekable_elements->data);
367 query = gst_query_new_buffering (GST_FORMAT_PERCENT);
368 if (gst_element_query (element, query)) {
369 gint64 start, stop, buffering_total;
374 GstBufferingMode mode;
375 gint avg_in, avg_out;
376 gint64 buffering_left;
378 gst_query_parse_buffering_percent (query, &busy, &percent);
379 gst_query_parse_buffering_range (query, &format, &start, &stop,
381 gst_query_parse_buffering_stats (query, &mode, &avg_in, &avg_out,
384 /* note that we could start the playback when buffering_left < remaining
386 GST_DEBUG ("buffering total %" G_GINT64_FORMAT " ms, left %"
387 G_GINT64_FORMAT " ms", buffering_total, buffering_left);
388 GST_DEBUG ("start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT,
392 fill = N_GRAD * stop / GST_FORMAT_PERCENT_MAX;
396 gtk_range_set_fill_level (GTK_RANGE (hscale), fill);
398 gst_query_unref (query);
404 update_scale (gpointer data)
406 if (seekable_elements) {
407 GstElement *element = GST_ELEMENT (seekable_elements->data);
409 gst_element_query_position (element, GST_FORMAT_TIME, &position);
410 gst_element_query_duration (element, GST_FORMAT_TIME, &duration);
414 query_positions_elems ();
417 if (position >= duration)
421 set_scale (position * N_GRAD / duration);
424 /* FIXME: see make_playerbin2_pipeline() and volume_notify_cb() */
425 if (pipeline_type == 16) {
426 g_object_notify (G_OBJECT (pipeline), "volume");
432 static void do_seek (GtkWidget * widget);
433 static void connect_bus_signals (GstElement * pipeline);
434 static void set_update_scale (gboolean active);
435 static void set_update_fill (gboolean active);
438 end_scrub (GtkWidget * widget)
440 GST_DEBUG ("end scrub, PAUSE");
441 gst_element_set_state (pipeline, GST_STATE_PAUSED);
448 send_event (GstEvent * event)
450 gboolean res = FALSE;
451 GList *walk = seekable_elements;
454 GstElement *seekable = GST_ELEMENT (walk->data);
456 GST_DEBUG ("send event on element %s", GST_ELEMENT_NAME (seekable));
458 gst_event_ref (event);
459 res = gst_element_send_event (seekable, event);
461 walk = g_list_next (walk);
463 gst_event_unref (event);
468 do_seek (GtkWidget * widget)
471 gboolean res = FALSE;
475 real = gtk_range_get_value (GTK_RANGE (widget)) * duration / N_GRAD;
477 GST_DEBUG ("value=%f, real=%" G_GINT64_FORMAT,
478 gtk_range_get_value (GTK_RANGE (widget)), real);
482 flags |= GST_SEEK_FLAG_FLUSH;
484 flags |= GST_SEEK_FLAG_ACCURATE;
486 flags |= GST_SEEK_FLAG_KEY_UNIT;
488 flags |= GST_SEEK_FLAG_SEGMENT;
490 flags |= GST_SEEK_FLAG_SKIP;
493 s_event = gst_event_new_seek (rate,
494 GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, real, GST_SEEK_TYPE_SET,
495 GST_CLOCK_TIME_NONE);
496 GST_DEBUG ("seek with rate %lf to %" GST_TIME_FORMAT " / %" GST_TIME_FORMAT,
497 rate, GST_TIME_ARGS (real), GST_TIME_ARGS (duration));
499 s_event = gst_event_new_seek (rate,
500 GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, G_GINT64_CONSTANT (0),
501 GST_SEEK_TYPE_SET, real);
502 GST_DEBUG ("seek with rate %lf to %" GST_TIME_FORMAT " / %" GST_TIME_FORMAT,
503 rate, GST_TIME_ARGS (0), GST_TIME_ARGS (real));
506 res = send_event (s_event);
510 gst_element_get_state (GST_ELEMENT (pipeline), NULL, NULL, SEEK_TIMEOUT);
512 set_update_scale (TRUE);
515 g_print ("seek failed\n");
516 set_update_scale (TRUE);
521 seek_cb (GtkWidget * widget)
523 /* If the timer hasn't expired yet, then the pipeline is running */
524 if (play_scrub && seek_timeout_id != 0) {
525 GST_DEBUG ("do scrub seek, PAUSED");
526 gst_element_set_state (pipeline, GST_STATE_PAUSED);
529 GST_DEBUG ("do seek");
533 GST_DEBUG ("do scrub seek, PLAYING");
534 gst_element_set_state (pipeline, GST_STATE_PLAYING);
536 if (seek_timeout_id == 0) {
538 g_timeout_add (SCRUB_TIME, (GSourceFunc) end_scrub, widget);
544 set_update_fill (gboolean active)
546 GST_DEBUG ("fill scale is %d", active);
551 g_timeout_add (FILL_INTERVAL, (GSourceFunc) update_fill, pipeline);
555 g_source_remove (fill_id);
562 set_update_scale (gboolean active)
565 GST_DEBUG ("update scale is %d", active);
568 if (update_id == 0) {
570 g_timeout_add (UPDATE_INTERVAL, (GSourceFunc) update_scale, pipeline);
574 g_source_remove (update_id);
581 start_seek (GtkWidget * widget, GdkEventButton * event, gpointer user_data)
583 if (event->type != GDK_BUTTON_PRESS)
586 set_update_scale (FALSE);
588 if (state == GST_STATE_PLAYING && flush_seek && scrub) {
589 GST_DEBUG ("start scrub seek, PAUSE");
590 gst_element_set_state (pipeline, GST_STATE_PAUSED);
593 if (changed_id == 0 && flush_seek && scrub) {
595 g_signal_connect (hscale, "value_changed", G_CALLBACK (seek_cb),
603 stop_seek (GtkWidget * widget, GdkEventButton * event, gpointer user_data)
606 g_signal_handler_disconnect (hscale, changed_id);
610 if (!flush_seek || !scrub) {
611 GST_DEBUG ("do final seek");
615 if (seek_timeout_id != 0) {
616 g_source_remove (seek_timeout_id);
618 /* Still scrubbing, so the pipeline is playing, see if we need PAUSED
620 if (state == GST_STATE_PAUSED) {
621 GST_DEBUG ("stop scrub seek, PAUSED");
622 gst_element_set_state (pipeline, GST_STATE_PAUSED);
625 if (state == GST_STATE_PLAYING) {
626 GST_DEBUG ("stop scrub seek, PLAYING");
627 gst_element_set_state (pipeline, GST_STATE_PLAYING);
635 play_cb (GtkButton * button, gpointer data)
637 GstStateChangeReturn ret;
639 if (state != GST_STATE_PLAYING) {
640 g_print ("PLAY pipeline\n");
641 gtk_statusbar_pop (GTK_STATUSBAR (statusbar), status_id);
643 ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
645 case GST_STATE_CHANGE_FAILURE:
647 case GST_STATE_CHANGE_NO_PREROLL:
653 state = GST_STATE_PLAYING;
654 gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Playing");
661 g_print ("PLAY failed\n");
662 gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Play failed");
667 pause_cb (GtkButton * button, gpointer data)
669 g_static_mutex_lock (&state_mutex);
670 if (state != GST_STATE_PAUSED) {
671 GstStateChangeReturn ret;
673 gtk_statusbar_pop (GTK_STATUSBAR (statusbar), status_id);
674 g_print ("PAUSE pipeline\n");
675 ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
677 case GST_STATE_CHANGE_FAILURE:
679 case GST_STATE_CHANGE_NO_PREROLL:
686 state = GST_STATE_PAUSED;
687 gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Paused");
689 g_static_mutex_unlock (&state_mutex);
695 g_static_mutex_unlock (&state_mutex);
696 g_print ("PAUSE failed\n");
697 gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Pause failed");
702 stop_cb (GtkButton * button, gpointer data)
704 if (state != STOP_STATE) {
705 GstStateChangeReturn ret;
707 g_print ("READY pipeline\n");
708 gtk_statusbar_pop (GTK_STATUSBAR (statusbar), status_id);
710 g_static_mutex_lock (&state_mutex);
711 ret = gst_element_set_state (pipeline, STOP_STATE);
712 if (ret == GST_STATE_CHANGE_FAILURE)
716 gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Stopped");
720 set_update_scale (FALSE);
722 set_update_fill (FALSE);
724 if (pipeline_type == 16)
725 clear_streams (pipeline);
726 g_static_mutex_unlock (&state_mutex);
729 /* if one uses parse_launch, play, stop and play again it fails as all the
730 * pads after the demuxer can't be reconnected
732 if (!strcmp (pipelines[pipeline_type].name, "parse-launch")) {
733 gst_element_set_state (pipeline, GST_STATE_NULL);
734 gst_object_unref (pipeline);
736 g_list_free (seekable_elements);
737 seekable_elements = NULL;
739 pipeline = pipelines[pipeline_type].func (pipeline_spec);
741 gst_element_set_state (pipeline, STOP_STATE);
742 connect_bus_signals (pipeline);
750 g_static_mutex_unlock (&state_mutex);
751 g_print ("STOP failed\n");
752 gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Stop failed");
757 accurate_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
759 accurate_seek = gtk_toggle_button_get_active (button);
763 key_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
765 keyframe_seek = gtk_toggle_button_get_active (button);
769 loop_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
771 loop_seek = gtk_toggle_button_get_active (button);
772 if (state == GST_STATE_PLAYING) {
778 flush_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
780 flush_seek = gtk_toggle_button_get_active (button);
784 scrub_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
786 scrub = gtk_toggle_button_get_active (button);
790 play_scrub_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
792 play_scrub = gtk_toggle_button_get_active (button);
796 skip_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
798 skip_seek = gtk_toggle_button_get_active (button);
799 if (state == GST_STATE_PLAYING) {
805 rate_spinbutton_changed_cb (GtkSpinButton * button, GstPipeline * pipeline)
807 gboolean res = FALSE;
811 rate = gtk_spin_button_get_value (button);
813 GST_DEBUG ("rate changed to %lf", rate);
817 flags |= GST_SEEK_FLAG_FLUSH;
819 flags |= GST_SEEK_FLAG_SEGMENT;
821 flags |= GST_SEEK_FLAG_ACCURATE;
823 flags |= GST_SEEK_FLAG_KEY_UNIT;
825 flags |= GST_SEEK_FLAG_SKIP;
828 s_event = gst_event_new_seek (rate,
829 GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, position,
830 GST_SEEK_TYPE_SET, GST_CLOCK_TIME_NONE);
832 s_event = gst_event_new_seek (rate,
833 GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, G_GINT64_CONSTANT (0),
834 GST_SEEK_TYPE_SET, position);
837 res = send_event (s_event);
841 gst_element_get_state (GST_ELEMENT (pipeline), NULL, NULL, SEEK_TIMEOUT);
844 g_print ("seek failed\n");
848 update_flag (GstPipeline * pipeline, gint num, gboolean state)
852 g_object_get (pipeline, "flags", &flags, NULL);
856 flags &= ~(1 << num);
857 g_object_set (pipeline, "flags", flags, NULL);
861 vis_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
865 state = gtk_toggle_button_get_active (button);
866 update_flag (pipeline, 3, state);
867 gtk_widget_set_sensitive (vis_combo, state);
871 audio_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
875 state = gtk_toggle_button_get_active (button);
876 update_flag (pipeline, 1, state);
877 gtk_widget_set_sensitive (audio_combo, state);
881 video_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
885 state = gtk_toggle_button_get_active (button);
886 update_flag (pipeline, 0, state);
887 gtk_widget_set_sensitive (video_combo, state);
891 text_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
895 state = gtk_toggle_button_get_active (button);
896 update_flag (pipeline, 2, state);
897 gtk_widget_set_sensitive (text_combo, state);
901 mute_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
905 mute = gtk_toggle_button_get_active (button);
906 g_object_set (pipeline, "mute", mute, NULL);
910 download_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
914 state = gtk_toggle_button_get_active (button);
915 update_flag (pipeline, 7, state);
919 buffer_toggle_cb (GtkToggleButton * button, GstPipeline * pipeline)
923 state = gtk_toggle_button_get_active (button);
924 update_flag (pipeline, 8, state);
928 clear_streams (GstElement * pipeline)
932 /* remove previous info */
933 for (i = 0; i < n_video; i++)
934 gtk_combo_box_text_remove (GTK_COMBO_BOX_TEXT (video_combo), 0);
935 for (i = 0; i < n_audio; i++)
936 gtk_combo_box_text_remove (GTK_COMBO_BOX_TEXT (audio_combo), 0);
937 for (i = 0; i < n_text; i++)
938 gtk_combo_box_text_remove (GTK_COMBO_BOX_TEXT (text_combo), 0);
940 n_audio = n_video = n_text = 0;
941 gtk_widget_set_sensitive (video_combo, FALSE);
942 gtk_widget_set_sensitive (audio_combo, FALSE);
943 gtk_widget_set_sensitive (text_combo, FALSE);
949 update_streams (GstPipeline * pipeline)
953 if (pipeline_type == 16 && need_streams) {
959 /* remove previous info */
960 clear_streams (GST_ELEMENT_CAST (pipeline));
962 /* here we get and update the different streams detected by playbin2 */
963 g_object_get (pipeline, "n-video", &n_video, NULL);
964 g_object_get (pipeline, "n-audio", &n_audio, NULL);
965 g_object_get (pipeline, "n-text", &n_text, NULL);
967 g_print ("video %d, audio %d, text %d\n", n_video, n_audio, n_text);
970 for (i = 0; i < n_video; i++) {
971 g_signal_emit_by_name (pipeline, "get-video-tags", i, &tags);
973 str = gst_structure_to_string ((GstStructure *) tags);
974 g_print ("video %d: %s\n", i, str);
977 /* find good name for the label */
978 name = g_strdup_printf ("video %d", i + 1);
979 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (video_combo), name);
982 state = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (video_checkbox));
983 gtk_widget_set_sensitive (video_combo, state && n_video > 0);
984 gtk_combo_box_set_active (GTK_COMBO_BOX (video_combo), active_idx);
987 for (i = 0; i < n_audio; i++) {
988 g_signal_emit_by_name (pipeline, "get-audio-tags", i, &tags);
990 str = gst_structure_to_string ((GstStructure *) tags);
991 g_print ("audio %d: %s\n", i, str);
994 /* find good name for the label */
995 name = g_strdup_printf ("audio %d", i + 1);
996 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (audio_combo), name);
999 state = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (audio_checkbox));
1000 gtk_widget_set_sensitive (audio_combo, state && n_audio > 0);
1001 gtk_combo_box_set_active (GTK_COMBO_BOX (audio_combo), active_idx);
1004 for (i = 0; i < n_text; i++) {
1005 g_signal_emit_by_name (pipeline, "get-text-tags", i, &tags);
1009 const GValue *value;
1011 str = gst_structure_to_string ((GstStructure *) tags);
1012 g_print ("text %d: %s\n", i, str);
1015 /* get the language code if we can */
1016 value = gst_tag_list_get_value_index (tags, GST_TAG_LANGUAGE_CODE, 0);
1017 if (value && G_VALUE_HOLDS_STRING (value)) {
1018 name = g_strdup_printf ("text %s", g_value_get_string (value));
1021 /* find good name for the label if we didn't use a tag */
1023 name = g_strdup_printf ("text %d", i + 1);
1025 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (text_combo), name);
1028 state = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (text_checkbox));
1029 gtk_widget_set_sensitive (text_combo, state && n_text > 0);
1030 gtk_combo_box_set_active (GTK_COMBO_BOX (text_combo), active_idx);
1032 need_streams = FALSE;
1037 video_combo_cb (GtkComboBox * combo, GstPipeline * pipeline)
1041 active = gtk_combo_box_get_active (combo);
1043 g_print ("setting current video track %d\n", active);
1044 g_object_set (pipeline, "current-video", active, NULL);
1048 audio_combo_cb (GtkComboBox * combo, GstPipeline * pipeline)
1052 active = gtk_combo_box_get_active (combo);
1054 g_print ("setting current audio track %d\n", active);
1055 g_object_set (pipeline, "current-audio", active, NULL);
1059 text_combo_cb (GtkComboBox * combo, GstPipeline * pipeline)
1063 active = gtk_combo_box_get_active (combo);
1065 g_print ("setting current text track %d\n", active);
1066 g_object_set (pipeline, "current-text", active, NULL);
1070 filter_features (GstPluginFeature * feature, gpointer data)
1072 GstElementFactory *f;
1074 if (!GST_IS_ELEMENT_FACTORY (feature))
1076 f = GST_ELEMENT_FACTORY (feature);
1077 if (!g_strrstr (gst_element_factory_get_klass (f), "Visualization"))
1084 init_visualization_features (void)
1088 vis_entries = g_array_new (FALSE, FALSE, sizeof (VisEntry));
1090 list = gst_registry_feature_filter (gst_registry_get_default (),
1091 filter_features, FALSE, NULL);
1093 for (walk = list; walk; walk = g_list_next (walk)) {
1097 entry.factory = GST_ELEMENT_FACTORY (walk->data);
1098 name = gst_element_factory_get_longname (entry.factory);
1100 g_array_append_val (vis_entries, entry);
1101 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (vis_combo), name);
1103 gtk_combo_box_set_active (GTK_COMBO_BOX (vis_combo), 0);
1107 vis_combo_cb (GtkComboBox * combo, GstPipeline * pipeline)
1111 GstElement *element;
1113 /* get the selected index and get the factory for this index */
1114 index = gtk_combo_box_get_active (GTK_COMBO_BOX (vis_combo));
1115 if (vis_entries->len > 0) {
1116 entry = &g_array_index (vis_entries, VisEntry, index);
1118 /* create an instance of the element from the factory */
1119 element = gst_element_factory_create (entry->factory, NULL);
1123 /* set vis plugin for playbin2 */
1124 g_object_set (pipeline, "vis-plugin", element, NULL);
1129 volume_spinbutton_changed_cb (GtkSpinButton * button, GstPipeline * pipeline)
1133 volume = gtk_spin_button_get_value (button);
1135 g_object_set (pipeline, "volume", volume, NULL);
1139 volume_notify_cb (GstElement * pipeline, GParamSpec * arg, gpointer user_dat)
1141 gdouble cur_volume, new_volume;
1143 if (volume_spinbutton == NULL)
1146 g_object_get (pipeline, "volume", &new_volume, NULL);
1147 cur_volume = gtk_spin_button_get_value (GTK_SPIN_BUTTON (volume_spinbutton));
1148 if (fabs (cur_volume - new_volume) > 0.001) {
1149 g_signal_handlers_block_by_func (volume_spinbutton,
1150 volume_spinbutton_changed_cb, pipeline);
1151 gtk_spin_button_set_value (GTK_SPIN_BUTTON (volume_spinbutton), new_volume);
1152 g_signal_handlers_unblock_by_func (volume_spinbutton,
1153 volume_spinbutton_changed_cb, pipeline);
1158 shot_cb (GtkButton * button, gpointer data)
1163 /* convert to our desired format (RGB24) */
1164 caps = gst_caps_new_simple ("video/x-raw-rgb",
1165 "bpp", G_TYPE_INT, 24, "depth", G_TYPE_INT, 24,
1166 /* Note: we don't ask for a specific width/height here, so that
1167 * videoscale can adjust dimensions from a non-1/1 pixel aspect
1168 * ratio to a 1/1 pixel-aspect-ratio */
1169 "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
1170 "endianness", G_TYPE_INT, G_BIG_ENDIAN,
1171 "red_mask", G_TYPE_INT, 0xff0000,
1172 "green_mask", G_TYPE_INT, 0x00ff00,
1173 "blue_mask", G_TYPE_INT, 0x0000ff, NULL);
1175 /* convert the latest frame to the requested format */
1176 g_signal_emit_by_name (pipeline, "convert-frame", caps, &buffer);
1177 gst_caps_unref (caps);
1185 GError *error = NULL;
1189 /* get the snapshot buffer format now. We set the caps on the appsink so
1190 * that it can only be an rgb buffer. The only thing we have not specified
1191 * on the caps is the height, which is dependant on the pixel-aspect-ratio
1192 * of the source material */
1194 caps = GST_BUFFER_CAPS (buffer);
1196 /* FIXME, need to get the caps of the buffer somehow */
1199 g_warning ("could not get snapshot format\n");
1202 s = gst_caps_get_structure (caps, 0);
1204 /* we need to get the final caps on the buffer to get the size */
1205 res = gst_structure_get_int (s, "width", &width);
1206 res |= gst_structure_get_int (s, "height", &height);
1208 g_warning ("could not get snapshot dimension\n");
1212 /* create pixmap from buffer and save, gstreamer video buffers have a stride
1213 * that is rounded up to the nearest multiple of 4 */
1214 data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
1215 pixbuf = gdk_pixbuf_new_from_data (data,
1216 GDK_COLORSPACE_RGB, FALSE, 8, width, height,
1217 GST_ROUND_UP_4 (width * 3), NULL, NULL);
1219 /* save the pixbuf */
1220 gdk_pixbuf_save (pixbuf, "snapshot.png", "png", &error, NULL);
1221 gst_buffer_unmap (buffer, data, size);
1224 gst_buffer_unref (buffer);
1228 /* called when the Step button is pressed */
1230 step_cb (GtkButton * button, gpointer data)
1236 gboolean flush, res;
1239 active = gtk_combo_box_get_active (GTK_COMBO_BOX (format_combo));
1241 gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
1242 (step_amount_spinbutton));
1243 rate = gtk_spin_button_get_value (GTK_SPIN_BUTTON (step_rate_spinbutton));
1248 format = GST_FORMAT_BUFFERS;
1251 format = GST_FORMAT_TIME;
1252 amount *= GST_MSECOND;
1255 format = GST_FORMAT_UNDEFINED;
1259 event = gst_event_new_step (format, amount, rate, flush, FALSE);
1261 res = send_event (event);
1264 g_print ("Sending step event failed\n");
1269 message_received (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
1271 const GstStructure *s;
1273 s = gst_message_get_structure (message);
1274 g_print ("message from \"%s\" (%s): ",
1275 GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))),
1276 gst_message_type_get_name (GST_MESSAGE_TYPE (message)));
1280 sstr = gst_structure_to_string (s);
1281 g_print ("%s\n", sstr);
1284 g_print ("no message details\n");
1288 static gboolean shuttling = FALSE;
1289 static gdouble shuttle_rate = 0.0;
1290 static gdouble play_rate = 1.0;
1293 do_shuttle (GstElement * element)
1298 duration = 40 * GST_MSECOND;
1302 gst_element_send_event (element,
1303 gst_event_new_step (GST_FORMAT_TIME, duration, shuttle_rate, FALSE,
1308 msg_sync_step_done (GstBus * bus, GstMessage * message, GstElement * element)
1314 gboolean intermediate;
1318 gst_message_parse_step_done (message, &format, &amount, &rate, &flush,
1319 &intermediate, &duration, &eos);
1322 g_print ("stepped till EOS\n");
1326 if (g_static_mutex_trylock (&state_mutex)) {
1328 do_shuttle (element);
1329 g_static_mutex_unlock (&state_mutex);
1331 /* ignore step messages that come while we are doing a state change */
1332 g_print ("state change is busy\n");
1337 shuttle_toggled (GtkToggleButton * button, GstElement * element)
1341 active = gtk_toggle_button_get_active (button);
1343 if (active != shuttling) {
1345 g_print ("shuttling %s\n", shuttling ? "active" : "inactive");
1349 pause_cb (NULL, NULL);
1350 gst_element_get_state (element, NULL, NULL, -1);
1356 shuttle_rate_switch (GstElement * element)
1362 if (state == GST_STATE_PLAYING) {
1363 /* pause when we need to */
1364 pause_cb (NULL, NULL);
1365 gst_element_get_state (element, NULL, NULL, -1);
1368 if (play_rate == 1.0)
1373 g_print ("rate changed to %lf %" GST_TIME_FORMAT "\n", play_rate,
1374 GST_TIME_ARGS (position));
1376 flags = GST_SEEK_FLAG_FLUSH;
1377 flags |= GST_SEEK_FLAG_ACCURATE;
1379 if (play_rate >= 0.0) {
1380 s_event = gst_event_new_seek (play_rate,
1381 GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, position,
1382 GST_SEEK_TYPE_SET, GST_CLOCK_TIME_NONE);
1384 s_event = gst_event_new_seek (play_rate,
1385 GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, G_GINT64_CONSTANT (0),
1386 GST_SEEK_TYPE_SET, position);
1388 res = send_event (s_event);
1390 gst_element_get_state (element, NULL, NULL, SEEK_TIMEOUT);
1392 g_print ("seek failed\n");
1397 shuttle_value_changed (GtkRange * range, GstElement * element)
1401 rate = gtk_adjustment_get_value (shuttle_adjustment);
1404 g_print ("rate 0.0, pause\n");
1405 pause_cb (NULL, NULL);
1406 gst_element_get_state (element, NULL, NULL, -1);
1408 g_print ("rate changed %0.3g\n", rate);
1410 if ((rate < 0.0 && play_rate > 0.0) || (rate > 0.0 && play_rate < 0.0)) {
1411 shuttle_rate_switch (element);
1414 shuttle_rate = ABS (rate);
1415 if (state != GST_STATE_PLAYING) {
1416 do_shuttle (element);
1417 play_cb (NULL, NULL);
1423 msg_async_done (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
1425 GST_DEBUG ("async done");
1426 /* when we get ASYNC_DONE we can query position, duration and other
1428 update_scale (pipeline);
1430 /* update the available streams */
1431 update_streams (pipeline);
1435 msg_state_changed (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
1437 const GstStructure *s;
1439 s = gst_message_get_structure (message);
1441 /* We only care about state changed on the pipeline */
1442 if (s && GST_MESSAGE_SRC (message) == GST_OBJECT_CAST (pipeline)) {
1443 GstState old, new, pending;
1445 gst_message_parse_state_changed (message, &old, &new, &pending);
1447 /* When state of the pipeline changes to paused or playing we start updating scale */
1448 if (new == GST_STATE_PLAYING) {
1449 set_update_scale (TRUE);
1451 set_update_scale (FALSE);
1457 msg_segment_done (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
1464 GST_DEBUG ("position is %" GST_TIME_FORMAT, GST_TIME_ARGS (position));
1465 gst_message_parse_segment_done (message, &format, &position);
1466 GST_DEBUG ("end of segment at %" GST_TIME_FORMAT, GST_TIME_ARGS (position));
1469 /* in the segment-done callback we never flush as this would not make sense
1470 * for seamless playback. */
1472 flags |= GST_SEEK_FLAG_SEGMENT;
1474 flags |= GST_SEEK_FLAG_SKIP;
1476 s_event = gst_event_new_seek (rate,
1477 GST_FORMAT_TIME, flags, GST_SEEK_TYPE_SET, G_GINT64_CONSTANT (0),
1478 GST_SEEK_TYPE_SET, duration);
1480 GST_DEBUG ("restart loop with rate %lf to 0 / %" GST_TIME_FORMAT,
1481 rate, GST_TIME_ARGS (duration));
1483 res = send_event (s_event);
1485 g_print ("segment seek failed\n");
1488 /* in stream buffering mode we PAUSE the pipeline until we receive a 100%
1491 do_stream_buffering (gint percent)
1495 gtk_statusbar_pop (GTK_STATUSBAR (statusbar), status_id);
1496 bufstr = g_strdup_printf ("Buffering...%d", percent);
1497 gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, bufstr);
1500 if (percent == 100) {
1501 /* a 100% message means buffering is done */
1503 /* if the desired state is playing, go back */
1504 if (state == GST_STATE_PLAYING) {
1505 /* no state management needed for live pipelines */
1507 fprintf (stderr, "Done buffering, setting pipeline to PLAYING ...\n");
1508 gst_element_set_state (pipeline, GST_STATE_PLAYING);
1510 gtk_statusbar_pop (GTK_STATUSBAR (statusbar), status_id);
1511 gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Playing");
1514 /* buffering busy */
1515 if (buffering == FALSE && state == GST_STATE_PLAYING) {
1516 /* we were not buffering but PLAYING, PAUSE the pipeline. */
1518 fprintf (stderr, "Buffering, setting pipeline to PAUSED ...\n");
1519 gst_element_set_state (pipeline, GST_STATE_PAUSED);
1527 do_download_buffering (gint percent)
1529 if (!buffering && percent < 100) {
1534 bufstr = g_strdup_printf ("Downloading...");
1535 gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, bufstr);
1538 /* once we get a buffering message, we'll do the fill update */
1539 set_update_fill (TRUE);
1541 if (state == GST_STATE_PLAYING && !is_live) {
1542 fprintf (stderr, "Downloading, setting pipeline to PAUSED ...\n");
1543 gst_element_set_state (pipeline, GST_STATE_PAUSED);
1544 /* user has to manually start the playback */
1545 state = GST_STATE_PAUSED;
1551 msg_buffering (GstBus * bus, GstMessage * message, GstPipeline * data)
1555 gst_message_parse_buffering (message, &percent);
1557 /* get more stats */
1558 gst_message_parse_buffering_stats (message, &mode, NULL, NULL,
1562 case GST_BUFFERING_DOWNLOAD:
1563 do_download_buffering (percent);
1565 case GST_BUFFERING_LIVE:
1567 case GST_BUFFERING_TIMESHIFT:
1568 case GST_BUFFERING_STREAM:
1569 do_stream_buffering (percent);
1575 msg_clock_lost (GstBus * bus, GstMessage * message, GstPipeline * data)
1577 g_print ("clock lost! PAUSE and PLAY to select a new clock\n");
1578 if (state == GST_STATE_PLAYING) {
1579 gst_element_set_state (pipeline, GST_STATE_PAUSED);
1580 gst_element_set_state (pipeline, GST_STATE_PLAYING);
1584 #if defined (GDK_WINDOWING_X11) || defined (GDK_WINDOWING_WIN32)
1586 static gulong embed_xid = 0;
1588 /* We set the xid here in response to the prepare-window-handle message via a
1589 * bus sync handler because we don't know the actual videosink used from the
1590 * start (as we don't know the pipeline, or bin elements such as autovideosink
1591 * or gconfvideosink may be used which create the actual videosink only once
1592 * the pipeline is started) */
1593 static GstBusSyncReply
1594 bus_sync_handler (GstBus * bus, GstMessage * message, GstPipeline * data)
1596 GstElement *element;
1598 if (!gst_is_video_overlay_prepare_window_handle_message (message))
1599 return GST_BUS_PASS;
1601 element = GST_ELEMENT (GST_MESSAGE_SRC (message));
1603 g_print ("got prepare-window-handle, setting XID %lu\n", embed_xid);
1605 if (g_object_class_find_property (G_OBJECT_GET_CLASS (element),
1606 "force-aspect-ratio")) {
1607 g_object_set (element, "force-aspect-ratio", TRUE, NULL);
1610 /* Should have been initialised from main thread before (can't use
1611 * GDK_WINDOW_XID here with Gtk+ >= 2.18, because the sync handler will
1612 * be called from a streaming thread and GDK_WINDOW_XID maps to more than
1613 * a simple structure lookup with Gtk+ >= 2.18, where 'more' is stuff that
1614 * shouldn't be done from a non-GUI thread without explicit locking). */
1615 g_assert (embed_xid != 0);
1617 gst_video_overlay_set_window_handle (GST_VIDEO_OVERLAY (element), embed_xid);
1618 return GST_BUS_PASS;
1623 handle_expose_cb (GtkWidget * widget, GdkEventExpose * event, gpointer data)
1625 if (state < GST_STATE_PAUSED) {
1626 GtkAllocation allocation;
1627 GdkWindow *window = gtk_widget_get_window (widget);
1630 gtk_widget_get_allocation (widget, &allocation);
1631 cr = gdk_cairo_create (window);
1632 cairo_set_source_rgb (cr, 0, 0, 0);
1633 cairo_rectangle (cr, 0, 0, allocation.width, allocation.height);
1641 realize_cb (GtkWidget * widget, gpointer data)
1643 #if GTK_CHECK_VERSION(2,18,0)
1645 GdkWindow *window = gtk_widget_get_window (widget);
1647 /* This is here just for pedagogical purposes, GDK_WINDOW_XID will call it
1649 if (!gdk_window_ensure_native (window))
1650 g_error ("Couldn't create native window needed for GstVideoOverlay!");
1654 #if defined (GDK_WINDOWING_X11) || defined (GDK_WINDOWING_WIN32)
1656 GdkWindow *window = gtk_widget_get_window (video_window);
1658 #if defined (GDK_WINDOWING_WIN32)
1659 embed_xid = GDK_WINDOW_HWND (window);
1661 embed_xid = GDK_WINDOW_XID (window);
1663 g_print ("Window realize: video window XID = %lu\n", embed_xid);
1669 msg_eos (GstBus * bus, GstMessage * message, GstPipeline * data)
1671 message_received (bus, message, data);
1673 /* Set new uri for playerbins and continue playback */
1674 if (l && (pipeline_type == 14 || pipeline_type == 16)) {
1675 stop_cb (NULL, NULL);
1676 l = g_list_next (l);
1678 playerbin_set_uri (GST_ELEMENT (data), l->data);
1679 play_cb (NULL, NULL);
1685 msg_step_done (GstBus * bus, GstMessage * message, GstPipeline * data)
1688 message_received (bus, message, data);
1692 connect_bus_signals (GstElement * pipeline)
1694 GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
1696 #if defined (GDK_WINDOWING_X11) || defined (GDK_WINDOWING_WIN32)
1697 /* handle prepare-window-handle element message synchronously */
1698 gst_bus_set_sync_handler (bus, (GstBusSyncHandler) bus_sync_handler,
1702 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
1703 gst_bus_enable_sync_message_emission (bus);
1705 g_signal_connect (bus, "message::state-changed",
1706 (GCallback) msg_state_changed, pipeline);
1707 g_signal_connect (bus, "message::segment-done", (GCallback) msg_segment_done,
1709 g_signal_connect (bus, "message::async-done", (GCallback) msg_async_done,
1712 g_signal_connect (bus, "message::new-clock", (GCallback) message_received,
1714 g_signal_connect (bus, "message::clock-lost", (GCallback) msg_clock_lost,
1716 g_signal_connect (bus, "message::error", (GCallback) message_received,
1718 g_signal_connect (bus, "message::warning", (GCallback) message_received,
1720 g_signal_connect (bus, "message::eos", (GCallback) msg_eos, pipeline);
1721 g_signal_connect (bus, "message::tag", (GCallback) message_received,
1723 g_signal_connect (bus, "message::element", (GCallback) message_received,
1725 g_signal_connect (bus, "message::segment-done", (GCallback) message_received,
1727 g_signal_connect (bus, "message::buffering", (GCallback) msg_buffering,
1729 // g_signal_connect (bus, "message::step-done", (GCallback) msg_step_done,
1731 g_signal_connect (bus, "message::step-start", (GCallback) msg_step_done,
1733 g_signal_connect (bus, "sync-message::step-done",
1734 (GCallback) msg_sync_step_done, pipeline);
1736 gst_object_unref (bus);
1739 /* Return GList of paths described in location string */
1741 handle_wildcards (const gchar * location)
1744 gchar *path = g_path_get_dirname (location);
1745 gchar *pattern = g_path_get_basename (location);
1746 GPatternSpec *pspec = g_pattern_spec_new (pattern);
1747 GDir *dir = g_dir_open (path, 0, NULL);
1750 g_print ("matching %s from %s\n", pattern, path);
1753 g_print ("opening directory %s failed\n", path);
1757 while ((name = g_dir_read_name (dir)) != NULL) {
1758 if (g_pattern_match_string (pspec, name)) {
1759 res = g_list_append (res, g_strjoin ("/", path, name, NULL));
1760 g_print (" found clip %s\n", name);
1766 g_pattern_spec_free (pspec);
1774 delete_event_cb (void)
1776 stop_cb (NULL, NULL);
1781 print_usage (int argc, char **argv)
1785 g_print ("usage: %s <type> <filename>\n", argv[0]);
1786 g_print (" possible types:\n");
1788 for (i = 0; i < NUM_TYPES; i++) {
1789 g_print (" %d = %s\n", i, pipelines[i].name);
1794 main (int argc, char **argv)
1796 GtkWidget *window, *hbox, *vbox, *panel, *expander, *pb2vbox, *boxes,
1797 *flagtable, *boxes2, *step;
1798 GtkWidget *play_button, *pause_button, *stop_button, *shot_button;
1799 GtkWidget *accurate_checkbox, *key_checkbox, *loop_checkbox, *flush_checkbox;
1800 GtkWidget *scrub_checkbox, *play_scrub_checkbox;
1801 GtkWidget *rate_label, *volume_label;
1802 GOptionEntry options[] = {
1803 {"audiosink", '\0', 0, G_OPTION_ARG_STRING, &opt_audiosink_str,
1804 "audio sink to use (default: " DEFAULT_AUDIOSINK ")", NULL},
1805 {"stats", 's', 0, G_OPTION_ARG_NONE, &stats,
1806 "Show pad stats", NULL},
1807 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
1808 "Verbose properties", NULL},
1809 {"videosink", '\0', 0, G_OPTION_ARG_STRING, &opt_videosink_str,
1810 "video sink to use (default: " DEFAULT_VIDEOSINK ")", NULL},
1813 GOptionContext *ctx;
1816 if (!g_thread_supported ())
1817 g_thread_init (NULL);
1819 ctx = g_option_context_new ("- test seeking in gsteamer");
1820 g_option_context_add_main_entries (ctx, options, NULL);
1821 g_option_context_add_group (ctx, gst_init_get_option_group ());
1822 g_option_context_add_group (ctx, gtk_get_option_group (TRUE));
1824 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
1825 g_print ("Error initializing: %s\n", err->message);
1829 if (opt_audiosink_str == NULL)
1830 opt_audiosink_str = g_strdup (DEFAULT_AUDIOSINK);
1832 if (opt_videosink_str == NULL)
1833 opt_videosink_str = g_strdup (DEFAULT_VIDEOSINK);
1835 GST_DEBUG_CATEGORY_INIT (seek_debug, "seek", 0, "seek example");
1838 print_usage (argc, argv);
1842 pipeline_type = atoi (argv[1]);
1844 if (pipeline_type < 0 || pipeline_type >= NUM_TYPES) {
1845 print_usage (argc, argv);
1849 pipeline_spec = argv[2];
1851 if (g_path_is_absolute (pipeline_spec) &&
1852 (g_strrstr (pipeline_spec, "*") != NULL ||
1853 g_strrstr (pipeline_spec, "?") != NULL)) {
1854 paths = handle_wildcards (pipeline_spec);
1856 paths = g_list_prepend (paths, g_strdup (pipeline_spec));
1860 g_print ("opening %s failed\n", pipeline_spec);
1866 pipeline = pipelines[pipeline_type].func ((gchar *) l->data);
1867 g_assert (pipeline);
1869 /* initialize gui elements ... */
1870 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1871 video_window = gtk_drawing_area_new ();
1872 g_signal_connect (video_window, "expose-event",
1873 G_CALLBACK (handle_expose_cb), NULL);
1874 g_signal_connect (video_window, "realize", G_CALLBACK (realize_cb), NULL);
1875 gtk_widget_set_double_buffered (video_window, FALSE);
1877 statusbar = gtk_statusbar_new ();
1878 status_id = gtk_statusbar_get_context_id (GTK_STATUSBAR (statusbar), "seek");
1879 gtk_statusbar_push (GTK_STATUSBAR (statusbar), status_id, "Stopped");
1880 hbox = gtk_hbox_new (FALSE, 0);
1881 vbox = gtk_vbox_new (FALSE, 0);
1882 flagtable = gtk_table_new (4, 2, FALSE);
1883 gtk_container_set_border_width (GTK_CONTAINER (vbox), 3);
1885 /* media controls */
1886 play_button = gtk_button_new_from_stock (GTK_STOCK_MEDIA_PLAY);
1887 pause_button = gtk_button_new_from_stock (GTK_STOCK_MEDIA_PAUSE);
1888 stop_button = gtk_button_new_from_stock (GTK_STOCK_MEDIA_STOP);
1891 accurate_checkbox = gtk_check_button_new_with_label ("Accurate Seek");
1892 key_checkbox = gtk_check_button_new_with_label ("Key-unit Seek");
1893 loop_checkbox = gtk_check_button_new_with_label ("Loop");
1894 flush_checkbox = gtk_check_button_new_with_label ("Flush");
1895 scrub_checkbox = gtk_check_button_new_with_label ("Scrub");
1896 play_scrub_checkbox = gtk_check_button_new_with_label ("Play Scrub");
1897 skip_checkbox = gtk_check_button_new_with_label ("Play Skip");
1898 rate_spinbutton = gtk_spin_button_new_with_range (-100, 100, 0.1);
1899 gtk_spin_button_set_digits (GTK_SPIN_BUTTON (rate_spinbutton), 3);
1900 rate_label = gtk_label_new ("Rate");
1902 gtk_widget_set_tooltip_text (accurate_checkbox,
1903 "accurate position is requested, this might be considerably slower for some formats");
1904 gtk_widget_set_tooltip_text (key_checkbox,
1905 "seek to the nearest keyframe. This might be faster but less accurate");
1906 gtk_widget_set_tooltip_text (loop_checkbox, "loop playback");
1907 gtk_widget_set_tooltip_text (flush_checkbox, "flush pipeline after seeking");
1908 gtk_widget_set_tooltip_text (rate_spinbutton, "define the playback rate, "
1909 "negative value trigger reverse playback");
1910 gtk_widget_set_tooltip_text (scrub_checkbox, "show images while seeking");
1911 gtk_widget_set_tooltip_text (play_scrub_checkbox, "play video while seeking");
1912 gtk_widget_set_tooltip_text (skip_checkbox,
1913 "Skip frames while playing at high frame rates");
1915 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (flush_checkbox), TRUE);
1916 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (scrub_checkbox), TRUE);
1918 gtk_spin_button_set_value (GTK_SPIN_BUTTON (rate_spinbutton), rate);
1924 step = gtk_expander_new ("step options");
1925 hbox = gtk_hbox_new (FALSE, 0);
1927 format_combo = gtk_combo_box_text_new ();
1928 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (format_combo),
1930 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (format_combo),
1932 gtk_combo_box_set_active (GTK_COMBO_BOX (format_combo), 0);
1933 gtk_box_pack_start (GTK_BOX (hbox), format_combo, FALSE, FALSE, 2);
1935 step_amount_spinbutton = gtk_spin_button_new_with_range (1, 1000, 1);
1936 gtk_spin_button_set_digits (GTK_SPIN_BUTTON (step_amount_spinbutton), 0);
1937 gtk_spin_button_set_value (GTK_SPIN_BUTTON (step_amount_spinbutton), 1.0);
1938 gtk_box_pack_start (GTK_BOX (hbox), step_amount_spinbutton, FALSE, FALSE,
1941 step_rate_spinbutton = gtk_spin_button_new_with_range (0.0, 100, 0.1);
1942 gtk_spin_button_set_digits (GTK_SPIN_BUTTON (step_rate_spinbutton), 3);
1943 gtk_spin_button_set_value (GTK_SPIN_BUTTON (step_rate_spinbutton), 1.0);
1944 gtk_box_pack_start (GTK_BOX (hbox), step_rate_spinbutton, FALSE, FALSE, 2);
1946 step_button = gtk_button_new_from_stock (GTK_STOCK_MEDIA_FORWARD);
1947 gtk_button_set_label (GTK_BUTTON (step_button), "Step");
1948 gtk_box_pack_start (GTK_BOX (hbox), step_button, FALSE, FALSE, 2);
1950 g_signal_connect (G_OBJECT (step_button), "clicked", G_CALLBACK (step_cb),
1954 shuttle_checkbox = gtk_check_button_new_with_label ("Shuttle");
1955 gtk_box_pack_start (GTK_BOX (hbox), shuttle_checkbox, FALSE, FALSE, 2);
1956 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (shuttle_checkbox), FALSE);
1957 g_signal_connect (shuttle_checkbox, "toggled", G_CALLBACK (shuttle_toggled),
1960 shuttle_adjustment =
1961 GTK_ADJUSTMENT (gtk_adjustment_new (0.0, -3.00, 4.0, 0.1, 1.0, 1.0));
1962 shuttle_hscale = gtk_hscale_new (shuttle_adjustment);
1963 gtk_scale_set_digits (GTK_SCALE (shuttle_hscale), 2);
1964 gtk_scale_set_value_pos (GTK_SCALE (shuttle_hscale), GTK_POS_TOP);
1965 g_signal_connect (shuttle_hscale, "value_changed",
1966 G_CALLBACK (shuttle_value_changed), pipeline);
1967 g_signal_connect (shuttle_hscale, "format_value",
1968 G_CALLBACK (shuttle_format_value), pipeline);
1970 gtk_box_pack_start (GTK_BOX (hbox), shuttle_hscale, TRUE, TRUE, 2);
1972 gtk_container_add (GTK_CONTAINER (step), hbox);
1977 GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.00, N_GRAD, 0.1, 1.0, 1.0));
1978 hscale = gtk_hscale_new (adjustment);
1979 gtk_scale_set_digits (GTK_SCALE (hscale), 2);
1980 gtk_scale_set_value_pos (GTK_SCALE (hscale), GTK_POS_RIGHT);
1981 gtk_range_set_show_fill_level (GTK_RANGE (hscale), TRUE);
1982 gtk_range_set_fill_level (GTK_RANGE (hscale), N_GRAD);
1984 g_signal_connect (hscale, "button_press_event", G_CALLBACK (start_seek),
1986 g_signal_connect (hscale, "button_release_event", G_CALLBACK (stop_seek),
1988 g_signal_connect (hscale, "format_value", G_CALLBACK (format_value),
1991 if (pipeline_type == 0) {
1992 /* the playbin2 panel controls for the video/audio/subtitle tracks */
1993 panel = gtk_hbox_new (FALSE, 0);
1994 video_combo = gtk_combo_box_text_new ();
1995 audio_combo = gtk_combo_box_text_new ();
1996 text_combo = gtk_combo_box_text_new ();
1997 gtk_widget_set_sensitive (video_combo, FALSE);
1998 gtk_widget_set_sensitive (audio_combo, FALSE);
1999 gtk_widget_set_sensitive (text_combo, FALSE);
2000 gtk_box_pack_start (GTK_BOX (panel), video_combo, TRUE, TRUE, 2);
2001 gtk_box_pack_start (GTK_BOX (panel), audio_combo, TRUE, TRUE, 2);
2002 gtk_box_pack_start (GTK_BOX (panel), text_combo, TRUE, TRUE, 2);
2003 g_signal_connect (G_OBJECT (video_combo), "changed",
2004 G_CALLBACK (video_combo_cb), pipeline);
2005 g_signal_connect (G_OBJECT (audio_combo), "changed",
2006 G_CALLBACK (audio_combo_cb), pipeline);
2007 g_signal_connect (G_OBJECT (text_combo), "changed",
2008 G_CALLBACK (text_combo_cb), pipeline);
2009 /* playbin2 panel for flag checkboxes and volume/mute */
2010 boxes = gtk_hbox_new (FALSE, 0);
2011 vis_checkbox = gtk_check_button_new_with_label ("Vis");
2012 video_checkbox = gtk_check_button_new_with_label ("Video");
2013 audio_checkbox = gtk_check_button_new_with_label ("Audio");
2014 text_checkbox = gtk_check_button_new_with_label ("Text");
2015 mute_checkbox = gtk_check_button_new_with_label ("Mute");
2016 download_checkbox = gtk_check_button_new_with_label ("Download");
2017 buffer_checkbox = gtk_check_button_new_with_label ("Buffer");
2018 volume_label = gtk_label_new ("Volume");
2019 volume_spinbutton = gtk_spin_button_new_with_range (0, 10.0, 0.1);
2020 gtk_spin_button_set_value (GTK_SPIN_BUTTON (volume_spinbutton), 1.0);
2021 gtk_box_pack_start (GTK_BOX (boxes), video_checkbox, TRUE, TRUE, 2);
2022 gtk_box_pack_start (GTK_BOX (boxes), audio_checkbox, TRUE, TRUE, 2);
2023 gtk_box_pack_start (GTK_BOX (boxes), text_checkbox, TRUE, TRUE, 2);
2024 gtk_box_pack_start (GTK_BOX (boxes), vis_checkbox, TRUE, TRUE, 2);
2025 gtk_box_pack_start (GTK_BOX (boxes), mute_checkbox, TRUE, TRUE, 2);
2026 gtk_box_pack_start (GTK_BOX (boxes), download_checkbox, TRUE, TRUE, 2);
2027 gtk_box_pack_start (GTK_BOX (boxes), buffer_checkbox, TRUE, TRUE, 2);
2028 gtk_box_pack_start (GTK_BOX (boxes), volume_label, TRUE, TRUE, 2);
2029 gtk_box_pack_start (GTK_BOX (boxes), volume_spinbutton, TRUE, TRUE, 2);
2030 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (vis_checkbox), FALSE);
2031 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (audio_checkbox), TRUE);
2032 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (video_checkbox), TRUE);
2033 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (text_checkbox), TRUE);
2034 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (mute_checkbox), FALSE);
2035 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (download_checkbox), FALSE);
2036 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (buffer_checkbox), FALSE);
2037 g_signal_connect (G_OBJECT (vis_checkbox), "toggled",
2038 G_CALLBACK (vis_toggle_cb), pipeline);
2039 g_signal_connect (G_OBJECT (audio_checkbox), "toggled",
2040 G_CALLBACK (audio_toggle_cb), pipeline);
2041 g_signal_connect (G_OBJECT (video_checkbox), "toggled",
2042 G_CALLBACK (video_toggle_cb), pipeline);
2043 g_signal_connect (G_OBJECT (text_checkbox), "toggled",
2044 G_CALLBACK (text_toggle_cb), pipeline);
2045 g_signal_connect (G_OBJECT (mute_checkbox), "toggled",
2046 G_CALLBACK (mute_toggle_cb), pipeline);
2047 g_signal_connect (G_OBJECT (download_checkbox), "toggled",
2048 G_CALLBACK (download_toggle_cb), pipeline);
2049 g_signal_connect (G_OBJECT (buffer_checkbox), "toggled",
2050 G_CALLBACK (buffer_toggle_cb), pipeline);
2051 g_signal_connect (G_OBJECT (volume_spinbutton), "value_changed",
2052 G_CALLBACK (volume_spinbutton_changed_cb), pipeline);
2053 /* playbin2 panel for snapshot */
2054 boxes2 = gtk_hbox_new (FALSE, 0);
2055 shot_button = gtk_button_new_from_stock (GTK_STOCK_SAVE);
2056 gtk_widget_set_tooltip_text (shot_button,
2057 "save a screenshot .png in the current directory");
2058 g_signal_connect (G_OBJECT (shot_button), "clicked", G_CALLBACK (shot_cb),
2060 vis_combo = gtk_combo_box_text_new ();
2061 g_signal_connect (G_OBJECT (vis_combo), "changed",
2062 G_CALLBACK (vis_combo_cb), pipeline);
2063 gtk_widget_set_sensitive (vis_combo, FALSE);
2064 gtk_box_pack_start (GTK_BOX (boxes2), shot_button, TRUE, TRUE, 2);
2065 gtk_box_pack_start (GTK_BOX (boxes2), vis_combo, TRUE, TRUE, 2);
2067 /* fill the vis combo box and the array of factories */
2068 init_visualization_features ();
2070 panel = boxes = boxes2 = NULL;
2073 /* do the packing stuff ... */
2074 gtk_window_set_default_size (GTK_WINDOW (window), 250, 96);
2075 /* FIXME: can we avoid this for audio only? */
2076 gtk_widget_set_size_request (GTK_WIDGET (video_window), -1,
2077 DEFAULT_VIDEO_HEIGHT);
2078 gtk_container_add (GTK_CONTAINER (window), vbox);
2079 gtk_box_pack_start (GTK_BOX (vbox), video_window, TRUE, TRUE, 2);
2080 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 2);
2081 gtk_box_pack_start (GTK_BOX (hbox), play_button, FALSE, FALSE, 2);
2082 gtk_box_pack_start (GTK_BOX (hbox), pause_button, FALSE, FALSE, 2);
2083 gtk_box_pack_start (GTK_BOX (hbox), stop_button, FALSE, FALSE, 2);
2084 gtk_box_pack_start (GTK_BOX (hbox), flagtable, FALSE, FALSE, 2);
2085 gtk_table_attach_defaults (GTK_TABLE (flagtable), accurate_checkbox, 0, 1, 0,
2087 gtk_table_attach_defaults (GTK_TABLE (flagtable), flush_checkbox, 1, 2, 0, 1);
2088 gtk_table_attach_defaults (GTK_TABLE (flagtable), loop_checkbox, 2, 3, 0, 1);
2089 gtk_table_attach_defaults (GTK_TABLE (flagtable), key_checkbox, 0, 1, 1, 2);
2090 gtk_table_attach_defaults (GTK_TABLE (flagtable), scrub_checkbox, 1, 2, 1, 2);
2091 gtk_table_attach_defaults (GTK_TABLE (flagtable), play_scrub_checkbox, 2, 3,
2093 gtk_table_attach_defaults (GTK_TABLE (flagtable), skip_checkbox, 3, 4, 0, 1);
2094 gtk_table_attach_defaults (GTK_TABLE (flagtable), rate_label, 4, 5, 0, 1);
2095 gtk_table_attach_defaults (GTK_TABLE (flagtable), rate_spinbutton, 4, 5, 1,
2097 if (panel && boxes && boxes2) {
2098 expander = gtk_expander_new ("playbin2 options");
2099 pb2vbox = gtk_vbox_new (FALSE, 0);
2100 gtk_box_pack_start (GTK_BOX (pb2vbox), panel, FALSE, FALSE, 2);
2101 gtk_box_pack_start (GTK_BOX (pb2vbox), boxes, FALSE, FALSE, 2);
2102 gtk_box_pack_start (GTK_BOX (pb2vbox), boxes2, FALSE, FALSE, 2);
2103 gtk_container_add (GTK_CONTAINER (expander), pb2vbox);
2104 gtk_box_pack_start (GTK_BOX (vbox), expander, FALSE, FALSE, 2);
2106 gtk_box_pack_start (GTK_BOX (vbox), step, FALSE, FALSE, 2);
2107 gtk_box_pack_start (GTK_BOX (vbox), hscale, FALSE, FALSE, 2);
2108 gtk_box_pack_start (GTK_BOX (vbox), statusbar, FALSE, FALSE, 2);
2110 /* connect things ... */
2111 g_signal_connect (G_OBJECT (play_button), "clicked", G_CALLBACK (play_cb),
2113 g_signal_connect (G_OBJECT (pause_button), "clicked", G_CALLBACK (pause_cb),
2115 g_signal_connect (G_OBJECT (stop_button), "clicked", G_CALLBACK (stop_cb),
2117 g_signal_connect (G_OBJECT (accurate_checkbox), "toggled",
2118 G_CALLBACK (accurate_toggle_cb), pipeline);
2119 g_signal_connect (G_OBJECT (key_checkbox), "toggled",
2120 G_CALLBACK (key_toggle_cb), pipeline);
2121 g_signal_connect (G_OBJECT (loop_checkbox), "toggled",
2122 G_CALLBACK (loop_toggle_cb), pipeline);
2123 g_signal_connect (G_OBJECT (flush_checkbox), "toggled",
2124 G_CALLBACK (flush_toggle_cb), pipeline);
2125 g_signal_connect (G_OBJECT (scrub_checkbox), "toggled",
2126 G_CALLBACK (scrub_toggle_cb), pipeline);
2127 g_signal_connect (G_OBJECT (play_scrub_checkbox), "toggled",
2128 G_CALLBACK (play_scrub_toggle_cb), pipeline);
2129 g_signal_connect (G_OBJECT (skip_checkbox), "toggled",
2130 G_CALLBACK (skip_toggle_cb), pipeline);
2131 g_signal_connect (G_OBJECT (rate_spinbutton), "value_changed",
2132 G_CALLBACK (rate_spinbutton_changed_cb), pipeline);
2134 g_signal_connect (G_OBJECT (window), "delete-event", delete_event_cb, NULL);
2137 gtk_widget_show_all (window);
2139 /* realize window now so that the video window gets created and we can
2140 * obtain its XID before the pipeline is started up and the videosink
2141 * asks for the XID of the window to render onto */
2142 gtk_widget_realize (window);
2144 #if defined (GDK_WINDOWING_X11) || defined (GDK_WINDOWING_WIN32)
2145 /* we should have the XID now */
2146 g_assert (embed_xid != 0);
2150 g_signal_connect (pipeline, "deep_notify",
2151 G_CALLBACK (gst_object_default_deep_notify), NULL);
2154 connect_bus_signals (pipeline);
2157 g_print ("NULL pipeline\n");
2158 gst_element_set_state (pipeline, GST_STATE_NULL);
2160 g_print ("free pipeline\n");
2161 gst_object_unref (pipeline);
2163 g_list_foreach (paths, (GFunc) g_free, NULL);
2164 g_list_free (paths);