1 <chapter id="cha-hello2">
2 <title>Your second application</title>
4 FIXME: delete this section, talk more about the spider. In a previous chapter we created a first
5 version of the helloworld application. We then explained a better way of creating the elements
6 using factories identified by MIME types and the autoplugger.
10 <title>Autoplugging helloworld </title>
12 We will create a second version of the helloworld application using
13 autoplugging. Its source code is a bit more complicated but
14 it can handle many more data types. It can even play the audio track
18 Here is the full program listing. Start by looking at the main ()
23 /* example-begin helloworld2.c */
24 #include <gst/gst.h>
26 static void gst_play_have_type (GstElement *typefind, GstCaps *caps, GstElement *pipeline);
27 static void gst_play_cache_empty (GstElement *element, GstElement *pipeline);
30 gst_play_have_type (GstElement *typefind, GstCaps *caps, GstElement *pipeline)
33 GstElement *new_element;
34 GstAutoplug *autoplug;
39 g_print ("GstPipeline: play have type\n");
41 gst_element_set_state (pipeline, GST_STATE_PAUSED);
43 filesrc = gst_bin_get_by_name (GST_BIN (pipeline), "disk_source");
44 autobin = gst_bin_get_by_name (GST_BIN (pipeline), "autobin");
45 cache = gst_bin_get_by_name (GST_BIN (autobin), "cache");
47 /* unlink the typefind from the pipeline and remove it */
48 gst_element_unlink (cache, typefind);
49 gst_bin_remove (GST_BIN (autobin), typefind);
51 /* and an audio sink */
52 osssink = gst_element_factory_make ("osssink", "play_audio");
53 g_assert (osssink != NULL);
55 autoplug = gst_autoplug_factory_make ("staticrender");
56 g_assert (autoplug != NULL);
58 new_element = gst_autoplug_to_renderers (autoplug, caps, osssink, NULL);
61 g_print ("could not autoplug, no suitable codecs found...\n");
65 gst_element_set_name (new_element, "new_element");
67 gst_bin_add (GST_BIN (autobin), new_element);
69 g_object_set (G_OBJECT (cache), "reset", TRUE, NULL);
71 gst_element_link (cache, new_element);
73 gst_element_set_state (pipeline, GST_STATE_PLAYING);
77 gst_play_cache_empty (GstElement *element, GstElement *pipeline)
82 GstElement *new_element;
84 g_print ("have cache empty\n");
86 gst_element_set_state (pipeline, GST_STATE_PAUSED);
88 filesrc = gst_bin_get_by_name (GST_BIN (pipeline), "disk_source");
89 autobin = gst_bin_get_by_name (GST_BIN (pipeline), "autobin");
90 cache = gst_bin_get_by_name (GST_BIN (autobin), "cache");
91 new_element = gst_bin_get_by_name (GST_BIN (autobin), "new_element");
93 gst_element_unlink (filesrc, cache);
94 gst_element_unlink (cache, new_element);
95 gst_bin_remove (GST_BIN (autobin), cache);
96 gst_element_link (filesrc, new_element);
98 gst_element_set_state (pipeline, GST_STATE_PLAYING);
100 g_print ("done with cache_empty\n");
104 main (int argc, char *argv[])
107 GstElement *pipeline;
109 GstElement *typefind;
112 gst_init (&argc, &argv);
115 g_print ("usage: %s <filename with audio>\n", argv[0]);
119 /* create a new pipeline to hold the elements */
120 pipeline = gst_pipeline_new ("pipeline");
121 g_assert (pipeline != NULL);
123 /* create a disk reader */
124 filesrc = gst_element_factory_make ("filesrc", "disk_source");
125 g_assert (filesrc != NULL);
126 g_object_set (G_OBJECT (filesrc), "location", argv[1], NULL);
127 gst_bin_add (GST_BIN (pipeline), filesrc);
129 autobin = gst_bin_new ("autobin");
130 cache = gst_element_factory_make ("autoplugcache", "cache");
131 g_signal_connect (G_OBJECT (cache), "cache_empty",
132 G_CALLBACK (gst_play_cache_empty), pipeline);
134 typefind = gst_element_factory_make ("typefind", "typefind");
135 g_signal_connect (G_OBJECT (typefind), "have_type",
136 G_CALLBACK (gst_play_have_type), pipeline);
137 gst_bin_add (GST_BIN (autobin), cache);
138 gst_bin_add (GST_BIN (autobin), typefind);
140 gst_element_link (cache, typefind);
141 gst_element_add_ghost_pad (autobin,
142 gst_element_get_pad (cache, "sink"), "sink");
144 gst_bin_add (GST_BIN( pipeline), autobin);
145 gst_element_link (filesrc, autobin);
148 gst_element_set_state( GST_ELEMENT (pipeline), GST_STATE_PLAYING);
150 while (gst_bin_iterate (GST_BIN (pipeline)));
152 /* stop the pipeline */
153 gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
155 gst_object_unref (GST_OBJECT (pipeline));
159 /* example-end helloworld2.c */
162 We start by constructing a 'filesrc' element and an 'autobin' element that
163 holds the autoplugcache and the typefind element.
166 We attach the "cache_empty" signal to gst_play_cache_empty and the
167 "have_type" to our gst_play_have_type function.
171 The _have_type function first sets the pipeline to the PAUSED state
172 so that it can safely modify the pipeline. It then finds the elements
173 it is going to manipulate in the pipeline with:
176 filesrc = gst_bin_get_by_name (GST_BIN (pipeline), "disk_source");
177 autobin = gst_bin_get_by_name (GST_BIN (pipeline), "autobin");
178 cache = gst_bin_get_by_name (GST_BIN (autobin), "cache");
182 Now we have a handle to the elements we are going to manipulate in
186 We don't need the typefind element anymore so we remove it from
190 /* unlink the typefind from the pipeline and remove it */
191 gst_element_unlink (cache, "src", typefind, "sink");
192 gst_bin_remove (GST_BIN (autobin), typefind);
196 Our next step is to construct an element that can play the type we just
197 detected. We are going to use the autoplugger to create an element that
198 links the type to an osssink. We add the new element to our
203 /* and an audio sink */
204 osssink = gst_element_factory_make("osssink", "play_audio");
205 g_assert(osssink != NULL);
207 autoplug = gst_autoplug_factory_make ("staticrender");
208 g_assert (autoplug != NULL);
210 new_element = gst_autoplug_to_renderers (autoplug,
216 g_print ("could not autoplug, no suitable codecs found...\n");
220 gst_element_set_name (new_element, "new_element");
222 gst_bin_add (GST_BIN (autobin), new_element);
226 Our next step is to reset the cache so that the buffers used by the
227 typefind element are fed into the new element we just created. We reset
228 the cache by setting the "reset" property of the cache element to TRUE.
231 g_object_set (G_OBJECT (cache), "reset", TRUE, NULL);
233 gst_element_link (cache, "src", new_element, "sink");
236 Finally we set the pipeline back to the playing state. At this point the
237 cache will replay the buffers. We will be notified when the cache is empty
238 by the gst_play_cache_empty callback function.
242 The cache empty function simply removes the autoplugcache element from
243 the pipeline and relinks the filesrc to the autoplugged element.
247 To compile the helloworld2 example, use:
250 gcc -Wall `pkg-config gstreamer-0.7 --cflags --libs` helloworld2.c \
254 You can run the example with
255 (substitute helloworld.mp3 with you favorite audio file):
258 ./helloworld2 helloworld.mp3
261 You can also try to use an AVI or MPEG file as its input.
263 <application>GStreamer</application>
264 will automatically figure out how to
266 Remember that only the audio part will be played because
267 we have only added an osssink to the pipeline.
270 ./helloworld2 mymovie.mpeg