More on factories The small application we created in the previous chapter used the concept of a factory to create the elements. In this chapter we will show you how to use the factory concepts to create elements based on what they do instead of how they are called. We will first explain the concepts involved before we move on to the reworked helloworld example using autoplugging. The problems with the helloworld example If we take a look at how the elements were created in the previous example we used a rather crude mechanism: ... /* now it's time to get the parser */ parse = gst_elementfactory_make("mp3parse","parse"); decoder = gst_elementfactory_make("mpg123","decoder"); ... While this mechanism is quite effective it also has some big problems: The elements are created based on their name. Indeed, we create an element mpg123 by explicitly stating the mpg123 elements name. Our little program therefore always uses the mpg123 decoder element to decode the MP3 audio stream, even if there are 3 other MP3 decoders in the system. We will see how we can use a more general way to create an MP3 decoder element. We have to introduce the concept of MIME types and capabilities added to the source and sink pads. more on MIME Types GStreamer uses MIME types to indentify the different types of data that can be handled by the elements. They are the high level mechanisms to make sure that everyone is talking about the right kind of data. A MIME (Multipurpose Internet Mail Extension) types are a set of string that denote a certain type of data. examples include: audio/raw : raw audio samples audio/mpeg : mpeg audio video/mpeg : mpeg video An element must associate a MIME type to its source and sink pads when it is loaded into the system. GStreamer knows about the different elements and what type of data they expect and emit. This allows for very dynamic and extensible element creation as we will see. As we have seen in the previous chapter, the MIME types are added to the Capability structure of a pad. In our helloworld example the elements we constructed would have the following MIME types associated with their source and sink pads:
The Hello world pipeline with MIME types
We will see how you can create an element based on the MIME types of its source and sink pads. This way the end-user will have the ability to choose his/her favorite audio/mpeg decoder without you even having to care about it. The typing of the source and sink pads also makes it possible to 'autoplug' a pipeline. We will have the ability to say: "construct me a pipeline that does an audio/mpeg to audio/raw conversion". The basic GStreamer library does not try to solve all of your autoplug problems. It leaves the hard decisions to the application programmer, where they belong.
GStreamer types GStreamer assigns a unique number to all registered MIME types. GStreamer also keeps a reference to a function that can be used to determine if a given buffer is of the given MIME type. There is also an association between a MIME type and a file extension. The type information is maintained in a list of GstType. The definition of a GstType is like: typedef GstCaps (*GstTypeFindFunc) (GstBuffer *buf,gpointer *priv); typedef struct _GstType GstType; struct _GstType { guint16 id; /* type id (assigned) */ gchar *mime; /* MIME type */ gchar *exts; /* space-delimited list of extensions */ GstTypeFindFunc typefindfunc; /* typefind function */ }; All operations on GstType occur via their guint16 id numbers, with GstType structure private to the GStreamer library. MIME type to id conversion We can obtain the id for a given MIME type with the following piece of code: guint16 id; id = gst_type_find_by_mime("audio/mpeg"); This function will return 0 if the type was not known. id to <classname>GstType</classname> conversion We can obtain the GstType for a given id with the following piece of code: GstType *type; type = gst_type_find_by_id(id); This function will return NULL if the id was associated with any known GstType extension to id conversion We can obtain the id for a given file extension with the following piece of code: guint16 id; id = gst_type_find_by_ext(".mp3"); This function will return 0 if the extension was not known. id to <classname>GstElementFactory</classname> conversion When we have obtained a given type id using one of the above methods, we can obtain a list of all the elements that operate on this MIME type or extension. Obtain a list of all the elements that use this id as source with: GList *list; list = gst_type_gst_srcs(id); Obtain a list of all the elements that use this id as sink with: GList *list; list = gst_type_gst_sinks(id); When you have a list of elements, you can simply take the first element of the list to obtain an appropriate element. As you can see, there might be a multitude of elements that are able to operate on audio/raw types. some might include: an MP3 audio encoder. an audio sink. an audio resampler. a spectrum filter. Depending on the application, you might want to use a different element. This is why GStreamer leaves that decision up to the application programmer. id to id path detection You can obtain a GList of elements that will transform the source id into the destination id. GList *list; list = gst_type_gst_sink_to_src(sourceid, sinkid); This piece of code will give you the elements needed to construct a path from sourceid to sinkid. This function is mainly used in autoplugging the pipeline. creating elements with the factory In the previous section we described how you could obtain an element factory using MIME types. One the factory has been obtained, you can create an element using: GstElementFactory *factory; GstElement *element; // obtain the factory factory = ... element = gst_elementfactory_create(factory, "name"); This way, you do not have to create elements by name which allows the end-user to select the elements he/she prefers for the given MIME types. GStreamer basic types GStreamer only has two builtin types: audio/raw : raw audio samples video/raw and image/raw : raw video data All other MIME types are maintained by the plugin elements.