docs/manual/advanced-dataaccess.xml: Add section on how to use fakesrc/fakesink/ident...
[platform/upstream/gstreamer.git] / docs / pwg / appendix-checklist.xml
1 <chapter id="chapter-checklist-element">
2   <title>Things to check when writing an element</title>
3   <para>
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
10     set.
11   </para>
12
13   <sect1 id="section-checklist-states">
14     <title>About states</title>
15
16     <itemizedlist>
17       <listitem>
18         <para>
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.
23         </para>
24       </listitem>
25       <listitem>
26         <para>
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.
35         </para>
36       </listitem>
37       <listitem>
38         <para>
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.
46         </para>
47       </listitem>
48     </itemizedlist>
49   </sect1>
50
51   <sect1 id="section-checklist-debug">
52     <title>Debugging</title>
53
54     <itemizedlist>
55       <listitem>
56         <para>
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.
69         </para>
70       </listitem>
71       <listitem>
72         <para>
73           Ideally, elements should use their own debugging category. Most
74           elements use the following code to do that:
75         </para>
76         <programlisting>
77 GST_DEBUG_CATEGORY_STATIC (myelement_debug);
78 #define GST_CAT_DEFAULT myelement_debug
79
80 [..]
81
82 static void
83 gst_myelement_class_init (GstMyelementClass *klass)
84 {
85 [..]
86   GST_DEBUG_CATEGORY_INIT (myelement_debug, "myelement",
87                            0, "My own element");
88 }
89         </programlisting>
90         <para>
91           At runtime, you can turn on debugging using the commandline
92           option <command>--gst-debug=myelement:5</command>.
93         </para>
94       </listitem>
95     </itemizedlist>
96   </sect1>
97
98   <sect1 id="section-checklist-query">
99     <title>Querying, events and the like</title>
100
101     <itemizedlist>
102       <listitem>
103         <para>
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.
108         </para>
109       </listitem>
110       <listitem>
111         <para>
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
120           intended.
121         </para>
122       </listitem>
123       <listitem>
124         <para>
125           Loop-based elements should always implement event handling,
126           in order to prevent hangs (infinite loop) on state changes.
127         </para>
128       </listitem>
129     </itemizedlist>
130   </sect1>
131
132   <sect1 id="section-checklist-testing">
133     <title>Testing your element</title>
134
135     <itemizedlist>
136       <listitem>
137         <para>
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.
144         </para>
145       </listitem>
146       <listitem>
147         <para>
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.
158         </para>
159       </listitem>
160       <listitem>
161         <para>
162           Demuxers should not assume that seeking works. Be prepared to
163           work with unseekable input streams (e.g. network sources) as
164           well.
165         </para>
166       </listitem>
167       <listitem>
168         <para>
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.
172         </para>
173       </listitem>
174     </itemizedlist>
175   </sect1>
176 </chapter>