1 /* GStreamer
2 * Copyright (C) 2009 Edward Hervey <edward.hervey@collabora.co.uk>
3 *
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.
8 *
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.
13 *
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.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <locale.h>
25
26 #include <stdlib.h>
27 #include <glib.h>
28 #include <gst/gst.h>
29 #include <gst/pbutils/pbutils.h>
30 #include <gst/audio/audio.h>
31
32 #define MAX_INDENT 40
33
34 /* *INDENT-OFF* */
35 static void my_g_string_append_printf (GString * str, int depth, const gchar * format, ...) G_GNUC_PRINTF (3, 4);
36 /* *INDENT-ON* */
37
38 static gboolean async = FALSE;
39 static gboolean show_toc = FALSE;
40 static gboolean verbose = FALSE;
41
42 typedef struct
43 {
44 GstDiscoverer *dc;
45 int argc;
46 char **argv;
47 } PrivStruct;
48
49 static gboolean
structure_remove_buffers_ip(GQuark field_id,GValue * value,gpointer user_data)50 structure_remove_buffers_ip (GQuark field_id, GValue * value,
51 gpointer user_data)
52 {
53 if (G_VALUE_HOLDS (value, GST_TYPE_BUFFER))
54 return FALSE;
55
56 if (GST_VALUE_HOLDS_ARRAY (value)) {
57 gint i;
58
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))
62 return TRUE;
63 }
64
65 return FALSE;
66 }
67 return TRUE;
68 }
69
70 static gboolean
caps_remove_buffers_ip(GstCapsFeatures * features,GstStructure * structure,gpointer user_data)71 caps_remove_buffers_ip (GstCapsFeatures * features, GstStructure * structure,
72 gpointer user_data)
73 {
74 gst_structure_filter_and_map_in_place (structure,
75 structure_remove_buffers_ip, NULL);
76
77 return TRUE;
78 }
79
80 static void
my_g_string_append_printf(GString * str,int depth,const gchar * format,...)81 my_g_string_append_printf (GString * str, int depth, const gchar * format, ...)
82 {
83 va_list args;
84
85 while (depth-- > 0) {
86 g_string_append (str, " ");
87 }
88
89 va_start (args, format);
90 g_string_append_vprintf (str, format, args);
91 va_end (args);
92 }
93
94 static gchar *
caps_to_string(GstCaps * caps)95 caps_to_string (GstCaps * caps)
96 {
97 gchar *res = NULL;
98
99 if (verbose) {
100 res = gst_caps_to_string (caps);
101 goto done;
102 }
103
104 caps = gst_caps_make_writable (caps);
105
106 gst_caps_map_in_place (caps, caps_remove_buffers_ip, NULL);
107 res = gst_caps_to_string (caps);
108
109 done:
110 gst_caps_unref (caps);
111 return res;
112 }
113
114 static void
gst_stream_information_to_string(GstDiscovererStreamInfo * info,GString * s,guint depth)115 gst_stream_information_to_string (GstDiscovererStreamInfo * info, GString * s,
116 guint depth)
117 {
118 gchar *tmp;
119 GstCaps *caps;
120 #ifndef GST_DISABLE_DEPRECATED
121 const GstStructure *misc;
122 #endif
123
124 if (verbose) {
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);
129 g_free (tmp);
130 }
131 #ifndef GST_DISABLE_DEPRECATED
132 if (verbose) {
133 misc = gst_discoverer_stream_info_get_misc (info);
134 if (misc) {
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);
138 g_free (tmp);
139 }
140 }
141 #endif
142
143 my_g_string_append_printf (s, depth, "Stream ID: %s\n",
144 gst_discoverer_stream_info_get_stream_id (info));
145 }
146
147 static void
print_tag_foreach(const GstTagList * tags,const gchar * tag,gpointer user_data)148 print_tag_foreach (const GstTagList * tags, const gchar * tag,
149 gpointer user_data)
150 {
151 GValue val = { 0, };
152 gchar *str;
153 guint depth = GPOINTER_TO_UINT (user_data);
154
155 if (!gst_tag_list_copy_value (&val, tags, tag))
156 return;
157
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);
164
165 if (img) {
166 if (caps) {
167 gchar *caps_str;
168
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);
172 g_free (caps_str);
173 } else {
174 str = g_strdup_printf ("buffer of %" G_GSIZE_FORMAT " bytes",
175 gst_buffer_get_size (img));
176 }
177 } else {
178 str = g_strdup ("NULL buffer");
179 }
180 } else {
181 str = gst_value_serialize (&val);
182 }
183
184 g_print ("%*s%s: %s\n", 2 * depth, " ", gst_tag_get_nick (tag), str);
185 g_free (str);
186
187 g_value_unset (&val);
188 }
189
190 static void
print_tags_topology(guint depth,const GstTagList * tags)191 print_tags_topology (guint depth, const GstTagList * tags)
192 {
193 if (!verbose)
194 return;
195
196 g_print ("%*sTags:\n", 2 * depth, " ");
197 if (tags) {
198 gst_tag_list_foreach (tags, print_tag_foreach,
199 GUINT_TO_POINTER (depth + 1));
200 } else {
201 g_print ("%*sNone\n", 2 * (depth + 1), " ");
202 }
203 g_print ("%*s\n", 2 * depth, " ");
204 }
205
206 static gchar *
format_channel_mask(GstDiscovererAudioInfo * ainfo)207 format_channel_mask (GstDiscovererAudioInfo * ainfo)
208 {
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);
213 guint i;
214 guint64 channel_mask;
215
216 if (channels == 0)
217 goto done;
218
219 channel_mask = gst_discoverer_audio_info_get_channel_mask (ainfo);
220
221 if (channel_mask != 0) {
222 gst_audio_channel_positions_from_mask (channels, channel_mask, position);
223
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 ? "" : ", ");
228 }
229 } else {
230 g_string_append (s, "unknown layout");
231 }
232
233 g_type_class_unref (enum_class);
234
235 done:
236 return g_string_free (s, FALSE);
237 }
238
239 static gchar *
gst_stream_audio_information_to_string(GstDiscovererStreamInfo * info,guint depth)240 gst_stream_audio_information_to_string (GstDiscovererStreamInfo * info,
241 guint depth)
242 {
243 GstDiscovererAudioInfo *audio_info;
244 GString *s;
245 const gchar *ctmp;
246 int len = 400;
247 const GstTagList *tags;
248 gchar *channel_positions;
249
250 g_return_val_if_fail (info != NULL, NULL);
251
252 s = g_string_sized_new (len);
253
254 gst_stream_information_to_string (info, s, depth);
255
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>");
260
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);
265
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));
270
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));
275
276 tags = gst_discoverer_stream_info_get_tags (info);
277 print_tags_topology (depth, tags);
278
279 return g_string_free (s, FALSE);
280 }
281
282 static gchar *
gst_stream_video_information_to_string(GstDiscovererStreamInfo * info,guint depth)283 gst_stream_video_information_to_string (GstDiscovererStreamInfo * info,
284 guint depth)
285 {
286 GstDiscovererVideoInfo *video_info;
287 GString *s;
288 int len = 500;
289 const GstTagList *tags;
290
291 g_return_val_if_fail (info != NULL, NULL);
292
293 s = g_string_sized_new (len);
294
295 gst_stream_information_to_string (info, s, depth);
296
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));
304
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));
308
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));
312
313 my_g_string_append_printf (s, depth, "Interlaced: %s\n",
314 gst_discoverer_video_info_is_interlaced (video_info) ? "true" : "false");
315
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));
320
321 tags = gst_discoverer_stream_info_get_tags (info);
322 print_tags_topology (depth, tags);
323
324 return g_string_free (s, FALSE);
325 }
326
327 static gchar *
gst_stream_subtitle_information_to_string(GstDiscovererStreamInfo * info,guint depth)328 gst_stream_subtitle_information_to_string (GstDiscovererStreamInfo * info,
329 guint depth)
330 {
331 GstDiscovererSubtitleInfo *subtitle_info;
332 GString *s;
333 const gchar *ctmp;
334 int len = 400;
335 const GstTagList *tags;
336
337 g_return_val_if_fail (info != NULL, NULL);
338
339 s = g_string_sized_new (len);
340
341 gst_stream_information_to_string (info, s, depth);
342
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>");
347
348 tags = gst_discoverer_stream_info_get_tags (info);
349 print_tags_topology (depth, tags);
350
351 return g_string_free (s, FALSE);
352 }
353
354 static void
print_stream_info(GstDiscovererStreamInfo * info,void * depth)355 print_stream_info (GstDiscovererStreamInfo * info, void *depth)
356 {
357 gchar *desc = NULL;
358 GstCaps *caps;
359
360 caps = gst_discoverer_stream_info_get_caps (info);
361
362 if (caps) {
363 if (gst_caps_is_fixed (caps) && !verbose)
364 desc = gst_pb_utils_get_codec_description (caps);
365 else
366 desc = caps_to_string (gst_caps_ref (caps));
367 gst_caps_unref (caps);
368 }
369
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));
373
374 if (desc) {
375 g_free (desc);
376 desc = NULL;
377 }
378
379 if (GST_IS_DISCOVERER_AUDIO_INFO (info))
380 desc =
381 gst_stream_audio_information_to_string (info,
382 GPOINTER_TO_INT (depth) + 1);
383 else if (GST_IS_DISCOVERER_VIDEO_INFO (info))
384 desc =
385 gst_stream_video_information_to_string (info,
386 GPOINTER_TO_INT (depth) + 1);
387 else if (GST_IS_DISCOVERER_SUBTITLE_INFO (info))
388 desc =
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
394 (info));
395 print_tags_topology (GPOINTER_TO_INT (depth) + 1, tags);
396 }
397 if (desc) {
398 g_print ("%s", desc);
399 g_free (desc);
400 }
401 }
402
403 static void
print_topology(GstDiscovererStreamInfo * info,guint depth)404 print_topology (GstDiscovererStreamInfo * info, guint depth)
405 {
406 GstDiscovererStreamInfo *next;
407
408 if (!info)
409 return;
410
411 print_stream_info (info, GINT_TO_POINTER (depth));
412
413 next = gst_discoverer_stream_info_get_next (info);
414 if (next) {
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;
419
420 streams =
421 gst_discoverer_container_info_get_streams (GST_DISCOVERER_CONTAINER_INFO
422 (info));
423 for (tmp = streams; tmp; tmp = tmp->next) {
424 GstDiscovererStreamInfo *tmpinf = (GstDiscovererStreamInfo *) tmp->data;
425 print_topology (tmpinf, depth + 1);
426 }
427 gst_discoverer_stream_info_list_free (streams);
428 }
429 }
430
431 static void
print_toc_entry(gpointer data,gpointer user_data)432 print_toc_entry (gpointer data, gpointer user_data)
433 {
434 GstTocEntry *entry = (GstTocEntry *) data;
435 guint depth = GPOINTER_TO_UINT (user_data);
436 guint indent = MIN (GPOINTER_TO_UINT (user_data), MAX_INDENT);
437 GstTagList *tags;
438 GList *subentries;
439 gint64 start, stop;
440
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",
443 depth, " ",
444 gst_toc_entry_type_get_nick (gst_toc_entry_get_entry_type (entry)),
445 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
446 indent += 2;
447
448 /* print tags */
449 tags = gst_toc_entry_get_tags (entry);
450 if (tags) {
451 g_print ("%*sTags:\n", 2 * depth, " ");
452 gst_tag_list_foreach (tags, print_tag_foreach, GUINT_TO_POINTER (indent));
453 }
454
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));
458 }
459
460 static void
print_properties(GstDiscovererInfo * info,gint tab)461 print_properties (GstDiscovererInfo * info, gint tab)
462 {
463 const GstTagList *tags;
464 const GstToc *toc;
465
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));
475 }
476 if (show_toc && (toc = gst_discoverer_info_get_toc (info))) {
477 GList *entries;
478
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));
482 }
483 }
484
485 static void
print_info(GstDiscovererInfo * info,GError * err)486 print_info (GstDiscovererInfo * info, GError * err)
487 {
488 GstDiscovererResult result;
489 GstDiscovererStreamInfo *sinfo;
490
491 if (!info) {
492 g_print ("Could not discover URI\n");
493 g_print (" %s\n", err->message);
494 return;
495 }
496
497 result = gst_discoverer_info_get_result (info);
498 g_print ("Done discovering %s\n", gst_discoverer_info_get_uri (info));
499 switch (result) {
500 case GST_DISCOVERER_OK:
501 {
502 break;
503 }
504 case GST_DISCOVERER_URI_INVALID:
505 {
506 g_print ("URI is not valid\n");
507 break;
508 }
509 case GST_DISCOVERER_ERROR:
510 {
511 g_print ("An error was encountered while discovering the file\n");
512 g_print (" %s\n", err->message);
513 break;
514 }
515 case GST_DISCOVERER_TIMEOUT:
516 {
517 g_print ("Analyzing URI timed out\n");
518 break;
519 }
520 case GST_DISCOVERER_BUSY:
521 {
522 g_print ("Discoverer was busy\n");
523 break;
524 }
525 case GST_DISCOVERER_MISSING_PLUGINS:
526 {
527 gint i = 0;
528 const gchar **installer_details =
529 gst_discoverer_info_get_missing_elements_installer_details (info);
530
531 g_print ("Missing plugins\n");
532
533 while (installer_details[i]) {
534 g_print (" (%s)\n", installer_details[i]);
535
536 i++;
537 }
538 break;
539 }
540 }
541
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);
547 }
548
549 g_print ("\n");
550 }
551
552 static void
process_file(GstDiscoverer * dc,const gchar * filename)553 process_file (GstDiscoverer * dc, const gchar * filename)
554 {
555 GError *err = NULL;
556 GDir *dir;
557 gchar *uri, *path;
558 GstDiscovererInfo *info;
559
560 if (!gst_uri_is_valid (filename)) {
561 /* Recurse into directories */
562 if ((dir = g_dir_open (filename, 0, NULL))) {
563 const gchar *entry;
564
565 while ((entry = g_dir_read_name (dir))) {
566 gchar *path;
567 path = g_strconcat (filename, G_DIR_SEPARATOR_S, entry, NULL);
568 process_file (dc, path);
569 g_free (path);
570 }
571
572 g_dir_close (dir);
573 return;
574 }
575
576 if (!g_path_is_absolute (filename)) {
577 gchar *cur_dir;
578
579 cur_dir = g_get_current_dir ();
580 path = g_build_filename (cur_dir, filename, NULL);
581 g_free (cur_dir);
582 } else {
583 path = g_strdup (filename);
584 }
585
586 uri = g_filename_to_uri (path, NULL, &err);
587 g_free (path);
588 path = NULL;
589
590 if (err) {
591 g_warning ("Couldn't convert filename to URI: %s\n", err->message);
592 g_clear_error (&err);
593 return;
594 }
595 } else {
596 uri = g_strdup (filename);
597 }
598
599 if (!async) {
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);
604 if (info)
605 gst_discoverer_info_unref (info);
606 } else {
607 gst_discoverer_discover_uri_async (dc, uri);
608 }
609
610 g_free (uri);
611 }
612
613 static void
_new_discovered_uri(GstDiscoverer * dc,GstDiscovererInfo * info,GError * err)614 _new_discovered_uri (GstDiscoverer * dc, GstDiscovererInfo * info, GError * err)
615 {
616 print_info (info, err);
617 }
618
619 static gboolean
_run_async(PrivStruct * ps)620 _run_async (PrivStruct * ps)
621 {
622 gint i;
623
624 for (i = 1; i < ps->argc; i++)
625 process_file (ps->dc, ps->argv[i]);
626
627 return FALSE;
628 }
629
630 static void
_discoverer_finished(GstDiscoverer * dc,GMainLoop * ml)631 _discoverer_finished (GstDiscoverer * dc, GMainLoop * ml)
632 {
633 g_main_loop_quit (ml);
634 }
635
636 int
main(int argc,char ** argv)637 main (int argc, char **argv)
638 {
639 GError *err = NULL;
640 GstDiscoverer *dc;
641 gint timeout = 10;
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},
658 {NULL}
659 };
660 GOptionContext *ctx;
661
662 setlocale (LC_ALL, "");
663
664 ctx =
665 g_option_context_new
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 ());
669
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);
674 exit (1);
675 }
676
677 g_option_context_free (ctx);
678
679 if (argc < 2) {
680 g_print ("usage: %s <uris>\n", argv[0]);
681 exit (-1);
682 }
683
684 if (print_cache_dir) {
685 gchar *cache_dir =
686 g_build_filename (g_get_user_cache_dir (), "gstreamer-" GST_API_VERSION,
687 "discoverer", NULL);
688 g_print ("\nGstDiscoverer cache directory:\n\n %s\n\n", cache_dir);
689 g_free (cache_dir);
690 exit (0);
691 }
692
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);
697 exit (1);
698 }
699
700 g_object_set (dc, "use-cache", use_cache, NULL);
701
702 if (!async) {
703 gint i;
704 for (i = 1; i < argc; i++)
705 process_file (dc, argv[i]);
706 } else {
707 PrivStruct *ps = g_new0 (PrivStruct, 1);
708 GMainLoop *ml = g_main_loop_new (NULL, FALSE);
709
710 ps->dc = dc;
711 ps->argc = argc;
712 ps->argv = argv;
713
714 /* adding uris will be started when the mainloop runs */
715 g_idle_add ((GSourceFunc) _run_async, ps);
716
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);
720
721 gst_discoverer_start (dc);
722 /* run mainloop */
723 g_main_loop_run (ml);
724
725 gst_discoverer_stop (dc);
726 g_free (ps);
727 g_main_loop_unref (ml);
728 }
729 g_object_unref (dc);
730
731 return 0;
732 }
733