Added gst-plugins-base-subtitles0.10-0.10.34 for Meego Harmattan 1.2
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / tests / old / examples / seek / cdparanoia.c
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4 #include <stdlib.h>
5 #include <gst/gst.h>
6 #include <string.h>
7
8 static void
9 get_position_info (GstElement * cdparanoia)
10 {
11   GstFormat track_format;
12   const GstFormat *formats;
13   GstPad *pad;
14
15   track_format = gst_format_get_by_nick ("track");
16   g_assert (track_format != 0);
17
18   pad = gst_element_get_pad (cdparanoia, "src");
19   formats = gst_pad_get_formats (pad);
20
21   while (*formats) {
22     const GstFormatDefinition *definition;
23     GstFormat format;
24     gint64 position;
25     gboolean res;
26
27     definition = gst_format_get_details (*formats);
28
29     format = *formats;
30     res = gst_pad_query (pad, GST_QUERY_POSITION, &format, &position);
31
32     if (format == GST_FORMAT_TIME) {
33       position /= GST_SECOND;
34       g_print ("%s: %" G_GINT64_FORMAT ":%02" G_GINT64_FORMAT,
35           definition->nick, position / 60, position % 60);
36     } else {
37       g_print ("%s: %" G_GINT64_FORMAT, definition->nick, position);
38     }
39
40     formats++;
41     if (*formats) {
42       g_print (", ");
43     }
44   }
45   g_print ("\r");
46 }
47
48 static void
49 get_track_info (GstElement * cdparanoia)
50 {
51   GstFormat track_format;
52   gint64 total_tracks = 0, total_time = 0;
53   GstPad *pad;
54   const GstFormat *formats;
55   gint i;
56   gint64 time_count = 0;
57
58   track_format = gst_format_get_by_nick ("track");
59   g_assert (track_format != 0);
60
61   pad = gst_element_get_pad (cdparanoia, "src");
62   formats = gst_pad_get_formats (pad);
63
64   /* we loop over all supported formats and report the total
65    * number of them */
66   while (*formats) {
67     const GstFormatDefinition *definition;
68     gint64 total;
69     GstFormat format;
70     gboolean res;
71
72     definition = gst_format_get_details (*formats);
73
74     format = *formats;
75     res = gst_pad_query (pad, GST_QUERY_TOTAL, &format, &total);
76     if (res) {
77       if (format == GST_FORMAT_TIME) {
78         total /= GST_SECOND;
79         g_print ("%s total: %" G_GINT64_FORMAT ":%02" G_GINT64_FORMAT "\n",
80             definition->nick, total / 60, total % 60);
81       } else
82         g_print ("%s total: %" G_GINT64_FORMAT "\n", definition->nick, total);
83
84       if (format == track_format)
85         total_tracks = total;
86       else if (format == GST_FORMAT_TIME)
87         total_time = total;
88     } else
89       g_print ("failed to get %s total\n", definition->nick);
90
91     formats++;
92   }
93
94   /* then we loop over all the tracks to get more info.
95    * since pad_convert always works from 0, the time from track 1 needs
96    * to be substracted from track 2 */
97   for (i = 0; i <= total_tracks; i++) {
98     gint64 time;
99     gboolean res;
100
101     if (i < total_tracks) {
102       GstFormat format;
103
104       format = GST_FORMAT_TIME;
105       res = gst_pad_convert (pad, track_format, i, &format, &time);
106       time /= GST_SECOND;
107     } else {
108       time = total_time;
109       res = TRUE;
110     }
111
112     if (res) {
113       /* for the first track (i==0) we wait until we have the
114        * time of the next track */
115       if (i > 0) {
116         gint64 length = time - time_count;
117
118         g_print ("track %d: %" G_GINT64_FORMAT ":%02" G_GINT64_FORMAT
119             " -> %" G_GINT64_FORMAT ":%02" G_GINT64_FORMAT ", length: %"
120             G_GINT64_FORMAT ":%02" G_GINT64_FORMAT "\n",
121             i - 1,
122             time_count / 60, time_count % 60,
123             time / 60, time % 60, length / 60, length % 60);
124       }
125     } else {
126       g_print ("could not get time for track %d\n", i);
127     }
128
129     time_count = time;
130   }
131 }
132
133 int
134 main (int argc, char **argv)
135 {
136   GstElement *pipeline;
137   GstElement *cdparanoia;
138   GstElement *audiosink;
139   GstPad *pad;
140   GstFormat track_format;
141   GstEvent *event;
142   gint count;
143   gboolean res;
144
145   gst_init (&argc, &argv);
146
147   pipeline = gst_pipeline_new ("pipeline");
148
149   cdparanoia = gst_element_factory_make ("cdparanoia", "cdparanoia");
150   g_assert (cdparanoia);
151   g_object_set (G_OBJECT (cdparanoia), "paranoia_mode", 0, NULL);
152
153   audiosink = gst_element_factory_make (DEFAULT_AUDIOSINK, DEFAULT_AUDIOSINK);
154   g_assert (audiosink);
155
156   gst_bin_add (GST_BIN (pipeline), cdparanoia);
157   gst_bin_add (GST_BIN (pipeline), audiosink);
158
159   gst_element_link_pads (cdparanoia, "src", audiosink, "sink");
160
161   g_signal_connect (G_OBJECT (pipeline), "deep_notify",
162       G_CALLBACK (gst_object_default_deep_notify), NULL);
163
164   gst_element_set_state (pipeline, GST_STATE_PAUSED);
165
166   /* now we go into probe mode */
167   get_track_info (cdparanoia);
168
169   track_format = gst_format_get_by_nick ("track");
170   g_assert (track_format != 0);
171
172   pad = gst_element_get_pad (cdparanoia, "src");
173   g_assert (pad);
174
175   g_print ("playing from track 3\n");
176   /* seek to track3 */
177   event = gst_event_new_seek (track_format |
178       GST_SEEK_METHOD_SET | GST_SEEK_FLAG_FLUSH, 3);
179
180   res = gst_pad_send_event (pad, event);
181   if (!res)
182     g_warning ("seek failed");
183
184   gst_element_set_state (pipeline, GST_STATE_PLAYING);
185
186   count = 0;
187   while (count++ < 500) {
188     get_position_info (cdparanoia);
189     g_usleep (G_USEC_PER_SEC / 2);
190   }
191   gst_element_set_state (pipeline, GST_STATE_PAUSED);
192
193   g_print ("\nplaying from second 25 to second 29\n");
194   /* seek to some seconds */
195   event = gst_event_new_segment_seek (GST_FORMAT_TIME |
196       GST_SEEK_METHOD_SET |
197       GST_SEEK_FLAG_FLUSH, 25 * GST_SECOND, 29 * GST_SECOND);
198   res = gst_pad_send_event (pad, event);
199   if (!res)
200     g_warning ("seek failed");
201
202   gst_element_set_state (pipeline, GST_STATE_PLAYING);
203
204   count = 0;
205   while (count++ < 500) {
206     get_position_info (cdparanoia);
207     g_usleep (G_USEC_PER_SEC / 2);
208   }
209   g_print ("\n");
210
211   /* shutdown everything again */
212   gst_element_set_state (pipeline, GST_STATE_NULL);
213
214   return 0;
215 }