gst-discoverer: The 'ready' signal was renamed to 'finished'
[platform/upstream/gstreamer.git] / tools / gst-discoverer.c
1 /* GStreamer
2  * Copyright (C) 2009 Edward Hervey <edward.hervey@collabora.co.uk>
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 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <stdlib.h>
25 #include <glib.h>
26 #include <gst/gst.h>
27 #include <gst/pbutils/pbutils.h>
28
29 static gboolean async = FALSE;
30 static gboolean silent = FALSE;
31 static gboolean verbose = FALSE;
32
33 typedef struct
34 {
35   GstDiscoverer *dc;
36   int argc;
37   char **argv;
38 } PrivStruct;
39
40 #define my_g_string_append_printf(str, format, ...) \
41   g_string_append_printf (str, "%*s" format, 2*depth, " ", ##__VA_ARGS__)
42
43 static gchar *
44 gst_stream_audio_information_to_string (GstDiscovererStreamInfo * info,
45     gint depth)
46 {
47   GString *s;
48   gchar *tmp;
49   int len = 400;
50   const GstTagList *tags;
51   GstCaps *caps;
52
53   g_return_val_if_fail (info != NULL, NULL);
54
55   s = g_string_sized_new (len);
56
57   my_g_string_append_printf (s, "Codec:\n");
58   caps = gst_discoverer_stream_info_get_caps (info);
59   tmp = gst_caps_to_string (caps);
60   gst_caps_unref (caps);
61   my_g_string_append_printf (s, "  %s\n", tmp);
62   g_free (tmp);
63
64   my_g_string_append_printf (s, "Additional info:\n");
65   if (gst_discoverer_stream_info_get_misc (info)) {
66     tmp = gst_structure_to_string (gst_discoverer_stream_info_get_misc (info));
67     my_g_string_append_printf (s, "  %s\n", tmp);
68     g_free (tmp);
69   } else {
70     my_g_string_append_printf (s, "  None\n");
71   }
72
73   my_g_string_append_printf (s, "Channels: %u\n",
74       gst_discoverer_audio_info_get_channels (info));
75   my_g_string_append_printf (s, "Sample rate: %u\n",
76       gst_discoverer_audio_info_get_sample_rate (info));
77   my_g_string_append_printf (s, "Depth: %u\n",
78       gst_discoverer_audio_info_get_depth (info));
79
80   my_g_string_append_printf (s, "Bitrate: %u\n",
81       gst_discoverer_audio_info_get_bitrate (info));
82   my_g_string_append_printf (s, "Max bitrate: %u\n",
83       gst_discoverer_audio_info_get_max_bitrate (info));
84
85   my_g_string_append_printf (s, "Tags:\n");
86   tags = gst_discoverer_stream_info_get_tags (info);
87   if (tags) {
88     tmp = gst_structure_to_string ((GstStructure *) tags);
89     my_g_string_append_printf (s, "  %s\n", tmp);
90     g_free (tmp);
91   } else {
92     my_g_string_append_printf (s, "  None\n");
93   }
94   if (verbose)
95     my_g_string_append_printf (s, "\n");
96
97   return g_string_free (s, FALSE);
98 }
99
100 static gchar *
101 gst_stream_video_information_to_string (GstDiscovererStreamInfo * info,
102     gint depth)
103 {
104   GString *s;
105   gchar *tmp;
106   int len = 500;
107   const GstStructure *misc;
108   const GstTagList *tags;
109   GstCaps *caps;
110
111   g_return_val_if_fail (info != NULL, NULL);
112
113   s = g_string_sized_new (len);
114
115   my_g_string_append_printf (s, "Codec:\n");
116   caps = gst_discoverer_stream_info_get_caps (info);
117   tmp = gst_caps_to_string (caps);
118   gst_caps_unref (caps);
119   my_g_string_append_printf (s, "  %s\n", tmp);
120   g_free (tmp);
121
122   my_g_string_append_printf (s, "Additional info:\n");
123   misc = gst_discoverer_stream_info_get_misc (info);
124   if (misc) {
125     tmp = gst_structure_to_string (misc);
126     my_g_string_append_printf (s, "  %s\n", tmp);
127     g_free (tmp);
128   } else {
129     my_g_string_append_printf (s, "  None\n");
130   }
131
132   my_g_string_append_printf (s, "Width: %u\n",
133       gst_discoverer_video_info_get_width (info));
134   my_g_string_append_printf (s, "Height: %u\n",
135       gst_discoverer_video_info_get_height (info));
136   my_g_string_append_printf (s, "Depth: %u\n",
137       gst_discoverer_video_info_get_depth (info));
138
139   my_g_string_append_printf (s, "Frame rate: %u/%u\n",
140       gst_discoverer_video_info_get_framerate_num (info),
141       gst_discoverer_video_info_get_framerate_denom (info));
142
143   my_g_string_append_printf (s, "Pixel aspect ratio: %u/%u\n",
144       gst_discoverer_video_info_get_par_num (info),
145       gst_discoverer_video_info_get_par_denom (info));
146
147   my_g_string_append_printf (s, "Interlaced: %s\n",
148       gst_discoverer_video_info_get_interlaced (info) ? "true" : "false");
149
150   my_g_string_append_printf (s, "Bitrate: %u\n",
151       gst_discoverer_video_info_get_bitrate (info));
152   my_g_string_append_printf (s, "Max bitrate: %u\n",
153       gst_discoverer_video_info_get_max_bitrate (info));
154
155   my_g_string_append_printf (s, "Tags:\n");
156   tags = gst_discoverer_stream_info_get_tags (info);
157   if (tags) {
158     tmp = gst_structure_to_string ((GstStructure *) tags);
159     my_g_string_append_printf (s, "  %s\n", tmp);
160     g_free (tmp);
161   } else {
162     my_g_string_append_printf (s, "  None\n");
163   }
164   if (verbose)
165     my_g_string_append_printf (s, "\n");
166
167   return g_string_free (s, FALSE);
168 }
169
170 static void
171 print_stream_info (GstDiscovererStreamInfo * info, void *depth)
172 {
173   gchar *desc = NULL;
174   GstCaps *caps;
175
176   caps = gst_discoverer_stream_info_get_caps (info);
177
178   if (caps) {
179     if (gst_caps_is_fixed (caps) && !verbose)
180       desc = gst_pb_utils_get_codec_description (caps);
181     else
182       desc = gst_caps_to_string (caps);
183     gst_caps_unref (caps);
184   }
185
186   g_print ("%*s%s: %s\n", 2 * GPOINTER_TO_INT (depth), " ",
187       gst_discoverer_stream_info_get_stream_type_nick (info), desc);
188
189   if (desc) {
190     g_free (desc);
191     desc = NULL;
192   }
193
194   if (verbose) {
195     if (GST_IS_DISCOVERER_AUDIO_INFO (info))
196       desc =
197           gst_stream_audio_information_to_string (info,
198           GPOINTER_TO_INT (depth) + 1);
199     else if (GST_IS_DISCOVERER_VIDEO_INFO (info))
200       desc =
201           gst_stream_video_information_to_string (info,
202           GPOINTER_TO_INT (depth) + 1);
203   }
204
205   if (desc) {
206     g_print ("%s", desc);
207     g_free (desc);
208   }
209 }
210
211 static void
212 print_topology (GstDiscovererStreamInfo * info, gint depth)
213 {
214   GstDiscovererStreamInfo *next;
215
216   if (!info)
217     return;
218
219   print_stream_info (info, GINT_TO_POINTER (depth));
220
221   next = gst_discoverer_stream_info_get_next (info);
222   if (next) {
223     print_topology (next, depth + 1);
224     gst_discoverer_stream_info_unref (next);
225   } else if (GST_IS_DISCOVERER_CONTAINER_INFO (info)) {
226     GList *tmp, *streams;
227
228     streams = gst_discoverer_container_info_get_streams (info);
229     for (tmp = streams; tmp; tmp = tmp->next) {
230       GstDiscovererStreamInfo *tmpinf = (GstDiscovererStreamInfo *) tmp->data;
231       print_topology (tmpinf, depth + 1);
232     }
233     gst_discoverer_stream_info_list_free (streams);
234   }
235 }
236
237 static void
238 print_duration (GstDiscovererInfo * info, gint tab)
239 {
240   g_print ("%*s%" GST_TIME_FORMAT "\n", tab + 1, " ",
241       GST_TIME_ARGS (gst_discoverer_info_get_duration (info)));
242 }
243
244 static void
245 print_info (GstDiscovererInfo * info, GError * err)
246 {
247   GstDiscovererResult result = gst_discoverer_info_get_result (info);
248   GstDiscovererStreamInfo *sinfo;
249
250   g_print ("Done discovering %s\n", gst_discoverer_info_get_uri (info));
251   switch (result) {
252     case GST_DISCOVERER_OK:
253     {
254       break;
255     }
256     case GST_DISCOVERER_URI_INVALID:
257     {
258       g_print ("URI is not valid\n");
259       break;
260     }
261     case GST_DISCOVERER_ERROR:
262     {
263       g_print ("An error was encountered while discovering the file\n");
264       g_print (" %s\n", err->message);
265       break;
266     }
267     case GST_DISCOVERER_TIMEOUT:
268     {
269       g_print ("Analyzing URI timed out\n");
270       break;
271     }
272     case GST_DISCOVERER_BUSY:
273     {
274       g_print ("Discoverer was busy\n");
275       break;
276     }
277     case GST_DISCOVERER_MISSING_PLUGINS:
278     {
279       g_print ("Missing plugins\n");
280       if (verbose) {
281         gchar *tmp =
282             gst_structure_to_string (gst_discoverer_info_get_misc (info));
283         g_print (" (%s)\n", tmp);
284         g_free (tmp);
285       }
286       break;
287     }
288   }
289
290   if ((sinfo = gst_discoverer_info_get_stream_info (info))) {
291     g_print ("\nTopology:\n");
292     print_topology (sinfo, 1);
293     g_print ("\nDuration:\n");
294     print_duration (info, 1);
295     gst_discoverer_stream_info_unref (sinfo);
296   }
297
298   g_print ("\n");
299 }
300
301 static void
302 process_file (GstDiscoverer * dc, const gchar * filename)
303 {
304   GError *err = NULL;
305   GDir *dir;
306   gchar *uri, *path;
307   GstDiscovererInfo *info;
308   GstStructure *st = NULL;
309
310   if (!gst_uri_is_valid (filename)) {
311     /* Recurse into directories */
312     if ((dir = g_dir_open (filename, 0, NULL))) {
313       const gchar *entry;
314
315       while ((entry = g_dir_read_name (dir))) {
316         gchar *path;
317         path = g_strconcat (filename, G_DIR_SEPARATOR_S, entry, NULL);
318         process_file (dc, path);
319         g_free (path);
320       }
321
322       g_dir_close (dir);
323       return;
324     }
325
326     if (!g_path_is_absolute (filename)) {
327       gchar *cur_dir;
328
329       cur_dir = g_get_current_dir ();
330       path = g_build_filename (cur_dir, filename, NULL);
331       g_free (cur_dir);
332     } else {
333       path = g_strdup (filename);
334     }
335
336     uri = g_filename_to_uri (path, NULL, &err);
337     g_free (path);
338     path = NULL;
339
340     if (err) {
341       g_warning ("Couldn't convert filename to URI: %s\n", err->message);
342       g_error_free (err);
343       return;
344     }
345   } else {
346     uri = g_strdup (filename);
347   }
348
349   if (async == FALSE) {
350     g_print ("Analyzing %s\n", uri);
351     info = gst_discoverer_discover_uri (dc, uri, &err);
352     print_info (info, err);
353     gst_discoverer_info_unref (info);
354     if (st)
355       gst_structure_free (st);
356   } else {
357     gst_discoverer_discover_uri_async (dc, uri);
358   }
359
360   g_free (uri);
361 }
362
363 static void
364 _new_discovered_uri (GstDiscoverer * dc, GstDiscovererInfo * info, GError * err)
365 {
366   print_info (info, err);
367 }
368
369 static gboolean
370 _run_async (PrivStruct * ps)
371 {
372   gint i;
373
374   for (i = 1; i < ps->argc; i++)
375     process_file (ps->dc, ps->argv[i]);
376
377   return FALSE;
378 }
379
380 static void
381 _discoverer_finished (GstDiscoverer * dc, GMainLoop * ml)
382 {
383   g_main_loop_quit (ml);
384 }
385
386 int
387 main (int argc, char **argv)
388 {
389   GError *err = NULL;
390   GstDiscoverer *dc;
391   gint timeout = 10;
392   GOptionEntry options[] = {
393     {"async", 'a', 0, G_OPTION_ARG_NONE, &async,
394         "Run asynchronously", NULL},
395     {"silent", 's', 0, G_OPTION_ARG_NONE, &silent,
396         "Don't output the information structure", NULL},
397     {"timeout", 't', 0, G_OPTION_ARG_INT, &timeout,
398         "Specify timeout (in seconds, default 10)", "T"},
399     /* {"elem", 'e', 0, G_OPTION_ARG_NONE, &elem_seek, */
400     /*     "Seek on elements instead of pads", NULL}, */
401     {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
402         "Verbose properties", NULL},
403     {NULL}
404   };
405   GOptionContext *ctx;
406
407   if (!g_thread_supported ())
408     g_thread_init (NULL);
409
410   ctx =
411       g_option_context_new
412       ("- discover files synchronously with GstDiscoverer");
413   g_option_context_add_main_entries (ctx, options, NULL);
414   g_option_context_add_group (ctx, gst_init_get_option_group ());
415
416   if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
417     g_print ("Error initializing: %s\n", err->message);
418     exit (1);
419   }
420
421   g_option_context_free (ctx);
422
423   if (argc < 2) {
424     g_print ("usage: %s <uris>\n", argv[0]);
425     exit (-1);
426   }
427
428   dc = gst_discoverer_new (timeout * GST_SECOND, &err);
429   if (G_UNLIKELY (dc == NULL)) {
430     g_print ("Error initializing: %s\n", err->message);
431     exit (1);
432   }
433
434   if (async == FALSE) {
435     gint i;
436     for (i = 1; i < argc; i++)
437       process_file (dc, argv[i]);
438   } else {
439     PrivStruct *ps = g_new0 (PrivStruct, 1);
440     GMainLoop *ml = g_main_loop_new (NULL, FALSE);
441
442     ps->dc = dc;
443     ps->argc = argc;
444     ps->argv = argv;
445
446     /* adding uris will be started when the mainloop runs */
447     g_idle_add ((GSourceFunc) _run_async, ps);
448
449     /* connect signals */
450     g_signal_connect (dc, "discovered", G_CALLBACK (_new_discovered_uri), NULL);
451     g_signal_connect (dc, "finished", G_CALLBACK (_discoverer_finished), ml);
452
453     gst_discoverer_start (dc);
454     /* run mainloop */
455     g_main_loop_run (ml);
456
457     gst_discoverer_stop (dc);
458     g_free (ps);
459   }
460   g_object_unref (dc);
461
462   return 0;
463 }