2000-08-22 21:18:18 +00:00
|
|
|
<chapter id="cha-hello2">
|
|
|
|
<title>Your second application</title>
|
2002-04-07 23:32:16 +00:00
|
|
|
<para>
|
|
|
|
FIXME: delete this section, talk more about the spider. In a previous chapter we created a first
|
|
|
|
version of the helloworld application. We then explained a better way of creating the elements
|
2001-07-07 14:17:22 +00:00
|
|
|
using factories identified by MIME types and the autoplugger.
|
2000-08-22 21:18:18 +00:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<sect1>
|
2001-07-07 14:17:22 +00:00
|
|
|
<title>Autoplugging helloworld </title>
|
2000-08-22 21:18:18 +00:00
|
|
|
<para>
|
|
|
|
We will create a second version of the helloworld application using
|
2001-07-07 14:17:22 +00:00
|
|
|
autoplugging. Its source code is a bit more complicated but
|
2002-09-15 13:57:34 +00:00
|
|
|
it can handle many more data types. It can even play the audio track
|
|
|
|
of a video file.
|
2001-07-07 14:17:22 +00:00
|
|
|
</para>
|
|
|
|
<para>
|
2002-09-15 13:57:34 +00:00
|
|
|
Here is the full program listing. Start by looking at the main ()
|
|
|
|
function.
|
2000-08-22 21:18:18 +00:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<programlisting>
|
2002-09-15 13:57:34 +00:00
|
|
|
/* example-begin helloworld2.c */
|
2000-08-22 21:18:18 +00:00
|
|
|
#include <gst/gst.h>
|
|
|
|
|
2001-07-07 14:17:22 +00:00
|
|
|
static void gst_play_have_type (GstElement *typefind, GstCaps *caps, GstElement *pipeline);
|
|
|
|
static void gst_play_cache_empty (GstElement *element, GstElement *pipeline);
|
2000-08-22 21:18:18 +00:00
|
|
|
|
2002-09-15 13:57:34 +00:00
|
|
|
static void
|
|
|
|
gst_play_have_type (GstElement *typefind, GstCaps *caps, GstElement *pipeline)
|
|
|
|
{
|
|
|
|
GstElement *osssink;
|
|
|
|
GstElement *new_element;
|
|
|
|
GstAutoplug *autoplug;
|
|
|
|
GstElement *autobin;
|
|
|
|
GstElement *filesrc;
|
|
|
|
GstElement *cache;
|
|
|
|
|
2002-09-15 14:07:41 +00:00
|
|
|
g_print ("GstPipeline: play have type\n");
|
2002-09-15 13:57:34 +00:00
|
|
|
|
|
|
|
gst_element_set_state (pipeline, GST_STATE_PAUSED);
|
|
|
|
|
|
|
|
filesrc = gst_bin_get_by_name (GST_BIN (pipeline), "disk_source");
|
|
|
|
autobin = gst_bin_get_by_name (GST_BIN (pipeline), "autobin");
|
|
|
|
cache = gst_bin_get_by_name (GST_BIN (autobin), "cache");
|
|
|
|
|
2003-01-09 14:15:37 +00:00
|
|
|
/* unlink the typefind from the pipeline and remove it */
|
|
|
|
gst_element_unlink (cache, typefind);
|
2002-09-15 13:57:34 +00:00
|
|
|
gst_bin_remove (GST_BIN (autobin), typefind);
|
|
|
|
|
|
|
|
/* and an audio sink */
|
|
|
|
osssink = gst_element_factory_make ("osssink", "play_audio");
|
2002-09-15 14:07:41 +00:00
|
|
|
g_assert (osssink != NULL);
|
2002-09-15 13:57:34 +00:00
|
|
|
|
|
|
|
autoplug = gst_autoplug_factory_make ("staticrender");
|
|
|
|
g_assert (autoplug != NULL);
|
|
|
|
|
|
|
|
new_element = gst_autoplug_to_renderers (autoplug, caps, osssink, NULL);
|
|
|
|
|
|
|
|
if (!new_element) {
|
|
|
|
g_print ("could not autoplug, no suitable codecs found...\n");
|
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_element_set_name (new_element, "new_element");
|
|
|
|
|
|
|
|
gst_bin_add (GST_BIN (autobin), new_element);
|
|
|
|
|
|
|
|
g_object_set (G_OBJECT (cache), "reset", TRUE, NULL);
|
|
|
|
|
2003-01-09 14:15:37 +00:00
|
|
|
gst_element_link (cache, new_element);
|
2002-09-15 13:57:34 +00:00
|
|
|
|
|
|
|
gst_element_set_state (pipeline, GST_STATE_PLAYING);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_play_cache_empty (GstElement *element, GstElement *pipeline)
|
|
|
|
{
|
|
|
|
GstElement *autobin;
|
|
|
|
GstElement *filesrc;
|
|
|
|
GstElement *cache;
|
|
|
|
GstElement *new_element;
|
|
|
|
|
2002-09-15 22:35:55 +00:00
|
|
|
g_print ("have cache empty\n");
|
2002-09-15 13:57:34 +00:00
|
|
|
|
|
|
|
gst_element_set_state (pipeline, GST_STATE_PAUSED);
|
|
|
|
|
|
|
|
filesrc = gst_bin_get_by_name (GST_BIN (pipeline), "disk_source");
|
|
|
|
autobin = gst_bin_get_by_name (GST_BIN (pipeline), "autobin");
|
|
|
|
cache = gst_bin_get_by_name (GST_BIN (autobin), "cache");
|
|
|
|
new_element = gst_bin_get_by_name (GST_BIN (autobin), "new_element");
|
|
|
|
|
2003-01-09 14:15:37 +00:00
|
|
|
gst_element_unlink (filesrc, cache);
|
|
|
|
gst_element_unlink (cache, new_element);
|
2002-09-15 13:57:34 +00:00
|
|
|
gst_bin_remove (GST_BIN (autobin), cache);
|
2003-01-09 14:15:37 +00:00
|
|
|
gst_element_link (filesrc, new_element);
|
2002-09-15 13:57:34 +00:00
|
|
|
|
|
|
|
gst_element_set_state (pipeline, GST_STATE_PLAYING);
|
|
|
|
|
2002-09-15 14:07:41 +00:00
|
|
|
g_print ("done with cache_empty\n");
|
2002-09-15 13:57:34 +00:00
|
|
|
}
|
|
|
|
|
2001-01-05 18:50:41 +00:00
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
2000-08-22 21:18:18 +00:00
|
|
|
{
|
2002-01-06 04:26:37 +00:00
|
|
|
GstElement *filesrc;
|
2000-09-09 16:36:10 +00:00
|
|
|
GstElement *pipeline;
|
2001-07-07 14:17:22 +00:00
|
|
|
GstElement *autobin;
|
|
|
|
GstElement *typefind;
|
|
|
|
GstElement *cache;
|
|
|
|
|
|
|
|
gst_init (&argc, &argv);
|
2000-08-22 21:18:18 +00:00
|
|
|
|
|
|
|
if (argc != 2) {
|
2002-09-15 14:07:41 +00:00
|
|
|
g_print ("usage: %s <filename with audio>\n", argv[0]);
|
2001-01-05 18:50:41 +00:00
|
|
|
exit (-1);
|
2000-08-22 21:18:18 +00:00
|
|
|
}
|
|
|
|
|
2001-07-07 14:17:22 +00:00
|
|
|
/* create a new pipeline to hold the elements */
|
2001-01-05 18:50:41 +00:00
|
|
|
pipeline = gst_pipeline_new ("pipeline");
|
2001-07-07 14:17:22 +00:00
|
|
|
g_assert (pipeline != NULL);
|
2000-08-22 21:18:18 +00:00
|
|
|
|
|
|
|
/* create a disk reader */
|
2002-04-11 20:35:18 +00:00
|
|
|
filesrc = gst_element_factory_make ("filesrc", "disk_source");
|
2002-01-06 04:26:37 +00:00
|
|
|
g_assert (filesrc != NULL);
|
|
|
|
g_object_set (G_OBJECT (filesrc), "location", argv[1], NULL);
|
|
|
|
gst_bin_add (GST_BIN (pipeline), filesrc);
|
2000-08-22 21:18:18 +00:00
|
|
|
|
2001-07-07 14:17:22 +00:00
|
|
|
autobin = gst_bin_new ("autobin");
|
2002-04-11 20:35:18 +00:00
|
|
|
cache = gst_element_factory_make ("autoplugcache", "cache");
|
2001-08-13 19:00:13 +00:00
|
|
|
g_signal_connect (G_OBJECT (cache), "cache_empty",
|
|
|
|
G_CALLBACK (gst_play_cache_empty), pipeline);
|
2000-08-22 21:18:18 +00:00
|
|
|
|
2002-04-11 20:35:18 +00:00
|
|
|
typefind = gst_element_factory_make ("typefind", "typefind");
|
2001-08-13 19:00:13 +00:00
|
|
|
g_signal_connect (G_OBJECT (typefind), "have_type",
|
|
|
|
G_CALLBACK (gst_play_have_type), pipeline);
|
2001-07-07 14:17:22 +00:00
|
|
|
gst_bin_add (GST_BIN (autobin), cache);
|
|
|
|
gst_bin_add (GST_BIN (autobin), typefind);
|
2000-08-22 21:18:18 +00:00
|
|
|
|
2003-01-09 14:15:37 +00:00
|
|
|
gst_element_link (cache, typefind);
|
2002-09-15 13:57:34 +00:00
|
|
|
gst_element_add_ghost_pad (autobin,
|
|
|
|
gst_element_get_pad (cache, "sink"), "sink");
|
2000-08-22 21:18:18 +00:00
|
|
|
|
2001-07-07 14:17:22 +00:00
|
|
|
gst_bin_add (GST_BIN( pipeline), autobin);
|
2003-01-09 14:15:37 +00:00
|
|
|
gst_element_link (filesrc, autobin);
|
2000-08-22 21:18:18 +00:00
|
|
|
|
2001-07-07 14:17:22 +00:00
|
|
|
/* start playing */
|
|
|
|
gst_element_set_state( GST_ELEMENT (pipeline), GST_STATE_PLAYING);
|
2000-08-22 21:18:18 +00:00
|
|
|
|
2001-07-07 14:17:22 +00:00
|
|
|
while (gst_bin_iterate (GST_BIN (pipeline)));
|
2000-08-22 21:18:18 +00:00
|
|
|
|
2001-07-07 14:17:22 +00:00
|
|
|
/* stop the pipeline */
|
2001-01-05 18:50:41 +00:00
|
|
|
gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
|
2000-08-22 21:18:18 +00:00
|
|
|
|
2001-07-07 14:17:22 +00:00
|
|
|
gst_object_unref (GST_OBJECT (pipeline));
|
2000-08-22 21:18:18 +00:00
|
|
|
|
2001-07-07 14:17:22 +00:00
|
|
|
exit(0);
|
2000-08-22 21:18:18 +00:00
|
|
|
}
|
2002-09-15 13:57:34 +00:00
|
|
|
/* example-end helloworld2.c */
|
2000-08-22 21:18:18 +00:00
|
|
|
</programlisting>
|
|
|
|
<para>
|
2002-01-06 04:26:37 +00:00
|
|
|
We start by constructing a 'filesrc' element and an 'autobin' element that
|
2001-07-07 14:17:22 +00:00
|
|
|
holds the autoplugcache and the typefind element.
|
2000-08-22 21:18:18 +00:00
|
|
|
</para>
|
|
|
|
<para>
|
2001-07-07 14:17:22 +00:00
|
|
|
We attach the "cache_empty" signal to gst_play_cache_empty and the
|
2002-09-15 13:57:34 +00:00
|
|
|
"have_type" to our gst_play_have_type function.
|
2000-08-22 21:18:18 +00:00
|
|
|
</para>
|
2001-07-07 14:17:22 +00:00
|
|
|
|
|
|
|
<para>
|
2002-09-15 13:57:34 +00:00
|
|
|
The _have_type function first sets the pipeline to the PAUSED state
|
|
|
|
so that it can safely modify the pipeline. It then finds the elements
|
|
|
|
it is going to manipulate in the pipeline with:
|
2001-07-07 14:17:22 +00:00
|
|
|
</para>
|
|
|
|
<programlisting>
|
2002-01-06 04:26:37 +00:00
|
|
|
filesrc = gst_bin_get_by_name (GST_BIN (pipeline), "disk_source");
|
2001-07-07 14:17:22 +00:00
|
|
|
autobin = gst_bin_get_by_name (GST_BIN (pipeline), "autobin");
|
|
|
|
cache = gst_bin_get_by_name (GST_BIN (autobin), "cache");
|
2000-08-22 21:18:18 +00:00
|
|
|
</programlisting>
|
|
|
|
|
|
|
|
<para>
|
2001-07-07 14:17:22 +00:00
|
|
|
Now we have a handle to the elements we are going to manipulate in
|
|
|
|
the next step.
|
2000-08-22 21:18:18 +00:00
|
|
|
</para>
|
2001-07-07 14:17:22 +00:00
|
|
|
<para>
|
2002-09-15 13:57:34 +00:00
|
|
|
We don't need the typefind element anymore so we remove it from
|
|
|
|
the pipeline:
|
2001-07-07 14:17:22 +00:00
|
|
|
</para>
|
|
|
|
<programlisting>
|
2003-01-09 14:15:37 +00:00
|
|
|
/* unlink the typefind from the pipeline and remove it */
|
|
|
|
gst_element_unlink (cache, "src", typefind, "sink");
|
2001-07-07 14:17:22 +00:00
|
|
|
gst_bin_remove (GST_BIN (autobin), typefind);
|
|
|
|
</programlisting>
|
2000-08-22 21:18:18 +00:00
|
|
|
|
|
|
|
<para>
|
2001-07-07 14:17:22 +00:00
|
|
|
Our next step is to construct an element that can play the type we just
|
|
|
|
detected. We are going to use the autoplugger to create an element that
|
2003-01-09 14:15:37 +00:00
|
|
|
links the type to an osssink. We add the new element to our
|
2001-07-07 14:17:22 +00:00
|
|
|
autobin.
|
2000-08-22 21:18:18 +00:00
|
|
|
</para>
|
|
|
|
|
2001-07-07 14:17:22 +00:00
|
|
|
<programlisting>
|
|
|
|
/* and an audio sink */
|
2002-04-11 20:35:18 +00:00
|
|
|
osssink = gst_element_factory_make("osssink", "play_audio");
|
2001-07-07 14:17:22 +00:00
|
|
|
g_assert(osssink != NULL);
|
|
|
|
|
2002-04-11 20:35:18 +00:00
|
|
|
autoplug = gst_autoplug_factory_make ("staticrender");
|
2001-07-07 14:17:22 +00:00
|
|
|
g_assert (autoplug != NULL);
|
|
|
|
|
|
|
|
new_element = gst_autoplug_to_renderers (autoplug,
|
|
|
|
caps,
|
|
|
|
osssink,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
if (!new_element) {
|
|
|
|
g_print ("could not autoplug, no suitable codecs found...\n");
|
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_element_set_name (new_element, "new_element");
|
|
|
|
|
|
|
|
gst_bin_add (GST_BIN (autobin), new_element);
|
|
|
|
</programlisting>
|
|
|
|
|
2000-08-22 21:18:18 +00:00
|
|
|
<para>
|
2001-07-07 14:17:22 +00:00
|
|
|
Our next step is to reset the cache so that the buffers used by the
|
|
|
|
typefind element are fed into the new element we just created. We reset
|
|
|
|
the cache by setting the "reset" property of the cache element to TRUE.
|
2000-08-22 21:18:18 +00:00
|
|
|
</para>
|
2001-07-07 14:17:22 +00:00
|
|
|
<programlisting>
|
|
|
|
g_object_set (G_OBJECT (cache), "reset", TRUE, NULL);
|
|
|
|
|
2003-01-09 14:15:37 +00:00
|
|
|
gst_element_link (cache, "src", new_element, "sink");
|
2001-07-07 14:17:22 +00:00
|
|
|
</programlisting>
|
|
|
|
<para>
|
|
|
|
Finally we set the pipeline back to the playing state. At this point the
|
|
|
|
cache will replay the buffers. We will be notified when the cache is empty
|
2002-09-15 13:57:34 +00:00
|
|
|
by the gst_play_cache_empty callback function.
|
2001-07-18 22:18:02 +00:00
|
|
|
</para>
|
2001-07-07 14:17:22 +00:00
|
|
|
|
2000-08-22 21:18:18 +00:00
|
|
|
<para>
|
2001-07-07 14:17:22 +00:00
|
|
|
The cache empty function simply removes the autoplugcache element from
|
2003-01-09 14:15:37 +00:00
|
|
|
the pipeline and relinks the filesrc to the autoplugged element.
|
2000-08-22 21:18:18 +00:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
To compile the helloworld2 example, use:
|
|
|
|
</para>
|
|
|
|
<programlisting>
|
2003-10-08 18:21:41 +00:00
|
|
|
gcc -Wall `pkg-config gstreamer-&GST_MAJORMINOR; --cflags --libs` helloworld2.c \
|
2000-09-09 16:36:10 +00:00
|
|
|
-o helloworld2
|
2000-08-22 21:18:18 +00:00
|
|
|
</programlisting>
|
|
|
|
<para>
|
2002-09-15 13:57:34 +00:00
|
|
|
You can run the example with
|
|
|
|
(substitute helloworld.mp3 with you favorite audio file):
|
2000-08-22 21:18:18 +00:00
|
|
|
</para>
|
|
|
|
<programlisting>
|
|
|
|
./helloworld2 helloworld.mp3
|
|
|
|
</programlisting>
|
|
|
|
<para>
|
2002-09-15 13:57:34 +00:00
|
|
|
You can also try to use an AVI or MPEG file as its input.
|
|
|
|
Using autoplugging,
|
|
|
|
<application>GStreamer</application>
|
|
|
|
will automatically figure out how to
|
|
|
|
handle the stream.
|
|
|
|
Remember that only the audio part will be played because
|
2001-07-07 14:17:22 +00:00
|
|
|
we have only added an osssink to the pipeline.
|
2000-08-22 21:18:18 +00:00
|
|
|
</para>
|
|
|
|
<programlisting>
|
2000-09-09 16:36:10 +00:00
|
|
|
./helloworld2 mymovie.mpeg
|
2000-08-22 21:18:18 +00:00
|
|
|
</programlisting>
|
|
|
|
|
|
|
|
</sect1>
|
|
|
|
</chapter>
|