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