2 <!-- ############ chapter ############# -->
4 <chapter id="chapter-building-chainfn">
5 <title>The chain function</title>
7 The chain function is the function in which all data processing takes
8 place. In the case of a simple filter, <function>_chain ()</function>
9 functions are mostly linear functions - so for each incoming buffer,
10 one buffer will go out, too. Below is a very simple implementation of
13 <programlisting><!-- example-begin chain.c a --><!--
17 gst_my_filter_event (GstPad * pad, GstObject * parent, GstEvent * event)
19 return gst_pad_event_default (pad, parent, event);
21 --><!-- example-end chain.c a -->
22 <!-- example-begin chain.c b -->
23 static GstFlowReturn gst_my_filter_chain (GstPad *pad,
30 gst_my_filter_init (GstMyFilter * filter)
33 /* configure chain function on the pad before adding
34 * the pad to the element */
35 gst_pad_set_chain_function (filter->sinkpad,
41 gst_my_filter_chain (GstPad *pad,
45 GstMyFilter *filter = GST_MY_FILTER (parent);
48 g_print ("Have data of size %" G_GSIZE_FORMAT" bytes!\n",
49 gst_buffer_get_size (buf));
51 return gst_pad_push (filter->srcpad, buf);
53 <!-- example-end chain.c b -->
54 <!-- example-begin chain.c c --><!--
55 static GstStateChangeReturn
56 gst_my_filter_change_state (GstElement * element, GstStateChange transition)
58 return GST_CALL_PARENT_WITH_DEFAULT (GST_ELEMENT_CLASS,
59 change_state, (element, transition), GST_STATE_CHANGE_SUCCESS);
61 #include "register.func"
62 --><!-- example-end chain.c c --></programlisting>
64 Obviously, the above doesn't do much useful. Instead of printing that the
65 data is in, you would normally process the data there. Remember, however,
66 that buffers are not always writeable.
69 In more advanced elements (the ones that do event processing), you may want
70 to additionally specify an event handling function, which will be called
71 when stream-events are sent (such as caps, end-of-stream, newsegment, tags, etc.).
75 gst_my_filter_init (GstMyFilter * filter)
78 gst_pad_set_event_function (filter->sinkpad,
79 gst_my_filter_sink_event);
82 <!-- example-begin chain2.c a --><!--
86 --><!-- example-end chain2.c a -->
87 <!-- example-begin chain.func a --><!--
89 gst_my_filter_stop_processing (GstMyFilter * filter)
94 gst_my_filter_process_data (GstMyFilter * filter, const GstBuffer * buf)
98 --><!-- example-end chain.func a -->
99 <!-- example-begin chain.func b -->
101 gst_my_filter_sink_event (GstPad *pad,
105 GstMyFilter *filter = GST_MY_FILTER (parent);
107 switch (GST_EVENT_TYPE (event)) {
109 /* we should handle the format here */
112 /* end-of-stream, we should close down all stream leftovers here */
113 gst_my_filter_stop_processing (filter);
119 return gst_pad_event_default (pad, parent, event);
123 gst_my_filter_chain (GstPad *pad,
127 GstMyFilter *filter = GST_MY_FILTER (parent);
130 outbuf = gst_my_filter_process_data (filter, buf);
131 gst_buffer_unref (buf);
133 /* something went wrong - signal an error */
134 GST_ELEMENT_ERROR (GST_ELEMENT (filter), STREAM, FAILED, (NULL), (NULL));
135 return GST_FLOW_ERROR;
138 return gst_pad_push (filter->srcpad, outbuf);
140 <!-- example-end chain.func b -->
141 <!-- example-begin chain2.c b --><!--
142 static GstStateChangeReturn
143 gst_my_filter_change_state (GstElement * element, GstStateChange transition)
145 return GST_CALL_PARENT_WITH_DEFAULT (GST_ELEMENT_CLASS,
146 change_state, (element, transition), GST_STATE_CHANGE_SUCCESS);
148 #include "register.func"
149 --><!-- example-end chain2.c b --></programlisting>
151 In some cases, it might be useful for an element to have control over the
152 input data rate, too. In that case, you probably want to write a so-called
153 <emphasis>loop-based</emphasis> element. Source elements (with only source
154 pads) can also be <emphasis>get-based</emphasis> elements. These concepts
155 will be explained in the advanced section of this guide, and in the section
156 that specifically discusses source pads.