c5c14df5a45f80ce3aee97160b661329a31d36c0
[platform/upstream/gstreamer.git] / gst / autoplug / spidertest.c
1 #include <stdlib.h>
2 #include <gst/gst.h>
3
4 /* returns all factories which have a maximum of maxtemplates GstPadTemplates in direction dir
5  */
6 GList *
7 gst_factories_at_most_templates(GList *factories, GstPadDirection dir, guint maxtemplates)
8 {
9   GList *ret = NULL;
10   
11   while (factories)
12   {
13     guint count = 0;
14     GList *templs = ((GstElementFactory *) factories->data)->padtemplates;
15
16     while (templs)
17     {
18       if (GST_PAD_TEMPLATE_DIRECTION (templs->data) == dir)
19       {
20         count++;
21       }
22       if (count > maxtemplates)
23         break;
24       templs = g_list_next (templs);
25     }
26     if (count <= maxtemplates)
27       ret = g_list_prepend (ret, factories->data);
28     
29     factories = g_list_next (factories);
30   }
31   return ret;
32 }
33
34 static void
35 property_change_callback (GObject *object, GstObject *orig, GParamSpec *pspec)
36 {
37   GValue value = { 0, }; /* the important thing is that value.type = 0 */
38   gchar *str = 0;
39   
40   if (pspec->flags & G_PARAM_READABLE) {
41     g_value_init(&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
42     g_object_get_property (G_OBJECT (orig), pspec->name, &value);
43     if (G_IS_PARAM_SPEC_STRING (pspec))
44       str = g_value_dup_string (&value);
45     else if (G_IS_PARAM_SPEC_ENUM (pspec))
46       str = g_strdup_printf ("%d", g_value_get_enum (&value));
47     else if (G_IS_PARAM_SPEC_INT64 (pspec))
48       str = g_strdup_printf ("%lld", g_value_get_int64 (&value));
49     else
50       str = g_strdup_value_contents (&value);
51       
52     g_print ("%s: %s = %s\n", GST_OBJECT_NAME (orig), pspec->name, str);
53     g_free (str);
54     g_value_unset(&value);
55   } else {
56     g_warning ("Parameter not readable. What's up with that?");
57   }
58 }
59
60 static void
61 error_callback (GObject *object, GstObject *orig, gchar *error)
62 {
63   g_print ("ERROR: %s: %s\n", GST_OBJECT_NAME (orig), error);
64 }
65
66 /*
67  * Test program for the autoplugger.
68  * Uses new API extensions (2002-01-28), too.
69  *
70  * USAGE: spidertest <mediafile>
71  * If mediafile can be recognized, xvideo and oss audio output are tried.
72  */
73 int main(int argc,char *argv[]) 
74 {
75   GstElement *bin, *filesrc, *decoder, *osssink, *videosink;
76   GList *facs;
77   
78   if (argc < 2) {
79     g_print("usage: %s <file>\n", argv[0]);
80     exit(-1);
81   }
82
83    gst_init(&argc,&argv);
84
85   /* create a new bin to hold the elements */
86   bin = gst_pipeline_new("pipeline");
87   g_signal_connect (bin, "deep_notify", G_CALLBACK (property_change_callback), NULL);
88   g_signal_connect (bin, "error", G_CALLBACK (error_callback), NULL);
89    
90   /* create a disk reader */
91   filesrc = gst_element_factory_make("filesrc", "disk_source");
92   g_object_set(G_OBJECT(filesrc),"location", argv[1], NULL);
93
94   /* now it's time to get the decoder */
95   decoder = gst_element_factory_make("spider", "spider");
96   if (!decoder) {
97     g_print ("could not find plugin \"spider\"\n");
98     exit (-2);
99   }
100   
101   /* only use decoding plugins */
102   g_object_get (decoder, "factories", &facs, NULL);
103   facs = gst_factories_at_most_templates(facs, GST_PAD_SINK, 1);
104   g_object_set (decoder, "factories", facs, NULL);
105
106   /* create video and audio sink */
107   osssink = gst_element_factory_make("osssink", "audio");
108   videosink = gst_element_factory_make("xvideosink", "video");
109
110   if ((!osssink) || (!videosink)) {
111     g_print ("could not create output plugins\n");
112     exit (-3);
113   }
114
115   /* add objects to the main pipeline */
116   gst_bin_add(GST_BIN(bin), filesrc);
117   gst_bin_add(GST_BIN(bin), decoder);
118   gst_bin_add(GST_BIN(bin), osssink);
119   gst_bin_add(GST_BIN(bin), videosink);
120
121   /* link objects */
122   if (!(gst_element_link(filesrc, decoder) &&
123         gst_element_link(decoder, osssink) &&
124         gst_element_link(decoder, videosink)))
125   {
126     g_print ("the pipeline could not be linked\n");
127     exit (-4);
128   }
129
130 /*  gst_bin_use_clock (GST_BIN (bin), gst_system_clock_obtain ());*/
131
132   /* start playing */
133   gst_element_set_state(bin, GST_STATE_PLAYING);
134
135   while (gst_bin_iterate(GST_BIN(bin)));
136
137   exit(0);
138 }
139