- Cleanups
[platform/upstream/gstreamer.git] / tests / old / examples / seek / cdplayer.c
1 #include <stdlib.h>
2 #include <glib.h>
3 #include <gtk/gtk.h>
4 #include <gst/gst.h>
5 #include <string.h>
6
7 static GList *seekable_elements = NULL;
8
9 static GstElement *pipeline;
10 static GtkAdjustment *adjustment;
11 static gboolean stats = FALSE;
12 static guint64 duration;
13
14 static guint update_id;
15
16 #define UPDATE_INTERVAL 500
17
18 static GstElement*
19 make_cdaudio_pipeline (void) 
20 {
21   GstElement *cdaudio;
22   
23   cdaudio = gst_element_factory_make ("cdaudio", "cdaudio");
24   g_assert (cdaudio != NULL);
25
26   seekable_elements = g_list_prepend (seekable_elements, cdaudio);
27
28   return cdaudio;
29 }
30
31 static gchar*
32 format_value (GtkScale *scale,
33               gdouble   value)
34 {
35   gint64 real;
36   gint64 seconds;
37   gint64 subseconds;
38
39   real = value * duration / 100;
40   seconds = (gint64) real / GST_SECOND;
41   subseconds = (gint64) real / (GST_SECOND / 100);
42
43   return g_strdup_printf ("%02lld:%02lld:%02lld",
44                           seconds/60, 
45                           seconds%60, 
46                           subseconds%100);
47 }
48
49 typedef struct
50 {
51   const gchar *name;
52   const GstFormat format;
53 } seek_format;
54
55 static seek_format seek_formats[] = 
56 {
57   { "tim",  GST_FORMAT_TIME    },
58   { "byt",  GST_FORMAT_BYTES   },
59   { "unt",  GST_FORMAT_UNITS   },
60   { "buf",  GST_FORMAT_BUFFERS },
61   { "def",  GST_FORMAT_DEFAULT },
62   { NULL, 0 }, 
63 };
64
65
66 G_GNUC_UNUSED static void
67 query_durations ()
68 {
69   GList *walk = seekable_elements;
70
71   while (walk) {
72     GstElement *element = GST_ELEMENT (walk->data);
73     gint i = 0;
74
75     g_print ("durations %8.8s: ", GST_ELEMENT_NAME (element));
76     while (seek_formats[i].name) {
77       gboolean res;
78       gint64 value;
79       GstFormat format;
80
81       format = seek_formats[i].format;
82       res = gst_element_query (element, GST_QUERY_TOTAL, &format, &value);
83       if (res) {
84         g_print ("%s %13lld | ", seek_formats[i].name, value);
85       }
86       else {
87         g_print ("%s %13.13s | ", seek_formats[i].name, "*NA*");
88       }
89       i++;
90     }
91     g_print (" %s\n", GST_ELEMENT_NAME (element));
92     walk = g_list_next (walk);
93   }
94 }
95
96 G_GNUC_UNUSED static void
97 query_positions ()
98 {
99   GList *walk = seekable_elements;
100
101   while (walk) {
102     GstElement *element = GST_ELEMENT (walk->data);
103     gint i = 0;
104
105     g_print ("positions %8.8s: ", GST_ELEMENT_NAME (element));
106     while (seek_formats[i].name) {
107       gboolean res;
108       gint64 value;
109       GstFormat format;
110
111       format = seek_formats[i].format;
112       res = gst_element_query (element, GST_QUERY_POSITION, &format, &value);
113       if (res) {
114         g_print ("%s %13lld | ", seek_formats[i].name, value);
115       }
116       else {
117         g_print ("%s %13.13s | ", seek_formats[i].name, "*NA*");
118       }
119       i++;
120     }
121     g_print (" %s\n", GST_ELEMENT_NAME (element));
122     walk = g_list_next (walk);
123   }
124 }
125
126 static gboolean
127 update_scale (gpointer data) 
128 {
129   GstClock *clock;
130   guint64 position = 0;
131   GstFormat format = GST_FORMAT_TIME;
132
133   duration = 0;
134   clock = gst_bin_get_clock (GST_BIN (pipeline));
135
136   if (seekable_elements) {
137     GstElement *element = GST_ELEMENT (seekable_elements->data);
138     gst_element_query (element, GST_QUERY_TOTAL, &format, &duration);
139   }
140   if (clock)
141     position = gst_clock_get_time (clock);
142
143   if (stats) {
144     if (clock)
145       g_print ("clock:                  %13llu  (%s)\n", position, gst_object_get_name (GST_OBJECT (clock)));
146     query_durations ();
147     query_positions ();
148   }
149   if (duration > 0) {
150     gtk_adjustment_set_value (adjustment, position * 100.0 / duration);
151   }
152
153   return TRUE;
154 }
155
156 static gboolean
157 iterate (gpointer data)
158 {
159   gboolean res = TRUE;
160
161   g_print ("iterate\n");
162   res = gst_bin_iterate (GST_BIN (data));
163   if (!res) {
164     gtk_timeout_remove (update_id);
165     g_print ("stopping iterations\n");
166   }
167   return res;
168 }
169
170 static gboolean
171 start_seek (GtkWidget *widget, GdkEventButton *event, gpointer user_data)
172 {
173   gst_element_set_state (pipeline, GST_STATE_PAUSED);
174   gtk_timeout_remove (update_id);
175
176   return FALSE;
177 }
178
179 static gboolean
180 stop_seek (GtkWidget *widget, GdkEventButton *event, gpointer user_data)
181 {
182   gint64 real = gtk_range_get_value (GTK_RANGE (widget)) * duration / 100;
183   gboolean res;
184   GstEvent *s_event;
185   GList *walk = seekable_elements;
186
187   while (walk) {
188     GstElement *seekable = GST_ELEMENT (walk->data);
189
190     g_print ("seek to %lld on element %s\n", real, GST_ELEMENT_NAME (seekable));
191     s_event = gst_event_new_seek (GST_FORMAT_TIME |
192                                   GST_SEEK_METHOD_SET |
193                                   GST_SEEK_FLAG_FLUSH, real);
194
195     res = gst_element_send_event (seekable, s_event);
196
197     walk = g_list_next (walk);
198   }
199
200   gst_element_set_state (pipeline, GST_STATE_PLAYING);
201   if (!GST_FLAG_IS_SET (pipeline, GST_BIN_SELF_SCHEDULABLE))
202     gtk_idle_add ((GtkFunction) iterate, pipeline);
203   update_id = gtk_timeout_add (UPDATE_INTERVAL, (GtkFunction) update_scale, pipeline);
204
205   return FALSE;
206 }
207
208 static void
209 play_cb (GtkButton * button, gpointer data)
210 {
211   if (gst_element_get_state (pipeline) != GST_STATE_PLAYING) {
212     gst_element_set_state (pipeline, GST_STATE_PLAYING);
213     if (!GST_FLAG_IS_SET (pipeline, GST_BIN_SELF_SCHEDULABLE))
214       gtk_idle_add ((GtkFunction) iterate, pipeline);
215     update_id = gtk_timeout_add (UPDATE_INTERVAL, (GtkFunction) update_scale, pipeline);
216   }
217 }
218
219 static void
220 pause_cb (GtkButton * button, gpointer data)
221 {
222   if (gst_element_get_state (pipeline) != GST_STATE_PAUSED) {
223     gst_element_set_state (pipeline, GST_STATE_PAUSED);
224     gtk_timeout_remove (update_id);
225   }
226 }
227
228 static void
229 stop_cb (GtkButton * button, gpointer data)
230 {
231   if (gst_element_get_state (pipeline) != GST_STATE_READY) {
232     gst_element_set_state (pipeline, GST_STATE_READY);
233     gtk_timeout_remove (update_id);
234   }
235 }
236
237 int
238 main (int argc, char **argv)
239 {
240   GtkWidget *window, *hbox, *vbox, 
241             *play_button, *pause_button, *stop_button, 
242             *hscale;
243   struct poptOption options[] = {
244     {"stats",  's',  POPT_ARG_NONE|POPT_ARGFLAG_STRIP,   &stats,   0,
245          "Show element stats", NULL},
246      POPT_TABLEEND
247     };
248
249   gst_init_with_popt_table (&argc, &argv, options);
250   gtk_init (&argc, &argv);
251
252   pipeline = make_cdaudio_pipeline ();
253
254   g_signal_connect (pipeline, "deep_notify", G_CALLBACK (gst_element_default_deep_notify), NULL);
255   g_signal_connect (pipeline, "error", G_CALLBACK (gst_element_default_error), NULL);
256
257   /* initialize gui elements ... */
258   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
259   hbox = gtk_hbox_new (FALSE, 0);
260   vbox = gtk_vbox_new (FALSE, 0);
261   play_button = gtk_button_new_with_label ("play");
262   pause_button = gtk_button_new_with_label ("pause");
263   stop_button = gtk_button_new_with_label ("stop");
264
265   adjustment = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.00, 100.0, 0.1, 1.0, 1.0));
266   hscale = gtk_hscale_new (adjustment);
267   gtk_scale_set_digits (GTK_SCALE (hscale), 2);
268   gtk_range_set_update_policy (GTK_RANGE (hscale), GTK_UPDATE_CONTINUOUS);
269
270   gtk_signal_connect(GTK_OBJECT(hscale),
271                              "button_press_event", G_CALLBACK (start_seek), pipeline);
272   gtk_signal_connect(GTK_OBJECT(hscale),
273                              "button_release_event", G_CALLBACK (stop_seek), pipeline);
274   gtk_signal_connect(GTK_OBJECT(hscale),
275                              "format_value", G_CALLBACK (format_value), pipeline);
276
277   /* do the packing stuff ... */
278   gtk_window_set_default_size (GTK_WINDOW (window), 96, 96);
279   gtk_container_add (GTK_CONTAINER (window), vbox);
280   gtk_container_add (GTK_CONTAINER (vbox), hbox);
281   gtk_box_pack_start (GTK_BOX (hbox), play_button, FALSE, FALSE, 2);
282   gtk_box_pack_start (GTK_BOX (hbox), pause_button, FALSE, FALSE, 2);
283   gtk_box_pack_start (GTK_BOX (hbox), stop_button, FALSE, FALSE, 2);
284   gtk_box_pack_start (GTK_BOX (vbox), hscale, TRUE, TRUE, 2);
285
286   /* connect things ... */
287   g_signal_connect (G_OBJECT (play_button), "clicked", G_CALLBACK (play_cb), pipeline);
288   g_signal_connect (G_OBJECT (pause_button), "clicked", G_CALLBACK (pause_cb), pipeline);
289   g_signal_connect (G_OBJECT (stop_button), "clicked", G_CALLBACK (stop_cb), pipeline);
290   g_signal_connect (G_OBJECT (window), "delete_event", gtk_main_quit, NULL);
291
292   /* show the gui. */
293   gtk_widget_show_all (window);
294
295   gtk_main ();
296
297   gst_element_set_state (pipeline, GST_STATE_NULL);
298
299   return 0;
300 }