base: Avoid usage of deprecated API
[platform/upstream/gstreamer.git] / tests / examples / seek / scrubby.c
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 *
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 *
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 *
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 *
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
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
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
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
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   gboolean valid;
203   gdouble new_range;
204
205   if (segment)
206     new_range = gtk_range_get_value (GTK_RANGE (widget));
207   else {
208     new_range = (gdouble) RANGE_PREC;
209     cur_time = -1;
210   }
211
212   valid = prev_time != -1;
213
214   GST_DEBUG ("flush %d, segment %d, valid %d", flush, segment, valid);
215
216   if (new_range == cur_range)
217     return FALSE;
218
219   prev_time = cur_time;
220   prev_range = cur_range;
221
222   cur_range = new_range;
223
224   cur_time = g_get_real_time () * GST_USECOND;
225
226   if (!valid)
227     return FALSE;
228
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));
233
234   diff = cur_time - prev_time;
235
236   GST_DEBUG ("diff: %" GST_STIME_FORMAT, GST_STIME_ARGS (diff));
237
238   start = prev_range * duration / RANGE_PREC;
239   /* play 50 milliseconds */
240   stop = segment ? cur_range * duration / RANGE_PREC : duration;
241
242   if (start == stop)
243     return FALSE;
244
245   if (segment)
246     rate = (stop - start) / (gdouble) diff;
247   else
248     rate = cur_speed;
249
250   if (start > stop) {
251     gint64 tmp;
252
253     tmp = start;
254     start = stop;
255     stop = tmp;
256   }
257
258   if (rate == 0.0)
259     return TRUE;
260
261   GST_DEBUG ("seek to %" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT ", rate %lf"
262       " on element %s",
263       GST_TIME_ARGS (start), GST_TIME_ARGS (stop), rate,
264       GST_ELEMENT_NAME (pipeline));
265
266   s_event = gst_event_new_seek (rate,
267       GST_FORMAT_TIME,
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);
271
272   res = gst_element_send_event (pipeline, s_event);
273   if (!res)
274     g_print ("seek failed\n");
275
276   gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
277
278   return TRUE;
279 }
280
281 static gboolean
282 start_seek (GtkWidget * widget, GdkEventButton * event, gpointer user_data)
283 {
284   if (update_id) {
285     g_source_remove (update_id);
286     update_id = 0;
287   }
288
289   if (changed_id == 0) {
290     changed_id =
291         g_signal_connect (hscale, "value_changed", G_CALLBACK (seek_cb),
292         pipeline);
293   }
294
295   GST_DEBUG ("start seek");
296
297   return FALSE;
298 }
299
300 static gboolean
301 stop_seek (GtkWidget * widget, gpointer user_data)
302 {
303   update_id =
304       g_timeout_add (UPDATE_INTERVAL, (GSourceFunc) update_scale, pipeline);
305
306   GST_DEBUG ("stop seek");
307
308   if (changed_id) {
309     g_signal_handler_disconnect (hscale, changed_id);
310     changed_id = 0;
311   }
312
313   do_seek (hscale, FALSE, FALSE);
314
315   return FALSE;
316 }
317
318 static void
319 play_cb (GtkButton * button, gpointer data)
320 {
321   GstState state;
322
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);
327     update_id =
328         g_timeout_add (UPDATE_INTERVAL, (GSourceFunc) update_scale, pipeline);
329   }
330 }
331
332 static void
333 pause_cb (GtkButton * button, gpointer data)
334 {
335   GstState state;
336
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);
342   }
343 }
344
345 static void
346 stop_cb (GtkButton * button, gpointer data)
347 {
348   GstState state;
349
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);
358   }
359 }
360
361 static void
362 print_message (GstMessage * message)
363 {
364   const GstStructure *s;
365
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))));
369
370   if (s) {
371     gchar *sstr;
372
373     sstr = gst_structure_to_string (s);
374     g_print ("%s\n", sstr);
375     g_free (sstr);
376   }
377 }
378
379 static gboolean
380 bus_message (GstBus * bus, GstMessage * message, gpointer data)
381 {
382   switch (GST_MESSAGE_TYPE (message)) {
383     case GST_MESSAGE_EOS:
384       g_print ("EOS\n");
385       break;
386     case GST_MESSAGE_ERROR:
387     case GST_MESSAGE_WARNING:
388       print_message (message);
389       break;
390     case GST_MESSAGE_SEGMENT_START:
391       break;
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) {
396           changed_id =
397               g_signal_connect (hscale, "value_changed", G_CALLBACK (seek_cb),
398               pipeline);
399         }
400       }
401       break;
402     default:
403       break;
404   }
405
406   return TRUE;
407 }
408
409 typedef struct
410 {
411   const gchar *name;
412   GstElement *(*func) (const gchar * location);
413 }
414 Pipeline;
415
416 static Pipeline pipelines[] = {
417   {"wav", make_wav_pipeline},
418   {"playerbin", make_playerbin_pipeline},
419   {NULL, NULL},
420 };
421
422 #define NUM_TYPES       ((sizeof (pipelines) / sizeof (Pipeline)) - 1)
423
424 static void
425 print_usage (int argc, char **argv)
426 {
427   gint i;
428
429   g_print ("usage: %s <type> <filename>\n", argv[0]);
430   g_print ("   possible types:\n");
431
432   for (i = 0; i < NUM_TYPES; i++) {
433     g_print ("     %d = %s\n", i, pipelines[i].name);
434   }
435 }
436
437 int
438 main (int argc, char **argv)
439 {
440   GtkWidget *window, *hbox, *vbox, *play_button, *pause_button, *stop_button;
441   GstBus *bus;
442   GOptionEntry options[] = {
443     {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
444         "Verbose properties", NULL},
445     {NULL}
446   };
447   gint type;
448   GOptionContext *ctx;
449   GError *err = NULL;
450
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 ());
454
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);
459     exit (1);
460   }
461
462   GST_DEBUG_CATEGORY_INIT (scrubby_debug, "scrubby", 0, "scrubby example");
463
464   gtk_init (&argc, &argv);
465
466   if (argc != 3) {
467     print_usage (argc, argv);
468     exit (-1);
469   }
470
471   type = atoi (argv[1]);
472
473   if (type < 0 || type >= NUM_TYPES) {
474     print_usage (argc, argv);
475     exit (-1);
476   }
477
478   pipeline = pipelines[type].func (argv[2]);
479   g_assert (pipeline);
480
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");
488
489   adjustment =
490       GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, (gdouble) RANGE_PREC, 0.1,
491           1.0, 1.0));
492   hscale = gtk_scale_new (GTK_ORIENTATION_HORIZONTAL, adjustment);
493   gtk_scale_set_digits (GTK_SCALE (hscale), 2);
494
495   sadjustment =
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);
499
500   schanged_id =
501       g_signal_connect (shscale, "value_changed", G_CALLBACK (speed_cb),
502       pipeline);
503
504   g_signal_connect (hscale, "button_press_event", G_CALLBACK (start_seek),
505       pipeline);
506   g_signal_connect (hscale, "button_release_event", G_CALLBACK (stop_seek),
507       pipeline);
508   g_signal_connect (hscale, "format_value", G_CALLBACK (format_value),
509       pipeline);
510
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);
520
521   /* connect things ... */
522   g_signal_connect (G_OBJECT (play_button), "clicked", G_CALLBACK (play_cb),
523       pipeline);
524   g_signal_connect (G_OBJECT (pause_button), "clicked", G_CALLBACK (pause_cb),
525       pipeline);
526   g_signal_connect (G_OBJECT (stop_button), "clicked", G_CALLBACK (stop_cb),
527       pipeline);
528   g_signal_connect (G_OBJECT (window), "delete_event", gtk_main_quit, NULL);
529
530   /* show the gui. */
531   gtk_widget_show_all (window);
532
533   if (verbose) {
534     g_signal_connect (pipeline, "deep_notify",
535         G_CALLBACK (gst_object_default_deep_notify), NULL);
536   }
537   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
538   g_assert (bus);
539
540   bus_watch = gst_bus_add_watch_full (bus,
541       G_PRIORITY_HIGH, bus_message, pipeline, NULL);
542
543   gtk_main ();
544
545   g_print ("NULL pipeline\n");
546   gst_element_set_state (pipeline, GST_STATE_NULL);
547
548   gst_object_unref (bus);
549
550   g_print ("free pipeline\n");
551   gst_object_unref (pipeline);
552
553   return 0;
554 }