2004-01-27 13:33:39 +00:00
|
|
|
<!-- ############ chapter ############# -->
|
2002-09-27 18:34:33 +00:00
|
|
|
|
2004-01-28 15:51:14 +00:00
|
|
|
<chapter id="chapter-building-testapp">
|
2004-01-27 13:33:39 +00:00
|
|
|
<title>Building a Test Application</title>
|
2002-09-27 18:34:33 +00:00
|
|
|
<para>
|
2004-01-27 13:33:39 +00:00
|
|
|
Often, you will want to test your newly written plugin in an as small
|
2004-11-06 10:28:07 +00:00
|
|
|
setting as possible. Usually, <filename>gst-launch</filename> is a
|
2007-07-26 08:46:46 +00:00
|
|
|
good first step at testing a plugin. If you have not installed your
|
|
|
|
plugin in a directory that GStreamer searches, then you will need to
|
|
|
|
set the plugin path. Either set GST_PLUGIN_PATH to the directory
|
|
|
|
containing your plugin, or use the command-line option --gst-plugin-path.
|
|
|
|
If you based your plugin off of the gst-plugin template, then this
|
|
|
|
will look something like
|
|
|
|
<command>
|
|
|
|
gst-launch --gst-plugin-path=$HOME/gst-template/gst-plugin/src/.libs TESTPIPELINE
|
|
|
|
</command>
|
|
|
|
However, you will often need more
|
2004-01-27 13:33:39 +00:00
|
|
|
testing features than gst-launch can provide, such as seeking, events,
|
|
|
|
interactivity and more. Writing your own small testing program is the
|
|
|
|
easiest way to accomplish this. This section explains - in a few words
|
|
|
|
- how to do that. For a complete application development guide, see the
|
2004-05-01 19:44:24 +00:00
|
|
|
<ulink type="http" url="../../manual/html/index.html">Application Development
|
2004-01-27 13:33:39 +00:00
|
|
|
Manual</ulink>.
|
2002-09-27 18:34:33 +00:00
|
|
|
</para>
|
|
|
|
|
2004-01-27 13:33:39 +00:00
|
|
|
<para>
|
|
|
|
At the start, you need to initialize the &GStreamer; core library by
|
|
|
|
calling <function>gst_init ()</function>. You can alternatively call
|
|
|
|
<function>gst_init_with_popt_tables ()</function>, which will return
|
|
|
|
a pointer to popt tables. You can then use libpopt to handle the
|
|
|
|
given argument table, and this will finish the &GStreamer; intialization.
|
|
|
|
</para>
|
2002-09-27 18:34:33 +00:00
|
|
|
|
2004-01-27 13:33:39 +00:00
|
|
|
<para>
|
|
|
|
You can create elements using <function>gst_element_factory_make ()</function>,
|
|
|
|
where the first argument is the element type that you want to create,
|
|
|
|
and the second argument is a free-form name. The example at the end uses
|
|
|
|
a simple filesource - decoder - soundcard output pipeline, but you can
|
|
|
|
use specific debugging elements if that's necessary. For example, an
|
|
|
|
<classname>identity</classname> element can be used in the middle of
|
|
|
|
the pipeline to act as a data-to-application transmitter. This can be
|
|
|
|
used to check the data for misbehaviours or correctness in your test
|
|
|
|
application. Also, you can use a <classname>fakesink</classname>
|
|
|
|
element at the end of the pipeline to dump your data to the stdout
|
|
|
|
(in order to do this, set the <function>dump</function> property to
|
|
|
|
TRUE). Lastly, you can use the <classname>efence</classname> element
|
|
|
|
(indeed, an eletric fence memory debugger wrapper element) to check
|
|
|
|
for memory errors.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
During linking, your test application can use fixation or filtered caps
|
|
|
|
as a way to drive a specific type of data to or from your element. This
|
|
|
|
is a very simple and effective way of checking multiple types of input
|
|
|
|
and output in your element.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Running the pipeline happens through the <function>gst_bin_iterate ()</function>
|
|
|
|
function. Note that during running, you should connect to at least the
|
|
|
|
<quote>error</quote> and <quote>eos</quote> signals on the pipeline
|
|
|
|
and/or your plugin/element to check for correct handling of this. Also,
|
|
|
|
you should add events into the pipeline and make sure your plugin handles
|
|
|
|
these correctly (with respect to clocking, internal caching, etc.).
|
|
|
|
</para>
|
2002-09-27 18:34:33 +00:00
|
|
|
|
2004-01-27 13:33:39 +00:00
|
|
|
<para>
|
|
|
|
Never forget to clean up memory in your plugin or your test application.
|
|
|
|
When going to the NULL state, your element should clean up allocated
|
|
|
|
memory and caches. Also, it should close down any references held to
|
|
|
|
possible support libraries. Your application should <function>unref ()</function>
|
|
|
|
the pipeline and make sure it doesn't crash.
|
|
|
|
</para>
|
|
|
|
|
2005-07-01 12:43:03 +00:00
|
|
|
<programlisting><!-- example-begin test.c -->
|
2004-01-27 13:33:39 +00:00
|
|
|
#include <gst/gst.h>
|
|
|
|
|
2005-07-01 12:43:03 +00:00
|
|
|
static gboolean
|
|
|
|
bus_call (GstBus *bus,
|
|
|
|
GstMessage *msg,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GMainLoop *loop = data;
|
|
|
|
|
|
|
|
switch (GST_MESSAGE_TYPE (msg)) {
|
|
|
|
case GST_MESSAGE_EOS:
|
|
|
|
g_print ("End-of-stream\n");
|
|
|
|
g_main_loop_quit (loop);
|
|
|
|
break;
|
|
|
|
case GST_MESSAGE_ERROR: {
|
2007-01-26 18:24:56 +00:00
|
|
|
gchar *debug = NULL;
|
|
|
|
GError *err = NULL;
|
2005-07-01 12:43:03 +00:00
|
|
|
|
|
|
|
gst_message_parse_error (msg, &err, &debug);
|
|
|
|
|
|
|
|
g_print ("Error: %s\n", err->message);
|
|
|
|
g_error_free (err);
|
|
|
|
|
2007-01-26 18:24:56 +00:00
|
|
|
if (debug) {
|
|
|
|
g_print ("Debug deails: %s\n", debug);
|
|
|
|
g_free (debug);
|
|
|
|
}
|
|
|
|
|
2005-07-01 12:43:03 +00:00
|
|
|
g_main_loop_quit (loop);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2004-01-27 13:33:39 +00:00
|
|
|
gint
|
2005-07-01 12:43:03 +00:00
|
|
|
main (gint argc,
|
2004-01-27 13:33:39 +00:00
|
|
|
gchar *argv[])
|
|
|
|
{
|
2007-01-26 18:24:56 +00:00
|
|
|
GstStateChangeReturn ret;
|
2004-01-27 13:33:39 +00:00
|
|
|
GstElement *pipeline, *filesrc, *decoder, *filter, *sink;
|
2007-01-26 18:24:56 +00:00
|
|
|
GstElement *convert1, *convert2, *resample;
|
2005-07-01 12:43:03 +00:00
|
|
|
GMainLoop *loop;
|
2007-01-26 18:24:56 +00:00
|
|
|
GstBus *bus;
|
2004-01-27 13:33:39 +00:00
|
|
|
|
|
|
|
/* initialization */
|
|
|
|
gst_init (&argc, &argv);
|
2005-07-01 12:43:03 +00:00
|
|
|
loop = g_main_loop_new (NULL, FALSE);
|
2005-07-01 13:01:47 +00:00
|
|
|
if (argc != 2) {
|
|
|
|
g_print ("Usage: %s <mp3 filename>\n", argv[0]);
|
|
|
|
return 01;
|
|
|
|
}
|
2004-01-27 13:33:39 +00:00
|
|
|
|
|
|
|
/* create elements */
|
|
|
|
pipeline = gst_pipeline_new ("my_pipeline");
|
2007-01-26 18:24:56 +00:00
|
|
|
|
|
|
|
/* watch for messages on the pipeline's bus (note that this will only
|
|
|
|
* work like this when a GLib main loop is running) */
|
|
|
|
bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
|
|
|
|
gst_bus_add_watch (bus, bus_call, loop);
|
|
|
|
gst_object_unref (bus);
|
2004-01-27 13:33:39 +00:00
|
|
|
|
|
|
|
filesrc = gst_element_factory_make ("filesrc", "my_filesource");
|
|
|
|
decoder = gst_element_factory_make ("mad", "my_decoder");
|
2007-01-26 18:24:56 +00:00
|
|
|
|
|
|
|
/* putting an audioconvert element here to convert the output of the
|
|
|
|
* decoder into a format that my_filter can handle (we are assuming it
|
|
|
|
* will handle any sample rate here though) */
|
|
|
|
convert1 = gst_element_factory_make ("audioconvert", "audioconvert1");
|
|
|
|
|
|
|
|
/* use "identity" here for a filter that does nothing */
|
2004-01-27 13:33:39 +00:00
|
|
|
filter = gst_element_factory_make ("my_filter", "my_filter");
|
2007-01-26 18:24:56 +00:00
|
|
|
|
|
|
|
/* there should always be audioconvert and audioresample elements before
|
|
|
|
* the audio sink, since the capabilities of the audio sink usually vary
|
|
|
|
* depending on the environment (output used, sound card, driver etc.) */
|
|
|
|
convert2 = gst_element_factory_make ("audioconvert", "audioconvert2");
|
|
|
|
resample = gst_element_factory_make ("audioresample", "audioresample");
|
2004-01-27 13:33:39 +00:00
|
|
|
sink = gst_element_factory_make ("osssink", "audiosink");
|
2007-01-26 18:24:56 +00:00
|
|
|
|
2005-07-01 12:43:03 +00:00
|
|
|
if (!sink || !decoder) {
|
|
|
|
g_print ("Decoder or output could not be found - check your install\n");
|
|
|
|
return -1;
|
2007-01-26 18:24:56 +00:00
|
|
|
} else if (!convert1 || !convert2 || !resample) {
|
|
|
|
g_print ("Could not create audioconvert or audioresample element, "
|
|
|
|
"check your installation\n");
|
|
|
|
return -1;
|
2005-07-01 12:43:03 +00:00
|
|
|
} else if (!filter) {
|
|
|
|
g_print ("Your self-written filter could not be found. Make sure it "
|
2007-01-26 18:24:56 +00:00
|
|
|
"is installed correctly in $(libdir)/gstreamer-0.10/ or "
|
|
|
|
"~/.gstreamer-0.10/plugins/ and that gst-inspect-0.10 lists it. "
|
|
|
|
"If it doesn't, check with 'GST_DEBUG=*:2 gst-inspect-0.10' for "
|
|
|
|
"the reason why it is not being loaded.");
|
2005-07-01 12:43:03 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2004-01-27 13:33:39 +00:00
|
|
|
|
|
|
|
g_object_set (G_OBJECT (filesrc), "location", argv[1], NULL);
|
|
|
|
|
2007-01-26 18:24:56 +00:00
|
|
|
gst_bin_add_many (GST_BIN (pipeline), filesrc, decoder, convert1, filter,
|
|
|
|
convert2, resample, sink, NULL);
|
|
|
|
|
2004-01-27 13:33:39 +00:00
|
|
|
/* link everything together */
|
2007-01-26 18:24:56 +00:00
|
|
|
if (!gst_element_link_many (filesrc, decoder, convert1, filter, convert2,
|
|
|
|
resample, sink, NULL)) {
|
|
|
|
g_print ("Failed to link one or more elements!\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2004-01-27 13:33:39 +00:00
|
|
|
|
|
|
|
/* run */
|
2007-01-26 18:24:56 +00:00
|
|
|
ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
|
|
|
|
if (ret == GST_STATE_CHANGE_FAILURE) {
|
2008-07-12 17:43:15 +00:00
|
|
|
GstMessage *msg;
|
|
|
|
|
2007-01-26 18:24:56 +00:00
|
|
|
g_print ("Failed to start up pipeline!\n");
|
|
|
|
|
|
|
|
/* check if there is an error message with details on the bus */
|
|
|
|
msg = gst_bus_poll (bus, GST_MESSAGE_ERROR, 0);
|
|
|
|
if (msg) {
|
|
|
|
GError *err = NULL;
|
|
|
|
|
|
|
|
gst_message_parse_error (msg, &err, NULL);
|
|
|
|
g_print ("ERROR: %s\n", err->message);
|
|
|
|
g_error_free (err);
|
|
|
|
gst_message_unref (msg);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-07-01 12:43:03 +00:00
|
|
|
g_main_loop_run (loop);
|
2004-01-27 13:33:39 +00:00
|
|
|
|
|
|
|
/* clean up */
|
|
|
|
gst_element_set_state (pipeline, GST_STATE_NULL);
|
2007-01-26 18:24:56 +00:00
|
|
|
gst_object_unref (pipeline);
|
2004-01-27 13:33:39 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2005-07-01 12:43:03 +00:00
|
|
|
<!-- example-end test.c --></programlisting>
|
2004-01-27 13:33:39 +00:00
|
|
|
</chapter>
|