29b89280b66fed9a338ac38801b6f947012f1601
[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),
37                       GST_INDEX_FORMAT_KEY (entry));
38       break;
39     case GST_INDEX_ENTRY_ASSOCIATION:
40     {
41       gint i;
42
43       g_print ("%p, %d: %08x ", entry, entry->id, 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, 
83                           GstPad *target, 
84                           GstElement *bin,
85                           GstIndex *index)
86 {
87   dyn_link *link;
88
89   link = g_new0 (dyn_link, 1);
90   link->padname      = g_strdup (padname);
91   link->target       = target;
92   link->bin          = bin;
93   link->pipeline     = pipeline;
94   link->index        = index;
95
96   g_signal_connect (G_OBJECT (element), "new_pad", G_CALLBACK (dynamic_link), link);
97 }
98
99 static GstElement*
100 make_mpeg_systems_pipeline (const gchar *path, GstIndex *index)
101 {
102   GstElement *pipeline;
103   GstElement *src, *demux;
104
105   pipeline = gst_pipeline_new ("pipeline");
106
107   src = gst_element_factory_make ("filesrc", "src");
108   g_object_set (G_OBJECT (src), "location", path, NULL);
109
110   demux = gst_element_factory_make ("mpegdemux", "demux");
111
112   gst_bin_add (GST_BIN (pipeline), src);
113   gst_bin_add (GST_BIN (pipeline), demux);
114
115   if (index) {
116     gst_element_set_index (pipeline, index);
117   }
118
119   gst_element_link_pads (src, "src", demux, "sink");
120   
121   return pipeline;
122 }
123
124 static GstElement*
125 make_mpeg_decoder_pipeline (const gchar *path, GstIndex *index)
126 {
127   GstElement *pipeline;
128   GstElement *src, *demux;
129   GstElement *video_bin, *audio_bin;
130   GstElement *video_decoder, *audio_decoder;
131
132   pipeline = gst_pipeline_new ("pipeline");
133
134   src = gst_element_factory_make ("filesrc", "src");
135   g_object_set (G_OBJECT (src), "location", path, NULL);
136
137   demux = gst_element_factory_make ("mpegdemux", "demux");
138
139   gst_bin_add (GST_BIN (pipeline), src);
140   gst_bin_add (GST_BIN (pipeline), demux);
141
142   gst_element_link_pads (src, "src", demux, "sink");
143
144   video_bin = gst_bin_new ("video_bin");
145   video_decoder = gst_element_factory_make ("mpeg2dec", "video_decoder");
146
147   gst_bin_add (GST_BIN (video_bin), video_decoder);
148   
149   setup_dynamic_linking (pipeline, demux, "video_00", 
150                          gst_element_get_pad (video_decoder, "sink"),
151                          video_bin, index);
152
153   audio_bin = gst_bin_new ("audio_bin");
154   audio_decoder = gst_element_factory_make ("mad", "audio_decoder");
155
156   setup_dynamic_linking (pipeline, demux, "audio_00", 
157                          gst_element_get_pad (audio_decoder, "sink"),
158                          audio_bin, index);
159
160   gst_bin_add (GST_BIN (audio_bin), audio_decoder);
161
162   if (index) {
163     gst_element_set_index (pipeline, index);
164   }
165   
166   return pipeline;
167 }
168
169 static void
170 print_progress (GstPad *pad)
171 {
172   gint i = 0;
173   gchar status[53];
174   GstFormat format;
175   gboolean res;
176   gint64 value;
177   gint percent = 0;
178
179   status[0] = '|';
180
181   format = GST_FORMAT_PERCENT;
182   res = gst_pad_query (pad, GST_QUERY_POSITION, &format, &value);
183   if (res) {
184     percent = value / (2 * GST_FORMAT_PERCENT_SCALE); 
185   }
186   
187   for (i = 0; i < percent; i++) {
188     status[i+1] = '=';
189   }
190   for (i = percent; i < 50; i++) {
191     status[i+1] = ' ';
192   }
193   status[51] = '|';
194   status[52] = 0;
195
196   g_print ("%s\r", status);
197 }
198
199 gint 
200 main (gint argc, gchar *argv[])
201 {
202   GstElement *pipeline;
203   GstElement *src;
204   GstPad *pad;
205   GstIndex *index;
206   gint count = 0;
207   GstEvent *event;
208   gboolean res;
209   GstElement *sink;
210   struct poptOption options[] = {
211     { "verbose",  'v',  POPT_ARG_NONE|POPT_ARGFLAG_STRIP,   &verbose,   0,
212       "Print index entries", NULL},
213     { "quiet",  'q',  POPT_ARG_NONE|POPT_ARGFLAG_STRIP,   &quiet,   0,
214       "don't print progress bar", NULL},
215       POPT_TABLEEND
216     };
217
218   if (!gst_init_check_with_popt_table (&argc, &argv, options) || argc < 3) {
219     g_print ("usage: %s [-v] <type> <filename>  \n" 
220              "  type can be: 0 mpeg_systems\n"
221              "               1 mpeg_decoder\n"
222              "  -v : report added index entries\n"
223              "  -q : don't print progress\n" , argv[0]);
224     return -1;
225   }
226
227   /* create index that elements can fill */
228   index = gst_index_factory_make ("memindex");
229   if (index) {
230     if (verbose) 
231       g_signal_connect (G_OBJECT (index), "entry_added", G_CALLBACK (entry_added), NULL);
232
233     g_object_set (G_OBJECT (index), "resolver", 1, NULL);
234   }
235
236   /* construct pipeline */
237   switch (atoi (argv[1])) {
238     case 0:
239       pipeline = make_mpeg_systems_pipeline (argv[2], index);
240       break;
241     case 1:
242       pipeline = make_mpeg_decoder_pipeline (argv[2], index);
243       break;
244     default:
245       g_print ("unknown type %d\n", atoi (argv[1]));
246       return -1;
247   }
248
249   /* setup some default info/error handlers */
250   g_signal_connect (G_OBJECT (pipeline), "deep_notify", 
251                     G_CALLBACK (gst_element_default_deep_notify), NULL);
252   g_signal_connect (G_OBJECT (pipeline), "error", 
253                     G_CALLBACK (gst_element_default_error), NULL);
254
255   /* get a pad to perform progress reporting on */
256   src = gst_bin_get_by_name (GST_BIN (pipeline), "src");
257   pad = gst_element_get_pad (src, "src");
258
259   /* prepare for iteration */
260   gst_element_set_state (pipeline, GST_STATE_PLAYING);
261
262   g_print ("indexing %s...\n", argv [2]);
263   /* run through the complete stream to let it generate an index */
264   while (gst_bin_iterate (GST_BIN (pipeline))) {
265     if (!quiet && (count % 1000 == 0)) {
266       print_progress (pad);
267     }
268     count++;
269   }
270   g_print ("\n");
271
272   /* bring to ready to restart the pipeline */
273   gst_element_set_state (pipeline, GST_STATE_READY);
274   gst_element_set_state (pipeline, GST_STATE_PAUSED);
275
276   if (index)
277     GST_FLAG_UNSET (index, GST_INDEX_WRITABLE);
278
279   src = gst_bin_get_by_name (GST_BIN (pipeline), "video_decoder");
280
281   {
282     gint id;
283     GstIndexEntry *entry;
284     gint64 result;
285     gint total_tm;
286
287     gst_index_get_writer_id (index, GST_OBJECT (src), &id);
288     
289     entry = gst_index_get_assoc_entry (index, id, GST_INDEX_LOOKUP_BEFORE, 0,
290                                        GST_FORMAT_TIME, G_MAXINT64);
291     g_assert (entry);
292     gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &result);
293     total_tm = result * 60 / GST_SECOND;
294     g_print ("total time = %.2fs\n", total_tm / 60.0);
295   }
296   
297   pad = gst_element_get_pad (src, "src");
298   sink = gst_element_factory_make ("fakesink", "sink");
299   gst_element_link_pads (src, "src", sink, "sink");
300   gst_bin_add (GST_BIN (pipeline), sink);
301
302   g_print ("seeking %s...\n", argv [2]);
303   event = gst_event_new_seek (GST_FORMAT_TIME |
304                               GST_SEEK_METHOD_SET |
305                               GST_SEEK_FLAG_FLUSH, 5 * GST_SECOND);
306
307   res = gst_pad_send_event (pad, event);
308   if (!res) {
309     g_warning ("seek failed");
310   }
311
312   gst_element_set_state (pipeline, GST_STATE_PLAYING);
313   count = 0;
314   while (gst_bin_iterate (GST_BIN (pipeline))) {
315     if (!quiet && (count % 1000 == 0)) {
316       print_progress (pad);
317     }
318     count++;
319   }
320
321   gst_element_set_state (pipeline, GST_STATE_NULL);
322
323   return 1;
324 }
325