merge TYPEFIND branch. Major changes:
[platform/upstream/gstreamer.git] / tools / gst-typefind.c
1 #ifdef HAVE_CONFIG_H
2 #  include "config.h"
3 #endif
4
5 #include <string.h>
6 #include <stdlib.h>
7 #include <locale.h>
8 #include <gst/gst.h>
9
10 /*
11  * find the type of a media file and display it's properties
12  **/
13
14 gboolean FOUND = FALSE;
15 gchar *filename = NULL;
16
17 void
18 gst_caps_print (const char *filename, GstCaps *caps)
19 {
20   gchar *caps_str = gst_caps_to_string (caps);
21   g_print ("%s - %s\n", filename, caps_str);
22   g_free (caps_str);
23 }
24
25 void
26 have_type_handler (GstElement *typefind, guint probability, GstCaps *caps, gpointer unused)
27 {
28   gst_caps_print (filename, caps);
29   FOUND = TRUE;
30 }
31
32 int
33 main (int argc, char *argv[])
34 {
35   guint i = 1;
36   GstElement *pipeline;
37   GstElement *source, *typefind;
38
39   setlocale (LC_ALL, "");
40
41   gst_init (&argc, &argv);
42
43   if (argc < 2) { 
44     g_print ("Please give a filename to typefind\n\n");
45     return 1;
46   }
47   
48   pipeline = gst_pipeline_new (NULL);
49   source = gst_element_factory_make ("filesrc", "source");
50   g_assert (GST_IS_ELEMENT (source));
51   typefind = gst_element_factory_make ("typefind", "typefind");
52   g_assert (GST_IS_ELEMENT (typefind));
53   gst_bin_add_many (GST_BIN (pipeline), source, typefind, NULL);
54   gst_element_link (source, typefind);
55   g_signal_connect (G_OBJECT (typefind), "have-type", 
56                     G_CALLBACK (have_type_handler), NULL);
57
58   while (i < argc) {
59     FOUND = FALSE;
60     gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
61     filename = argv[i];
62     g_object_set (source, "location", filename, NULL);
63     /* set to play */
64     gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
65
66     while (!FOUND) { 
67       if (!gst_bin_iterate (GST_BIN (pipeline)))
68         break;
69     }
70     if (!FOUND) {
71       g_print ("%s - No type found\n", argv[i]);
72     }
73     i++;
74   }
75   g_object_unref (pipeline);
76   return 0;
77 }