1 <chapter id="cha-cothreads">
2 <title>Cothreads</title>
4 Cothreads are user-space threads that greatly reduce context switching overhead introduced by
5 regular kernel threads. Cothreads are also used to handle the more complex elements. They differ
6 from other user-space threading libraries in that they are scheduled explictly by GStreamer.
9 A cothread is created by a <classname>GstBin</classname> whenever an element is found
10 inside the bin that has one or more of the following properties:
14 The element is loop-based instead of chain-based
19 The element has multiple input pads
24 The element has the MULTI_IN flag set
28 The <classname>GstBin</classname> will create a cothread context for all the elements
29 in the bin so that the elements will interact in cooperative
33 Before proceding to the concept of loop-based elements we will first
34 explain the chain-based elements.
37 <sect1 id="sec-chain-based">
38 <title>Chain-based elements</title>
40 Chain based elements receive a buffer of data and are supposed
41 to handle the data and perform a gst_pad_push.
44 The basic main function of a chain-based element is like:
48 chain_function (GstPad *pad, GstBuffer *buffer)
53 // process the buffer, create a new outbuffer
56 gst_pad_push (srcpad, outbuffer);
60 Chain based function are mainly used for elements that have a one to one
61 relation between their input and output behaviour. An example of such an
62 element can be a simple video blur filter. The filter takes a buffer in, performs
63 the blur operation on it and sends out the resulting buffer.
66 Another element, for example, is a volume filter. The filter takes audio samples as
67 input, performs the volume effect and sends out the resulting buffer.
72 <sect1 id="sec-loop-based">
73 <title>Loop-based elements</title>
75 As opposed to chain-based elements, loop-based elements enter an
76 infinite loop that looks like this:
79 GstBuffer *buffer, *outbuffer;
82 buffer = gst_pad_pull (sinkpad);
84 // process buffer, create outbuffer
87 // optionally request another buffer
88 buffer = gst_pad_pull (sinkpad);
92 gst_pad_push (srcpad, outbuffer);
96 The loop-based elements request a buffer whenever they need one.
100 When the request for a buffer cannot immediatly satisfied, the control
101 will be given to the source element of the loop-based element until it
102 performs a push on its source pad. At that time the control is handed
103 back to the loop-based element, etc... The execution trace can get
104 fairly complex using cothreads when there are multiple input/output
105 pads for the loop-based element. Cothread switches are performed within
106 the call to gst_pad_pull and gst_pad_push; from the perspective of
107 the loop-based element, it just "appears" that gst_pad_push (or _pull)
108 might take a long time to return.
111 Loop based elements are mainly used for the more complex elements
112 that need a specific amount of data before they can start to produce
113 output. An example of such an element is the MPEG video decoder. The
114 element will pull a buffer, perform some decoding on it and optionally
115 request more buffers to decode, and when a complete video frame has
116 been decoded, a buffer is sent out. For example, any plugin using the
117 bytestream library will need to be loop-based.
120 There is no problem in putting cothreaded elements into a <classname>GstThread</classname> to
121 create even more complex pipelines with both user and kernel space threads.