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