Threads
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.
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 one big problems:
The elements are created base 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 added to the source and
sink pads.
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 donote 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.
In our helloworld example the elements we constructed would have the
following MIME types associated with their source and sink pads:
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. It
also maintains a list of all elements that either uses this type
as a source or as a sink. 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 gboolean (*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 */
GList *srcs; /* list of src objects for this type */
GList *sinks; /* list of sink objects for type */
};
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 GstType 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 GstElementFactory 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. Once 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.