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