summaryrefslogtreecommitdiff
path: root/examples/seeking/cdparanoia.c
blob: 646c121c10359c0f29c93eaf7763a7741b1a762e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <stdlib.h>
#include <gst/gst.h>
#include <string.h>

static void
get_position_info (GstElement *cdparanoia)
{
  GstFormat track_format;
  const GstFormat *formats;
  GstPad *pad;

  track_format = gst_format_get_by_nick ("track");
  g_assert (track_format != 0);

  pad = gst_element_get_pad (cdparanoia, "src");
  formats = gst_pad_get_formats (pad);

  while (*formats) {
    const GstFormatDefinition *definition;
    GstFormat format;
    gint64 position;
    gboolean res;

    definition = gst_format_get_details (*formats);

    format = *formats;
    res = gst_pad_query (pad, GST_QUERY_POSITION,
		         &format, &position);

    if (format == GST_FORMAT_TIME) {
      position /= GST_SECOND;
      g_print ("%s: %lld:%02lld", definition->nick, position/60, position%60);
    }
    else {
      g_print ("%s: %lld", definition->nick, position);
    }

    formats++;
    if (*formats) {
      g_print (", ");
    }
  }
  g_print ("\r");
}

static void
get_track_info (GstElement *cdparanoia)
{
  GstFormat track_format;
  gint64 total_tracks = 0, total_time = 0;
  GstPad *pad;
  const GstFormat *formats;
  gint i;
  gint64 time_count = 0;
  
  track_format = gst_format_get_by_nick ("track");
  g_assert (track_format != 0);

  pad = gst_element_get_pad (cdparanoia, "src");
  formats = gst_pad_get_formats (pad);

  /* we loop over all supported formats and report the total
   * number of them */
  while (*formats) {
    const GstFormatDefinition *definition;
    gint64 total;
    GstFormat format;
    gboolean res;
    
    definition = gst_format_get_details (*formats);

    format = *formats;
    res = gst_pad_query (pad, GST_QUERY_TOTAL,
		         &format, &total);
    if (res) {
      if (format == GST_FORMAT_TIME) {
	total /= GST_SECOND;
        g_print ("%s total: %lld:%02lld\n", definition->nick, total/60, total%60);
      }
      else
        g_print ("%s total: %lld\n", definition->nick, total);

      if (format == track_format)
	total_tracks = total;
      else if (format == GST_FORMAT_TIME)
	total_time = total;
    }
    else
      g_print ("failed to get %s total\n", definition->nick);

    formats++;
  }

  /* then we loop over all the tracks to get more info.
   * since pad_convert always works from 0, the time from track 1 needs
   * to be substracted from track 2 */
  for (i = 0; i <= total_tracks; i++) {
    gint64 time;
    gboolean res;

    if (i < total_tracks) {
      GstFormat format;

      format = GST_FORMAT_TIME;
      res = gst_pad_convert (pad, track_format, i,
		             &format, &time);
      time /= GST_SECOND;
    }
    else {
      time = total_time;
      res = TRUE;
    }

    if (res) {
      /* for the first track (i==0) we wait until we have the
       * time of the next track */
      if (i > 0) {
	gint64 length = time - time_count;

        g_print ("track %d: %lld:%02lld -> %lld:%02lld, length: %lld:%02lld\n",
		 i-1,
		 time_count / 60, time_count % 60,
		 time / 60, time % 60,
		 length / 60, length % 60);
      }
    }
    else {
      g_print ("could not get time for track %d\n", i);
    }

    time_count = time;
  }
}

int
main (int argc, char **argv)
{
  GstElement *pipeline;
  GstElement *cdparanoia;
  GstElement *osssink;
  GstPad *pad;
  GstFormat track_format;
  GstEvent *event;
  gint count;
  gboolean res;

  gst_init (&argc, &argv);

  pipeline = gst_pipeline_new ("pipeline");

  cdparanoia = gst_element_factory_make ("cdparanoia", "cdparanoia");
  g_assert (cdparanoia);
  g_object_set (G_OBJECT (cdparanoia), "paranoia_mode", 0, NULL);

  osssink = gst_element_factory_make ("osssink", "osssink");
  g_assert (osssink);

  gst_bin_add (GST_BIN (pipeline), cdparanoia);
  gst_bin_add (GST_BIN (pipeline), osssink);

  gst_element_link_pads (cdparanoia, "src", osssink, "sink");

  g_signal_connect (G_OBJECT (pipeline), "deep_notify",
		  G_CALLBACK (gst_element_default_deep_notify), NULL);

  gst_element_set_state (pipeline, GST_STATE_PAUSED);

  /* now we go into probe mode */
  get_track_info (cdparanoia);

  track_format = gst_format_get_by_nick ("track");
  g_assert (track_format != 0);

  pad = gst_element_get_pad (cdparanoia, "src");
  g_assert (pad);

  g_print ("playing from track 3\n");
  /* seek to track3 */
  event = gst_event_new_seek (track_format |
		              GST_SEEK_METHOD_SET |
			      GST_SEEK_FLAG_FLUSH,
			      3);

  res = gst_pad_send_event (pad, event);
  if (!res)
    g_warning ("seek failed");

  gst_element_set_state (pipeline, GST_STATE_PLAYING);

  count = 0;
  while (gst_bin_iterate (GST_BIN (pipeline))) {
    get_position_info (cdparanoia);
    if (count++ > 500)
      break;
  }
  gst_element_set_state (pipeline, GST_STATE_PAUSED);

  g_print ("\nplaying from second 25 to second 29\n");
  /* seek to some seconds */
  event = gst_event_new_segment_seek (GST_FORMAT_TIME |
		                      GST_SEEK_METHOD_SET |
			              GST_SEEK_FLAG_FLUSH,
			              25 * GST_SECOND, 29 * GST_SECOND);
  res = gst_pad_send_event (pad, event);
  if (!res)
    g_warning ("seek failed");

  gst_element_set_state (pipeline, GST_STATE_PLAYING);

  while (gst_bin_iterate (GST_BIN (pipeline))) {
    get_position_info (cdparanoia);
  }
  g_print ("\n");

  /* shutdown everything again */
  gst_element_set_state (pipeline, GST_STATE_NULL);

  return 0;
}