3 * scrubby.c: sample application to change the playback speed dynamically
5 * Copyright (C) 2005 Wim Taymans <wim.taymans@gmail.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
32 GST_DEBUG_CATEGORY_STATIC (scrubby_debug);
33 #define GST_CAT_DEFAULT (scrubby_debug)
35 static GstElement *pipeline;
36 static gint64 position;
37 static gint64 duration;
38 static GtkAdjustment *adjustment;
39 static GtkWidget *hscale;
40 static GtkAdjustment *sadjustment;
41 static GtkWidget *shscale;
42 static gboolean verbose = FALSE;
44 static guint bus_watch = 0;
45 static guint update_id = 0;
46 static guint changed_id = 0;
47 static guint schanged_id = 0;
49 #define SOURCE "filesrc"
50 #define ASINK "autoaudiosink"
51 //#define ASINK "alsasink"
52 //#define ASINK "osssink"
53 #define VSINK "autovideosink"
54 //#define VSINK "xvimagesink"
55 //#define VSINK "ximagesink"
56 //#define VSINK "aasink"
57 //#define VSINK "cacasink"
59 #define RANGE_PREC 10000
60 #define SEGMENT_LEN 100
61 #define UPDATE_INTERVAL 500
63 static gdouble prev_range = -1.0;
64 static GstClockTime prev_time = GST_CLOCK_TIME_NONE;
65 static gdouble cur_range;
66 static GstClockTime cur_time;
67 static GstClockTimeDiff diff;
68 static gdouble cur_speed = 1.0;
79 gst_element_factory_make_or_warn (const gchar * type, const gchar * name)
81 GstElement *element = gst_element_factory_make (type, name);
84 g_warning ("Failed to create element %s of type %s", name, type);
91 make_wav_pipeline (const gchar * location)
94 GstElement *src, *decoder, *audiosink;
96 pipeline = gst_pipeline_new ("app");
98 src = gst_element_factory_make_or_warn (SOURCE, "src");
99 decoder = gst_element_factory_make_or_warn ("wavparse", "decoder");
100 audiosink = gst_element_factory_make_or_warn (ASINK, "sink");
102 g_object_set (G_OBJECT (src), "location", location, NULL);
104 gst_bin_add_many (GST_BIN (pipeline), src, decoder, audiosink, NULL);
105 gst_element_link_many (src, decoder, audiosink, NULL);
111 make_playerbin_pipeline (const gchar * location)
114 const gchar *uri = g_filename_to_uri (location, NULL, NULL);
116 player = gst_element_factory_make ("playbin", "player");
119 g_object_set (G_OBJECT (player), "uri", uri, NULL);
125 format_value (GtkScale * scale, gdouble value)
131 real = value * duration / RANGE_PREC;
132 seconds = (gint64) real / GST_SECOND;
133 subseconds = (gint64) real / (GST_SECOND / RANGE_PREC);
135 return g_strdup_printf ("%02" G_GINT64_FORMAT ":%02" G_GINT64_FORMAT ":%02"
136 G_GINT64_FORMAT, seconds / 60, seconds % 60, subseconds % 100);
140 update_scale (gpointer data)
145 gst_element_query_position (pipeline, GST_FORMAT_TIME, &position);
146 gst_element_query_duration (pipeline, GST_FORMAT_TIME, &duration);
148 if (position >= duration)
152 gtk_adjustment_set_value (adjustment,
153 position * (gdouble) RANGE_PREC / duration);
154 gtk_widget_queue_draw (hscale);
161 speed_cb (GtkWidget * widget)
166 GST_DEBUG ("speed change");
167 cur_speed = gtk_range_get_value (GTK_RANGE (widget));
169 if (cur_speed == 0.0)
172 s_event = gst_event_new_seek (cur_speed,
173 GST_FORMAT_TIME, 0, GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_NONE, -1);
175 res = gst_element_send_event (pipeline, s_event);
177 g_print ("speed change failed\n");
180 static gboolean do_seek (GtkWidget * widget, gboolean flush, gboolean segment);
183 seek_cb (GtkWidget * widget)
186 GST_DEBUG ("seek because of slider move");
188 if (do_seek (widget, TRUE, TRUE)) {
189 g_signal_handler_disconnect (hscale, changed_id);
196 do_seek (GtkWidget * widget, gboolean flush, gboolean segment)
199 gboolean res = FALSE;
206 new_range = gtk_range_get_value (GTK_RANGE (widget));
208 new_range = (gdouble) RANGE_PREC;
212 valid = prev_time != -1;
214 GST_DEBUG ("flush %d, segment %d, valid %d", flush, segment, valid);
216 if (new_range == cur_range)
219 prev_time = cur_time;
220 prev_range = cur_range;
222 cur_range = new_range;
224 cur_time = g_get_real_time () * GST_USECOND;
229 GST_DEBUG ("cur: %lf, %" GST_TIME_FORMAT, cur_range,
230 GST_TIME_ARGS (cur_time));
231 GST_DEBUG ("prev: %lf, %" GST_TIME_FORMAT, prev_range,
232 GST_TIME_ARGS (prev_time));
234 diff = cur_time - prev_time;
236 GST_DEBUG ("diff: %" GST_STIME_FORMAT, GST_STIME_ARGS (diff));
238 start = prev_range * duration / RANGE_PREC;
239 /* play 50 milliseconds */
240 stop = segment ? cur_range * duration / RANGE_PREC : duration;
246 rate = (stop - start) / (gdouble) diff;
261 GST_DEBUG ("seek to %" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT ", rate %lf"
263 GST_TIME_ARGS (start), GST_TIME_ARGS (stop), rate,
264 GST_ELEMENT_NAME (pipeline));
266 s_event = gst_event_new_seek (rate,
268 (flush ? GST_SEEK_FLAG_FLUSH : 0) |
269 (segment ? GST_SEEK_FLAG_SEGMENT : 0),
270 GST_SEEK_TYPE_SET, start, GST_SEEK_TYPE_SET, stop);
272 res = gst_element_send_event (pipeline, s_event);
274 g_print ("seek failed\n");
276 gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
282 start_seek (GtkWidget * widget, GdkEventButton * event, gpointer user_data)
285 g_source_remove (update_id);
289 if (changed_id == 0) {
291 g_signal_connect (hscale, "value_changed", G_CALLBACK (seek_cb),
295 GST_DEBUG ("start seek");
301 stop_seek (GtkWidget * widget, gpointer user_data)
304 g_timeout_add (UPDATE_INTERVAL, (GSourceFunc) update_scale, pipeline);
306 GST_DEBUG ("stop seek");
309 g_signal_handler_disconnect (hscale, changed_id);
313 do_seek (hscale, FALSE, FALSE);
319 play_cb (GtkButton * button, gpointer data)
323 gst_element_get_state (pipeline, &state, NULL, GST_CLOCK_TIME_NONE);
324 if (state != GST_STATE_PLAYING) {
325 g_print ("PLAY pipeline\n");
326 gst_element_set_state (pipeline, GST_STATE_PLAYING);
328 g_timeout_add (UPDATE_INTERVAL, (GSourceFunc) update_scale, pipeline);
333 pause_cb (GtkButton * button, gpointer data)
337 gst_element_get_state (pipeline, &state, NULL, GST_CLOCK_TIME_NONE);
338 if (state != GST_STATE_PAUSED) {
339 g_print ("PAUSE pipeline\n");
340 gst_element_set_state (pipeline, GST_STATE_PAUSED);
341 g_source_remove (update_id);
346 stop_cb (GtkButton * button, gpointer data)
350 gst_element_get_state (pipeline, &state, NULL, GST_CLOCK_TIME_NONE);
351 if (state != GST_STATE_READY) {
352 g_print ("READY pipeline\n");
353 gst_element_set_state (pipeline, GST_STATE_READY);
354 /* position and speed return to their default values */
355 gtk_adjustment_set_value (adjustment, 0.0);
356 gtk_adjustment_set_value (sadjustment, 1.0);
357 g_source_remove (update_id);
362 print_message (GstMessage * message)
364 const GstStructure *s;
366 s = gst_message_get_structure (message);
367 g_print ("Got Message from element \"%s\"\n",
368 GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))));
373 sstr = gst_structure_to_string (s);
374 g_print ("%s\n", sstr);
380 bus_message (GstBus * bus, GstMessage * message, gpointer data)
382 switch (GST_MESSAGE_TYPE (message)) {
383 case GST_MESSAGE_EOS:
386 case GST_MESSAGE_ERROR:
387 case GST_MESSAGE_WARNING:
388 print_message (message);
390 case GST_MESSAGE_SEGMENT_START:
392 case GST_MESSAGE_SEGMENT_DONE:
393 GST_DEBUG ("segment_done, doing next seek");
394 if (!do_seek (hscale, FALSE, update_id == 0)) {
395 if (changed_id == 0) {
397 g_signal_connect (hscale, "value_changed", G_CALLBACK (seek_cb),
412 GstElement *(*func) (const gchar * location);
416 static Pipeline pipelines[] = {
417 {"wav", make_wav_pipeline},
418 {"playerbin", make_playerbin_pipeline},
422 #define NUM_TYPES ((sizeof (pipelines) / sizeof (Pipeline)) - 1)
425 print_usage (int argc, char **argv)
429 g_print ("usage: %s <type> <filename>\n", argv[0]);
430 g_print (" possible types:\n");
432 for (i = 0; i < NUM_TYPES; i++) {
433 g_print (" %d = %s\n", i, pipelines[i].name);
438 main (int argc, char **argv)
440 GtkWidget *window, *hbox, *vbox, *play_button, *pause_button, *stop_button;
442 GOptionEntry options[] = {
443 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
444 "Verbose properties", NULL},
451 ctx = g_option_context_new ("seek");
452 g_option_context_add_main_entries (ctx, options, NULL);
453 g_option_context_add_group (ctx, gst_init_get_option_group ());
455 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
456 g_print ("Error initializing: %s\n", err->message);
457 g_option_context_free (ctx);
458 g_clear_error (&err);
462 GST_DEBUG_CATEGORY_INIT (scrubby_debug, "scrubby", 0, "scrubby example");
464 gtk_init (&argc, &argv);
467 print_usage (argc, argv);
471 type = atoi (argv[1]);
473 if (type < 0 || type >= NUM_TYPES) {
474 print_usage (argc, argv);
478 pipeline = pipelines[type].func (argv[2]);
481 /* initialize gui elements ... */
482 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
483 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
484 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
485 play_button = gtk_button_new_with_label ("play");
486 pause_button = gtk_button_new_with_label ("pause");
487 stop_button = gtk_button_new_with_label ("stop");
490 GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, (gdouble) RANGE_PREC, 0.1,
492 hscale = gtk_scale_new (GTK_ORIENTATION_HORIZONTAL, adjustment);
493 gtk_scale_set_digits (GTK_SCALE (hscale), 2);
496 GTK_ADJUSTMENT (gtk_adjustment_new (1.0, 0.0, 5.0, 0.1, 1.0, 0.0));
497 shscale = gtk_scale_new (GTK_ORIENTATION_HORIZONTAL, sadjustment);
498 gtk_scale_set_digits (GTK_SCALE (shscale), 2);
501 g_signal_connect (shscale, "value_changed", G_CALLBACK (speed_cb),
504 g_signal_connect (hscale, "button_press_event", G_CALLBACK (start_seek),
506 g_signal_connect (hscale, "button_release_event", G_CALLBACK (stop_seek),
508 g_signal_connect (hscale, "format_value", G_CALLBACK (format_value),
511 /* do the packing stuff ... */
512 gtk_window_set_default_size (GTK_WINDOW (window), 96, 96);
513 gtk_container_add (GTK_CONTAINER (window), vbox);
514 gtk_container_add (GTK_CONTAINER (vbox), hbox);
515 gtk_box_pack_start (GTK_BOX (hbox), play_button, FALSE, FALSE, 2);
516 gtk_box_pack_start (GTK_BOX (hbox), pause_button, FALSE, FALSE, 2);
517 gtk_box_pack_start (GTK_BOX (hbox), stop_button, FALSE, FALSE, 2);
518 gtk_box_pack_start (GTK_BOX (vbox), hscale, TRUE, TRUE, 2);
519 gtk_box_pack_start (GTK_BOX (vbox), shscale, TRUE, TRUE, 2);
521 /* connect things ... */
522 g_signal_connect (G_OBJECT (play_button), "clicked", G_CALLBACK (play_cb),
524 g_signal_connect (G_OBJECT (pause_button), "clicked", G_CALLBACK (pause_cb),
526 g_signal_connect (G_OBJECT (stop_button), "clicked", G_CALLBACK (stop_cb),
528 g_signal_connect (G_OBJECT (window), "delete_event", gtk_main_quit, NULL);
531 gtk_widget_show_all (window);
534 g_signal_connect (pipeline, "deep_notify",
535 G_CALLBACK (gst_object_default_deep_notify), NULL);
537 bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
540 bus_watch = gst_bus_add_watch_full (bus,
541 G_PRIORITY_HIGH, bus_message, pipeline, NULL);
545 g_print ("NULL pipeline\n");
546 gst_element_set_state (pipeline, GST_STATE_NULL);
548 gst_object_unref (bus);
550 g_print ("free pipeline\n");
551 gst_object_unref (pipeline);