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