c22507e8e197f224926ea67c3e2e0da63235708b
[platform/upstream/gstreamer.git] / examples / autoplug / autoplug.c
1 #include <gst/gst.h>
2
3 static void
4 gst_play_have_type (GstElement *typefind, GstCaps *caps, GstElement *pipeline)
5 {
6   GstElement *osssink, *videosink, *videoelement, *colorspace;
7   GstElement *new_element;
8   GstAutoplug *autoplug;
9   GstElement *autobin;
10   GstElement *filesrc;
11   GstElement *cache;
12
13   GST_DEBUG (0,"GstPipeline: play have type");
14
15   gst_element_set_state (pipeline, GST_STATE_PAUSED);
16
17   filesrc = gst_bin_get_by_name (GST_BIN (pipeline), "disk_source");
18   autobin = gst_bin_get_by_name (GST_BIN (pipeline), "autobin");
19   cache = gst_bin_get_by_name (GST_BIN (autobin), "cache");
20
21   /* unlink the typefind from the pipeline and remove it */
22   gst_element_unlink_pads (cache, "src", typefind, "sink");
23   gst_bin_remove (GST_BIN (autobin), typefind);
24       
25   /* and an audio sink */
26   osssink = gst_element_factory_make ("osssink", "play_audio");
27   g_assert (osssink != NULL);
28
29   videosink = gst_bin_new ("videosink");
30   /* and an video sink */
31   videoelement = gst_element_factory_make ("xvideosink", "play_video");
32   g_assert (videosink != NULL);
33
34   colorspace = gst_element_factory_make ("colorspace", "colorspace");
35   g_assert (colorspace != NULL);
36
37   gst_element_link_pads (colorspace, "src", videoelement, "sink");
38   gst_bin_add (GST_BIN (videosink), colorspace);
39   gst_bin_add (GST_BIN (videosink), videoelement);
40
41   gst_element_add_ghost_pad (videosink, 
42                   gst_element_get_pad (colorspace, "sink"), "sink");
43
44   autoplug = gst_autoplug_factory_make ("staticrender");
45   g_assert (autoplug != NULL);
46
47   new_element = gst_autoplug_to_renderers (autoplug,
48            caps,
49            videosink,
50            osssink,
51            NULL);
52
53   if (!new_element) {
54     g_print ("could not autoplug, no suitable codecs found...\n");
55     exit (-1);
56   }
57
58   gst_element_set_name (new_element, "new_element");
59
60   gst_bin_add (GST_BIN (autobin), new_element);
61
62   g_object_set (G_OBJECT (cache), "reset", TRUE, NULL);
63
64   gst_element_link_pads (cache, "src", new_element, "sink");
65
66   gst_element_set_state (pipeline, GST_STATE_PLAYING);
67       
68 #ifndef GST_DISABLE_LOADSAVE
69   gst_xml_write_file (GST_ELEMENT (pipeline), fopen ("xmlTest.gst", "w"));
70 #endif
71 }
72
73 static void
74 gst_play_cache_empty (GstElement *element, GstElement *pipeline)
75 {
76   GstElement *autobin;
77   GstElement *filesrc;
78   GstElement *cache;
79   GstElement *new_element;
80
81   fprintf (stderr, "have cache empty\n");
82
83   gst_element_set_state (pipeline, GST_STATE_PAUSED);
84
85   filesrc = gst_bin_get_by_name (GST_BIN (pipeline), "disk_source");
86   autobin = gst_bin_get_by_name (GST_BIN (pipeline), "autobin");
87   cache = gst_bin_get_by_name (GST_BIN (autobin), "cache");
88   new_element = gst_bin_get_by_name (GST_BIN (autobin), "new_element");
89
90   gst_element_unlink_many (filesrc, cache, new_element, NULL);
91   gst_bin_remove (GST_BIN (autobin), cache);
92   gst_element_link_pads (filesrc, "src", new_element, "sink");
93
94   gst_element_set_state (pipeline, GST_STATE_PLAYING);
95
96   fprintf (stderr, "done with cache_empty\n");
97 }
98
99 int main(int argc,char *argv[]) 
100 {
101   GstElement *filesrc;
102   GstElement *pipeline;
103   GstElement *autobin;
104   GstElement *typefind;
105   GstElement *cache;
106
107   g_thread_init(NULL);
108   gst_init(&argc,&argv);
109
110   if (argc != 2) {
111     g_print("usage: %s <filename>\n", argv[0]);
112     exit(-1);
113   }
114
115   /* create a new pipeline to hold the elements */
116   pipeline = gst_pipeline_new("pipeline");
117   g_assert(pipeline != NULL);
118
119   /* create a disk reader */
120   filesrc = gst_element_factory_make("filesrc", "disk_source");
121   g_assert(filesrc != NULL);
122   g_object_set(G_OBJECT(filesrc),"location", argv[1],NULL);
123   gst_bin_add (GST_BIN (pipeline), filesrc);
124
125   autobin = gst_bin_new ("autobin");
126   cache = gst_element_factory_make ("autoplugcache", "cache");
127   g_signal_connect (G_OBJECT (cache), "cache_empty", G_CALLBACK (gst_play_cache_empty), pipeline);
128
129   typefind = gst_element_factory_make ("typefind", "typefind");
130   g_signal_connect (G_OBJECT (typefind), "have_type", G_CALLBACK (gst_play_have_type), pipeline);
131   gst_bin_add (GST_BIN (autobin), cache);
132   gst_bin_add (GST_BIN (autobin), typefind);
133
134   gst_element_link_pads (cache, "src", typefind, "sink");
135   gst_element_add_ghost_pad (autobin, gst_element_get_pad (cache, "sink"), "sink");
136
137   gst_bin_add (GST_BIN( pipeline), autobin);
138   gst_element_link_pads (filesrc, "src", autobin, "sink");
139
140   /* start playing */
141   gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
142
143   while (gst_bin_iterate (GST_BIN (pipeline)));
144
145   /* stop the pipeline */
146   gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
147
148   gst_object_unref (GST_OBJECT (pipeline));
149
150   exit(0);
151 }
152