1 <chapter id="cha-factories">
2 <title>More on factories</title>
4 The small application we created in the previous chapter used the
5 concept of a factory to create the elements. In this chapter we will
6 show you how to use the factory concepts to create elements based
7 on what they do instead of what they are called.
11 We will first explain the concepts involved before we move on
12 to the reworked helloworld example using autoplugging.
15 <title>The problems with the helloworld example</title>
17 If we take a look at how the elements were created in the previous
18 example we used a rather crude mechanism:
23 /* now it's time to get the parser */
24 decoder = gst_element_factory_make ("mad", "decoder");
29 While this mechanism is quite effective it also has some big problems:
30 The elements are created based on their name. Indeed, we create an
31 element, mad, by explicitly stating the mad element's name. Our little
32 program therefore always uses the mad decoder element to decode
33 the MP3 audio stream, even if there are three other MP3 decoders in the
34 system. We will see how we can use a more general way to create an
38 We have to introduce the concept of MIME types and capabilities
39 added to the source and sink pads.
44 <title>More on MIME Types</title>
46 GStreamer uses MIME types to identify the different types of data
47 that can be handled by the elements. They are the high level
48 mechanisms to make sure that everyone is talking about the right
52 A MIME (Multipurpose Internet Mail Extension) type is a pair of
53 strings that denote a certain type of data. Examples include:
57 audio/raw : raw audio samples
62 audio/mpeg : MPEG audio
67 video/mpeg : MPEG video
73 An element must associate a MIME type to its source and sink pads
74 when it is loaded into the system. GStreamer knows about the
75 different elements and what type of data they expect and emit.
76 This allows for very dynamic and extensible element creation as we
80 As we have seen in the previous chapter, MIME types are added
81 to the Capability structure of a pad.
85 In our helloworld example the elements we constructed would have the
86 following MIME types associated with their source and sink pads:
88 <figure float="1" id="sec-mime-img">
89 <title>The Hello world pipeline with MIME types</title>
92 <imagedata fileref="images/mime-world.&magic;" format="&MAGIC;" />
98 We will see how you can create an element based on the MIME types
99 of its source and sink pads. This way the end-user will have the
100 ability to choose his/her favorite audio/mpeg decoder without
101 you even having to care about it.
104 The typing of the source and sink pads also makes it possible to
105 'autoplug' a pipeline. We will have the ability to say: "construct
106 me a pipeline that does an audio/mpeg to audio/raw conversion".
110 The basic GStreamer library does not try to solve all of your
111 autoplug problems. It leaves the hard decisions to the application
112 programmer, where they belong.
119 <title>GStreamer types</title>
121 GStreamer assigns a unique number to all registered MIME types.
122 GStreamer also keeps a reference to
123 a function that can be used to determine if a given buffer is of
127 There is also an association between a MIME type and a file extension,
128 but the use of typefind functions (similar to file(1)) is preferred.
131 The type information is maintained in a list of
132 <classname>GstType</classname>. The definition of a
133 <classname>GstType</classname> is like:
137 typedef GstCaps (*GstTypeFindFunc) (GstBuffer *buf,gpointer *priv);
139 typedef struct _GstType GstType;
142 guint16 id; /* type id (assigned) */
144 gchar *mime; /* MIME type */
145 gchar *exts; /* space-delimited list of extensions */
147 GstTypeFindFunc typefindfunc; /* typefind function */
152 All operations on <classname>GstType</classname> occur
153 via their <classname>guint16 id</classname> numbers, with
154 the <classname>GstType</classname> structure private to the GStreamer
159 <title>MIME type to id conversion</title>
162 We can obtain the id for a given MIME type
163 with the following piece of code:
168 id = gst_type_find_by_mime ("audio/mpeg");
171 This function will return 0 if the type was not known.
176 <title>id to <classname>GstType</classname> conversion</title>
178 We can obtain the <classname>GstType</classname> for a given id
179 with the following piece of code:
184 type = gst_type_find_by_id (id);
187 This function will return NULL if the id was not associated with
188 any known <classname>GstType</classname>
193 <title>extension to id conversion</title>
195 We can obtain the id for a given file extension
196 with the following piece of code:
201 id = gst_type_find_by_ext (".mp3");
204 This function will return 0 if the extension was not known.
207 For more information, see <xref linkend="cha-autoplug"/>.
213 <title>Creating elements with the factory</title>
215 In the previous section we described how you could obtain
216 an element factory using MIME types. One the factory has been
217 obtained, you can create an element using:
220 GstElementFactory *factory;
223 // obtain the factory
226 element = gst_element_factory_create (factory, "name");
229 This way, you do not have to create elements by name which
230 allows the end-user to select the elements he/she prefers for the
236 <title>GStreamer basic types</title>
238 GStreamer only has two builtin types:
243 audio/raw : raw audio samples
248 video/raw and image/raw : raw video data
253 All other MIME types are maintained by the plugin elements.