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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
29 #include <gst/pbutils/pbutils.h>
34 static void my_g_string_append_printf (GString * str, int depth, const gchar * format, ...) G_GNUC_PRINTF (3, 4);
37 static gboolean async = FALSE;
38 static gboolean show_toc = FALSE;
39 static gboolean verbose = FALSE;
49 my_g_string_append_printf (GString * str, int depth, const gchar * format, ...)
54 g_string_append (str, " ");
57 va_start (args, format);
58 g_string_append_vprintf (str, format, args);
63 gst_stream_information_to_string (GstDiscovererStreamInfo * info, GString * s,
68 const GstStructure *misc;
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);
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);
83 my_g_string_append_printf (s, depth, " None\n");
86 my_g_string_append_printf (s, depth, "Stream ID: %s\n",
87 gst_discoverer_stream_info_get_stream_id (info));
91 print_tag_foreach (const GstTagList * tags, const gchar * tag,
96 guint depth = GPOINTER_TO_UINT (user_data);
98 if (!gst_tag_list_copy_value (&val, tags, tag))
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);
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);
117 str = g_strdup_printf ("buffer of %" G_GSIZE_FORMAT " bytes",
118 gst_buffer_get_size (img));
121 str = g_strdup ("NULL buffer");
124 str = gst_value_serialize (&val);
127 g_print ("%*s%s: %s\n", 2 * depth, " ", gst_tag_get_nick (tag), str);
130 g_value_unset (&val);
134 print_tags_topology (guint depth, const GstTagList * tags)
136 g_print ("%*sTags:\n", 2 * depth, " ");
138 gst_tag_list_foreach (tags, print_tag_foreach,
139 GUINT_TO_POINTER (depth + 1));
141 g_print ("%*sNone\n", 2 * (depth + 1), " ");
144 g_print ("%*s\n", 2 * depth, " ");
148 gst_stream_audio_information_to_string (GstDiscovererStreamInfo * info,
151 GstDiscovererAudioInfo *audio_info;
155 const GstTagList *tags;
157 g_return_val_if_fail (info != NULL, NULL);
159 s = g_string_sized_new (len);
161 gst_stream_information_to_string (info, s, depth);
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));
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));
179 tags = gst_discoverer_stream_info_get_tags (info);
180 print_tags_topology (depth, tags);
182 return g_string_free (s, FALSE);
186 gst_stream_video_information_to_string (GstDiscovererStreamInfo * info,
189 GstDiscovererVideoInfo *video_info;
192 const GstTagList *tags;
194 g_return_val_if_fail (info != NULL, NULL);
196 s = g_string_sized_new (len);
198 gst_stream_information_to_string (info, s, depth);
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));
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));
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));
216 my_g_string_append_printf (s, depth, "Interlaced: %s\n",
217 gst_discoverer_video_info_is_interlaced (video_info) ? "true" : "false");
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));
224 tags = gst_discoverer_stream_info_get_tags (info);
225 print_tags_topology (depth, tags);
227 return g_string_free (s, FALSE);
231 gst_stream_subtitle_information_to_string (GstDiscovererStreamInfo * info,
234 GstDiscovererSubtitleInfo *subtitle_info;
238 const GstTagList *tags;
240 g_return_val_if_fail (info != NULL, NULL);
242 s = g_string_sized_new (len);
244 gst_stream_information_to_string (info, s, depth);
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>");
251 tags = gst_discoverer_stream_info_get_tags (info);
252 print_tags_topology (depth, tags);
254 return g_string_free (s, FALSE);
258 print_stream_info (GstDiscovererStreamInfo * info, void *depth)
263 caps = gst_discoverer_stream_info_get_caps (info);
266 if (gst_caps_is_fixed (caps) && !verbose)
267 desc = gst_pb_utils_get_codec_description (caps);
269 desc = gst_caps_to_string (caps);
270 gst_caps_unref (caps);
273 g_print ("%*s%s: %s\n", 2 * GPOINTER_TO_INT (depth), " ",
274 gst_discoverer_stream_info_get_stream_type_nick (info), desc);
282 if (GST_IS_DISCOVERER_AUDIO_INFO (info))
284 gst_stream_audio_information_to_string (info,
285 GPOINTER_TO_INT (depth) + 1);
286 else if (GST_IS_DISCOVERER_VIDEO_INFO (info))
288 gst_stream_video_information_to_string (info,
289 GPOINTER_TO_INT (depth) + 1);
290 else if (GST_IS_DISCOVERER_SUBTITLE_INFO (info))
292 gst_stream_subtitle_information_to_string (info,
293 GPOINTER_TO_INT (depth) + 1);
295 g_print ("%s", desc);
302 print_topology (GstDiscovererStreamInfo * info, guint depth)
304 GstDiscovererStreamInfo *next;
309 print_stream_info (info, GINT_TO_POINTER (depth));
311 next = gst_discoverer_stream_info_get_next (info);
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;
319 gst_discoverer_container_info_get_streams (GST_DISCOVERER_CONTAINER_INFO
321 for (tmp = streams; tmp; tmp = tmp->next) {
322 GstDiscovererStreamInfo *tmpinf = (GstDiscovererStreamInfo *) tmp->data;
323 print_topology (tmpinf, depth + 1);
325 gst_discoverer_stream_info_list_free (streams);
330 print_toc_entry (gpointer data, gpointer user_data)
332 GstTocEntry *entry = (GstTocEntry *) data;
333 guint depth = GPOINTER_TO_UINT (user_data);
334 guint indent = MIN (GPOINTER_TO_UINT (user_data), MAX_INDENT);
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",
342 gst_toc_entry_type_get_nick (gst_toc_entry_get_entry_type (entry)),
343 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
347 tags = gst_toc_entry_get_tags (entry);
349 g_print ("%*sTags:\n", 2 * depth, " ");
350 gst_tag_list_foreach (tags, print_tag_foreach, GUINT_TO_POINTER (indent));
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));
359 print_properties (GstDiscovererInfo * info, gint tab)
361 const GstTagList *tags;
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 if ((tags = gst_discoverer_info_get_tags (info))) {
369 g_print ("%*sTags: \n", tab + 1, " ");
370 gst_tag_list_foreach (tags, print_tag_foreach, GUINT_TO_POINTER (tab + 2));
372 if (show_toc && (toc = gst_discoverer_info_get_toc (info))) {
375 g_print ("%*sTOC: \n", tab + 1, " ");
376 entries = gst_toc_get_entries (toc);
377 g_list_foreach (entries, print_toc_entry, GUINT_TO_POINTER (tab + 5));
382 print_info (GstDiscovererInfo * info, GError * err)
384 GstDiscovererResult result;
385 GstDiscovererStreamInfo *sinfo;
388 g_print ("Could not discover URI\n");
389 g_print (" %s\n", err->message);
393 result = gst_discoverer_info_get_result (info);
394 g_print ("Done discovering %s\n", gst_discoverer_info_get_uri (info));
396 case GST_DISCOVERER_OK:
400 case GST_DISCOVERER_URI_INVALID:
402 g_print ("URI is not valid\n");
405 case GST_DISCOVERER_ERROR:
407 g_print ("An error was encountered while discovering the file\n");
408 g_print (" %s\n", err->message);
411 case GST_DISCOVERER_TIMEOUT:
413 g_print ("Analyzing URI timed out\n");
416 case GST_DISCOVERER_BUSY:
418 g_print ("Discoverer was busy\n");
421 case GST_DISCOVERER_MISSING_PLUGINS:
423 g_print ("Missing plugins\n");
426 const gchar **installer_details =
427 gst_discoverer_info_get_missing_elements_installer_details (info);
429 while (installer_details[i]) {
430 g_print (" (%s)\n", installer_details[i]);
439 if ((sinfo = gst_discoverer_info_get_stream_info (info))) {
440 g_print ("\nTopology:\n");
441 print_topology (sinfo, 1);
442 g_print ("\nProperties:\n");
443 print_properties (info, 1);
444 gst_discoverer_stream_info_unref (sinfo);
451 process_file (GstDiscoverer * dc, const gchar * filename)
456 GstDiscovererInfo *info;
458 if (!gst_uri_is_valid (filename)) {
459 /* Recurse into directories */
460 if ((dir = g_dir_open (filename, 0, NULL))) {
463 while ((entry = g_dir_read_name (dir))) {
465 path = g_strconcat (filename, G_DIR_SEPARATOR_S, entry, NULL);
466 process_file (dc, path);
474 if (!g_path_is_absolute (filename)) {
477 cur_dir = g_get_current_dir ();
478 path = g_build_filename (cur_dir, filename, NULL);
481 path = g_strdup (filename);
484 uri = g_filename_to_uri (path, NULL, &err);
489 g_warning ("Couldn't convert filename to URI: %s\n", err->message);
490 g_clear_error (&err);
494 uri = g_strdup (filename);
498 g_print ("Analyzing %s\n", uri);
499 info = gst_discoverer_discover_uri (dc, uri, &err);
500 print_info (info, err);
501 g_clear_error (&err);
503 gst_discoverer_info_unref (info);
505 gst_discoverer_discover_uri_async (dc, uri);
512 _new_discovered_uri (GstDiscoverer * dc, GstDiscovererInfo * info, GError * err)
514 print_info (info, err);
518 _run_async (PrivStruct * ps)
522 for (i = 1; i < ps->argc; i++)
523 process_file (ps->dc, ps->argv[i]);
529 _discoverer_finished (GstDiscoverer * dc, GMainLoop * ml)
531 g_main_loop_quit (ml);
535 main (int argc, char **argv)
540 GOptionEntry options[] = {
541 {"async", 'a', 0, G_OPTION_ARG_NONE, &async,
542 "Run asynchronously", NULL},
543 {"timeout", 't', 0, G_OPTION_ARG_INT, &timeout,
544 "Specify timeout (in seconds, default 10)", "T"},
545 /* {"elem", 'e', 0, G_OPTION_ARG_NONE, &elem_seek, */
546 /* "Seek on elements instead of pads", NULL}, */
547 {"toc", 'c', 0, G_OPTION_ARG_NONE, &show_toc,
548 "Output TOC (chapters and editions)", NULL},
549 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
550 "Verbose properties", NULL},
555 setlocale (LC_ALL, "");
559 ("- discover files synchronously with GstDiscoverer");
560 g_option_context_add_main_entries (ctx, options, NULL);
561 g_option_context_add_group (ctx, gst_init_get_option_group ());
563 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
564 g_print ("Error initializing: %s\n", err->message);
565 g_option_context_free (ctx);
566 g_clear_error (&err);
570 g_option_context_free (ctx);
573 g_print ("usage: %s <uris>\n", argv[0]);
577 dc = gst_discoverer_new (timeout * GST_SECOND, &err);
578 if (G_UNLIKELY (dc == NULL)) {
579 g_print ("Error initializing: %s\n", err->message);
580 g_clear_error (&err);
586 for (i = 1; i < argc; i++)
587 process_file (dc, argv[i]);
589 PrivStruct *ps = g_new0 (PrivStruct, 1);
590 GMainLoop *ml = g_main_loop_new (NULL, FALSE);
596 /* adding uris will be started when the mainloop runs */
597 g_idle_add ((GSourceFunc) _run_async, ps);
599 /* connect signals */
600 g_signal_connect (dc, "discovered", G_CALLBACK (_new_discovered_uri), NULL);
601 g_signal_connect (dc, "finished", G_CALLBACK (_discoverer_finished), ml);
603 gst_discoverer_start (dc);
605 g_main_loop_run (ml);
607 gst_discoverer_stop (dc);
609 g_main_loop_unref (ml);