mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer.git
synced 2024-09-25 21:40:34 +00:00
e6a59c8a62
Original commit message from CVS: Merged HEAD from BRANCH-INCSCHED1-200104161 into BRANCH-INCSCHED1.
146 lines
4.6 KiB
Text
146 lines
4.6 KiB
Text
<chapter id="cha-typedetection">
|
|
<title>Typedetection</title>
|
|
<para>
|
|
Sometimes the capabilities of a pad are not specificied. The disksrc, for
|
|
example, does not know what type of file it is reading. Before you can attach
|
|
an element to the pad of the disksrc, you need to determine the media type in
|
|
order to be able to choose a compatible element.
|
|
</para>
|
|
<para>
|
|
To solve this problem, a plugin can provide the <application>GStreamer</application>
|
|
core library with a typedefinition library with a typedefinition. The typedefinition
|
|
will contain the following information:
|
|
<itemizedlist>
|
|
<listitem>
|
|
<para>
|
|
The MIME type we are going to define.
|
|
</para>
|
|
</listitem>
|
|
<listitem>
|
|
<para>
|
|
An optional string with a list of possible file extensions this
|
|
type usually is associated with. the list entries are separated with
|
|
a space. eg, ".mp3 .mpa .mpg".
|
|
</para>
|
|
</listitem>
|
|
<listitem>
|
|
<para>
|
|
An optional typefind function.
|
|
</para>
|
|
</listitem>
|
|
</itemizedlist>
|
|
</para>
|
|
<para>
|
|
The typefind functions give a meaning to the MIME types that are used
|
|
in GStreamer. The typefind function is a function with the following definition:
|
|
</para>
|
|
<programlisting>
|
|
typedef GstCaps *(*GstTypeFindFunc) (GstBuffer *buf, gpointer priv);
|
|
</programlisting>
|
|
<para>
|
|
This typefind function will inspect a GstBuffer with data and will output
|
|
a GstCaps structure describing the type. If the typefind function does not
|
|
understand the buffer contents, it will return NULL.
|
|
</para>
|
|
<para>
|
|
<application>GStreamer</application> has a typefind element in its core elements
|
|
that can be used to determine the type of a given pad.
|
|
</para>
|
|
<para>
|
|
The next example will show how a typefind element can be inserted into a pipeline
|
|
to detect the media type of a file. It will output the capabilities of the pad into
|
|
an XML representation.
|
|
</para>
|
|
<programlisting>
|
|
#include <gst/gst.h>
|
|
|
|
void type_found (GstElement *typefind, GstCaps* caps);
|
|
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
GstElement *bin, *disksrc, *typefind;
|
|
|
|
gst_init(&argc,&argv);
|
|
|
|
if (argc != 2) {
|
|
g_print("usage: %s <filename>\n", argv[0]);
|
|
exit(-1);
|
|
}
|
|
|
|
/* create a new bin to hold the elements */
|
|
bin = gst_bin_new("bin");
|
|
g_assert(bin != NULL);
|
|
|
|
/* create a disk reader */
|
|
disksrc = gst_elementfactory_make("disksrc", "disk_source");
|
|
g_assert(disksrc != NULL);
|
|
gtk_object_set(GTK_OBJECT(disksrc),"location", argv[1],NULL);
|
|
|
|
/* create the typefind element */
|
|
typefind = gst_elementfactory_make("typefind", "typefind");
|
|
g_assert(typefind != NULL);
|
|
|
|
/* add objects to the main pipeline */
|
|
gst_bin_add(GST_BIN(bin), disksrc);
|
|
gst_bin_add(GST_BIN(bin), typefind);
|
|
|
|
gtk_signal_connect (GTK_OBJECT (typefind), "have_type",
|
|
type_found, NULL);
|
|
|
|
gst_pad_connect(gst_element_get_pad(disksrc,"src"),
|
|
gst_element_get_pad(typefind,"sink"));
|
|
|
|
/* start playing */
|
|
gst_element_set_state(GST_ELEMENT(bin), GST_STATE_PLAYING);
|
|
|
|
gst_bin_iterate(GST_BIN(bin));
|
|
|
|
gst_element_set_state(GST_ELEMENT(bin), GST_STATE_NULL);
|
|
|
|
exit(0);
|
|
}
|
|
</programlisting>
|
|
<para>
|
|
We create a very simple pipeline with only a disksrc and the typefind element
|
|
in it. The sinkpad of the typefind element has been connected to the src pad
|
|
of the disksrc.
|
|
</para>
|
|
<para>
|
|
We attached a signal 'have_type' to the typefind element which will be called
|
|
when the type of the media stream as been detected.
|
|
</para>
|
|
<para>
|
|
the typefind function will loop over all the registered types and will execute
|
|
each of the typefind functions. As soon as a function returns a GstCaps pointer,
|
|
the type_found function will be called:
|
|
</para>
|
|
|
|
<programlisting>
|
|
void
|
|
type_found (GstElement *typefind, GstCaps* caps)
|
|
{
|
|
xmlDocPtr doc;
|
|
xmlNodePtr parent;
|
|
|
|
doc = xmlNewDoc ("1.0");
|
|
doc->root = xmlNewDocNode (doc, NULL, "Capabilities", NULL);
|
|
|
|
parent = xmlNewChild (doc->root, NULL, "Caps1", NULL);
|
|
gst_caps_save_thyself (caps, parent);
|
|
|
|
xmlDocDump (stdout, doc);
|
|
}
|
|
</programlisting>
|
|
<para>
|
|
In the type_found function we can print or inspect the type that has been
|
|
detected using the GstCaps APIs. In this example, we just print out the
|
|
XML representation of the caps structure to stdout.
|
|
</para>
|
|
<para>
|
|
A more usefull option would be to use the registry to look up an element
|
|
that can handle this particular caps structure, or we can also use the
|
|
autoplugger to connect this caps structure to, for example, a videosink.
|
|
</para>
|
|
|
|
</chapter>
|