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