2 title: The chain function
7 The chain function is the function in which all data processing takes
8 place. In the case of a simple filter, `_chain ()` functions are mostly
9 linear functions - so for each incoming buffer, one buffer will go out,
10 too. Below is a very simple implementation of a chain function:
14 static GstFlowReturn gst_my_filter_chain (GstPad *pad,
21 gst_my_filter_init (GstMyFilter * filter)
24 /* configure chain function on the pad before adding
25 * the pad to the element */
26 gst_pad_set_chain_function (filter->sinkpad,
32 gst_my_filter_chain (GstPad *pad,
36 GstMyFilter *filter = GST_MY_FILTER (parent);
39 g_print ("Have data of size %" G_GSIZE_FORMAT" bytes!\n",
40 gst_buffer_get_size (buf));
42 return gst_pad_push (filter->srcpad, buf);
46 Obviously, the above doesn't do much useful. Instead of printing that
47 the data is in, you would normally process the data there. Remember,
48 however, that buffers are not always writeable.
50 In more advanced elements (the ones that do event processing), you may
51 want to additionally specify an event handling function, which will be
52 called when stream-events are sent (such as caps, end-of-stream,
53 newsegment, tags, etc.).
56 gst_my_filter_init (GstMyFilter * filter)
59 gst_pad_set_event_function (filter->sinkpad,
60 gst_my_filter_sink_event);
67 gst_my_filter_sink_event (GstPad *pad,
71 GstMyFilter *filter = GST_MY_FILTER (parent);
73 switch (GST_EVENT_TYPE (event)) {
75 /* we should handle the format here */
78 /* end-of-stream, we should close down all stream leftovers here */
79 gst_my_filter_stop_processing (filter);
85 return gst_pad_event_default (pad, parent, event);
89 gst_my_filter_chain (GstPad *pad,
93 GstMyFilter *filter = GST_MY_FILTER (parent);
96 outbuf = gst_my_filter_process_data (filter, buf);
97 gst_buffer_unref (buf);
99 /* something went wrong - signal an error */
100 GST_ELEMENT_ERROR (GST_ELEMENT (filter), STREAM, FAILED, (NULL), (NULL));
101 return GST_FLOW_ERROR;
104 return gst_pad_push (filter->srcpad, outbuf);
107 In some cases, it might be useful for an element to have control over
108 the input data rate, too. In that case, you probably want to write a
109 so-called *loop-based* element. Source elements (with only source pads)
110 can also be *get-based* elements. These concepts will be explained in
111 the advanced section of this guide, and in the section that specifically
112 discusses source pads.