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 my_g_string_append_printf (GString * str, int depth, const gchar * format, ...)
55 g_string_append (str, " ");
58 va_start (args, format);
59 g_string_append_vprintf (str, format, args);
64 gst_stream_information_to_string (GstDiscovererStreamInfo * info, GString * s,
69 #ifndef GST_DISABLE_DEPRECATED
70 const GstStructure *misc;
73 my_g_string_append_printf (s, depth, "Codec:\n");
74 caps = gst_discoverer_stream_info_get_caps (info);
75 tmp = gst_caps_to_string (caps);
76 gst_caps_unref (caps);
77 my_g_string_append_printf (s, depth, " %s\n", tmp);
80 #ifndef GST_DISABLE_DEPRECATED
81 my_g_string_append_printf (s, depth, "Additional info:\n");
82 if ((misc = gst_discoverer_stream_info_get_misc (info))) {
83 tmp = gst_structure_to_string (misc);
84 my_g_string_append_printf (s, depth, " %s\n", tmp);
87 my_g_string_append_printf (s, depth, " None\n");
91 my_g_string_append_printf (s, depth, "Stream ID: %s\n",
92 gst_discoverer_stream_info_get_stream_id (info));
96 print_tag_foreach (const GstTagList * tags, const gchar * tag,
101 guint depth = GPOINTER_TO_UINT (user_data);
103 if (!gst_tag_list_copy_value (&val, tags, tag))
106 if (G_VALUE_HOLDS_STRING (&val)) {
107 str = g_value_dup_string (&val);
108 } else if (G_VALUE_TYPE (&val) == GST_TYPE_SAMPLE) {
109 GstSample *sample = gst_value_get_sample (&val);
110 GstBuffer *img = gst_sample_get_buffer (sample);
111 GstCaps *caps = gst_sample_get_caps (sample);
117 caps_str = gst_caps_to_string (caps);
118 str = g_strdup_printf ("buffer of %" G_GSIZE_FORMAT " bytes, "
119 "type: %s", gst_buffer_get_size (img), caps_str);
122 str = g_strdup_printf ("buffer of %" G_GSIZE_FORMAT " bytes",
123 gst_buffer_get_size (img));
126 str = g_strdup ("NULL buffer");
129 str = gst_value_serialize (&val);
132 g_print ("%*s%s: %s\n", 2 * depth, " ", gst_tag_get_nick (tag), str);
135 g_value_unset (&val);
139 print_tags_topology (guint depth, const GstTagList * tags)
141 g_print ("%*sTags:\n", 2 * depth, " ");
143 gst_tag_list_foreach (tags, print_tag_foreach,
144 GUINT_TO_POINTER (depth + 1));
146 g_print ("%*sNone\n", 2 * (depth + 1), " ");
149 g_print ("%*s\n", 2 * depth, " ");
153 format_channel_mask (GstDiscovererAudioInfo * ainfo)
155 GString *s = g_string_sized_new (32);
156 GstAudioChannelPosition position[64];
157 guint channels = gst_discoverer_audio_info_get_channels (ainfo);
158 GEnumClass *enum_class = g_type_class_ref (GST_TYPE_AUDIO_CHANNEL_POSITION);
160 guint64 channel_mask;
165 channel_mask = gst_discoverer_audio_info_get_channel_mask (ainfo);
167 if (channel_mask != 0) {
168 gst_audio_channel_positions_from_mask (channels, channel_mask, position);
170 for (i = 0; i < channels; i++) {
171 GEnumValue *value = g_enum_get_value (enum_class, position[i]);
172 my_g_string_append_printf (s, 0, "%s%s", value->value_nick,
173 i + 1 == channels ? "" : ", ");
176 g_string_append (s, "unknown layout");
179 g_type_class_unref (enum_class);
182 return g_string_free (s, FALSE);
186 gst_stream_audio_information_to_string (GstDiscovererStreamInfo * info,
189 GstDiscovererAudioInfo *audio_info;
193 const GstTagList *tags;
194 gchar *channel_positions;
196 g_return_val_if_fail (info != NULL, NULL);
198 s = g_string_sized_new (len);
200 gst_stream_information_to_string (info, s, depth);
202 audio_info = (GstDiscovererAudioInfo *) info;
203 ctmp = gst_discoverer_audio_info_get_language (audio_info);
204 my_g_string_append_printf (s, depth, "Language: %s\n",
205 ctmp ? ctmp : "<unknown>");
207 channel_positions = format_channel_mask (audio_info);
208 my_g_string_append_printf (s, depth, "Channels: %u (%s)\n",
209 gst_discoverer_audio_info_get_channels (audio_info), channel_positions);
210 g_free (channel_positions);
212 my_g_string_append_printf (s, depth, "Sample rate: %u\n",
213 gst_discoverer_audio_info_get_sample_rate (audio_info));
214 my_g_string_append_printf (s, depth, "Depth: %u\n",
215 gst_discoverer_audio_info_get_depth (audio_info));
217 my_g_string_append_printf (s, depth, "Bitrate: %u\n",
218 gst_discoverer_audio_info_get_bitrate (audio_info));
219 my_g_string_append_printf (s, depth, "Max bitrate: %u\n",
220 gst_discoverer_audio_info_get_max_bitrate (audio_info));
222 tags = gst_discoverer_stream_info_get_tags (info);
223 print_tags_topology (depth, tags);
225 return g_string_free (s, FALSE);
229 gst_stream_video_information_to_string (GstDiscovererStreamInfo * info,
232 GstDiscovererVideoInfo *video_info;
235 const GstTagList *tags;
237 g_return_val_if_fail (info != NULL, NULL);
239 s = g_string_sized_new (len);
241 gst_stream_information_to_string (info, s, depth);
243 video_info = (GstDiscovererVideoInfo *) info;
244 my_g_string_append_printf (s, depth, "Width: %u\n",
245 gst_discoverer_video_info_get_width (video_info));
246 my_g_string_append_printf (s, depth, "Height: %u\n",
247 gst_discoverer_video_info_get_height (video_info));
248 my_g_string_append_printf (s, depth, "Depth: %u\n",
249 gst_discoverer_video_info_get_depth (video_info));
251 my_g_string_append_printf (s, depth, "Frame rate: %u/%u\n",
252 gst_discoverer_video_info_get_framerate_num (video_info),
253 gst_discoverer_video_info_get_framerate_denom (video_info));
255 my_g_string_append_printf (s, depth, "Pixel aspect ratio: %u/%u\n",
256 gst_discoverer_video_info_get_par_num (video_info),
257 gst_discoverer_video_info_get_par_denom (video_info));
259 my_g_string_append_printf (s, depth, "Interlaced: %s\n",
260 gst_discoverer_video_info_is_interlaced (video_info) ? "true" : "false");
262 my_g_string_append_printf (s, depth, "Bitrate: %u\n",
263 gst_discoverer_video_info_get_bitrate (video_info));
264 my_g_string_append_printf (s, depth, "Max bitrate: %u\n",
265 gst_discoverer_video_info_get_max_bitrate (video_info));
267 tags = gst_discoverer_stream_info_get_tags (info);
268 print_tags_topology (depth, tags);
270 return g_string_free (s, FALSE);
274 gst_stream_subtitle_information_to_string (GstDiscovererStreamInfo * info,
277 GstDiscovererSubtitleInfo *subtitle_info;
281 const GstTagList *tags;
283 g_return_val_if_fail (info != NULL, NULL);
285 s = g_string_sized_new (len);
287 gst_stream_information_to_string (info, s, depth);
289 subtitle_info = (GstDiscovererSubtitleInfo *) info;
290 ctmp = gst_discoverer_subtitle_info_get_language (subtitle_info);
291 my_g_string_append_printf (s, depth, "Language: %s\n",
292 ctmp ? ctmp : "<unknown>");
294 tags = gst_discoverer_stream_info_get_tags (info);
295 print_tags_topology (depth, tags);
297 return g_string_free (s, FALSE);
301 print_stream_info (GstDiscovererStreamInfo * info, void *depth)
306 caps = gst_discoverer_stream_info_get_caps (info);
309 if (gst_caps_is_fixed (caps) && !verbose)
310 desc = gst_pb_utils_get_codec_description (caps);
312 desc = gst_caps_to_string (caps);
313 gst_caps_unref (caps);
316 g_print ("%*s%s: %s\n", 2 * GPOINTER_TO_INT (depth), " ",
317 gst_discoverer_stream_info_get_stream_type_nick (info), desc);
325 if (GST_IS_DISCOVERER_AUDIO_INFO (info))
327 gst_stream_audio_information_to_string (info,
328 GPOINTER_TO_INT (depth) + 1);
329 else if (GST_IS_DISCOVERER_VIDEO_INFO (info))
331 gst_stream_video_information_to_string (info,
332 GPOINTER_TO_INT (depth) + 1);
333 else if (GST_IS_DISCOVERER_SUBTITLE_INFO (info))
335 gst_stream_subtitle_information_to_string (info,
336 GPOINTER_TO_INT (depth) + 1);
338 g_print ("%s", desc);
345 print_topology (GstDiscovererStreamInfo * info, guint depth)
347 GstDiscovererStreamInfo *next;
352 print_stream_info (info, GINT_TO_POINTER (depth));
354 next = gst_discoverer_stream_info_get_next (info);
356 print_topology (next, depth + 1);
357 gst_discoverer_stream_info_unref (next);
358 } else if (GST_IS_DISCOVERER_CONTAINER_INFO (info)) {
359 GList *tmp, *streams;
362 gst_discoverer_container_info_get_streams (GST_DISCOVERER_CONTAINER_INFO
364 for (tmp = streams; tmp; tmp = tmp->next) {
365 GstDiscovererStreamInfo *tmpinf = (GstDiscovererStreamInfo *) tmp->data;
366 print_topology (tmpinf, depth + 1);
368 gst_discoverer_stream_info_list_free (streams);
373 print_toc_entry (gpointer data, gpointer user_data)
375 GstTocEntry *entry = (GstTocEntry *) data;
376 guint depth = GPOINTER_TO_UINT (user_data);
377 guint indent = MIN (GPOINTER_TO_UINT (user_data), MAX_INDENT);
382 gst_toc_entry_get_start_stop_times (entry, &start, &stop);
383 g_print ("%*s%s: start: %" GST_TIME_FORMAT " stop: %" GST_TIME_FORMAT "\n",
385 gst_toc_entry_type_get_nick (gst_toc_entry_get_entry_type (entry)),
386 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
390 tags = gst_toc_entry_get_tags (entry);
392 g_print ("%*sTags:\n", 2 * depth, " ");
393 gst_tag_list_foreach (tags, print_tag_foreach, GUINT_TO_POINTER (indent));
396 /* loop over sub-toc entries */
397 subentries = gst_toc_entry_get_sub_entries (entry);
398 g_list_foreach (subentries, print_toc_entry, GUINT_TO_POINTER (indent));
402 print_properties (GstDiscovererInfo * info, gint tab)
404 const GstTagList *tags;
407 g_print ("%*sDuration: %" GST_TIME_FORMAT "\n", tab + 1, " ",
408 GST_TIME_ARGS (gst_discoverer_info_get_duration (info)));
409 g_print ("%*sSeekable: %s\n", tab + 1, " ",
410 (gst_discoverer_info_get_seekable (info) ? "yes" : "no"));
411 g_print ("%*sLive: %s\n", tab + 1, " ",
412 (gst_discoverer_info_get_live (info) ? "yes" : "no"));
413 if ((tags = gst_discoverer_info_get_tags (info))) {
414 g_print ("%*sTags: \n", tab + 1, " ");
415 gst_tag_list_foreach (tags, print_tag_foreach, GUINT_TO_POINTER (tab + 2));
417 if (show_toc && (toc = gst_discoverer_info_get_toc (info))) {
420 g_print ("%*sTOC: \n", tab + 1, " ");
421 entries = gst_toc_get_entries (toc);
422 g_list_foreach (entries, print_toc_entry, GUINT_TO_POINTER (tab + 5));
427 print_info (GstDiscovererInfo * info, GError * err)
429 GstDiscovererResult result;
430 GstDiscovererStreamInfo *sinfo;
433 g_print ("Could not discover URI\n");
434 g_print (" %s\n", err->message);
438 result = gst_discoverer_info_get_result (info);
439 g_print ("Done discovering %s\n", gst_discoverer_info_get_uri (info));
441 case GST_DISCOVERER_OK:
445 case GST_DISCOVERER_URI_INVALID:
447 g_print ("URI is not valid\n");
450 case GST_DISCOVERER_ERROR:
452 g_print ("An error was encountered while discovering the file\n");
453 g_print (" %s\n", err->message);
456 case GST_DISCOVERER_TIMEOUT:
458 g_print ("Analyzing URI timed out\n");
461 case GST_DISCOVERER_BUSY:
463 g_print ("Discoverer was busy\n");
466 case GST_DISCOVERER_MISSING_PLUGINS:
468 g_print ("Missing plugins\n");
471 const gchar **installer_details =
472 gst_discoverer_info_get_missing_elements_installer_details (info);
474 while (installer_details[i]) {
475 g_print (" (%s)\n", installer_details[i]);
484 if ((sinfo = gst_discoverer_info_get_stream_info (info))) {
485 g_print ("\nTopology:\n");
486 print_topology (sinfo, 1);
487 g_print ("\nProperties:\n");
488 print_properties (info, 1);
489 gst_discoverer_stream_info_unref (sinfo);
496 process_file (GstDiscoverer * dc, const gchar * filename)
501 GstDiscovererInfo *info;
503 if (!gst_uri_is_valid (filename)) {
504 /* Recurse into directories */
505 if ((dir = g_dir_open (filename, 0, NULL))) {
508 while ((entry = g_dir_read_name (dir))) {
510 path = g_strconcat (filename, G_DIR_SEPARATOR_S, entry, NULL);
511 process_file (dc, path);
519 if (!g_path_is_absolute (filename)) {
522 cur_dir = g_get_current_dir ();
523 path = g_build_filename (cur_dir, filename, NULL);
526 path = g_strdup (filename);
529 uri = g_filename_to_uri (path, NULL, &err);
534 g_warning ("Couldn't convert filename to URI: %s\n", err->message);
535 g_clear_error (&err);
539 uri = g_strdup (filename);
543 g_print ("Analyzing %s\n", uri);
544 info = gst_discoverer_discover_uri (dc, uri, &err);
545 print_info (info, err);
546 g_clear_error (&err);
548 gst_discoverer_info_unref (info);
550 gst_discoverer_discover_uri_async (dc, uri);
557 _new_discovered_uri (GstDiscoverer * dc, GstDiscovererInfo * info, GError * err)
559 print_info (info, err);
563 _run_async (PrivStruct * ps)
567 for (i = 1; i < ps->argc; i++)
568 process_file (ps->dc, ps->argv[i]);
574 _discoverer_finished (GstDiscoverer * dc, GMainLoop * ml)
576 g_main_loop_quit (ml);
580 main (int argc, char **argv)
585 gboolean use_cache = FALSE;
586 GOptionEntry options[] = {
587 {"async", 'a', 0, G_OPTION_ARG_NONE, &async,
588 "Run asynchronously", NULL},
589 {"use-cache", 'a', 0, G_OPTION_ARG_NONE, &use_cache,
590 "Use GstDiscovererInfo from our cache.", NULL},
591 {"timeout", 't', 0, G_OPTION_ARG_INT, &timeout,
592 "Specify timeout (in seconds, default 10)", "T"},
593 /* {"elem", 'e', 0, G_OPTION_ARG_NONE, &elem_seek, */
594 /* "Seek on elements instead of pads", NULL}, */
595 {"toc", 'c', 0, G_OPTION_ARG_NONE, &show_toc,
596 "Output TOC (chapters and editions)", NULL},
597 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
598 "Verbose properties", NULL},
603 setlocale (LC_ALL, "");
607 ("- discover files synchronously with GstDiscoverer");
608 g_option_context_add_main_entries (ctx, options, NULL);
609 g_option_context_add_group (ctx, gst_init_get_option_group ());
611 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
612 g_print ("Error initializing: %s\n", err->message);
613 g_option_context_free (ctx);
614 g_clear_error (&err);
618 g_option_context_free (ctx);
621 g_print ("usage: %s <uris>\n", argv[0]);
625 dc = gst_discoverer_new (timeout * GST_SECOND, &err);
626 if (G_UNLIKELY (dc == NULL)) {
627 g_print ("Error initializing: %s\n", err->message);
628 g_clear_error (&err);
632 g_object_set (dc, "use-cache", use_cache, NULL);
636 for (i = 1; i < argc; i++)
637 process_file (dc, argv[i]);
639 PrivStruct *ps = g_new0 (PrivStruct, 1);
640 GMainLoop *ml = g_main_loop_new (NULL, FALSE);
646 /* adding uris will be started when the mainloop runs */
647 g_idle_add ((GSourceFunc) _run_async, ps);
649 /* connect signals */
650 g_signal_connect (dc, "discovered", G_CALLBACK (_new_discovered_uri), NULL);
651 g_signal_connect (dc, "finished", G_CALLBACK (_discoverer_finished), ml);
653 gst_discoverer_start (dc);
655 g_main_loop_run (ml);
657 gst_discoverer_stop (dc);
659 g_main_loop_unref (ml);