gst-indent run on core
[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,
8     guint maxtemplates)
9 {
10   GList *ret = NULL;
11
12   while (factories) {
13     guint count = 0;
14     GList *templs = ((GstElementFactory *) factories->data)->padtemplates;
15
16     while (templs) {
17       if (GST_PAD_TEMPLATE_DIRECTION (templs->data) == dir) {
18         count++;
19       }
20       if (count > maxtemplates)
21         break;
22       templs = g_list_next (templs);
23     }
24     if (count <= maxtemplates)
25       ret = g_list_prepend (ret, factories->data);
26
27     factories = g_list_next (factories);
28   }
29   return ret;
30 }
31
32 static void
33 property_change_callback (GObject * object, GstObject * orig,
34     GParamSpec * pspec)
35 {
36   GValue value = { 0, };        /* the important thing is that value.type = 0 */
37   gchar *str = 0;
38
39   if (pspec->flags & G_PARAM_READABLE) {
40     g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
41     g_object_get_property (G_OBJECT (orig), pspec->name, &value);
42     if (G_IS_PARAM_SPEC_STRING (pspec))
43       str = g_value_dup_string (&value);
44     else if (G_IS_PARAM_SPEC_ENUM (pspec))
45       str = g_strdup_printf ("%d", g_value_get_enum (&value));
46     else if (G_IS_PARAM_SPEC_INT64 (pspec))
47       str = g_strdup_printf ("%" G_GINT64_FORMAT, g_value_get_int64 (&value));
48     else
49       str = g_strdup_value_contents (&value);
50
51     g_print ("%s: %s = %s\n", GST_OBJECT_NAME (orig), pspec->name, str);
52     g_free (str);
53     g_value_unset (&value);
54   } else {
55     g_warning ("Parameter not readable. What's up with that?");
56   }
57 }
58
59 static void
60 error_callback (GObject * object, GstObject * orig, gchar * error)
61 {
62   g_print ("ERROR: %s: %s\n", GST_OBJECT_NAME (orig), error);
63 }
64
65 /*
66  * Test program for the autoplugger.
67  * Uses new API extensions (2002-01-28), too.
68  *
69  * USAGE: spidertest <mediafile>
70  * If mediafile can be recognized, xvideo and oss audio output are tried.
71  */
72 int
73 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),
88       NULL);
89   g_signal_connect (bin, "error", G_CALLBACK (error_callback), NULL);
90
91   /* create a disk reader */
92   filesrc = gst_element_factory_make ("filesrc", "disk_source");
93   g_object_set (G_OBJECT (filesrc), "location", argv[1], NULL);
94
95   /* now it's time to get the decoder */
96   decoder = gst_element_factory_make ("spider", "spider");
97   if (!decoder) {
98     g_print ("could not find plugin \"spider\"\n");
99     exit (-2);
100   }
101
102   /* only use decoding plugins */
103   g_object_get (decoder, "factories", &facs, NULL);
104   facs = gst_factories_at_most_templates (facs, GST_PAD_SINK, 1);
105   g_object_set (decoder, "factories", facs, NULL);
106
107   /* create video and audio sink */
108   osssink = gst_element_factory_make ("osssink", "audio");
109   videosink = gst_element_factory_make ("xvideosink", "video");
110
111   if ((!osssink) || (!videosink)) {
112     g_print ("could not create output plugins\n");
113     exit (-3);
114   }
115
116   /* add objects to the main pipeline */
117   gst_bin_add (GST_BIN (bin), filesrc);
118   gst_bin_add (GST_BIN (bin), decoder);
119   gst_bin_add (GST_BIN (bin), osssink);
120   gst_bin_add (GST_BIN (bin), videosink);
121
122   /* link objects */
123   if (!(gst_element_link (filesrc, decoder) &&
124           gst_element_link (decoder, osssink) &&
125           gst_element_link (decoder, videosink))) {
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 }