2000-08-22 21:18:18 +00:00
|
|
|
<chapter id="cha-cothreads">
|
|
|
|
<title>Cothreads</title>
|
2002-04-07 23:32:16 +00:00
|
|
|
<para>
|
|
|
|
Cothreads are user-space threads that greatly reduce context switching overhead introduced by
|
|
|
|
regular kernel threads. Cothreads are also used to handle the more complex elements. They differ
|
|
|
|
from other user-space threading libraries in that they are scheduled explictly by GStreamer.
|
2000-08-22 21:18:18 +00:00
|
|
|
</para>
|
2000-11-11 15:13:50 +00:00
|
|
|
<para>
|
|
|
|
A cothread is created by a <classname>GstBin</classname> whenever an element is found
|
|
|
|
inside the bin that has one or more of the following properties:
|
|
|
|
<itemizedlist>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
The element is loop-based instead of chain-based
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
The element has multiple input pads
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
The element has the MULTI_IN flag set
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</itemizedlist>
|
|
|
|
The <classname>GstBin</classname> will create a cothread context for all the elements
|
|
|
|
in the bin so that the elements will interact in cooperative
|
|
|
|
multithreading.
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
Before proceding to the concept of loop-based elements we will first
|
2002-09-14 14:13:34 +00:00
|
|
|
explain the chain-based elements.
|
2000-11-11 15:13:50 +00:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<sect1 id="sec-chain-based">
|
|
|
|
<title>Chain-based elements</title>
|
|
|
|
<para>
|
|
|
|
Chain based elements receive a buffer of data and are supposed
|
|
|
|
to handle the data and perform a gst_pad_push.
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
The basic main function of a chain-based element is like:
|
|
|
|
</para>
|
|
|
|
<programlisting>
|
2001-01-05 18:50:41 +00:00
|
|
|
static void
|
|
|
|
chain_function (GstPad *pad, GstBuffer *buffer)
|
2000-11-11 15:13:50 +00:00
|
|
|
{
|
|
|
|
GstBuffer *outbuffer;
|
|
|
|
|
|
|
|
....
|
|
|
|
// process the buffer, create a new outbuffer
|
|
|
|
...
|
|
|
|
|
|
|
|
gst_pad_push (srcpad, outbuffer);
|
|
|
|
}
|
|
|
|
</programlisting>
|
2001-01-05 18:50:41 +00:00
|
|
|
<para>
|
|
|
|
Chain based function are mainly used for elements that have a one to one
|
|
|
|
relation between their input and output behaviour. An example of such an
|
|
|
|
element can be a simple video blur filter. The filter takes a buffer in, performs
|
|
|
|
the blur operation on it and sends out the resulting buffer.
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
Another element, for example, is a volume filter. The filter takes audio samples as
|
|
|
|
input, performs the volume effect and sends out the resulting buffer.
|
|
|
|
</para>
|
2000-11-11 15:13:50 +00:00
|
|
|
|
|
|
|
</sect1>
|
|
|
|
|
|
|
|
<sect1 id="sec-loop-based">
|
|
|
|
<title>Loop-based elements</title>
|
|
|
|
<para>
|
2002-04-07 23:32:16 +00:00
|
|
|
As opposed to chain-based elements, loop-based elements enter an
|
2000-11-11 15:13:50 +00:00
|
|
|
infinite loop that looks like this:
|
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
GstBuffer *buffer, *outbuffer;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
buffer = gst_pad_pull (sinkpad);
|
|
|
|
...
|
|
|
|
// process buffer, create outbuffer
|
2001-01-05 18:50:41 +00:00
|
|
|
while (!done) {
|
|
|
|
....
|
|
|
|
// optionally request another buffer
|
|
|
|
buffer = gst_pad_pull (sinkpad);
|
|
|
|
....
|
|
|
|
}
|
2000-11-11 15:13:50 +00:00
|
|
|
...
|
|
|
|
gst_pad_push (srcpad, outbuffer);
|
|
|
|
}
|
|
|
|
</programlisting>
|
|
|
|
|
|
|
|
The loop-based elements request a buffer whenever they need one.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2003-10-09 12:42:49 +00:00
|
|
|
When the request for a buffer cannot be immediately satisfied, the control
|
2002-09-08 21:17:16 +00:00
|
|
|
will be given to the source element of the loop-based element until it
|
|
|
|
performs a push on its source pad. At that time the control is handed
|
2002-09-14 14:13:34 +00:00
|
|
|
back to the loop-based element, etc... The execution trace can get
|
2002-09-08 21:17:16 +00:00
|
|
|
fairly complex using cothreads when there are multiple input/output
|
|
|
|
pads for the loop-based element. Cothread switches are performed within
|
|
|
|
the call to gst_pad_pull and gst_pad_push; from the perspective of
|
|
|
|
the loop-based element, it just "appears" that gst_pad_push (or _pull)
|
|
|
|
might take a long time to return.
|
2000-11-11 15:13:50 +00:00
|
|
|
</para>
|
2001-01-05 18:50:41 +00:00
|
|
|
<para>
|
2002-09-08 21:17:16 +00:00
|
|
|
Loop based elements are mainly used for the more complex elements
|
|
|
|
that need a specific amount of data before they can start to produce
|
2002-09-14 14:13:34 +00:00
|
|
|
output. An example of such an element is the MPEG video decoder. The
|
|
|
|
element will pull a buffer, perform some decoding on it and optionally
|
|
|
|
request more buffers to decode, and when a complete video frame has
|
|
|
|
been decoded, a buffer is sent out. For example, any plugin using the
|
2002-09-08 21:17:16 +00:00
|
|
|
bytestream library will need to be loop-based.
|
2001-01-05 18:50:41 +00:00
|
|
|
</para>
|
2000-11-11 15:13:50 +00:00
|
|
|
<para>
|
2002-04-07 23:32:16 +00:00
|
|
|
There is no problem in putting cothreaded elements into a <classname>GstThread</classname> to
|
|
|
|
create even more complex pipelines with both user and kernel space threads.
|
2000-11-11 15:13:50 +00:00
|
|
|
</para>
|
2000-08-22 21:18:18 +00:00
|
|
|
|
2000-11-11 15:13:50 +00:00
|
|
|
</sect1>
|
2000-08-22 21:18:18 +00:00
|
|
|
</chapter>
|