7 GST_DEBUG_CATEGORY_STATIC (scrubby_debug);
8 #define GST_CAT_DEFAULT (scrubby_debug)
10 static GstElement *pipeline;
11 static gint64 position;
12 static gint64 duration;
13 static GtkAdjustment *adjustment;
14 static GtkWidget *hscale;
15 static GtkAdjustment *sadjustment;
16 static GtkWidget *shscale;
17 static gboolean verbose = FALSE;
19 static guint bus_watch = 0;
20 static guint update_id = 0;
21 static guint changed_id = 0;
22 static guint schanged_id = 0;
24 //#define SOURCE "filesrc"
25 #define SOURCE "gnomevfssrc"
26 #define ASINK "alsasink"
27 //#define ASINK "osssink"
28 #define VSINK "xvimagesink"
29 //#define VSINK "ximagesink"
30 //#define VSINK "aasink"
31 //#define VSINK "cacasink"
33 #define RANGE_PREC 10000
34 #define SEGMENT_LEN 100
35 #define UPDATE_INTERVAL 500
37 static gdouble prev_range = -1.0;
38 static GstClockTime prev_time = GST_CLOCK_TIME_NONE;
39 static gdouble cur_range;
40 static GstClockTime cur_time;
41 static GstClockTimeDiff diff;
42 static gdouble cur_speed = 1.0;
53 gst_element_factory_make_or_warn (const gchar * type, const gchar * name)
55 GstElement *element = gst_element_factory_make (type, name);
58 g_warning ("Failed to create element %s of type %s", name, type);
65 dynamic_link (GstPadTemplate * templ, GstPad * newpad, gpointer data)
67 dyn_link *connect = (dyn_link *) data;
69 if (connect->padname == NULL ||
70 !strcmp (gst_pad_get_name (newpad), connect->padname)) {
72 gst_bin_add (GST_BIN (pipeline), connect->bin);
73 gst_pad_link (newpad, connect->target);
78 setup_dynamic_link (GstElement * element, const gchar * padname,
79 GstPad * target, GstElement * bin)
83 connect = g_new0 (dyn_link, 1);
84 connect->padname = g_strdup (padname);
85 connect->target = target;
88 g_signal_connect (G_OBJECT (element), "pad-added", G_CALLBACK (dynamic_link),
93 make_wav_pipeline (const gchar * location)
96 GstElement *src, *decoder, *audiosink;
98 pipeline = gst_pipeline_new ("app");
100 src = gst_element_factory_make_or_warn (SOURCE, "src");
101 decoder = gst_element_factory_make_or_warn ("wavparse", "decoder");
102 audiosink = gst_element_factory_make_or_warn (ASINK, "sink");
104 g_object_set (G_OBJECT (src), "location", location, NULL);
106 gst_bin_add (GST_BIN (pipeline), src);
107 gst_bin_add (GST_BIN (pipeline), decoder);
108 gst_bin_add (GST_BIN (pipeline), audiosink);
110 gst_element_link (src, decoder);
112 setup_dynamic_link (decoder, "src", gst_element_get_static_pad (audiosink,
119 make_playerbin_pipeline (const gchar * location)
123 player = gst_element_factory_make ("playbin", "player");
126 g_object_set (G_OBJECT (player), "uri", location, NULL);
132 format_value (GtkScale * scale, gdouble value)
138 real = value * duration / RANGE_PREC;
139 seconds = (gint64) real / GST_SECOND;
140 subseconds = (gint64) real / (GST_SECOND / RANGE_PREC);
142 return g_strdup_printf ("%02" G_GINT64_FORMAT ":%02" G_GINT64_FORMAT ":%02"
143 G_GINT64_FORMAT, seconds / 60, seconds % 60, subseconds % 100);
147 update_scale (gpointer data)
154 format = GST_FORMAT_TIME;
156 gst_element_query_position (pipeline, &format, &position);
157 gst_element_query_duration (pipeline, &format, &duration);
159 if (position >= duration)
163 gtk_adjustment_set_value (adjustment,
164 position * (gdouble) RANGE_PREC / duration);
165 gtk_widget_queue_draw (hscale);
172 speed_cb (GtkWidget * widget)
177 GST_DEBUG ("speed change");
178 cur_speed = gtk_range_get_value (GTK_RANGE (widget));
180 if (cur_speed == 0.0)
183 s_event = gst_event_new_seek (cur_speed,
184 GST_FORMAT_TIME, 0, GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_NONE, -1);
186 res = gst_element_send_event (pipeline, s_event);
188 g_print ("speed change failed\n");
191 static gboolean do_seek (GtkWidget * widget, gboolean flush, gboolean segment);
194 seek_cb (GtkWidget * widget)
197 GST_DEBUG ("seek because of slider move");
199 if (do_seek (widget, TRUE, TRUE)) {
200 g_source_remove (changed_id);
207 do_seek (GtkWidget * widget, gboolean flush, gboolean segment)
210 gboolean res = FALSE;
218 new_range = gtk_range_get_value (GTK_RANGE (widget));
220 new_range = (gdouble) RANGE_PREC;
224 valid = prev_time != -1;
226 GST_DEBUG ("flush %d, segment %d, valid %d", flush, segment, valid);
228 if (new_range == cur_range)
231 prev_time = cur_time;
232 prev_range = cur_range;
234 cur_range = new_range;
236 g_get_current_time (&tv);
237 cur_time = GST_TIMEVAL_TO_TIME (tv);
242 GST_DEBUG ("cur: %lf, %" GST_TIME_FORMAT, cur_range,
243 GST_TIME_ARGS (cur_time));
244 GST_DEBUG ("prev: %lf, %" GST_TIME_FORMAT, prev_range,
245 GST_TIME_ARGS (prev_time));
247 diff = cur_time - prev_time;
249 GST_DEBUG ("diff: %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
251 start = prev_range * duration / RANGE_PREC;
252 /* play 50 milliseconds */
253 stop = segment ? cur_range * duration / RANGE_PREC : duration;
259 rate = (stop - start) / (gdouble) diff;
274 GST_DEBUG ("seek to %" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT ", rate %lf"
276 GST_TIME_ARGS (start), GST_TIME_ARGS (stop), rate,
277 GST_ELEMENT_NAME (pipeline));
279 s_event = gst_event_new_seek (rate,
281 (flush ? GST_SEEK_FLAG_FLUSH : 0) |
282 (segment ? GST_SEEK_FLAG_SEGMENT : 0),
283 GST_SEEK_TYPE_SET, start, GST_SEEK_TYPE_SET, stop);
285 res = gst_element_send_event (pipeline, s_event);
287 g_print ("seek failed\n");
289 gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
295 start_seek (GtkWidget * widget, GdkEventButton * event, gpointer user_data)
298 g_source_remove (update_id);
302 if (changed_id == 0) {
304 g_signal_connect (hscale, "value_changed", G_CALLBACK (seek_cb),
308 GST_DEBUG ("start seek");
314 stop_seek (GtkWidget * widget, gpointer user_data)
317 g_timeout_add (UPDATE_INTERVAL, (GSourceFunc) update_scale, pipeline);
319 GST_DEBUG ("stop seek");
322 g_source_remove (changed_id);
326 do_seek (hscale, FALSE, FALSE);
332 play_cb (GtkButton * button, gpointer data)
336 gst_element_get_state (pipeline, &state, NULL, GST_CLOCK_TIME_NONE);
337 if (state != GST_STATE_PLAYING) {
338 g_print ("PLAY pipeline\n");
339 gst_element_set_state (pipeline, GST_STATE_PAUSED);
340 gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
341 gst_element_set_state (pipeline, GST_STATE_PLAYING);
343 g_timeout_add (UPDATE_INTERVAL, (GSourceFunc) update_scale, pipeline);
348 pause_cb (GtkButton * button, gpointer data)
352 gst_element_get_state (pipeline, &state, NULL, GST_CLOCK_TIME_NONE);
353 if (state != GST_STATE_PAUSED) {
354 g_print ("PAUSE pipeline\n");
355 gst_element_set_state (pipeline, GST_STATE_PAUSED);
356 g_source_remove (update_id);
361 stop_cb (GtkButton * button, gpointer data)
365 gst_element_get_state (pipeline, &state, NULL, GST_CLOCK_TIME_NONE);
366 if (state != GST_STATE_READY) {
367 g_print ("READY pipeline\n");
368 gst_element_set_state (pipeline, GST_STATE_READY);
369 /* position and speed return to their default values */
370 gtk_adjustment_set_value (adjustment, 0.0);
371 gtk_adjustment_set_value (sadjustment, 1.0);
372 g_source_remove (update_id);
377 print_message (GstMessage * message)
379 const GstStructure *s;
381 s = gst_message_get_structure (message);
382 g_print ("Got Message from element \"%s\"\n",
383 GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))));
388 sstr = gst_structure_to_string (s);
389 g_print ("%s\n", sstr);
395 bus_message (GstBus * bus, GstMessage * message, gpointer data)
397 switch (GST_MESSAGE_TYPE (message)) {
398 case GST_MESSAGE_EOS:
401 case GST_MESSAGE_ERROR:
402 case GST_MESSAGE_WARNING:
403 print_message (message);
405 case GST_MESSAGE_SEGMENT_START:
407 case GST_MESSAGE_SEGMENT_DONE:
408 GST_DEBUG ("segment_done, doing next seek");
409 if (!do_seek (hscale, FALSE, update_id == 0)) {
410 if (changed_id == 0) {
412 g_signal_connect (hscale, "value_changed", G_CALLBACK (seek_cb),
427 GstElement *(*func) (const gchar * location);
431 static Pipeline pipelines[] = {
432 {"wav", make_wav_pipeline},
433 {"playerbin", make_playerbin_pipeline},
437 #define NUM_TYPES ((sizeof (pipelines) / sizeof (Pipeline)) - 1)
440 print_usage (int argc, char **argv)
444 g_print ("usage: %s <type> <filename>\n", argv[0]);
445 g_print (" possible types:\n");
447 for (i = 0; i < NUM_TYPES; i++) {
448 g_print (" %d = %s\n", i, pipelines[i].name);
453 main (int argc, char **argv)
455 GtkWidget *window, *hbox, *vbox, *play_button, *pause_button, *stop_button;
457 GOptionEntry options[] = {
458 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
459 "Verbose properties", NULL},
466 #if !GLIB_CHECK_VERSION (2, 31, 0)
467 if (!g_thread_supported ())
468 g_thread_init (NULL);
471 ctx = g_option_context_new ("seek");
472 g_option_context_add_main_entries (ctx, options, NULL);
473 g_option_context_add_group (ctx, gst_init_get_option_group ());
475 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
476 g_print ("Error initializing: %s\n", err->message);
480 GST_DEBUG_CATEGORY_INIT (scrubby_debug, "scrubby", 0, "scrubby example");
482 gtk_init (&argc, &argv);
485 print_usage (argc, argv);
489 type = atoi (argv[1]);
491 if (type < 0 || type >= NUM_TYPES) {
492 print_usage (argc, argv);
496 pipeline = pipelines[type].func (argv[2]);
499 /* initialize gui elements ... */
500 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
501 hbox = gtk_hbox_new (FALSE, 0);
502 vbox = gtk_vbox_new (FALSE, 0);
503 play_button = gtk_button_new_with_label ("play");
504 pause_button = gtk_button_new_with_label ("pause");
505 stop_button = gtk_button_new_with_label ("stop");
508 GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, (gdouble) RANGE_PREC, 0.1,
510 hscale = gtk_hscale_new (adjustment);
511 gtk_scale_set_digits (GTK_SCALE (hscale), 2);
514 GTK_ADJUSTMENT (gtk_adjustment_new (1.0, 0.0, 5.0, 0.1, 1.0, 0.0));
515 shscale = gtk_hscale_new (sadjustment);
516 gtk_scale_set_digits (GTK_SCALE (shscale), 2);
519 g_signal_connect (shscale, "value_changed", G_CALLBACK (speed_cb),
522 g_signal_connect (hscale, "button_press_event", G_CALLBACK (start_seek),
524 g_signal_connect (hscale, "button_release_event", G_CALLBACK (stop_seek),
526 g_signal_connect (hscale, "format_value", G_CALLBACK (format_value),
529 /* do the packing stuff ... */
530 gtk_window_set_default_size (GTK_WINDOW (window), 96, 96);
531 gtk_container_add (GTK_CONTAINER (window), vbox);
532 gtk_container_add (GTK_CONTAINER (vbox), hbox);
533 gtk_box_pack_start (GTK_BOX (hbox), play_button, FALSE, FALSE, 2);
534 gtk_box_pack_start (GTK_BOX (hbox), pause_button, FALSE, FALSE, 2);
535 gtk_box_pack_start (GTK_BOX (hbox), stop_button, FALSE, FALSE, 2);
536 gtk_box_pack_start (GTK_BOX (vbox), hscale, TRUE, TRUE, 2);
537 gtk_box_pack_start (GTK_BOX (vbox), shscale, TRUE, TRUE, 2);
539 /* connect things ... */
540 g_signal_connect (G_OBJECT (play_button), "clicked", G_CALLBACK (play_cb),
542 g_signal_connect (G_OBJECT (pause_button), "clicked", G_CALLBACK (pause_cb),
544 g_signal_connect (G_OBJECT (stop_button), "clicked", G_CALLBACK (stop_cb),
546 g_signal_connect (G_OBJECT (window), "delete_event", gtk_main_quit, NULL);
549 gtk_widget_show_all (window);
552 g_signal_connect (pipeline, "deep_notify",
553 G_CALLBACK (gst_object_default_deep_notify), NULL);
555 bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
558 bus_watch = gst_bus_add_watch_full (bus,
559 G_PRIORITY_HIGH, bus_message, pipeline, NULL);
563 g_print ("NULL pipeline\n");
564 gst_element_set_state (pipeline, GST_STATE_NULL);
566 g_print ("free pipeline\n");
567 gst_object_unref (pipeline);