• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  *
3  * scrubby.c: sample application to change the playback speed dynamically
4  *
5  * Copyright (C) 2005 Wim Taymans <wim.taymans@gmail.com>
6  *
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.
11  *
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.
16  *
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.
21  */
22 
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 #include <stdlib.h>
27 #include <glib.h>
28 #include <gtk/gtk.h>
29 #include <gst/gst.h>
30 #include <string.h>
31 
32 GST_DEBUG_CATEGORY_STATIC (scrubby_debug);
33 #define GST_CAT_DEFAULT (scrubby_debug)
34 
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;
43 
44 static guint bus_watch = 0;
45 static guint update_id = 0;
46 static guint changed_id = 0;
47 static guint schanged_id = 0;
48 
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"
58 
59 #define RANGE_PREC 10000
60 #define SEGMENT_LEN 100
61 #define UPDATE_INTERVAL 500
62 
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;
69 
70 typedef struct
71 {
72   const gchar *padname;
73   GstPad *target;
74   GstElement *bin;
75 }
76 dyn_link;
77 
78 static GstElement *
gst_element_factory_make_or_warn(const gchar * type,const gchar * name)79 gst_element_factory_make_or_warn (const gchar * type, const gchar * name)
80 {
81   GstElement *element = gst_element_factory_make (type, name);
82 
83   if (!element) {
84     g_warning ("Failed to create element %s of type %s", name, type);
85   }
86 
87   return element;
88 }
89 
90 static GstElement *
make_wav_pipeline(const gchar * location)91 make_wav_pipeline (const gchar * location)
92 {
93   GstElement *pipeline;
94   GstElement *src, *decoder, *audiosink;
95 
96   pipeline = gst_pipeline_new ("app");
97 
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");
101 
102   g_object_set (G_OBJECT (src), "location", location, NULL);
103 
104   gst_bin_add_many (GST_BIN (pipeline), src, decoder, audiosink, NULL);
105   gst_element_link_many (src, decoder, audiosink, NULL);
106 
107   return pipeline;
108 }
109 
110 static GstElement *
make_playerbin_pipeline(const gchar * location)111 make_playerbin_pipeline (const gchar * location)
112 {
113   GstElement *player;
114   const gchar *uri = g_filename_to_uri (location, NULL, NULL);
115 
116   player = gst_element_factory_make ("playbin", "player");
117   g_assert (player);
118 
119   g_object_set (G_OBJECT (player), "uri", uri, NULL);
120 
121   return player;
122 }
123 
124 static gchar *
format_value(GtkScale * scale,gdouble value)125 format_value (GtkScale * scale, gdouble value)
126 {
127   gint64 real;
128   gint64 seconds;
129   gint64 subseconds;
130 
131   real = value * duration / RANGE_PREC;
132   seconds = (gint64) real / GST_SECOND;
133   subseconds = (gint64) real / (GST_SECOND / RANGE_PREC);
134 
135   return g_strdup_printf ("%02" G_GINT64_FORMAT ":%02" G_GINT64_FORMAT ":%02"
136       G_GINT64_FORMAT, seconds / 60, seconds % 60, subseconds % 100);
137 }
138 
139 static gboolean
update_scale(gpointer data)140 update_scale (gpointer data)
141 {
142   position = 0;
143   duration = 0;
144 
145   gst_element_query_position (pipeline, GST_FORMAT_TIME, &position);
146   gst_element_query_duration (pipeline, GST_FORMAT_TIME, &duration);
147 
148   if (position >= duration)
149     duration = position;
150 
151   if (duration > 0) {
152     gtk_adjustment_set_value (adjustment,
153         position * (gdouble) RANGE_PREC / duration);
154     gtk_widget_queue_draw (hscale);
155   }
156 
157   return TRUE;
158 }
159 
160 static void
speed_cb(GtkWidget * widget)161 speed_cb (GtkWidget * widget)
162 {
163   GstEvent *s_event;
164   gboolean res;
165 
166   GST_DEBUG ("speed change");
167   cur_speed = gtk_range_get_value (GTK_RANGE (widget));
168 
169   if (cur_speed == 0.0)
170     return;
171 
172   s_event = gst_event_new_seek (cur_speed,
173       GST_FORMAT_TIME, 0, GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_NONE, -1);
174 
175   res = gst_element_send_event (pipeline, s_event);
176   if (!res)
177     g_print ("speed change failed\n");
178 }
179 
180 static gboolean do_seek (GtkWidget * widget, gboolean flush, gboolean segment);
181 
182 static void
seek_cb(GtkWidget * widget)183 seek_cb (GtkWidget * widget)
184 {
185   if (changed_id) {
186     GST_DEBUG ("seek because of slider move");
187 
188     if (do_seek (widget, TRUE, TRUE)) {
189       g_signal_handler_disconnect (hscale, changed_id);
190       changed_id = 0;
191     }
192   }
193 }
194 
195 static gboolean
do_seek(GtkWidget * widget,gboolean flush,gboolean segment)196 do_seek (GtkWidget * widget, gboolean flush, gboolean segment)
197 {
198   gint64 start, stop;
199   gboolean res = FALSE;
200   GstEvent *s_event;
201   gdouble rate;
202   GTimeVal tv;
203   gboolean valid;
204   gdouble new_range;
205 
206   if (segment)
207     new_range = gtk_range_get_value (GTK_RANGE (widget));
208   else {
209     new_range = (gdouble) RANGE_PREC;
210     cur_time = -1;
211   }
212 
213   valid = prev_time != -1;
214 
215   GST_DEBUG ("flush %d, segment %d, valid %d", flush, segment, valid);
216 
217   if (new_range == cur_range)
218     return FALSE;
219 
220   prev_time = cur_time;
221   prev_range = cur_range;
222 
223   cur_range = new_range;
224 
225   g_get_current_time (&tv);
226   cur_time = GST_TIMEVAL_TO_TIME (tv);
227 
228   if (!valid)
229     return FALSE;
230 
231   GST_DEBUG ("cur:  %lf, %" GST_TIME_FORMAT, cur_range,
232       GST_TIME_ARGS (cur_time));
233   GST_DEBUG ("prev: %lf, %" GST_TIME_FORMAT, prev_range,
234       GST_TIME_ARGS (prev_time));
235 
236   diff = cur_time - prev_time;
237 
238   GST_DEBUG ("diff: %" GST_STIME_FORMAT, GST_STIME_ARGS (diff));
239 
240   start = prev_range * duration / RANGE_PREC;
241   /* play 50 milliseconds */
242   stop = segment ? cur_range * duration / RANGE_PREC : duration;
243 
244   if (start == stop)
245     return FALSE;
246 
247   if (segment)
248     rate = (stop - start) / (gdouble) diff;
249   else
250     rate = cur_speed;
251 
252   if (start > stop) {
253     gint64 tmp;
254 
255     tmp = start;
256     start = stop;
257     stop = tmp;
258   }
259 
260   if (rate == 0.0)
261     return TRUE;
262 
263   GST_DEBUG ("seek to %" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT ", rate %lf"
264       " on element %s",
265       GST_TIME_ARGS (start), GST_TIME_ARGS (stop), rate,
266       GST_ELEMENT_NAME (pipeline));
267 
268   s_event = gst_event_new_seek (rate,
269       GST_FORMAT_TIME,
270       (flush ? GST_SEEK_FLAG_FLUSH : 0) |
271       (segment ? GST_SEEK_FLAG_SEGMENT : 0),
272       GST_SEEK_TYPE_SET, start, GST_SEEK_TYPE_SET, stop);
273 
274   res = gst_element_send_event (pipeline, s_event);
275   if (!res)
276     g_print ("seek failed\n");
277 
278   gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
279 
280   return TRUE;
281 }
282 
283 static gboolean
start_seek(GtkWidget * widget,GdkEventButton * event,gpointer user_data)284 start_seek (GtkWidget * widget, GdkEventButton * event, gpointer user_data)
285 {
286   if (update_id) {
287     g_source_remove (update_id);
288     update_id = 0;
289   }
290 
291   if (changed_id == 0) {
292     changed_id =
293         g_signal_connect (hscale, "value_changed", G_CALLBACK (seek_cb),
294         pipeline);
295   }
296 
297   GST_DEBUG ("start seek");
298 
299   return FALSE;
300 }
301 
302 static gboolean
stop_seek(GtkWidget * widget,gpointer user_data)303 stop_seek (GtkWidget * widget, gpointer user_data)
304 {
305   update_id =
306       g_timeout_add (UPDATE_INTERVAL, (GSourceFunc) update_scale, pipeline);
307 
308   GST_DEBUG ("stop seek");
309 
310   if (changed_id) {
311     g_signal_handler_disconnect (hscale, changed_id);
312     changed_id = 0;
313   }
314 
315   do_seek (hscale, FALSE, FALSE);
316 
317   return FALSE;
318 }
319 
320 static void
play_cb(GtkButton * button,gpointer data)321 play_cb (GtkButton * button, gpointer data)
322 {
323   GstState state;
324 
325   gst_element_get_state (pipeline, &state, NULL, GST_CLOCK_TIME_NONE);
326   if (state != GST_STATE_PLAYING) {
327     g_print ("PLAY pipeline\n");
328     gst_element_set_state (pipeline, GST_STATE_PLAYING);
329     update_id =
330         g_timeout_add (UPDATE_INTERVAL, (GSourceFunc) update_scale, pipeline);
331   }
332 }
333 
334 static void
pause_cb(GtkButton * button,gpointer data)335 pause_cb (GtkButton * button, gpointer data)
336 {
337   GstState state;
338 
339   gst_element_get_state (pipeline, &state, NULL, GST_CLOCK_TIME_NONE);
340   if (state != GST_STATE_PAUSED) {
341     g_print ("PAUSE pipeline\n");
342     gst_element_set_state (pipeline, GST_STATE_PAUSED);
343     g_source_remove (update_id);
344   }
345 }
346 
347 static void
stop_cb(GtkButton * button,gpointer data)348 stop_cb (GtkButton * button, gpointer data)
349 {
350   GstState state;
351 
352   gst_element_get_state (pipeline, &state, NULL, GST_CLOCK_TIME_NONE);
353   if (state != GST_STATE_READY) {
354     g_print ("READY pipeline\n");
355     gst_element_set_state (pipeline, GST_STATE_READY);
356     /* position and speed return to their default values */
357     gtk_adjustment_set_value (adjustment, 0.0);
358     gtk_adjustment_set_value (sadjustment, 1.0);
359     g_source_remove (update_id);
360   }
361 }
362 
363 static void
print_message(GstMessage * message)364 print_message (GstMessage * message)
365 {
366   const GstStructure *s;
367 
368   s = gst_message_get_structure (message);
369   g_print ("Got Message from element \"%s\"\n",
370       GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))));
371 
372   if (s) {
373     gchar *sstr;
374 
375     sstr = gst_structure_to_string (s);
376     g_print ("%s\n", sstr);
377     g_free (sstr);
378   }
379 }
380 
381 static gboolean
bus_message(GstBus * bus,GstMessage * message,gpointer data)382 bus_message (GstBus * bus, GstMessage * message, gpointer data)
383 {
384   switch (GST_MESSAGE_TYPE (message)) {
385     case GST_MESSAGE_EOS:
386       g_print ("EOS\n");
387       break;
388     case GST_MESSAGE_ERROR:
389     case GST_MESSAGE_WARNING:
390       print_message (message);
391       break;
392     case GST_MESSAGE_SEGMENT_START:
393       break;
394     case GST_MESSAGE_SEGMENT_DONE:
395       GST_DEBUG ("segment_done, doing next seek");
396       if (!do_seek (hscale, FALSE, update_id == 0)) {
397         if (changed_id == 0) {
398           changed_id =
399               g_signal_connect (hscale, "value_changed", G_CALLBACK (seek_cb),
400               pipeline);
401         }
402       }
403       break;
404     default:
405       break;
406   }
407 
408   return TRUE;
409 }
410 
411 typedef struct
412 {
413   const gchar *name;
414   GstElement *(*func) (const gchar * location);
415 }
416 Pipeline;
417 
418 static Pipeline pipelines[] = {
419   {"wav", make_wav_pipeline},
420   {"playerbin", make_playerbin_pipeline},
421   {NULL, NULL},
422 };
423 
424 #define NUM_TYPES       ((sizeof (pipelines) / sizeof (Pipeline)) - 1)
425 
426 static void
print_usage(int argc,char ** argv)427 print_usage (int argc, char **argv)
428 {
429   gint i;
430 
431   g_print ("usage: %s <type> <filename>\n", argv[0]);
432   g_print ("   possible types:\n");
433 
434   for (i = 0; i < NUM_TYPES; i++) {
435     g_print ("     %d = %s\n", i, pipelines[i].name);
436   }
437 }
438 
439 int
main(int argc,char ** argv)440 main (int argc, char **argv)
441 {
442   GtkWidget *window, *hbox, *vbox, *play_button, *pause_button, *stop_button;
443   GstBus *bus;
444   GOptionEntry options[] = {
445     {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
446         "Verbose properties", NULL},
447     {NULL}
448   };
449   gint type;
450   GOptionContext *ctx;
451   GError *err = NULL;
452 
453   ctx = g_option_context_new ("seek");
454   g_option_context_add_main_entries (ctx, options, NULL);
455   g_option_context_add_group (ctx, gst_init_get_option_group ());
456 
457   if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
458     g_print ("Error initializing: %s\n", err->message);
459     g_option_context_free (ctx);
460     g_clear_error (&err);
461     exit (1);
462   }
463 
464   GST_DEBUG_CATEGORY_INIT (scrubby_debug, "scrubby", 0, "scrubby example");
465 
466   gtk_init (&argc, &argv);
467 
468   if (argc != 3) {
469     print_usage (argc, argv);
470     exit (-1);
471   }
472 
473   type = atoi (argv[1]);
474 
475   if (type < 0 || type >= NUM_TYPES) {
476     print_usage (argc, argv);
477     exit (-1);
478   }
479 
480   pipeline = pipelines[type].func (argv[2]);
481   g_assert (pipeline);
482 
483   /* initialize gui elements ... */
484   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
485   hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
486   vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
487   play_button = gtk_button_new_with_label ("play");
488   pause_button = gtk_button_new_with_label ("pause");
489   stop_button = gtk_button_new_with_label ("stop");
490 
491   adjustment =
492       GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, (gdouble) RANGE_PREC, 0.1,
493           1.0, 1.0));
494   hscale = gtk_scale_new (GTK_ORIENTATION_HORIZONTAL, adjustment);
495   gtk_scale_set_digits (GTK_SCALE (hscale), 2);
496 
497   sadjustment =
498       GTK_ADJUSTMENT (gtk_adjustment_new (1.0, 0.0, 5.0, 0.1, 1.0, 0.0));
499   shscale = gtk_scale_new (GTK_ORIENTATION_HORIZONTAL, sadjustment);
500   gtk_scale_set_digits (GTK_SCALE (shscale), 2);
501 
502   schanged_id =
503       g_signal_connect (shscale, "value_changed", G_CALLBACK (speed_cb),
504       pipeline);
505 
506   g_signal_connect (hscale, "button_press_event", G_CALLBACK (start_seek),
507       pipeline);
508   g_signal_connect (hscale, "button_release_event", G_CALLBACK (stop_seek),
509       pipeline);
510   g_signal_connect (hscale, "format_value", G_CALLBACK (format_value),
511       pipeline);
512 
513   /* do the packing stuff ... */
514   gtk_window_set_default_size (GTK_WINDOW (window), 96, 96);
515   gtk_container_add (GTK_CONTAINER (window), vbox);
516   gtk_container_add (GTK_CONTAINER (vbox), hbox);
517   gtk_box_pack_start (GTK_BOX (hbox), play_button, FALSE, FALSE, 2);
518   gtk_box_pack_start (GTK_BOX (hbox), pause_button, FALSE, FALSE, 2);
519   gtk_box_pack_start (GTK_BOX (hbox), stop_button, FALSE, FALSE, 2);
520   gtk_box_pack_start (GTK_BOX (vbox), hscale, TRUE, TRUE, 2);
521   gtk_box_pack_start (GTK_BOX (vbox), shscale, TRUE, TRUE, 2);
522 
523   /* connect things ... */
524   g_signal_connect (G_OBJECT (play_button), "clicked", G_CALLBACK (play_cb),
525       pipeline);
526   g_signal_connect (G_OBJECT (pause_button), "clicked", G_CALLBACK (pause_cb),
527       pipeline);
528   g_signal_connect (G_OBJECT (stop_button), "clicked", G_CALLBACK (stop_cb),
529       pipeline);
530   g_signal_connect (G_OBJECT (window), "delete_event", gtk_main_quit, NULL);
531 
532   /* show the gui. */
533   gtk_widget_show_all (window);
534 
535   if (verbose) {
536     g_signal_connect (pipeline, "deep_notify",
537         G_CALLBACK (gst_object_default_deep_notify), NULL);
538   }
539   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
540   g_assert (bus);
541 
542   bus_watch = gst_bus_add_watch_full (bus,
543       G_PRIORITY_HIGH, bus_message, pipeline, NULL);
544 
545   gtk_main ();
546 
547   g_print ("NULL pipeline\n");
548   gst_element_set_state (pipeline, GST_STATE_NULL);
549 
550   gst_object_unref (bus);
551 
552   g_print ("free pipeline\n");
553   gst_object_unref (pipeline);
554 
555   return 0;
556 }
557