gst-indent
[platform/upstream/gst-plugins-good.git] / examples / indexing / indexmpeg.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include <string.h>
21 #include <gst/gst.h>
22
23 static gboolean verbose = FALSE;
24 static gboolean quiet = FALSE;
25
26 static void
27 entry_added (GstIndex * index, GstIndexEntry * entry)
28 {
29   switch (entry->type) {
30     case GST_INDEX_ENTRY_ID:
31       g_print ("id %d describes writer %s\n", entry->id,
32           GST_INDEX_ID_DESCRIPTION (entry));
33       break;
34     case GST_INDEX_ENTRY_FORMAT:
35       g_print ("%d: registered format %d for %s\n", entry->id,
36           GST_INDEX_FORMAT_FORMAT (entry), GST_INDEX_FORMAT_KEY (entry));
37       break;
38     case GST_INDEX_ENTRY_ASSOCIATION:
39     {
40       gint i;
41
42       g_print ("%p, %d: %08x ", entry, entry->id,
43           GST_INDEX_ASSOC_FLAGS (entry));
44       for (i = 0; i < GST_INDEX_NASSOCS (entry); i++) {
45         g_print ("%d %lld ", GST_INDEX_ASSOC_FORMAT (entry, i),
46             GST_INDEX_ASSOC_VALUE (entry, i));
47       }
48       g_print ("\n");
49       break;
50     }
51     default:
52       break;
53   }
54 }
55
56 typedef struct
57 {
58   const gchar *padname;
59   GstPad *target;
60   GstElement *bin;
61   GstElement *pipeline;
62   GstIndex *index;
63 } dyn_link;
64
65 static void
66 dynamic_link (GstPadTemplate * templ, GstPad * newpad, gpointer data)
67 {
68   dyn_link *link = (dyn_link *) data;
69
70   if (!strcmp (gst_pad_get_name (newpad), link->padname)) {
71     gst_element_set_state (link->pipeline, GST_STATE_PAUSED);
72     gst_bin_add (GST_BIN (link->pipeline), link->bin);
73     gst_pad_link (newpad, link->target);
74     gst_element_set_index (link->bin, link->index);
75     gst_element_set_state (link->pipeline, GST_STATE_PLAYING);
76   }
77 }
78
79 static void
80 setup_dynamic_linking (GstElement * pipeline,
81     GstElement * element,
82     const gchar * padname, GstPad * target, GstElement * bin, GstIndex * index)
83 {
84   dyn_link *link;
85
86   link = g_new0 (dyn_link, 1);
87   link->padname = g_strdup (padname);
88   link->target = target;
89   link->bin = bin;
90   link->pipeline = pipeline;
91   link->index = index;
92
93   g_signal_connect (G_OBJECT (element), "new_pad", G_CALLBACK (dynamic_link),
94       link);
95 }
96
97 static GstElement *
98 make_mpeg_systems_pipeline (const gchar * path, GstIndex * index)
99 {
100   GstElement *pipeline;
101   GstElement *src, *demux;
102
103   pipeline = gst_pipeline_new ("pipeline");
104
105   src = gst_element_factory_make ("filesrc", "src");
106   g_object_set (G_OBJECT (src), "location", path, NULL);
107
108   demux = gst_element_factory_make ("mpegdemux", "demux");
109
110   gst_bin_add (GST_BIN (pipeline), src);
111   gst_bin_add (GST_BIN (pipeline), demux);
112
113   if (index) {
114     gst_element_set_index (pipeline, index);
115   }
116
117   gst_element_link_pads (src, "src", demux, "sink");
118
119   return pipeline;
120 }
121
122 static GstElement *
123 make_mpeg_decoder_pipeline (const gchar * path, GstIndex * index)
124 {
125   GstElement *pipeline;
126   GstElement *src, *demux;
127   GstElement *video_bin, *audio_bin;
128   GstElement *video_decoder, *audio_decoder;
129
130   pipeline = gst_pipeline_new ("pipeline");
131
132   src = gst_element_factory_make ("filesrc", "src");
133   g_object_set (G_OBJECT (src), "location", path, NULL);
134
135   demux = gst_element_factory_make ("mpegdemux", "demux");
136
137   gst_bin_add (GST_BIN (pipeline), src);
138   gst_bin_add (GST_BIN (pipeline), demux);
139
140   gst_element_link_pads (src, "src", demux, "sink");
141
142   video_bin = gst_bin_new ("video_bin");
143   video_decoder = gst_element_factory_make ("mpeg2dec", "video_decoder");
144
145   gst_bin_add (GST_BIN (video_bin), video_decoder);
146
147   setup_dynamic_linking (pipeline, demux, "video_00",
148       gst_element_get_pad (video_decoder, "sink"), video_bin, index);
149
150   audio_bin = gst_bin_new ("audio_bin");
151   audio_decoder = gst_element_factory_make ("mad", "audio_decoder");
152
153   setup_dynamic_linking (pipeline, demux, "audio_00",
154       gst_element_get_pad (audio_decoder, "sink"), audio_bin, index);
155
156   gst_bin_add (GST_BIN (audio_bin), audio_decoder);
157
158   if (index) {
159     gst_element_set_index (pipeline, index);
160   }
161
162   return pipeline;
163 }
164
165 static void
166 print_progress (GstPad * pad)
167 {
168   gint i = 0;
169   gchar status[53];
170   GstFormat format;
171   gboolean res;
172   gint64 value;
173   gint percent = 0;
174
175   status[0] = '|';
176
177   format = GST_FORMAT_PERCENT;
178   res = gst_pad_query (pad, GST_QUERY_POSITION, &format, &value);
179   if (res) {
180     percent = value / (2 * GST_FORMAT_PERCENT_SCALE);
181   }
182
183   for (i = 0; i < percent; i++) {
184     status[i + 1] = '=';
185   }
186   for (i = percent; i < 50; i++) {
187     status[i + 1] = ' ';
188   }
189   status[51] = '|';
190   status[52] = 0;
191
192   g_print ("%s\r", status);
193 }
194
195 gint
196 main (gint argc, gchar * argv[])
197 {
198   GstElement *pipeline;
199   GstElement *src;
200   GstPad *pad;
201   GstIndex *index;
202   gint count = 0;
203   GstEvent *event;
204   gboolean res;
205   GstElement *sink;
206   struct poptOption options[] = {
207     {"verbose", 'v', POPT_ARG_NONE | POPT_ARGFLAG_STRIP, &verbose, 0,
208         "Print index entries", NULL},
209     {"quiet", 'q', POPT_ARG_NONE | POPT_ARGFLAG_STRIP, &quiet, 0,
210         "don't print progress bar", NULL},
211     POPT_TABLEEND
212   };
213
214   if (!gst_init_check_with_popt_table (&argc, &argv, options) || argc < 3) {
215     g_print ("usage: %s [-v] <type> <filename>  \n"
216         "  type can be: 0 mpeg_systems\n"
217         "               1 mpeg_decoder\n"
218         "  -v : report added index entries\n"
219         "  -q : don't print progress\n", argv[0]);
220     return -1;
221   }
222
223   /* create index that elements can fill */
224   index = gst_index_factory_make ("memindex");
225   if (index) {
226     if (verbose)
227       g_signal_connect (G_OBJECT (index), "entry_added",
228           G_CALLBACK (entry_added), NULL);
229
230     g_object_set (G_OBJECT (index), "resolver", 1, NULL);
231   }
232
233   /* construct pipeline */
234   switch (atoi (argv[1])) {
235     case 0:
236       pipeline = make_mpeg_systems_pipeline (argv[2], index);
237       break;
238     case 1:
239       pipeline = make_mpeg_decoder_pipeline (argv[2], index);
240       break;
241     default:
242       g_print ("unknown type %d\n", atoi (argv[1]));
243       return -1;
244   }
245
246   /* setup some default info/error handlers */
247   g_signal_connect (G_OBJECT (pipeline), "deep_notify",
248       G_CALLBACK (gst_element_default_deep_notify), NULL);
249   g_signal_connect (G_OBJECT (pipeline), "error",
250       G_CALLBACK (gst_element_default_error), NULL);
251
252   /* get a pad to perform progress reporting on */
253   src = gst_bin_get_by_name (GST_BIN (pipeline), "src");
254   pad = gst_element_get_pad (src, "src");
255
256   /* prepare for iteration */
257   gst_element_set_state (pipeline, GST_STATE_PLAYING);
258
259   g_print ("indexing %s...\n", argv[2]);
260   /* run through the complete stream to let it generate an index */
261   while (gst_bin_iterate (GST_BIN (pipeline))) {
262     if (!quiet && (count % 1000 == 0)) {
263       print_progress (pad);
264     }
265     count++;
266   }
267   g_print ("\n");
268
269   /* bring to ready to restart the pipeline */
270   gst_element_set_state (pipeline, GST_STATE_READY);
271   gst_element_set_state (pipeline, GST_STATE_PAUSED);
272
273   if (index)
274     GST_FLAG_UNSET (index, GST_INDEX_WRITABLE);
275
276   src = gst_bin_get_by_name (GST_BIN (pipeline), "video_decoder");
277
278   {
279     gint id;
280     GstIndexEntry *entry;
281     gint64 result;
282     gint total_tm;
283
284     gst_index_get_writer_id (index, GST_OBJECT (src), &id);
285
286     entry = gst_index_get_assoc_entry (index, id, GST_INDEX_LOOKUP_BEFORE, 0,
287         GST_FORMAT_TIME, G_MAXINT64);
288     g_assert (entry);
289     gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &result);
290     total_tm = result * 60 / GST_SECOND;
291     g_print ("total time = %.2fs\n", total_tm / 60.0);
292   }
293
294   pad = gst_element_get_pad (src, "src");
295   sink = gst_element_factory_make ("fakesink", "sink");
296   gst_element_link_pads (src, "src", sink, "sink");
297   gst_bin_add (GST_BIN (pipeline), sink);
298
299   g_print ("seeking %s...\n", argv[2]);
300   event = gst_event_new_seek (GST_FORMAT_TIME |
301       GST_SEEK_METHOD_SET | GST_SEEK_FLAG_FLUSH, 5 * GST_SECOND);
302
303   res = gst_pad_send_event (pad, event);
304   if (!res) {
305     g_warning ("seek failed");
306   }
307
308   gst_element_set_state (pipeline, GST_STATE_PLAYING);
309   count = 0;
310   while (gst_bin_iterate (GST_BIN (pipeline))) {
311     if (!quiet && (count % 1000 == 0)) {
312       print_progress (pad);
313     }
314     count++;
315   }
316
317   gst_element_set_state (pipeline, GST_STATE_NULL);
318
319   return 1;
320 }