2000-08-22 21:18:18 +00:00
|
|
|
<chapter id="cha-hello2">
|
|
|
|
<title>Your second application</title>
|
|
|
|
<para>
|
|
|
|
In the previous chapter we created a first version of the helloworld
|
|
|
|
application. We then explained a better way of creating the elements
|
|
|
|
using factories identified by MIME types.
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
In this chapter we will introduce you to autoplugging. Using the MIME
|
2001-01-05 18:50:41 +00:00
|
|
|
types of the elements <application>GStreamer</application> can automatically create a pipeline
|
2000-08-22 21:18:18 +00:00
|
|
|
for you.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<sect1>
|
|
|
|
<title>Autoplugging helloworld</title>
|
|
|
|
<para>
|
|
|
|
We will create a second version of the helloworld application using
|
2000-09-18 22:19:57 +00:00
|
|
|
autoplugging. Its source code is considerably easier to write and
|
|
|
|
it can also handle many more data types.
|
2000-08-22 21:18:18 +00:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
|
|
|
|
#include <gst/gst.h>
|
|
|
|
|
|
|
|
static gboolean playing;
|
|
|
|
|
2000-09-09 16:36:10 +00:00
|
|
|
/* eos will be called when the src element has an end of stream */
|
2001-01-05 18:50:41 +00:00
|
|
|
void
|
|
|
|
eos (GstSrc *src)
|
2000-08-22 21:18:18 +00:00
|
|
|
{
|
2001-01-05 18:50:41 +00:00
|
|
|
g_print ("have eos, quitting\n");
|
2000-08-22 21:18:18 +00:00
|
|
|
|
|
|
|
playing = FALSE;
|
|
|
|
}
|
|
|
|
|
2001-01-05 18:50:41 +00:00
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
2000-08-22 21:18:18 +00:00
|
|
|
{
|
|
|
|
GstElement *disksrc, *audiosink;
|
2000-09-09 16:36:10 +00:00
|
|
|
GstElement *pipeline;
|
2000-08-22 21:18:18 +00:00
|
|
|
|
|
|
|
if (argc != 2) {
|
2001-02-16 16:48:54 +00:00
|
|
|
g_print ("usage: %s <filename>\n", argv[0]);
|
2001-01-05 18:50:41 +00:00
|
|
|
exit (-1);
|
2000-08-22 21:18:18 +00:00
|
|
|
}
|
|
|
|
|
2001-01-05 18:50:41 +00:00
|
|
|
gst_init (&argc, &argv);
|
2000-08-22 21:18:18 +00:00
|
|
|
|
|
|
|
/* create a new bin to hold the elements */
|
2001-01-05 18:50:41 +00:00
|
|
|
pipeline = gst_pipeline_new ("pipeline");
|
2000-08-22 21:18:18 +00:00
|
|
|
|
|
|
|
/* create a disk reader */
|
2001-01-05 18:50:41 +00:00
|
|
|
disksrc = gst_elementfactory_make ("disksrc", "disk_source");
|
|
|
|
gtk_object_set (GTK_OBJECT (disksrc), "location", argv[1], NULL);
|
|
|
|
gtk_signal_connect (GTK_OBJECT (disksrc), "eos",
|
|
|
|
GTK_SIGNAL_FUNC (eos), NULL);
|
2000-08-22 21:18:18 +00:00
|
|
|
|
|
|
|
/* and an audio sink */
|
2001-01-05 18:50:41 +00:00
|
|
|
audiosink = gst_elementfactory_make ("audiosink", "play_audio");
|
2000-08-22 21:18:18 +00:00
|
|
|
|
|
|
|
/* add objects to the main pipeline */
|
2001-01-05 18:50:41 +00:00
|
|
|
gst_pipeline_add_src (GST_PIPELINE (pipeline), disksrc);
|
|
|
|
gst_pipeline_add_sink (GST_PIPELINE (pipeline), audiosink);
|
2000-08-22 21:18:18 +00:00
|
|
|
|
2001-01-05 18:50:41 +00:00
|
|
|
if (!gst_pipeline_autoplug (GST_PIPELINE (pipeline))) {
|
|
|
|
g_print ("unable to handle stream\n");
|
|
|
|
exit (-1);
|
2000-08-22 21:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* start playing */
|
2001-01-05 18:50:41 +00:00
|
|
|
gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
|
2000-08-22 21:18:18 +00:00
|
|
|
|
|
|
|
playing = TRUE;
|
|
|
|
|
|
|
|
while (playing) {
|
2001-01-05 18:50:41 +00:00
|
|
|
gst_bin_iterate (GST_BIN (pipeline));
|
2000-08-22 21:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* stop the bin */
|
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-01-05 18:50:41 +00:00
|
|
|
gst_pipeline_destroy (pipeline);
|
2000-08-22 21:18:18 +00:00
|
|
|
|
2001-01-05 18:50:41 +00:00
|
|
|
exit (0);
|
2000-08-22 21:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
</programlisting>
|
|
|
|
|
|
|
|
<para>
|
2000-09-18 22:19:57 +00:00
|
|
|
First of all, we do not use any mpg123 or mp3parse element in this example.
|
2000-08-22 21:18:18 +00:00
|
|
|
In fact, we only specify a source element and a sink element and add them
|
|
|
|
to a pipeline.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The most interesting change however is the following:
|
|
|
|
</para>
|
|
|
|
<programlisting>
|
|
|
|
|
|
|
|
...
|
2001-01-05 18:50:41 +00:00
|
|
|
if (!gst_pipeline_autoplug (pipeline)) {
|
|
|
|
g_print ("unable to handle stream\n");
|
|
|
|
exit (-1);
|
2000-08-22 21:18:18 +00:00
|
|
|
}
|
|
|
|
...
|
|
|
|
|
|
|
|
</programlisting>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
This piece of code does all the magic.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
<itemizedlist>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2000-09-17 22:08:09 +00:00
|
|
|
The pipeline will try to connect the src and the sink element.
|
2000-08-22 21:18:18 +00:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Since the source has no type, a typedetection will be started on
|
|
|
|
the source element.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
The best set of elements that connect the MIME type of the source
|
|
|
|
element to the MIME type of the sink are found.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
The elements are added to the pipeline and their pads are connected.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</itemizedlist>
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
After this autoplugging, the pipeline is ready to play. Remember that this
|
|
|
|
pipeline will be able to playback all of the media types for which an
|
|
|
|
appropriate plugin exists since the autoplugging is all done using MIME
|
|
|
|
types.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2000-09-18 22:19:57 +00:00
|
|
|
If you really want, you can use the GSteamer components to do the
|
2001-01-05 18:50:41 +00:00
|
|
|
autoplugging yourself. We will cover this topic in the dynamic pipeline chapter.
|
2000-08-22 21:18:18 +00:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
To compile the helloworld2 example, use:
|
|
|
|
</para>
|
|
|
|
<programlisting>
|
2000-09-09 16:36:10 +00:00
|
|
|
gcc -Wall `gstreamer-config --cflags --libs` helloworld2.c \
|
|
|
|
-o helloworld2
|
2000-08-22 21:18:18 +00:00
|
|
|
</programlisting>
|
|
|
|
<para>
|
|
|
|
You can run the example with (substitute helloworld.mp3 with you favorite MP3 file):
|
|
|
|
</para>
|
|
|
|
<programlisting>
|
|
|
|
./helloworld2 helloworld.mp3
|
|
|
|
</programlisting>
|
|
|
|
<para>
|
|
|
|
You can also try to use an AVI or MPEG file as its input. Using autoplugging,
|
2001-01-05 18:50:41 +00:00
|
|
|
<application>GStreamer</application> will automatically figure out how to
|
|
|
|
handle the stream. Remember that only the audio part will be played because
|
|
|
|
we have only added an audiosink 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>
|