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 #%d: %s\n", 2 * GPOINTER_TO_INT (depth), " ",
371 gst_discoverer_stream_info_get_stream_type_nick (info),
372 gst_discoverer_stream_info_get_stream_number (info), 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);
391 else if (GST_IS_DISCOVERER_CONTAINER_INFO (info)) {
392 const GstTagList *tags =
393 gst_discoverer_container_info_get_tags (GST_DISCOVERER_CONTAINER_INFO
395 print_tags_topology (GPOINTER_TO_INT (depth) + 1, tags);
398 g_print ("%s", desc);
404 print_topology (GstDiscovererStreamInfo * info, guint depth)
406 GstDiscovererStreamInfo *next;
411 print_stream_info (info, GINT_TO_POINTER (depth));
413 next = gst_discoverer_stream_info_get_next (info);
415 print_topology (next, depth + 1);
416 gst_discoverer_stream_info_unref (next);
417 } else if (GST_IS_DISCOVERER_CONTAINER_INFO (info)) {
418 GList *tmp, *streams;
421 gst_discoverer_container_info_get_streams (GST_DISCOVERER_CONTAINER_INFO
423 for (tmp = streams; tmp; tmp = tmp->next) {
424 GstDiscovererStreamInfo *tmpinf = (GstDiscovererStreamInfo *) tmp->data;
425 print_topology (tmpinf, depth + 1);
427 gst_discoverer_stream_info_list_free (streams);
432 print_toc_entry (gpointer data, gpointer user_data)
434 GstTocEntry *entry = (GstTocEntry *) data;
435 guint depth = GPOINTER_TO_UINT (user_data);
436 guint indent = MIN (GPOINTER_TO_UINT (user_data), MAX_INDENT);
441 gst_toc_entry_get_start_stop_times (entry, &start, &stop);
442 g_print ("%*s%s: start: %" GST_TIME_FORMAT " stop: %" GST_TIME_FORMAT "\n",
444 gst_toc_entry_type_get_nick (gst_toc_entry_get_entry_type (entry)),
445 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
449 tags = gst_toc_entry_get_tags (entry);
451 g_print ("%*sTags:\n", 2 * depth, " ");
452 gst_tag_list_foreach (tags, print_tag_foreach, GUINT_TO_POINTER (indent));
455 /* loop over sub-toc entries */
456 subentries = gst_toc_entry_get_sub_entries (entry);
457 g_list_foreach (subentries, print_toc_entry, GUINT_TO_POINTER (indent));
461 print_properties (GstDiscovererInfo * info, gint tab)
463 const GstTagList *tags;
466 g_print ("%*sDuration: %" GST_TIME_FORMAT "\n", tab + 1, " ",
467 GST_TIME_ARGS (gst_discoverer_info_get_duration (info)));
468 g_print ("%*sSeekable: %s\n", tab + 1, " ",
469 (gst_discoverer_info_get_seekable (info) ? "yes" : "no"));
470 g_print ("%*sLive: %s\n", tab + 1, " ",
471 (gst_discoverer_info_get_live (info) ? "yes" : "no"));
472 if (verbose && (tags = gst_discoverer_info_get_tags (info))) {
473 g_print ("%*sTags: \n", tab + 1, " ");
474 gst_tag_list_foreach (tags, print_tag_foreach, GUINT_TO_POINTER (tab + 2));
476 if (show_toc && (toc = gst_discoverer_info_get_toc (info))) {
479 g_print ("%*sTOC: \n", tab + 1, " ");
480 entries = gst_toc_get_entries (toc);
481 g_list_foreach (entries, print_toc_entry, GUINT_TO_POINTER (tab + 5));
486 print_info (GstDiscovererInfo * info, GError * err)
488 GstDiscovererResult result;
489 GstDiscovererStreamInfo *sinfo;
492 g_print ("Could not discover URI\n");
493 g_print (" %s\n", err->message);
497 result = gst_discoverer_info_get_result (info);
498 g_print ("Done discovering %s\n", gst_discoverer_info_get_uri (info));
500 case GST_DISCOVERER_OK:
504 case GST_DISCOVERER_URI_INVALID:
506 g_print ("URI is not valid\n");
509 case GST_DISCOVERER_ERROR:
511 g_print ("An error was encountered while discovering the file\n");
512 g_print (" %s\n", err->message);
515 case GST_DISCOVERER_TIMEOUT:
517 g_print ("Analyzing URI timed out\n");
520 case GST_DISCOVERER_BUSY:
522 g_print ("Discoverer was busy\n");
525 case GST_DISCOVERER_MISSING_PLUGINS:
528 const gchar **installer_details =
529 gst_discoverer_info_get_missing_elements_installer_details (info);
531 g_print ("Missing plugins\n");
533 while (installer_details[i]) {
534 g_print (" (%s)\n", installer_details[i]);
542 if ((sinfo = gst_discoverer_info_get_stream_info (info))) {
543 g_print ("\nProperties:\n");
544 print_properties (info, 1);
545 print_topology (sinfo, 1);
546 gst_discoverer_stream_info_unref (sinfo);
553 process_file (GstDiscoverer * dc, const gchar * filename)
558 GstDiscovererInfo *info;
560 if (!gst_uri_is_valid (filename)) {
561 /* Recurse into directories */
562 if ((dir = g_dir_open (filename, 0, NULL))) {
565 while ((entry = g_dir_read_name (dir))) {
567 path = g_strconcat (filename, G_DIR_SEPARATOR_S, entry, NULL);
568 process_file (dc, path);
576 if (!g_path_is_absolute (filename)) {
579 cur_dir = g_get_current_dir ();
580 path = g_build_filename (cur_dir, filename, NULL);
583 path = g_strdup (filename);
586 uri = g_filename_to_uri (path, NULL, &err);
591 g_warning ("Couldn't convert filename to URI: %s\n", err->message);
592 g_clear_error (&err);
596 uri = g_strdup (filename);
600 g_print ("Analyzing %s\n", uri);
601 info = gst_discoverer_discover_uri (dc, uri, &err);
602 print_info (info, err);
603 g_clear_error (&err);
605 gst_discoverer_info_unref (info);
607 gst_discoverer_discover_uri_async (dc, uri);
614 _new_discovered_uri (GstDiscoverer * dc, GstDiscovererInfo * info, GError * err)
616 print_info (info, err);
620 _run_async (PrivStruct * ps)
624 for (i = 1; i < ps->argc; i++)
625 process_file (ps->dc, ps->argv[i]);
631 _discoverer_finished (GstDiscoverer * dc, GMainLoop * ml)
633 g_main_loop_quit (ml);
637 main (int argc, char **argv)
642 gboolean use_cache = FALSE, print_cache_dir = FALSE;
643 GOptionEntry options[] = {
644 {"async", 'a', 0, G_OPTION_ARG_NONE, &async,
645 "Run asynchronously", NULL},
646 {"use-cache", 0, 0, G_OPTION_ARG_NONE, &use_cache,
647 "Use GstDiscovererInfo from our cache.", NULL},
648 {"print-cache-dir", 0, 0, G_OPTION_ARG_NONE, &print_cache_dir,
649 "Print the directory of the discoverer cache.", NULL},
650 {"timeout", 't', 0, G_OPTION_ARG_INT, &timeout,
651 "Specify timeout (in seconds, default 10)", "T"},
652 /* {"elem", 'e', 0, G_OPTION_ARG_NONE, &elem_seek, */
653 /* "Seek on elements instead of pads", NULL}, */
654 {"toc", 'c', 0, G_OPTION_ARG_NONE, &show_toc,
655 "Output TOC (chapters and editions)", NULL},
656 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
657 "Verbose properties", NULL},
662 setlocale (LC_ALL, "");
666 ("- discover files synchronously with GstDiscoverer");
667 g_option_context_add_main_entries (ctx, options, NULL);
668 g_option_context_add_group (ctx, gst_init_get_option_group ());
670 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
671 g_print ("Error initializing: %s\n", err->message);
672 g_option_context_free (ctx);
673 g_clear_error (&err);
677 g_option_context_free (ctx);
680 g_print ("usage: %s <uris>\n", argv[0]);
684 if (print_cache_dir) {
686 g_build_filename (g_get_user_cache_dir (), "gstreamer-" GST_API_VERSION,
688 g_print ("\nGstDiscoverer cache directory:\n\n %s\n\n", cache_dir);
693 dc = gst_discoverer_new (timeout * GST_SECOND, &err);
694 if (G_UNLIKELY (dc == NULL)) {
695 g_print ("Error initializing: %s\n", err->message);
696 g_clear_error (&err);
700 g_object_set (dc, "use-cache", use_cache, NULL);
704 for (i = 1; i < argc; i++)
705 process_file (dc, argv[i]);
707 PrivStruct *ps = g_new0 (PrivStruct, 1);
708 GMainLoop *ml = g_main_loop_new (NULL, FALSE);
714 /* adding uris will be started when the mainloop runs */
715 g_idle_add ((GSourceFunc) _run_async, ps);
717 /* connect signals */
718 g_signal_connect (dc, "discovered", G_CALLBACK (_new_discovered_uri), NULL);
719 g_signal_connect (dc, "finished", G_CALLBACK (_discoverer_finished), ml);
721 gst_discoverer_start (dc);
723 g_main_loop_run (ml);
725 gst_discoverer_stop (dc);
727 g_main_loop_unref (ml);