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