1 <!-- ############ chapter ############# -->
3 <chapter id="chapter-building-boiler" xreflabel="Constructing the Boilerplate">
4 <title>Constructing the Boilerplate</title>
6 In this chapter you will learn how to construct the bare minimum code for a
7 new plugin. Starting from ground zero, you will see how to get the
8 &GStreamer; template source. Then you will learn how to use a few basic
9 tools to copy and modify a template plugin to create a new plugin. If you
10 follow the examples here, then by the end of this chapter you will have a
11 functional audio filter plugin that you can compile and use in &GStreamer;
15 <!-- ############ sect1 ############# -->
17 <sect1 id="section-boiler-source" xreflabel="Getting the GStreamer Plugin Templates">
18 <title>Getting the GStreamer Plugin Templates</title>
20 There are currently two ways to develop a new plugin for &GStreamer;: You
21 can write the entire plugin by hand, or you can copy an existing plugin
22 template and write the plugin code you need. The second method is by far
23 the simpler of the two, so the first method will not even be described
24 here. (Errm, that is, <quote>it is left as an exercise to the
28 The first step is to check out a copy of the
29 <filename>gst-template</filename> CVS module to get an important tool and
30 the source code template for a basic &GStreamer; plugin. To check out the
31 <filename>gst-template</filename> module, make sure you are connected to
32 the internet, and type the following commands at a command console:
35 <prompt>shell $ </prompt><userinput>cd .</userinput>
36 <prompt>shell $ </prompt><userinput>cvs -d:pserver:anonymous@cvs.freedesktop.org:/home/cvs/gstreamer login</userinput>
37 Logging in to :pserver:anonymous@cvs.freedesktop.org:2401/home/cvs/gstreamer
39 <prompt>shell $ </prompt><userinput>cvs -z3 -d:pserver:anonymous@cvs.freedesktop.org:/home/cvs/gstreamer co gst-template</userinput>
41 U gst-template/gst-app/AUTHORS
42 U gst-template/gst-app/ChangeLog
43 U gst-template/gst-app/Makefile.am
44 U gst-template/gst-app/NEWS
45 U gst-template/gst-app/README
46 U gst-template/gst-app/autogen.sh
47 U gst-template/gst-app/configure.ac
48 U gst-template/gst-app/src/Makefile.am
52 After the first command, you will have to press <keycap>ENTER</keycap> to
53 log in to the CVS server. (You might have to log in twice.) The second
54 command will check out a series of files and directories into <filename
55 class="directory">./gst-template</filename>. The template you will be
57 class="directory">./gst-template/gst-plugin/</filename> directory. You
58 should look over the files in that directory to get a general idea of the
59 structure of a source tree for a plugin.
63 <!-- ############ sect1 ############# -->
65 <sect1 id="section-boiler-project-stamp" xreflabel="Using the Project Stamp">
66 <title>Using the Project Stamp</title>
68 The first thing to do when making a new element is to specify some basic
69 details about it: what its name is, who wrote it, what version number it
70 is, etc. We also need to define an object to represent the element and to
71 store the data the element needs. These details are collectively known as
72 the <emphasis>boilerplate</emphasis>.
75 The standard way of defining the boilerplate is simply to write some code,
76 and fill in some structures. As mentioned in the previous section, the
77 easiest way to do this is to copy a template and add functionality
78 according to your needs. To help you do so, there are some tools in the
79 <filename class="directory">./gst-template/tools/</filename> directory.
80 One tool, <filename>gst-quick-stamp</filename>, is a quick command line
81 tool. The other, <filename>gst-project-stamp</filename>, is a full GNOME
82 druid application that takes you through the steps of creating a new
83 project (either a plugin or an application).
86 To use <command>pluginstamp.sh</command>, first open up a terminal window.
87 Change to the <filename class="directory">gst-template</filename>
88 directory, and then run the <command>pluginstamp.sh</command> command. The
89 arguments to the <command>pluginstamp.sh</command> are:
93 <para>the name of the plugin, and</para>
97 the directory that should hold a new subdirectory for the source tree
103 Note that capitalization is important for the name of the plugin. Under
104 some operating systems, capitalization is also important when specifying
105 directory names. For example, the following commands create the
106 ExampleFilter plugin based on the plugin template and put the output files
107 in a new directory called <filename
108 class="directory">~/src/examplefilter/</filename>:
111 <prompt>shell $ </prompt><userinput>cd gst-template</userinput>
112 <prompt>shell $ </prompt><userinput>tools/pluginstamp.sh ExampleFilter ~/src</userinput>
116 <!-- ############ sect1 ############# -->
118 <sect1 id="section-boiler-examine">
119 <title>Examining the Basic Code</title>
121 First we will examine the code you would be likely to place in a header
122 file (although since the interface to the code is entirely defined by the
123 pluging system, and doesn't depend on reading a header file, this is not
126 The code here can be found in
127 <filename>examples/pwg/examplefilter/boiler/gstexamplefilter.h</filename>.
130 <example id="ex-boiler-examine-h">
131 <title>Example Plugin Header File</title>
133 /* Definition of structure storing data for this element. */
134 typedef struct _GstExample GstExample;
139 GstPad *sinkpad, *srcpad;
144 /* Standard definition defining a class for this element. */
145 typedef struct _GstExampleClass GstExampleClass;
146 struct _GstExampleClass {
147 GstElementClass parent_class;
150 /* Standard macros for defining types for this element. */
151 #define GST_TYPE_EXAMPLE \
152 (gst_example_get_type())
153 #define GST_EXAMPLE(obj) \
154 (GTK_CHECK_CAST((obj),GST_TYPE_EXAMPLE,GstExample))
155 #define GST_EXAMPLE_CLASS(klass) \
156 (GTK_CHECK_CLASS_CAST((klass),GST_TYPE_EXAMPLE,GstExample))
157 #define GST_IS_EXAMPLE(obj) \
158 (GTK_CHECK_TYPE((obj),GST_TYPE_EXAMPLE))
159 #define GST_IS_EXAMPLE_CLASS(obj) \
160 (GTK_CHECK_CLASS_TYPE((klass),GST_TYPE_EXAMPLE))
162 /* Standard function returning type information. */
163 GType gst_example_get_type (void);
168 <!-- ############ sect1 ############# -->
170 <sect1 id="section-boiler-details">
171 <title>GstElementDetails</title>
173 The GstElementDetails structure gives a heirarchical type for the element,
174 a human-readable description of the element, as well as author and version
175 data. The entries are:
179 A long, english, name for the element.
180 </para></listitem><listitem><para>
181 The type of the element, as a heirarchy. The heirarchy is defined by
182 specifying the top level category, followed by a "/", followed by the
183 next level category, etc. The type should be defined according to the
184 guidelines elsewhere in this document. (FIXME: write the guidelines, and
185 give a better reference to them)
186 </para></listitem><listitem><para>
187 A brief description of the purpose of the element.
188 </para></listitem><listitem><para>
189 The name of the author of the element, optionally followed by a contact
190 email address in angle brackets.
197 static GstElementDetails example_details = {
199 "Example/FirstExample",
200 "Shows the basic structure of a plugin",
201 "your name <your.name@your.isp>"
205 The element details are registered with the plugin during
206 <function>_base_init ()</function>.
210 gst_my_filter_base_init (GstMyFilterClass *klass)
212 static GstElementDetails my_filter_details = {
215 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
218 gst_element_class_set_details (element_class, &my_filter_details);
223 <!-- ############ sect1 ############# -->
225 <sect1 id="section-boiler-padtemplates">
226 <title>GstStaticPadTemplate</title>
228 A GstStaticPadTemplate is a description of a pad that the element will
229 (or might) create and use. It contains:
233 <para>A short name for the pad.</para>
236 <para>Pad direction.</para>
240 Existence property. This indicates whether the pad exists always (an
241 <quote>always</quote> pad), only in some cases (a
242 <quote>sometimes</quote> pad) or only if the application requested
243 such a pad (a <quote>request</quote> pad).
247 <para>Supported types by this element (capabilities).</para>
254 static GstStaticPadTemplate sink_factory =
255 GST_STATIC_PAD_TEMPLATE (
259 GST_STATIC_CAPS ("ANY")
263 Those pad templates are registered during the
264 <function>_base_init ()</function> function. Pads are created from these
265 templates in the element's <function>_init ()</function> function using
266 <function>gst_pad_new_from_template ()</function>. The template can be
267 retrieved from the element class using
268 <function>gst_element_class_get_pad_template ()</function>. See below
269 for more details on this.
273 gst_my_filter_base_init (GstMyFilterClass *klass)
275 static GstStaticPadTemplate sink_factory =
279 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
281 gst_element_class_add_pad_template (element_class,
282 gst_static_pad_template_get (&src_factory));
283 gst_element_class_add_pad_template (element_class,
284 gst_static_pad_template_get (&sink_factory));
289 The last argument in a template is its type
290 or list of supported types. In this example, we use 'ANY', which means
291 that this element will accept all input. In real-life situations, you
292 would set a mimetype and optionally a set of properties to make sure
293 that only supported input will come in. This representation should be
294 a string that starts with a mimetype, then a set of comma-separates
295 properties with their supported values. In case of an audio filter that
296 supports raw integer 16-bit audio, mono or stereo at any samplerate, the
297 correct template would look like this:
300 static GstStaticPadTemplate sink_factory =
301 GST_STATIC_PAD_TEMPLATE (
309 "endianness = (int) BYTE_ORDER, "
310 "channels = (int) { 1, 2 }, "
311 "rate = (int) [ 8000, 96000 ]"
316 Values surrounded by {} are lists, values surrounded by [] are ranges.
317 Multiple sets of types are supported too, and should be separated by
318 a semicolon (<quote>;</quote>). Later, in the chapter on pads, we will
319 see how to use types to know the exact format of a stream:
320 <xref linkend="chapter-building-pads"/>.
324 <!-- ############ sect1 ############# -->
326 <sect1 id="section-boiler-constructors">
327 <title>Constructor Functions</title>
329 Each element has three functions which are used for construction of an
330 element. These are the <function>_base_init()</function> function which
331 is meant to initialize class and child class properties during each new
332 child class creation; the <function>_class_init()</function> function,
333 which is used to initialise the class only once (specifying what signals,
334 arguments and virtual functions the class has and setting up global
335 state); and the <function>_init()</function> function, which is used to
336 initialise a specific instance of this type.
340 <!-- ############ sect1 ############# -->
342 <sect1 id="section-boiler-plugininit">
343 <title>The plugin_init function</title>
345 Once we have written code defining all the parts of the plugin, we need to
346 write the plugin_init() function. This is a special function, which is
347 called as soon as the plugin is loaded, and should return TRUE or FALSE
348 depending on whether it loaded initialized any dependencies correctly.
349 Also, in this function, any supported element type in the plugin should
354 plugin_init (GstPlugin *plugin)
356 return gst_element_register (plugin, "my_filter",
370 "http://gstreamer.net/"
374 Note that the information returned by the plugin_init() function will be
375 cached in a central registry. For this reason, it is important that the
376 same information is always returned by the function: for example, it
377 must not make element factories available based on runtime conditions.
378 If an element can only work in certain conditions (for example, if the
379 soundcard is not being used by some other process) this must be reflected
380 by the element being unable to enter the READY state if unavailable,
381 rather than the plugin attempting to deny existence of the plugin.