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>
30 #include <gst/audio/audio.h>
35 static void my_g_string_append_printf (GString * str, int depth, const gchar * format, ...) G_GNUC_PRINTF (3, 4);
38 static gboolean async = FALSE;
39 static gboolean show_toc = FALSE;
40 static gboolean verbose = FALSE;
50 structure_remove_buffers_ip (GQuark field_id, GValue * value,
53 if (G_VALUE_HOLDS (value, GST_TYPE_BUFFER))
56 if (GST_VALUE_HOLDS_ARRAY (value)) {
59 for (i = 0; i < gst_value_array_get_size (value); i++) {
60 if (structure_remove_buffers_ip (0,
61 (GValue *) gst_value_array_get_value (value, i), user_data))
71 caps_remove_buffers_ip (GstCapsFeatures * features, GstStructure * structure,
74 gst_structure_filter_and_map_in_place (structure,
75 structure_remove_buffers_ip, NULL);
81 my_g_string_append_printf (GString * str, int depth, const gchar * format, ...)
86 g_string_append (str, " ");
89 va_start (args, format);
90 g_string_append_vprintf (str, format, args);
95 caps_to_string (GstCaps * caps)
100 res = gst_caps_to_string (caps);
104 caps = gst_caps_make_writable (caps);
106 gst_caps_map_in_place (caps, caps_remove_buffers_ip, NULL);
107 res = gst_caps_to_string (caps);
110 gst_caps_unref (caps);
115 gst_stream_information_to_string (GstDiscovererStreamInfo * info, GString * s,
120 #ifndef GST_DISABLE_DEPRECATED
121 const GstStructure *misc;
125 my_g_string_append_printf (s, depth, "Codec:\n");
126 caps = gst_discoverer_stream_info_get_caps (info);
127 tmp = caps_to_string (caps);
128 my_g_string_append_printf (s, depth, " %s\n", tmp);
131 #ifndef GST_DISABLE_DEPRECATED
133 misc = gst_discoverer_stream_info_get_misc (info);
135 my_g_string_append_printf (s, depth, "Additional info:\n");
136 tmp = gst_structure_to_string (misc);
137 my_g_string_append_printf (s, depth, " %s\n", tmp);
143 my_g_string_append_printf (s, depth, "Stream ID: %s\n",
144 gst_discoverer_stream_info_get_stream_id (info));
148 print_tag_foreach (const GstTagList * tags, const gchar * tag,
153 guint depth = GPOINTER_TO_UINT (user_data);
155 if (!gst_tag_list_copy_value (&val, tags, tag))
158 if (G_VALUE_HOLDS_STRING (&val)) {
159 str = g_value_dup_string (&val);
160 } else if (G_VALUE_TYPE (&val) == GST_TYPE_SAMPLE) {
161 GstSample *sample = gst_value_get_sample (&val);
162 GstBuffer *img = gst_sample_get_buffer (sample);
163 GstCaps *caps = gst_sample_get_caps (sample);
169 caps_str = caps_to_string (gst_caps_ref (caps));
170 str = g_strdup_printf ("buffer of %" G_GSIZE_FORMAT " bytes, "
171 "type: %s", gst_buffer_get_size (img), caps_str);
174 str = g_strdup_printf ("buffer of %" G_GSIZE_FORMAT " bytes",
175 gst_buffer_get_size (img));
178 str = g_strdup ("NULL buffer");
181 str = gst_value_serialize (&val);
184 g_print ("%*s%s: %s\n", 2 * depth, " ", gst_tag_get_nick (tag), str);
187 g_value_unset (&val);
191 print_tags_topology (guint depth, const GstTagList * tags)
196 g_print ("%*sTags:\n", 2 * depth, " ");
198 gst_tag_list_foreach (tags, print_tag_foreach,
199 GUINT_TO_POINTER (depth + 1));
201 g_print ("%*sNone\n", 2 * (depth + 1), " ");
203 g_print ("%*s\n", 2 * depth, " ");
207 format_channel_mask (GstDiscovererAudioInfo * ainfo)
209 GString *s = g_string_sized_new (32);
210 GstAudioChannelPosition position[64];
211 guint channels = gst_discoverer_audio_info_get_channels (ainfo);
212 GEnumClass *enum_class = g_type_class_ref (GST_TYPE_AUDIO_CHANNEL_POSITION);
214 guint64 channel_mask;
219 channel_mask = gst_discoverer_audio_info_get_channel_mask (ainfo);
221 if (channel_mask != 0) {
222 gst_audio_channel_positions_from_mask (channels, channel_mask, position);
224 for (i = 0; i < channels; i++) {
225 GEnumValue *value = g_enum_get_value (enum_class, position[i]);
226 my_g_string_append_printf (s, 0, "%s%s", value->value_nick,
227 i + 1 == channels ? "" : ", ");
230 g_string_append (s, "unknown layout");
233 g_type_class_unref (enum_class);
236 return g_string_free (s, FALSE);
240 gst_stream_audio_information_to_string (GstDiscovererStreamInfo * info,
243 GstDiscovererAudioInfo *audio_info;
247 const GstTagList *tags;
248 gchar *channel_positions;
250 g_return_val_if_fail (info != NULL, NULL);
252 s = g_string_sized_new (len);
254 gst_stream_information_to_string (info, s, depth);
256 audio_info = (GstDiscovererAudioInfo *) info;
257 ctmp = gst_discoverer_audio_info_get_language (audio_info);
258 my_g_string_append_printf (s, depth, "Language: %s\n",
259 ctmp ? ctmp : "<unknown>");
261 channel_positions = format_channel_mask (audio_info);
262 my_g_string_append_printf (s, depth, "Channels: %u (%s)\n",
263 gst_discoverer_audio_info_get_channels (audio_info), channel_positions);
264 g_free (channel_positions);
266 my_g_string_append_printf (s, depth, "Sample rate: %u\n",
267 gst_discoverer_audio_info_get_sample_rate (audio_info));
268 my_g_string_append_printf (s, depth, "Depth: %u\n",
269 gst_discoverer_audio_info_get_depth (audio_info));
271 my_g_string_append_printf (s, depth, "Bitrate: %u\n",
272 gst_discoverer_audio_info_get_bitrate (audio_info));
273 my_g_string_append_printf (s, depth, "Max bitrate: %u\n",
274 gst_discoverer_audio_info_get_max_bitrate (audio_info));
276 tags = gst_discoverer_stream_info_get_tags (info);
277 print_tags_topology (depth, tags);
279 return g_string_free (s, FALSE);
283 gst_stream_video_information_to_string (GstDiscovererStreamInfo * info,
286 GstDiscovererVideoInfo *video_info;
289 const GstTagList *tags;
291 g_return_val_if_fail (info != NULL, NULL);
293 s = g_string_sized_new (len);
295 gst_stream_information_to_string (info, s, depth);
297 video_info = (GstDiscovererVideoInfo *) info;
298 my_g_string_append_printf (s, depth, "Width: %u\n",
299 gst_discoverer_video_info_get_width (video_info));
300 my_g_string_append_printf (s, depth, "Height: %u\n",
301 gst_discoverer_video_info_get_height (video_info));
302 my_g_string_append_printf (s, depth, "Depth: %u\n",
303 gst_discoverer_video_info_get_depth (video_info));
305 my_g_string_append_printf (s, depth, "Frame rate: %u/%u\n",
306 gst_discoverer_video_info_get_framerate_num (video_info),
307 gst_discoverer_video_info_get_framerate_denom (video_info));
309 my_g_string_append_printf (s, depth, "Pixel aspect ratio: %u/%u\n",
310 gst_discoverer_video_info_get_par_num (video_info),
311 gst_discoverer_video_info_get_par_denom (video_info));
313 my_g_string_append_printf (s, depth, "Interlaced: %s\n",
314 gst_discoverer_video_info_is_interlaced (video_info) ? "true" : "false");
316 my_g_string_append_printf (s, depth, "Bitrate: %u\n",
317 gst_discoverer_video_info_get_bitrate (video_info));
318 my_g_string_append_printf (s, depth, "Max bitrate: %u\n",
319 gst_discoverer_video_info_get_max_bitrate (video_info));
321 tags = gst_discoverer_stream_info_get_tags (info);
322 print_tags_topology (depth, tags);
324 return g_string_free (s, FALSE);
328 gst_stream_subtitle_information_to_string (GstDiscovererStreamInfo * info,
331 GstDiscovererSubtitleInfo *subtitle_info;
335 const GstTagList *tags;
337 g_return_val_if_fail (info != NULL, NULL);
339 s = g_string_sized_new (len);
341 gst_stream_information_to_string (info, s, depth);
343 subtitle_info = (GstDiscovererSubtitleInfo *) info;
344 ctmp = gst_discoverer_subtitle_info_get_language (subtitle_info);
345 my_g_string_append_printf (s, depth, "Language: %s\n",
346 ctmp ? ctmp : "<unknown>");
348 tags = gst_discoverer_stream_info_get_tags (info);
349 print_tags_topology (depth, tags);
351 return g_string_free (s, FALSE);
355 print_stream_info (GstDiscovererStreamInfo * info, void *depth)
360 caps = gst_discoverer_stream_info_get_caps (info);
363 if (gst_caps_is_fixed (caps) && !verbose)
364 desc = gst_pb_utils_get_codec_description (caps);
366 desc = caps_to_string (gst_caps_ref (caps));
367 gst_caps_unref (caps);
370 g_print ("%*s%s: %s\n", 2 * GPOINTER_TO_INT (depth), " ",
371 gst_discoverer_stream_info_get_stream_type_nick (info),
372 GST_STR_NULL (desc));
379 if (GST_IS_DISCOVERER_AUDIO_INFO (info))
381 gst_stream_audio_information_to_string (info,
382 GPOINTER_TO_INT (depth) + 1);
383 else if (GST_IS_DISCOVERER_VIDEO_INFO (info))
385 gst_stream_video_information_to_string (info,
386 GPOINTER_TO_INT (depth) + 1);
387 else if (GST_IS_DISCOVERER_SUBTITLE_INFO (info))
389 gst_stream_subtitle_information_to_string (info,
390 GPOINTER_TO_INT (depth) + 1);
392 g_print ("%s", desc);
398 print_topology (GstDiscovererStreamInfo * info, guint depth)
400 GstDiscovererStreamInfo *next;
405 print_stream_info (info, GINT_TO_POINTER (depth));
407 next = gst_discoverer_stream_info_get_next (info);
409 print_topology (next, depth + 1);
410 gst_discoverer_stream_info_unref (next);
411 } else if (GST_IS_DISCOVERER_CONTAINER_INFO (info)) {
412 GList *tmp, *streams;
415 gst_discoverer_container_info_get_streams (GST_DISCOVERER_CONTAINER_INFO
417 for (tmp = streams; tmp; tmp = tmp->next) {
418 GstDiscovererStreamInfo *tmpinf = (GstDiscovererStreamInfo *) tmp->data;
419 print_topology (tmpinf, depth + 1);
421 gst_discoverer_stream_info_list_free (streams);
426 print_toc_entry (gpointer data, gpointer user_data)
428 GstTocEntry *entry = (GstTocEntry *) data;
429 guint depth = GPOINTER_TO_UINT (user_data);
430 guint indent = MIN (GPOINTER_TO_UINT (user_data), MAX_INDENT);
435 gst_toc_entry_get_start_stop_times (entry, &start, &stop);
436 g_print ("%*s%s: start: %" GST_TIME_FORMAT " stop: %" GST_TIME_FORMAT "\n",
438 gst_toc_entry_type_get_nick (gst_toc_entry_get_entry_type (entry)),
439 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
443 tags = gst_toc_entry_get_tags (entry);
445 g_print ("%*sTags:\n", 2 * depth, " ");
446 gst_tag_list_foreach (tags, print_tag_foreach, GUINT_TO_POINTER (indent));
449 /* loop over sub-toc entries */
450 subentries = gst_toc_entry_get_sub_entries (entry);
451 g_list_foreach (subentries, print_toc_entry, GUINT_TO_POINTER (indent));
455 print_properties (GstDiscovererInfo * info, gint tab)
457 const GstTagList *tags;
460 g_print ("%*sDuration: %" GST_TIME_FORMAT "\n", tab + 1, " ",
461 GST_TIME_ARGS (gst_discoverer_info_get_duration (info)));
462 g_print ("%*sSeekable: %s\n", tab + 1, " ",
463 (gst_discoverer_info_get_seekable (info) ? "yes" : "no"));
464 g_print ("%*sLive: %s\n", tab + 1, " ",
465 (gst_discoverer_info_get_live (info) ? "yes" : "no"));
466 if (verbose && (tags = gst_discoverer_info_get_tags (info))) {
467 g_print ("%*sTags: \n", tab + 1, " ");
468 gst_tag_list_foreach (tags, print_tag_foreach, GUINT_TO_POINTER (tab + 2));
470 if (show_toc && (toc = gst_discoverer_info_get_toc (info))) {
473 g_print ("%*sTOC: \n", tab + 1, " ");
474 entries = gst_toc_get_entries (toc);
475 g_list_foreach (entries, print_toc_entry, GUINT_TO_POINTER (tab + 5));
480 print_info (GstDiscovererInfo * info, GError * err)
482 GstDiscovererResult result;
483 GstDiscovererStreamInfo *sinfo;
486 g_print ("Could not discover URI\n");
487 g_print (" %s\n", err->message);
491 result = gst_discoverer_info_get_result (info);
492 g_print ("Done discovering %s\n", gst_discoverer_info_get_uri (info));
494 case GST_DISCOVERER_OK:
498 case GST_DISCOVERER_URI_INVALID:
500 g_print ("URI is not valid\n");
503 case GST_DISCOVERER_ERROR:
505 g_print ("An error was encountered while discovering the file\n");
506 g_print (" %s\n", err->message);
509 case GST_DISCOVERER_TIMEOUT:
511 g_print ("Analyzing URI timed out\n");
514 case GST_DISCOVERER_BUSY:
516 g_print ("Discoverer was busy\n");
519 case GST_DISCOVERER_MISSING_PLUGINS:
522 const gchar **installer_details =
523 gst_discoverer_info_get_missing_elements_installer_details (info);
525 g_print ("Missing plugins\n");
527 while (installer_details[i]) {
528 g_print (" (%s)\n", installer_details[i]);
536 if ((sinfo = gst_discoverer_info_get_stream_info (info))) {
537 g_print ("\nProperties:\n");
538 print_properties (info, 1);
539 print_topology (sinfo, 1);
540 gst_discoverer_stream_info_unref (sinfo);
547 process_file (GstDiscoverer * dc, const gchar * filename)
552 GstDiscovererInfo *info;
554 if (!gst_uri_is_valid (filename)) {
555 /* Recurse into directories */
556 if ((dir = g_dir_open (filename, 0, NULL))) {
559 while ((entry = g_dir_read_name (dir))) {
561 path = g_strconcat (filename, G_DIR_SEPARATOR_S, entry, NULL);
562 process_file (dc, path);
570 if (!g_path_is_absolute (filename)) {
573 cur_dir = g_get_current_dir ();
574 path = g_build_filename (cur_dir, filename, NULL);
577 path = g_strdup (filename);
580 uri = g_filename_to_uri (path, NULL, &err);
585 g_warning ("Couldn't convert filename to URI: %s\n", err->message);
586 g_clear_error (&err);
590 uri = g_strdup (filename);
594 g_print ("Analyzing %s\n", uri);
595 info = gst_discoverer_discover_uri (dc, uri, &err);
596 print_info (info, err);
597 g_clear_error (&err);
599 gst_discoverer_info_unref (info);
601 gst_discoverer_discover_uri_async (dc, uri);
608 _new_discovered_uri (GstDiscoverer * dc, GstDiscovererInfo * info, GError * err)
610 print_info (info, err);
614 _run_async (PrivStruct * ps)
618 for (i = 1; i < ps->argc; i++)
619 process_file (ps->dc, ps->argv[i]);
625 _discoverer_finished (GstDiscoverer * dc, GMainLoop * ml)
627 g_main_loop_quit (ml);
631 main (int argc, char **argv)
636 gboolean use_cache = FALSE, print_cache_dir = FALSE;
637 GOptionEntry options[] = {
638 {"async", 'a', 0, G_OPTION_ARG_NONE, &async,
639 "Run asynchronously", NULL},
640 {"use-cache", 0, 0, G_OPTION_ARG_NONE, &use_cache,
641 "Use GstDiscovererInfo from our cache.", NULL},
642 {"print-cache-dir", 0, 0, G_OPTION_ARG_NONE, &print_cache_dir,
643 "Print the directory of the discoverer cache.", NULL},
644 {"timeout", 't', 0, G_OPTION_ARG_INT, &timeout,
645 "Specify timeout (in seconds, default 10)", "T"},
646 /* {"elem", 'e', 0, G_OPTION_ARG_NONE, &elem_seek, */
647 /* "Seek on elements instead of pads", NULL}, */
648 {"toc", 'c', 0, G_OPTION_ARG_NONE, &show_toc,
649 "Output TOC (chapters and editions)", NULL},
650 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
651 "Verbose properties", NULL},
656 setlocale (LC_ALL, "");
660 ("- discover files synchronously with GstDiscoverer");
661 g_option_context_add_main_entries (ctx, options, NULL);
662 g_option_context_add_group (ctx, gst_init_get_option_group ());
664 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
665 g_print ("Error initializing: %s\n", err->message);
666 g_option_context_free (ctx);
667 g_clear_error (&err);
671 g_option_context_free (ctx);
674 g_print ("usage: %s <uris>\n", argv[0]);
678 if (print_cache_dir) {
680 g_build_filename (g_get_user_cache_dir (), "gstreamer-" GST_API_VERSION,
682 g_print ("\nGstDiscoverer cache directory:\n\n %s\n\n", cache_dir);
687 dc = gst_discoverer_new (timeout * GST_SECOND, &err);
688 if (G_UNLIKELY (dc == NULL)) {
689 g_print ("Error initializing: %s\n", err->message);
690 g_clear_error (&err);
694 g_object_set (dc, "use-cache", use_cache, NULL);
698 for (i = 1; i < argc; i++)
699 process_file (dc, argv[i]);
701 PrivStruct *ps = g_new0 (PrivStruct, 1);
702 GMainLoop *ml = g_main_loop_new (NULL, FALSE);
708 /* adding uris will be started when the mainloop runs */
709 g_idle_add ((GSourceFunc) _run_async, ps);
711 /* connect signals */
712 g_signal_connect (dc, "discovered", G_CALLBACK (_new_discovered_uri), NULL);
713 g_signal_connect (dc, "finished", G_CALLBACK (_discoverer_finished), ml);
715 gst_discoverer_start (dc);
717 g_main_loop_run (ml);
719 gst_discoverer_stop (dc);
721 g_main_loop_unref (ml);