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 lineair functions - so for each incoming buffer,
10 one buffer will go out, too. Below is a very simple implementation of
15 gst_my_filter_chain (GstPad *pad,
18 GstMyFilter *filter = GST_MY_FILTER (gst_pad_get_parent (pad));
19 GstBuffer *buf = GST_BUFFER (data);
22 g_print ("Have data of size %u bytes!\n", GST_BUFFER_SIZE (buf));
24 gst_pad_push (filter->srcpad, GST_DATA (buf));
28 Obviously, the above doesn't do much useful. Instead of printing that the
29 data is in, you would normally process the data there. Remember, however,
30 that buffers are not always writable. In more advanced elements (the ones
31 that do event processing), the incoming data might not even be a buffer.
35 gst_my_filter_chain (GstPad *pad,
38 GstMyFilter *filter = GST_MY_FILTER (gst_pad_get_parent (pad));
39 GstBuffer *buf, *outbuf;
41 if (GST_IS_EVENT (data)) {
42 GstEvent *event = GST_EVENT (data);
44 switch (GST_EVENT_TYPE (event)) {
46 /* end-of-stream, we should close down all stream leftovers here */
47 gst_my_filter_stop_processing (filter);
48 /* fall-through to default event handling */
50 gst_pad_event_default (pad, event);
56 buf = GST_BUFFER (data);
57 outbuf = gst_my_filter_process_data (buf);
58 gst_buffer_unref (buf);
60 /* something went wrong - signal an error */
61 gst_element_error (GST_ELEMENT (filter), STREAM, FAILED, (NULL), (NULL));
65 gst_pad_push (filter->srcpad, GST_DATA (outbuf));
69 In some cases, it might be useful for an element to have control over the
70 input data rate, too. In that case, you probably want to write a so-called
71 <emphasis>loop-based</emphasis> element. Source elements (with only source
72 pads) can also be <emphasis>get-based</emphasis> elements. These concepts
73 will be explained in the advanced section of this guide, and in the section
74 that specifically discusses source pads.