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