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 how 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 parse = gst_elementfactory_make ("mp3parse", "parse");
25 decoder = gst_elementfactory_make ("mpg123", "decoder");
30 While this mechanism is quite effective it also has some big problems:
31 The elements are created based on their name. Indeed, we create an
32 element mpg123 by explicitly stating the mpg123 elements name.
33 Our little program therefore always uses the mpg123 decoder element
34 to decode the MP3 audio stream, even if there are 3 other MP3 decoders
35 in the system. We will see how we can use a more general way to create
36 an MP3 decoder element.
39 We have to introduce the concept of MIME types and capabilities
40 added to the source and sink pads.
45 <title>more on MIME Types</title>
47 GStreamer uses MIME types to indentify the different types of data
48 that can be handled by the elements. They are the high level
49 mechanisms to make sure that everyone is talking about the right
53 A MIME (Multipurpose Internet Mail Extension) types are a set of
54 string that denote a certain type of data. examples include:
58 audio/raw : raw audio samples
63 audio/mpeg : mpeg audio
68 video/mpeg : mpeg video
74 An element must associate a MIME type to its source and sink pads
75 when it is loaded into the system. GStreamer knows about the
76 different elements and what type of data they expect and emit.
77 This allows for very dynamic and extensible element creation as we
81 As we have seen in the previous chapter, the MIME types are added
82 to the Capability structure of a pad.
86 In our helloworld example the elements we constructed would have the
87 following MIME types associated with their source and sink pads:
89 <figure float="1" id="sec-mime-img">
90 <title>The Hello world pipeline with MIME types</title>
93 <imagedata fileref="images/mime-world.&magic;" format="&magic;" />
99 We will see how you can create an element based on the MIME types
100 of its source and sink pads. This way the end-user will have the
101 ability to choose his/her favorite audio/mpeg decoder without
102 you even having to care about it.
105 The typing of the source and sink pads also makes it possible to
106 'autoplug' a pipeline. We will have the ability to say: "construct
107 me a pipeline that does an audio/mpeg to audio/raw conversion".
111 The basic GStreamer library does not try to solve all of your
112 autoplug problems. It leaves the hard decisions to the application
113 programmer, where they belong.
120 <title>GStreamer types</title>
122 GStreamer assigns a unique number to all registered MIME types.
123 GStreamer also keeps a reference to
124 a function that can be used to determine if a given buffer is of
128 There is also an association between a MIME type and a file
132 The type information is maintained in a list of
133 <classname>GstType</classname>. The definition of a
134 <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 */
151 All operations on <classname>GstType</classname> occur via their
152 <classname>guint16 id</classname> numbers, with <classname>GstType</classname>
153 structure private to the GStreamer library.
157 <title>MIME type to id conversion</title>
160 We can obtain the id for a given MIME type
161 with the following piece of code:
166 id = gst_type_find_by_mime ("audio/mpeg");
169 This function will return 0 if the type was not known.
174 <title>id to <classname>GstType</classname> conversion</title>
176 We can obtain the <classname>GstType</classname> for a given id
177 with the following piece of code:
182 type = gst_type_find_by_id (id);
185 This function will return NULL if the id was associated with
186 any known <classname>GstType</classname>
191 <title>extension to id conversion</title>
193 We can obtain the id for a given file extension
194 with the following piece of code:
199 id = gst_type_find_by_ext (".mp3");
202 This function will return 0 if the extension was not known.
207 <title>id to <classname>GstElementFactory</classname> conversion</title>
209 When we have obtained a given type id using one of the above methods,
210 we can obtain a list of all the elements that operate on this MIME
214 Obtain a list of all the elements that use this id as source with:
219 list = gst_type_gst_srcs (id);
223 Obtain a list of all the elements that use this id as sink with:
228 list = gst_type_gst_sinks (id);
231 When you have a list of elements, you can simply take the first
232 element of the list to obtain an appropriate element.
236 As you can see, there might be a multitude of elements that
237 are able to operate on audio/raw types. some might include:
241 an MP3 audio encoder.
260 Depending on the application, you might want to use a different
261 element. This is why GStreamer leaves that decision up to the
262 application programmer.
269 <title>id to id path detection</title>
271 You can obtain a <classname>GList</classname> of elements that
272 will transform the source id into the destination id.
277 list = gst_type_gst_sink_to_src (sourceid, sinkid);
280 This piece of code will give you the elements needed to construct
281 a path from sourceid to sinkid. This function is mainly used in
282 autoplugging the pipeline.
288 <title>creating elements with the factory</title>
290 In the previous section we described how you could obtain
291 an element factory using MIME types. One the factory has been
292 obtained, you can create an element using:
295 GstElementFactory *factory;
298 // obtain the factory
301 element = gst_elementfactory_create (factory, "name");
304 This way, you do not have to create elements by name which
305 allows the end-user to select the elements he/she prefers for the
311 <title>GStreamer basic types</title>
313 GStreamer only has two builtin types:
318 audio/raw : raw audio samples
323 video/raw and image/raw : raw video data
328 All other MIME types are maintained by the plugin elements.