2004-12-15 07:30:55 +00:00
|
|
|
<chapter id="chapter-threads">
|
|
|
|
<title>Threads</title>
|
|
|
|
<para>
|
2005-06-29 09:25:51 +00:00
|
|
|
&GStreamer; is inherently multi-threaded, and is fully thread-safe.
|
|
|
|
Most threading internals are hidden from the application, which should
|
|
|
|
make application development easier. However, in some cases, applications
|
|
|
|
may want to have influence on some parts of those. &GStreamer; allows
|
|
|
|
applications to force the use of multiple threads over some parts of
|
|
|
|
a pipeline.
|
2004-12-15 07:30:55 +00:00
|
|
|
</para>
|
2004-12-19 22:54:12 +00:00
|
|
|
|
|
|
|
<sect1 id="section-threads-uses">
|
2005-06-29 09:25:51 +00:00
|
|
|
<title>When would you want to force a thread?</title>
|
2004-12-19 22:54:12 +00:00
|
|
|
<para>
|
2005-06-29 09:25:51 +00:00
|
|
|
There are several reasons to force the use of threads. However,
|
|
|
|
for performance reasons, you never want to use one thread for every
|
|
|
|
element out there, since that will create some overhead.
|
|
|
|
Let's now list some situations where threads can be particularly
|
|
|
|
useful:
|
2004-12-19 22:54:12 +00:00
|
|
|
</para>
|
|
|
|
<itemizedlist>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Data buffering, for example when dealing with network streams or
|
|
|
|
when recording data from a live stream such as a video or audio
|
|
|
|
card. Short hickups elsewhere in the pipeline will not cause data
|
2008-07-26 12:00:36 +00:00
|
|
|
loss.
|
2004-12-19 22:54:12 +00:00
|
|
|
</para>
|
2008-07-26 12:00:36 +00:00
|
|
|
<figure float="1" id="section-thread-buffering-img">
|
|
|
|
<title>Data buffering, from a networked source</title>
|
|
|
|
<mediaobject>
|
|
|
|
<imageobject>
|
|
|
|
<imagedata scale="75" fileref="images/thread-buffering.ℑ" format="&IMAGE;"/>
|
|
|
|
</imageobject>
|
|
|
|
</mediaobject>
|
|
|
|
</figure>
|
|
|
|
|
2004-12-19 22:54:12 +00:00
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Synchronizing output devices, e.g. when playing a stream containing
|
|
|
|
both video and audio data. By using threads for both outputs, they
|
|
|
|
will run independently and their synchronization will be better.
|
|
|
|
</para>
|
2008-07-26 12:00:36 +00:00
|
|
|
<figure float="1" id="section-thread-synchronizing-img">
|
|
|
|
<title>Synchronizing audio and video sinks</title>
|
|
|
|
<mediaobject>
|
|
|
|
<imageobject>
|
|
|
|
<imagedata scale="75" fileref="images/thread-synchronizing.ℑ" format="&IMAGE;"/>
|
|
|
|
</imageobject>
|
|
|
|
</mediaobject>
|
|
|
|
</figure>
|
2004-12-19 22:54:12 +00:00
|
|
|
</listitem>
|
|
|
|
</itemizedlist>
|
2008-06-27 07:03:05 +00:00
|
|
|
|
|
|
|
|
2004-12-19 22:54:12 +00:00
|
|
|
<para>
|
|
|
|
Above, we've mentioned the <quote>queue</quote> element several times
|
2005-06-29 09:25:51 +00:00
|
|
|
now. A queue is the thread boundary element through which you can
|
|
|
|
force the use of threads. It does so by using a classic
|
|
|
|
provider/receiver model as learned in threading classes at
|
2004-12-19 22:54:12 +00:00
|
|
|
universities all around the world. By doing this, it acts both as a
|
|
|
|
means to make data throughput between threads threadsafe, and it can
|
|
|
|
also act as a buffer. Queues have several <classname>GObject</classname>
|
|
|
|
properties to be configured for specific uses. For example, you can set
|
2011-01-18 22:51:49 +00:00
|
|
|
lower and upper thresholds for the element. If there's less data than
|
|
|
|
the lower threshold (default: disabled), it will block output. If
|
|
|
|
there's more data than the upper threshold, it will block input or
|
2004-12-19 22:54:12 +00:00
|
|
|
(if configured to do so) drop data.
|
|
|
|
</para>
|
2004-12-15 07:30:55 +00:00
|
|
|
<para>
|
2010-07-12 12:39:00 +00:00
|
|
|
To use a queue (and therefore force the use of two distinct threads
|
2005-06-29 09:25:51 +00:00
|
|
|
in the pipeline), one can simply create a <quote>queue</quote> element
|
|
|
|
and put this in as part of the pipeline. &GStreamer; will take care of
|
|
|
|
all threading details internally.
|
2004-12-15 07:30:55 +00:00
|
|
|
</para>
|
2005-06-29 09:25:51 +00:00
|
|
|
</sect1>
|
|
|
|
|
|
|
|
<sect1 id="section-threads-scheduling">
|
|
|
|
<title>Scheduling in &GStreamer;</title>
|
|
|
|
|
2004-12-19 22:54:12 +00:00
|
|
|
<para>
|
2005-06-29 09:25:51 +00:00
|
|
|
Scheduling of pipelines in &GStreamer; is done by using a thread for
|
|
|
|
each <quote>group</quote>, where a group is a set of elements separated
|
|
|
|
by <quote>queue</quote> elements. Within such a group, scheduling is
|
|
|
|
either push-based or pull-based, depending on which mode is supported
|
|
|
|
by the particular element. If elements support random access to data,
|
|
|
|
such as file sources, then elements downstream in the pipeline become
|
|
|
|
the entry point of this group (i.e. the element controlling the
|
|
|
|
scheduling of other elements). The entry point pulls data from upstream
|
|
|
|
and pushes data downstream, thereby calling data handling functions on
|
|
|
|
either type of element.
|
2004-12-15 07:30:55 +00:00
|
|
|
</para>
|
2004-12-19 22:54:12 +00:00
|
|
|
<para>
|
2005-06-29 09:25:51 +00:00
|
|
|
In practice, most elements in &GStreamer;, such as decoders, encoders,
|
|
|
|
etc. only support push-based scheduling, which means that in practice,
|
|
|
|
&GStreamer; uses a push-based scheduling model.
|
2004-12-15 07:30:55 +00:00
|
|
|
</para>
|
2004-12-15 17:32:49 +00:00
|
|
|
</sect1>
|
2004-12-15 07:30:55 +00:00
|
|
|
</chapter>
|