1 <chapter id="chapter-checklist-element">
2 <title>Things to check when writing an element</title>
4 This chapter contains a fairly random selection of things to take care
5 of when writing an element. It's up to you how far you're going to stick
6 to those guidelines. However, keep in mind that when you're writing an
7 element and hope for it to be included in the mainstream &GStreamer;
8 distribution, it <emphasis>has to</emphasis> meet those requirements.
9 As far as possible, we will try to explain why those requirements are
13 <sect1 id="section-checklist-states">
14 <title>About states</title>
19 Make sure the state of an element gets reset when going to
20 <classname>NULL</classname>. Ideally, this should set all
21 object properties to their original state. This function
22 should also be called from _init.
27 Make sure an element forgets <emphasis>everything</emphasis>
28 about its contained stream when going from
29 <classname>PAUSED</classname> to <classname>READY</classname>. In
30 <classname>READY</classname>, all stream states are reset. An
31 element that goes from <classname>PAUSED</classname> to
32 <classname>READY</classname> and back to
33 <classname>PAUSED</classname> should start reading the
34 stream from he start again.
39 People that use <command>gst-launch</command> for testing have
40 the tendency to not care about cleaning up. This is
41 <emphasis>wrong</emphasis>. An element should be tested using
42 various applications, where testing not only means to <quote>make
43 sure it doesn't crash</quote>, but also to test for memory leaks
44 using tools such as <command>valgrind</command>. Elements have to
45 be reusable in a pipeline after having been reset.
51 <sect1 id="section-checklist-debug">
52 <title>Debugging</title>
57 Elements should <emphasis>never</emphasis> use their standard
58 output for debugging (using functions such as <function>printf
59 ()</function> or <function>g_print ()</function>). Instead,
60 elements should use the logging functions provided by &GStreamer;,
61 named <function>GST_DEBUG ()</function>,
62 <function>GST_LOG ()</function>, <function>GST_INFO ()</function>,
63 <function>GST_WARNING ()</function> and
64 <function>GST_ERROR ()</function>. The various logging levels can
65 be turned on and off at runtime and can thus be used for solving
66 issues as they turn up. Instead of <function>GST_LOG ()</function>
67 (as an example), you can also use <function>GST_LOG_OBJECT
68 ()</function> to print the object that you're logging output for.
73 Ideally, elements should use their own debugging category. Most
74 elements use the following code to do that:
77 GST_DEBUG_CATEGORY_STATIC (myelement_debug);
78 #define GST_CAT_DEFAULT myelement_debug
83 gst_myelement_class_init (GstMyelementClass *klass)
86 GST_DEBUG_CATEGORY_INIT (myelement_debug, "myelement",
91 At runtime, you can turn on debugging using the commandline
92 option <command>--gst-debug=myelement:5</command>.
97 Elements should use GST_DEBUG_FUNCPTR when setting pad functions or
98 overriding element class methods, for example:
100 gst_pad_set_event_func (myelement->srcpad,
101 GST_DEBUG_FUNCPTR (my_element_src_event));
103 This makes debug output much easier to read later on.
108 Elements that are aimed for inclusion into one of the GStreamer
109 modules should ensure consistent naming of the element name,
110 structures and function names. For example, if the element type is
111 GstYellowFooDec, functions should be prefixed with
112 gst_yellow_foo_dec_ and the element should be registered
113 as 'yellowfoodec'. Separate words should be separate in this scheme,
114 so it should be GstFooDec and gst_foo_dec, and not GstFoodec and
121 <sect1 id="section-checklist-query">
122 <title>Querying, events and the like</title>
127 All elements to which it applies (sources, sinks, demuxers)
128 should implement query functions on their pads, so that
129 applications and neighbour elements can request the current
130 position, the stream length (if known) and so on.
135 Elements should make sure they forward events they do not
136 handle with gst_pad_event_default (pad, event) instead of
137 just dropping them. Events should never be dropped unless
138 specifically intended.
143 Elements should make sure they forward queries they do not
144 handle with gst_pad_query_default (pad, query) instead of
150 Elements should use gst_pad_get_parent() in event and query
151 functions, so that they hold a reference to the element while they
152 are operating. Note that gst_pad_get_parent() increases the
153 reference count of the element, so you must be very careful to call
154 gst_object_unref (element) before returning from your query or
155 event function, otherwise you will leak memory.
161 <sect1 id="section-checklist-testing">
162 <title>Testing your element</title>
167 <command>gst-launch</command> is <emphasis>not</emphasis> a good
168 tool to show that your element is finished. Applications such as
169 Rhythmbox and Totem (for GNOME) or AmaroK (for KDE)
170 <emphasis>are</emphasis>. <command>gst-launch</command> will not
171 test various things such as proper clean-up on reset, interrupt
172 event handling, querying and so on.
177 Parsers and demuxers should make sure to check their input. Input
178 cannot be trusted. Prevent possible buffer overflows and the like.
179 Feel free to error out on unrecoverable stream errors. Test your
180 demuxer using stream corruption elements such as
181 <classname>breakmydata</classname> (included in gst-plugins). It
182 will randomly insert, delete and modify bytes in a stream, and is
183 therefore a good test for robustness. If your element crashes
184 when adding this element, your element needs fixing. If it errors
185 out properly, it's good enough. Ideally, it'd just continue to
186 work and forward data as much as possible.
191 Demuxers should not assume that seeking works. Be prepared to
192 work with unseekable input streams (e.g. network sources) as
198 Sources and sinks should be prepared to be assigned another clock
199 then the one they expose themselves. Always use the provided clock
200 for synchronization, else you'll get A/V sync issues.