discoverer: Fixup DiscovererResult handling
[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       sinfo = gst_discoverer_info_get_stream_info (info);
255       g_print ("\nTopology:\n");
256       print_topology (sinfo, 1);
257       g_print ("\nDuration:\n");
258       print_duration (info, 1);
259       gst_discoverer_stream_info_unref (sinfo);
260       break;
261     }
262     case GST_DISCOVERER_URI_INVALID:
263     {
264       g_print ("URI is not valid\n");
265       break;
266     }
267     case GST_DISCOVERER_ERROR:
268     {
269       g_print ("An error was encountered while discovering the file\n");
270       g_print (" %s\n", err->message);
271       break;
272     }
273     case GST_DISCOVERER_TIMEOUT:
274     {
275       g_print ("Analyzing URI timed out\n");
276       break;
277     }
278     case GST_DISCOVERER_BUSY:
279     {
280       g_print ("Discoverer was busy\n");
281       break;
282     }
283     case GST_DISCOVERER_MISSING_PLUGINS:
284     {
285       g_print ("Missing plugins\n");
286       if (verbose) {
287         gchar *tmp =
288             gst_structure_to_string (gst_discoverer_info_get_misc (info));
289         g_print (" (%s)\n", tmp);
290         g_free (tmp);
291       }
292       break;
293     }
294   }
295
296   g_print ("\n");
297 }
298
299 static void
300 process_file (GstDiscoverer * dc, const gchar * filename)
301 {
302   GError *err = NULL;
303   GDir *dir;
304   gchar *uri, *path;
305   GstDiscovererInfo *info;
306   GstStructure *st = NULL;
307
308   if (!gst_uri_is_valid (filename)) {
309     /* Recurse into directories */
310     if ((dir = g_dir_open (filename, 0, NULL))) {
311       const gchar *entry;
312
313       while ((entry = g_dir_read_name (dir))) {
314         gchar *path;
315         path = g_strconcat (filename, G_DIR_SEPARATOR_S, entry, NULL);
316         process_file (dc, path);
317         g_free (path);
318       }
319
320       g_dir_close (dir);
321       return;
322     }
323
324     if (!g_path_is_absolute (filename)) {
325       gchar *cur_dir;
326
327       cur_dir = g_get_current_dir ();
328       path = g_build_filename (cur_dir, filename, NULL);
329       g_free (cur_dir);
330     } else {
331       path = g_strdup (filename);
332     }
333
334     uri = g_filename_to_uri (path, NULL, &err);
335     g_free (path);
336     path = NULL;
337
338     if (err) {
339       g_warning ("Couldn't convert filename to URI: %s\n", err->message);
340       g_error_free (err);
341       return;
342     }
343   } else {
344     uri = g_strdup (filename);
345   }
346
347   if (async == FALSE) {
348     g_print ("Analyzing %s\n", uri);
349     info = gst_discoverer_discover_uri (dc, uri, &err);
350     print_info (info, err);
351     gst_discoverer_info_unref (info);
352     if (st)
353       gst_structure_free (st);
354   } else {
355     gst_discoverer_discover_uri_async (dc, uri);
356   }
357
358   g_free (uri);
359 }
360
361 static void
362 _new_discovered_uri (GstDiscoverer * dc, GstDiscovererInfo * info, GError * err)
363 {
364   print_info (info, err);
365 }
366
367 static gboolean
368 _run_async (PrivStruct * ps)
369 {
370   gint i;
371
372   for (i = 1; i < ps->argc; i++)
373     process_file (ps->dc, ps->argv[i]);
374
375   return FALSE;
376 }
377
378 static void
379 _discoverer_ready (GstDiscoverer * dc, GMainLoop * ml)
380 {
381   g_main_loop_quit (ml);
382 }
383
384 int
385 main (int argc, char **argv)
386 {
387   GError *err = NULL;
388   GstDiscoverer *dc;
389   gint timeout = 10;
390   GOptionEntry options[] = {
391     {"async", 'a', 0, G_OPTION_ARG_NONE, &async,
392         "Run asynchronously", NULL},
393     {"silent", 's', 0, G_OPTION_ARG_NONE, &silent,
394         "Don't output the information structure", NULL},
395     {"timeout", 't', 0, G_OPTION_ARG_INT, &timeout,
396         "Specify timeout (in seconds, default 10)", "T"},
397     /* {"elem", 'e', 0, G_OPTION_ARG_NONE, &elem_seek, */
398     /*     "Seek on elements instead of pads", NULL}, */
399     {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
400         "Verbose properties", NULL},
401     {NULL}
402   };
403   GOptionContext *ctx;
404
405   if (!g_thread_supported ())
406     g_thread_init (NULL);
407
408   ctx =
409       g_option_context_new
410       ("- discover files synchronously with GstDiscoverer");
411   g_option_context_add_main_entries (ctx, options, NULL);
412   g_option_context_add_group (ctx, gst_init_get_option_group ());
413
414   if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
415     g_print ("Error initializing: %s\n", err->message);
416     exit (1);
417   }
418
419   g_option_context_free (ctx);
420
421   if (argc < 2) {
422     g_print ("usage: %s <uris>\n", argv[0]);
423     exit (-1);
424   }
425
426   dc = gst_discoverer_new (timeout * GST_SECOND, &err);
427   if (G_UNLIKELY (dc == NULL)) {
428     g_print ("Error initializing: %s\n", err->message);
429     exit (1);
430   }
431
432   if (async == FALSE) {
433     gint i;
434     for (i = 1; i < argc; i++)
435       process_file (dc, argv[i]);
436   } else {
437     PrivStruct *ps = g_new0 (PrivStruct, 1);
438     GMainLoop *ml = g_main_loop_new (NULL, FALSE);
439
440     ps->dc = dc;
441     ps->argc = argc;
442     ps->argv = argv;
443
444     /* adding uris will be started when the mainloop runs */
445     g_idle_add ((GSourceFunc) _run_async, ps);
446
447     /* connect signals */
448     g_signal_connect (dc, "discovered", G_CALLBACK (_new_discovered_uri), NULL);
449     g_signal_connect (dc, "ready", G_CALLBACK (_discoverer_ready), ml);
450
451     gst_discoverer_start (dc);
452     /* run mainloop */
453     g_main_loop_run (ml);
454
455     gst_discoverer_stop (dc);
456     g_free (ps);
457   }
458   g_object_unref (dc);
459
460   return 0;
461 }