2004-01-28 15:08:17 +00:00
|
|
|
<chapter id="chapter-threads">
|
2000-08-22 21:18:18 +00:00
|
|
|
<title>Threads</title>
|
|
|
|
<para>
|
2002-04-07 23:32:16 +00:00
|
|
|
GStreamer has support for multithreading through the use of
|
2000-09-09 16:36:10 +00:00
|
|
|
the <classname>GstThread</classname> object. This object is in fact
|
|
|
|
a special <classname>GstBin</classname> that will become a thread when started.
|
2000-08-22 21:18:18 +00:00
|
|
|
</para>
|
|
|
|
|
2000-09-09 16:36:10 +00:00
|
|
|
<para>
|
|
|
|
To construct a new thread you will perform something like:
|
|
|
|
</para>
|
|
|
|
|
2002-12-12 18:03:16 +00:00
|
|
|
<para>
|
|
|
|
<programlisting>
|
2000-09-09 16:36:10 +00:00
|
|
|
GstElement *my_thread;
|
|
|
|
|
2002-04-07 23:32:16 +00:00
|
|
|
/* create the thread object */
|
2001-01-05 18:50:41 +00:00
|
|
|
my_thread = gst_thread_new ("my_thread");
|
2002-04-11 20:35:18 +00:00
|
|
|
/* you could have used gst_element_factory_make ("thread", "my_thread"); */
|
2002-04-07 23:32:16 +00:00
|
|
|
g_return_if_fail (my_thread != NULL);
|
2000-09-09 16:36:10 +00:00
|
|
|
|
2002-04-07 23:32:16 +00:00
|
|
|
/* add some plugins */
|
2001-01-05 18:50:41 +00:00
|
|
|
gst_bin_add (GST_BIN (my_thread), GST_ELEMENT (funky_src));
|
|
|
|
gst_bin_add (GST_BIN (my_thread), GST_ELEMENT (cool_effect));
|
2000-09-09 16:36:10 +00:00
|
|
|
|
2003-01-24 18:08:39 +00:00
|
|
|
/* link the elements here... */
|
2000-09-09 16:36:10 +00:00
|
|
|
...
|
|
|
|
|
2002-04-07 23:32:16 +00:00
|
|
|
/* start playing */
|
2001-01-05 18:50:41 +00:00
|
|
|
gst_element_set_state (GST_ELEMENT (my_thread), GST_STATE_PLAYING);
|
2000-09-09 16:36:10 +00:00
|
|
|
|
2002-12-12 18:03:16 +00:00
|
|
|
</programlisting>
|
|
|
|
</para>
|
2002-04-07 23:32:16 +00:00
|
|
|
<para>
|
2002-09-08 21:17:16 +00:00
|
|
|
The above program will create a thread with two elements in it. As soon
|
|
|
|
as it is set to the PLAYING state, the thread will start to iterate
|
2002-09-14 14:13:34 +00:00
|
|
|
itself. You never need to explicitly iterate a thread.
|
2000-09-09 16:36:10 +00:00
|
|
|
</para>
|
|
|
|
|
2004-01-28 15:08:17 +00:00
|
|
|
<sect1 id="section-threads-constraints">
|
2002-04-07 23:32:16 +00:00
|
|
|
<title>Constraints placed on the pipeline by the GstThread</title>
|
|
|
|
<para>
|
2002-09-08 21:17:16 +00:00
|
|
|
Within the pipeline, everything is the same as in any other bin. The
|
2003-01-24 18:08:39 +00:00
|
|
|
difference lies at the thread boundary, at the link between the
|
2002-09-08 21:17:16 +00:00
|
|
|
thread and the outside world (containing bin). Since GStreamer is
|
|
|
|
fundamentally buffer-oriented rather than byte-oriented, the natural
|
|
|
|
solution to this problem is an element that can "buffer" the buffers
|
|
|
|
between the threads, in a thread-safe fashion. This element is the
|
2004-01-28 15:08:17 +00:00
|
|
|
queue, described more fully in <xref linkend="chapter-queues"/>. It doesn't
|
2002-09-08 21:17:16 +00:00
|
|
|
matter if the queue is placed in the containing bin or in the thread
|
2002-09-14 14:13:34 +00:00
|
|
|
itself, but it needs to be present on one side or the other to enable
|
2002-09-08 21:17:16 +00:00
|
|
|
inter-thread communication.
|
2002-04-07 23:32:16 +00:00
|
|
|
</para>
|
2002-12-12 18:03:16 +00:00
|
|
|
</sect1>
|
2004-01-28 15:08:17 +00:00
|
|
|
<sect1 id="section-threads-when">
|
2002-04-07 23:32:16 +00:00
|
|
|
<title>When would you want to use a thread?</title>
|
|
|
|
<para>
|
|
|
|
If you are writing a GUI application, making the top-level bin a thread will make your GUI
|
|
|
|
more responsive. If it were a pipeline instead, it would have to be iterated by your
|
|
|
|
application's event loop, which increases the latency between events (say, keyboard presses)
|
|
|
|
and responses from the GUI. In addition, any slight hang in the GUI would delay iteration of
|
|
|
|
the pipeline, which (for example) could cause pops in the output of the sound card, if it is
|
|
|
|
an audio pipeline.
|
|
|
|
</para>
|
2002-12-12 18:03:16 +00:00
|
|
|
<para>
|
2004-01-28 15:08:17 +00:00
|
|
|
<xref linkend="section-threads-img"/> shows how a thread can be visualised.
|
2002-12-12 18:03:16 +00:00
|
|
|
</para>
|
2004-01-28 15:08:17 +00:00
|
|
|
<figure float="1" id="section-threads-img">
|
2002-12-12 18:03:16 +00:00
|
|
|
<title>A thread</title>
|
|
|
|
<mediaobject>
|
|
|
|
<imageobject>
|
2003-10-08 14:34:09 +00:00
|
|
|
<imagedata fileref="images/thread.ℑ" format="&IMAGE;" />
|
2002-12-12 18:03:16 +00:00
|
|
|
</imageobject>
|
|
|
|
</mediaobject>
|
|
|
|
</figure>
|
|
|
|
|
|
|
|
<para>
|
2000-09-09 16:36:10 +00:00
|
|
|
As an example we show the helloworld program using a thread.
|
2002-12-12 18:03:16 +00:00
|
|
|
</para>
|
2000-09-09 16:36:10 +00:00
|
|
|
|
2002-12-12 18:03:16 +00:00
|
|
|
<para>
|
|
|
|
<programlisting>
|
2002-11-29 13:55:16 +00:00
|
|
|
/* example-begin threads.c */
|
2000-09-09 16:36:10 +00:00
|
|
|
#include <gst/gst.h>
|
|
|
|
|
2002-11-29 13:55:16 +00:00
|
|
|
/* we set this to TRUE right before gst_main (), but there could still
|
|
|
|
be a race condition between setting it and entering the function */
|
|
|
|
gboolean can_quit = FALSE;
|
|
|
|
|
2001-01-04 23:35:50 +00:00
|
|
|
/* eos will be called when the src element has an end of stream */
|
2001-01-05 18:50:41 +00:00
|
|
|
void
|
2002-09-06 16:10:31 +00:00
|
|
|
eos (GstElement *src, gpointer data)
|
2000-09-09 16:36:10 +00:00
|
|
|
{
|
2001-01-05 18:50:41 +00:00
|
|
|
GstThread *thread = GST_THREAD (data);
|
|
|
|
g_print ("have eos, quitting\n");
|
2000-09-09 16:36:10 +00:00
|
|
|
|
|
|
|
/* stop the bin */
|
2001-01-05 18:50:41 +00:00
|
|
|
gst_element_set_state (GST_ELEMENT (thread), GST_STATE_NULL);
|
2000-09-09 16:36:10 +00:00
|
|
|
|
2002-11-29 13:55:16 +00:00
|
|
|
while (!can_quit) /* waste cycles */ ;
|
2001-01-05 18:50:41 +00:00
|
|
|
gst_main_quit ();
|
2000-09-09 16:36:10 +00:00
|
|
|
}
|
|
|
|
|
2001-01-05 18:50:41 +00:00
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
2000-09-09 16:36:10 +00:00
|
|
|
{
|
2004-05-03 01:22:20 +00:00
|
|
|
GstElement *filesrc, *demuxer, *decoder, *converter, *audiosink;
|
2000-09-09 16:36:10 +00:00
|
|
|
GstElement *thread;
|
|
|
|
|
2002-11-29 22:04:55 +00:00
|
|
|
if (argc < 2) {
|
2002-11-29 13:55:16 +00:00
|
|
|
g_print ("usage: %s <Ogg/Vorbis filename>\n", argv[0]);
|
2001-01-05 18:50:41 +00:00
|
|
|
exit (-1);
|
2000-09-09 16:36:10 +00:00
|
|
|
}
|
|
|
|
|
2001-01-05 18:50:41 +00:00
|
|
|
gst_init (&argc, &argv);
|
2000-09-09 16:36:10 +00:00
|
|
|
|
|
|
|
/* create a new thread to hold the elements */
|
2001-01-05 18:50:41 +00:00
|
|
|
thread = gst_thread_new ("thread");
|
|
|
|
g_assert (thread != NULL);
|
2000-09-09 16:36:10 +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);
|
|
|
|
g_signal_connect (G_OBJECT (filesrc), "eos",
|
2001-08-13 19:00:13 +00:00
|
|
|
G_CALLBACK (eos), thread);
|
2000-09-09 16:36:10 +00:00
|
|
|
|
2004-05-02 23:30:31 +00:00
|
|
|
/* create an ogg demuxer */
|
|
|
|
demuxer = gst_element_factory_make ("oggdemux", "demuxer");
|
|
|
|
g_assert (demuxer != NULL);
|
|
|
|
|
|
|
|
/* create a vorbis decoder */
|
|
|
|
decoder = gst_element_factory_make ("vorbisdec", "decoder");
|
2002-11-29 13:55:16 +00:00
|
|
|
g_assert (decoder != NULL);
|
|
|
|
|
2004-05-02 23:49:11 +00:00
|
|
|
/* create an audio converter */
|
|
|
|
converter = gst_element_factory_make ("audioconvert", "converter");
|
|
|
|
g_assert (decoder != NULL);
|
|
|
|
|
2000-09-09 16:36:10 +00:00
|
|
|
/* and an audio sink */
|
2002-11-29 13:55:16 +00:00
|
|
|
audiosink = gst_element_factory_make ("osssink", "play_audio");
|
2001-01-05 18:50:41 +00:00
|
|
|
g_assert (audiosink != NULL);
|
2000-09-09 16:36:10 +00:00
|
|
|
|
2002-11-29 13:55:16 +00:00
|
|
|
/* add objects to the thread */
|
2004-05-02 23:49:11 +00:00
|
|
|
gst_bin_add_many (GST_BIN (thread), filesrc, demuxer, decoder, converter, audiosink, NULL);
|
2003-01-24 18:08:39 +00:00
|
|
|
/* link them in the logical order */
|
2004-05-02 23:49:11 +00:00
|
|
|
gst_element_link_many (filesrc, demuxer, decoder, converter, audiosink, NULL);
|
2000-09-09 16:36:10 +00:00
|
|
|
|
|
|
|
/* start playing */
|
2003-04-16 06:38:53 +00:00
|
|
|
gst_element_set_state (thread, GST_STATE_PLAYING);
|
2000-09-09 16:36:10 +00:00
|
|
|
|
|
|
|
/* do whatever you want here, the thread will be playing */
|
2002-11-29 13:55:16 +00:00
|
|
|
g_print ("thread is playing\n");
|
2000-09-09 16:36:10 +00:00
|
|
|
|
2002-11-29 13:55:16 +00:00
|
|
|
can_quit = TRUE;
|
2001-01-05 18:50:41 +00:00
|
|
|
gst_main ();
|
2000-09-09 16:36:10 +00:00
|
|
|
|
2003-04-16 06:38:53 +00:00
|
|
|
gst_object_unref (GST_OBJECT (thread));
|
2000-09-09 16:36:10 +00:00
|
|
|
|
2001-01-05 18:50:41 +00:00
|
|
|
exit (0);
|
2000-09-09 16:36:10 +00:00
|
|
|
}
|
2002-11-29 13:55:16 +00:00
|
|
|
/* example-end threads.c */
|
2002-12-12 18:03:16 +00:00
|
|
|
</programlisting>
|
|
|
|
</para>
|
|
|
|
</sect1>
|
2000-08-22 21:18:18 +00:00
|
|
|
</chapter>
|