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>.
98 <sect1 id="section-checklist-query">
99 <title>Querying, events and the like</title>
104 All elements to which it applies (sources, sinks, demuxers)
105 should implement query functions on their pads, so that
106 applications and neighbour elements can request the current
107 position, the stream length (if known) and so on.
112 All elements that are event-aware (their
113 <classname>GST_ELEMENT_EVENT_AWARE</classname> flag is set)
114 should implement event handling for <emphasis>all</emphasis>
115 events, either specifically or using
116 <function>gst_pad_event_default ()</function>. Elements that
117 you should handle specifically are the interrupt event, in
118 order to properly bail out as soon as possible if state is
119 changed. Events may never be dropped unless specifically
125 Loop-based elements should always implement event handling,
126 in order to prevent hangs (infinite loop) on state changes.
132 <sect1 id="section-checklist-testing">
133 <title>Testing your element</title>
138 <command>gst-launch</command> is <emphasis>not</emphasis> a good
139 tool to show that your element is finished. Applications such as
140 Rhythmbox and Totem (for GNOME) or AmaroK (for KDE)
141 <emphasis>are</emphasis>. <command>gst-launch</command> will not
142 test various things such as proper clean-up on reset, interrupt
143 event handling, querying and so on.
148 Parsers and demuxers should make sure to check their input. Input
149 cannot be trusted. Prevent possible buffer overflows and the like.
150 Feel free to error out on unrecoverable stream errors. Test your
151 demuxer using stream corruption elements such as
152 <classname>breakmydata</classname> (included in gst-plugins). It
153 will randomly insert, delete and modify bytes in a stream, and is
154 therefore a good test for robustness. If your element crashes
155 when adding this element, your element needs fixing. If it errors
156 out properly, it's good enough. Ideally, it'd just continue to
157 work and forward data as much as possible.
162 Demuxers should not assume that seeking works. Be prepared to
163 work with unseekable input streams (e.g. network sources) as
169 Sources and sinks should be prepared to be assigned another clock
170 then the one they expose themselves. Always use the provided clock
171 for synchronization, else you'll get A/V sync issues.