2000-08-22 21:18:18 +00:00
|
|
|
<chapter id="cha-threads">
|
|
|
|
<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>
|
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
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
|
|
|
|
2002-04-07 23:32:16 +00:00
|
|
|
/* connect 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
|
|
|
|
|
|
|
</programlisting>
|
|
|
|
|
2002-04-07 23:32:16 +00:00
|
|
|
<para>
|
|
|
|
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 itself. You never need to manually iterate a
|
|
|
|
thread.
|
2000-09-09 16:36:10 +00:00
|
|
|
</para>
|
|
|
|
|
2002-04-07 23:32:16 +00:00
|
|
|
<sect2>
|
|
|
|
<title>Constraints placed on the pipeline by the GstThread</title>
|
|
|
|
<para>
|
|
|
|
Within the pipeline, everything is the same as in any other bin. The difference lies at the
|
|
|
|
thread boundary, at the connection between the 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 queue, described more fully in <xref
|
|
|
|
linkend="cha-queues"/>. It doesn't matter if the queue is placed in the containing bin or in
|
|
|
|
the thread itself, but it needs to be present on one side of the other to enable inter-thread
|
|
|
|
communication.
|
|
|
|
</para>
|
|
|
|
</sect2>
|
|
|
|
<sect2>
|
|
|
|
<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>
|
|
|
|
</sect2>
|
2000-09-09 16:36:10 +00:00
|
|
|
<para>
|
2002-04-07 23:32:16 +00:00
|
|
|
A thread can be visualised as below
|
2000-09-09 16:36:10 +00:00
|
|
|
</para>
|
|
|
|
<figure float="1" id="sec-threads-img">
|
2002-04-07 23:32:16 +00:00
|
|
|
<title>A thread</title>
|
2001-12-13 15:24:00 +00:00
|
|
|
<mediaobject>
|
|
|
|
<imageobject>
|
2001-12-15 23:13:04 +00:00
|
|
|
<imagedata fileref="images/thread.&magic;" format="&magic;" />
|
2001-12-13 15:24:00 +00:00
|
|
|
</imageobject>
|
|
|
|
</mediaobject>
|
2000-09-09 16:36:10 +00:00
|
|
|
</figure>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
As an example we show the helloworld program using a thread.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
#include <gst/gst.h>
|
|
|
|
|
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
|
|
|
|
eos (GstSrc *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
|
|
|
|
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
|
|
|
{
|
2002-01-06 04:26:37 +00:00
|
|
|
GstElement *filesrc, *audiosink;
|
2000-09-09 16:36:10 +00:00
|
|
|
GstElement *pipeline;
|
|
|
|
GstElement *thread;
|
|
|
|
|
|
|
|
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-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 new bin to hold the elements */
|
2001-01-05 18:50:41 +00:00
|
|
|
pipeline = gst_pipeline_new ("pipeline");
|
|
|
|
g_assert (pipeline != 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
|
|
|
|
|
|
|
/* and an audio sink */
|
2002-04-11 20:35:18 +00:00
|
|
|
audiosink = gst_element_factory_make ("audiosink", "play_audio");
|
2001-01-05 18:50:41 +00:00
|
|
|
g_assert (audiosink != NULL);
|
2000-09-09 16:36:10 +00:00
|
|
|
|
|
|
|
/* add objects to the main pipeline */
|
2002-01-06 04:26:37 +00:00
|
|
|
gst_bin_add (GST_BIN (pipeline), filesrc);
|
2001-01-05 18:50:41 +00:00
|
|
|
gst_bin_add (GST_BIN (pipeline), audiosink);
|
2000-09-09 16:36:10 +00:00
|
|
|
|
|
|
|
/* automatically setup the pipeline */
|
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-09-09 16:36:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* remove the source element from the pipeline */
|
2002-01-06 04:26:37 +00:00
|
|
|
gst_bin_remove (GST_BIN (pipeline), filesrc);
|
2000-09-09 16:36:10 +00:00
|
|
|
|
|
|
|
/* insert the source element in the thread, remember a thread needs at
|
|
|
|
least one source or connection element */
|
2002-01-06 04:26:37 +00:00
|
|
|
gst_bin_add (GST_BIN (thread), filesrc);
|
2000-09-09 16:36:10 +00:00
|
|
|
|
|
|
|
/* add the pipeline to the thread too */
|
2001-01-05 18:50:41 +00:00
|
|
|
gst_bin_add (GST_BIN (thread), GST_ELEMENT (pipeline));
|
2000-09-09 16:36:10 +00:00
|
|
|
|
|
|
|
/* start playing */
|
2001-01-05 18:50:41 +00:00
|
|
|
gst_element_set_state (GST_ELEMENT (thread), GST_STATE_PLAYING);
|
2000-09-09 16:36:10 +00:00
|
|
|
|
|
|
|
/* do whatever you want here, the thread will be playing */
|
|
|
|
...
|
|
|
|
|
2001-01-05 18:50:41 +00:00
|
|
|
gst_main ();
|
2000-09-09 16:36:10 +00:00
|
|
|
|
2001-01-05 18:50:41 +00:00
|
|
|
gst_pipeline_destroy (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
|
|
|
}
|
|
|
|
</programlisting>
|
|
|
|
|
2000-08-22 21:18:18 +00:00
|
|
|
</chapter>
|