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