2 * Copyright (C) 2009 Edward Hervey <edward.hervey@collabora.co.uk>
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.
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.
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.
27 #include <gst/pbutils/pbutils.h>
29 static gboolean async = FALSE;
30 static gboolean silent = FALSE;
31 static gboolean verbose = FALSE;
40 #define my_g_string_append_printf(str, format, ...) \
41 g_string_append_printf (str, "%*s" format, 2*depth, " ", ##__VA_ARGS__)
44 gst_stream_audio_information_to_string (GstDiscovererStreamInfo * info,
50 const GstTagList *tags;
53 g_return_val_if_fail (info != NULL, NULL);
55 s = g_string_sized_new (len);
57 my_g_string_append_printf (s, "Codec:\n");
58 caps = gst_discoverer_stream_info_get_caps (info);
59 tmp = gst_caps_to_string (caps);
60 gst_caps_unref (caps);
61 my_g_string_append_printf (s, " %s\n", tmp);
64 my_g_string_append_printf (s, "Additional info:\n");
65 if (gst_discoverer_stream_info_get_misc (info)) {
66 tmp = gst_structure_to_string (gst_discoverer_stream_info_get_misc (info));
67 my_g_string_append_printf (s, " %s\n", tmp);
70 my_g_string_append_printf (s, " None\n");
73 my_g_string_append_printf (s, "Channels: %u\n",
74 gst_discoverer_audio_info_get_channels (info));
75 my_g_string_append_printf (s, "Sample rate: %u\n",
76 gst_discoverer_audio_info_get_sample_rate (info));
77 my_g_string_append_printf (s, "Depth: %u\n",
78 gst_discoverer_audio_info_get_depth (info));
80 my_g_string_append_printf (s, "Bitrate: %u\n",
81 gst_discoverer_audio_info_get_bitrate (info));
82 my_g_string_append_printf (s, "Max bitrate: %u\n",
83 gst_discoverer_audio_info_get_max_bitrate (info));
85 my_g_string_append_printf (s, "Tags:\n");
86 tags = gst_discoverer_stream_info_get_tags (info);
88 tmp = gst_structure_to_string ((GstStructure *) tags);
89 my_g_string_append_printf (s, " %s\n", tmp);
92 my_g_string_append_printf (s, " None\n");
95 my_g_string_append_printf (s, "\n");
97 return g_string_free (s, FALSE);
101 gst_stream_video_information_to_string (GstDiscovererStreamInfo * info,
107 const GstStructure *misc;
108 const GstTagList *tags;
111 g_return_val_if_fail (info != NULL, NULL);
113 s = g_string_sized_new (len);
115 my_g_string_append_printf (s, "Codec:\n");
116 caps = gst_discoverer_stream_info_get_caps (info);
117 tmp = gst_caps_to_string (caps);
118 gst_caps_unref (caps);
119 my_g_string_append_printf (s, " %s\n", tmp);
122 my_g_string_append_printf (s, "Additional info:\n");
123 misc = gst_discoverer_stream_info_get_misc (info);
125 tmp = gst_structure_to_string (misc);
126 my_g_string_append_printf (s, " %s\n", tmp);
129 my_g_string_append_printf (s, " None\n");
132 my_g_string_append_printf (s, "Width: %u\n",
133 gst_discoverer_video_info_get_width (info));
134 my_g_string_append_printf (s, "Height: %u\n",
135 gst_discoverer_video_info_get_height (info));
136 my_g_string_append_printf (s, "Depth: %u\n",
137 gst_discoverer_video_info_get_depth (info));
139 my_g_string_append_printf (s, "Frame rate: %u/%u\n",
140 gst_discoverer_video_info_get_framerate_num (info),
141 gst_discoverer_video_info_get_framerate_denom (info));
143 my_g_string_append_printf (s, "Pixel aspect ratio: %u/%u\n",
144 gst_discoverer_video_info_get_par_num (info),
145 gst_discoverer_video_info_get_par_denom (info));
147 my_g_string_append_printf (s, "Interlaced: %s\n",
148 gst_discoverer_video_info_get_interlaced (info) ? "true" : "false");
150 my_g_string_append_printf (s, "Bitrate: %u\n",
151 gst_discoverer_video_info_get_bitrate (info));
152 my_g_string_append_printf (s, "Max bitrate: %u\n",
153 gst_discoverer_video_info_get_max_bitrate (info));
155 my_g_string_append_printf (s, "Tags:\n");
156 tags = gst_discoverer_stream_info_get_tags (info);
158 tmp = gst_structure_to_string ((GstStructure *) tags);
159 my_g_string_append_printf (s, " %s\n", tmp);
162 my_g_string_append_printf (s, " None\n");
165 my_g_string_append_printf (s, "\n");
167 return g_string_free (s, FALSE);
171 print_stream_info (GstDiscovererStreamInfo * info, void *depth)
176 caps = gst_discoverer_stream_info_get_caps (info);
179 if (gst_caps_is_fixed (caps) && !verbose)
180 desc = gst_pb_utils_get_codec_description (caps);
182 desc = gst_caps_to_string (caps);
183 gst_caps_unref (caps);
186 g_print ("%*s%s: %s\n", 2 * GPOINTER_TO_INT (depth), " ",
187 gst_discoverer_stream_info_get_stream_type_nick (info), desc);
195 if (GST_IS_DISCOVERER_AUDIO_INFO (info))
197 gst_stream_audio_information_to_string (info,
198 GPOINTER_TO_INT (depth) + 1);
199 else if (GST_IS_DISCOVERER_VIDEO_INFO (info))
201 gst_stream_video_information_to_string (info,
202 GPOINTER_TO_INT (depth) + 1);
206 g_print ("%s", desc);
212 print_topology (GstDiscovererStreamInfo * info, gint depth)
214 GstDiscovererStreamInfo *next;
219 print_stream_info (info, GINT_TO_POINTER (depth));
221 next = gst_discoverer_stream_info_get_next (info);
223 print_topology (next, depth + 1);
224 gst_discoverer_stream_info_unref (next);
225 } else if (GST_IS_DISCOVERER_CONTAINER_INFO (info)) {
226 GList *tmp, *streams;
228 streams = gst_discoverer_container_info_get_streams (info);
229 for (tmp = streams; tmp; tmp = tmp->next) {
230 GstDiscovererStreamInfo *tmpinf = (GstDiscovererStreamInfo *) tmp->data;
231 print_topology (tmpinf, depth + 1);
233 gst_discoverer_stream_info_list_free (streams);
238 print_duration (GstDiscovererInfo * info, gint tab)
240 g_print ("%*s%" GST_TIME_FORMAT "\n", tab + 1, " ",
241 GST_TIME_ARGS (gst_discoverer_info_get_duration (info)));
245 print_info (GstDiscovererInfo * info, GError * err)
247 GstDiscovererResult result = gst_discoverer_info_get_result (info);
248 GstDiscovererStreamInfo *sinfo;
250 g_print ("Done discovering %s\n", gst_discoverer_info_get_uri (info));
252 case GST_DISCOVERER_OK:
256 case GST_DISCOVERER_URI_INVALID:
258 g_print ("URI is not valid\n");
261 case GST_DISCOVERER_ERROR:
263 g_print ("An error was encountered while discovering the file\n");
264 g_print (" %s\n", err->message);
267 case GST_DISCOVERER_TIMEOUT:
269 g_print ("Analyzing URI timed out\n");
272 case GST_DISCOVERER_BUSY:
274 g_print ("Discoverer was busy\n");
277 case GST_DISCOVERER_MISSING_PLUGINS:
279 g_print ("Missing plugins\n");
282 gst_structure_to_string (gst_discoverer_info_get_misc (info));
283 g_print (" (%s)\n", tmp);
290 if ((sinfo = gst_discoverer_info_get_stream_info (info))) {
291 g_print ("\nTopology:\n");
292 print_topology (sinfo, 1);
293 g_print ("\nDuration:\n");
294 print_duration (info, 1);
295 gst_discoverer_stream_info_unref (sinfo);
302 process_file (GstDiscoverer * dc, const gchar * filename)
307 GstDiscovererInfo *info;
308 GstStructure *st = NULL;
310 if (!gst_uri_is_valid (filename)) {
311 /* Recurse into directories */
312 if ((dir = g_dir_open (filename, 0, NULL))) {
315 while ((entry = g_dir_read_name (dir))) {
317 path = g_strconcat (filename, G_DIR_SEPARATOR_S, entry, NULL);
318 process_file (dc, path);
326 if (!g_path_is_absolute (filename)) {
329 cur_dir = g_get_current_dir ();
330 path = g_build_filename (cur_dir, filename, NULL);
333 path = g_strdup (filename);
336 uri = g_filename_to_uri (path, NULL, &err);
341 g_warning ("Couldn't convert filename to URI: %s\n", err->message);
346 uri = g_strdup (filename);
349 if (async == FALSE) {
350 g_print ("Analyzing %s\n", uri);
351 info = gst_discoverer_discover_uri (dc, uri, &err);
352 print_info (info, err);
353 gst_discoverer_info_unref (info);
355 gst_structure_free (st);
357 gst_discoverer_discover_uri_async (dc, uri);
364 _new_discovered_uri (GstDiscoverer * dc, GstDiscovererInfo * info, GError * err)
366 print_info (info, err);
370 _run_async (PrivStruct * ps)
374 for (i = 1; i < ps->argc; i++)
375 process_file (ps->dc, ps->argv[i]);
381 _discoverer_finished (GstDiscoverer * dc, GMainLoop * ml)
383 g_main_loop_quit (ml);
387 main (int argc, char **argv)
392 GOptionEntry options[] = {
393 {"async", 'a', 0, G_OPTION_ARG_NONE, &async,
394 "Run asynchronously", NULL},
395 {"silent", 's', 0, G_OPTION_ARG_NONE, &silent,
396 "Don't output the information structure", NULL},
397 {"timeout", 't', 0, G_OPTION_ARG_INT, &timeout,
398 "Specify timeout (in seconds, default 10)", "T"},
399 /* {"elem", 'e', 0, G_OPTION_ARG_NONE, &elem_seek, */
400 /* "Seek on elements instead of pads", NULL}, */
401 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
402 "Verbose properties", NULL},
407 if (!g_thread_supported ())
408 g_thread_init (NULL);
412 ("- discover files synchronously with GstDiscoverer");
413 g_option_context_add_main_entries (ctx, options, NULL);
414 g_option_context_add_group (ctx, gst_init_get_option_group ());
416 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
417 g_print ("Error initializing: %s\n", err->message);
421 g_option_context_free (ctx);
424 g_print ("usage: %s <uris>\n", argv[0]);
428 dc = gst_discoverer_new (timeout * GST_SECOND, &err);
429 if (G_UNLIKELY (dc == NULL)) {
430 g_print ("Error initializing: %s\n", err->message);
434 if (async == FALSE) {
436 for (i = 1; i < argc; i++)
437 process_file (dc, argv[i]);
439 PrivStruct *ps = g_new0 (PrivStruct, 1);
440 GMainLoop *ml = g_main_loop_new (NULL, FALSE);
446 /* adding uris will be started when the mainloop runs */
447 g_idle_add ((GSourceFunc) _run_async, ps);
449 /* connect signals */
450 g_signal_connect (dc, "discovered", G_CALLBACK (_new_discovered_uri), NULL);
451 g_signal_connect (dc, "finished", G_CALLBACK (_discoverer_finished), ml);
453 gst_discoverer_start (dc);
455 g_main_loop_run (ml);
457 gst_discoverer_stop (dc);