<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML ><HEAD ><TITLE >Type Detection</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="GStreamer Application Development Manual" HREF="index.html"><LINK REL="UP" TITLE="Advanced GStreamer concepts" HREF="part-advanced.html"><LINK REL="PREVIOUS" TITLE="Dynamic pipelines" HREF="chapter-dynamic.html"><LINK REL="NEXT" TITLE="Autoplugging" HREF="chapter-autoplug.html"></HEAD ><BODY CLASS="chapter" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" ><SPAN CLASS="application" >GStreamer</SPAN > Application Development Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="chapter-dynamic.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="chapter-autoplug.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="chapter" ><H1 ><A NAME="chapter-typedetection" ></A >Chapter 27. Type Detection</H1 ><P > Sometimes the capabilities of a pad are not specificied. The filesrc element, for example, does not know what type of file it is reading. Before you can attach an element to the pad of the filesrc, you need to determine the media type in order to be able to choose a compatible element. </P ><P > To solve this problem, a plugin can provide the <SPAN CLASS="application" >GStreamer</SPAN > core library with a type definition. The type definition will contain the following information: <P ></P ><UL ><LI ><P > The MIME type we are going to define. </P ></LI ><LI ><P > 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". </P ></LI ><LI ><P > An optional typefind function. </P ></LI ></UL > </P ><P > 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: </P ><PRE CLASS="programlisting" > typedef GstCaps *(*GstTypeFindFunc) (GstBuffer *buf, gpointer priv); </PRE ><P > 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. </P ><P > <SPAN CLASS="application" >GStreamer</SPAN > has a typefind element in the set of core elements that can be used to determine the type of a given pad. </P ><P > 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. </P ><PRE CLASS="programlisting" > #include <gst/gst.h> void type_found (GstElement *typefind, GstCaps* caps); int main(int argc, char *argv[]) { GstElement *bin, *filesrc, *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 */ filesrc = gst_element_factory_make ("filesrc", "disk_source"); g_assert (filesrc != NULL); g_object_set (G_OBJECT (filesrc), "location", argv[1], NULL); /* create the typefind element */ typefind = gst_element_factory_make ("typefind", "typefind"); g_assert (typefind != NULL); /* add objects to the main pipeline */ gst_bin_add_many (GST_BIN (bin), filesrc, typefind, NULL); g_signal_connect (G_OBJECT (typefind), "have_type", G_CALLBACK (type_found), NULL); gst_element_link (filesrc, typefind); /* 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); } </PRE ><P > We create a very simple pipeline with only a filesrc and the typefind element in it. The sinkpad of the typefind element has been linked to the source pad of the filesrc. </P ><P > We attached a signal 'have_type' to the typefind element which will be called when the type of the media stream as been detected. </P ><P > 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: </P ><PRE CLASS="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); } </PRE ><P > 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. </P ><P > A more useful 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 link this caps structure to, for example, a videosink. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="chapter-dynamic.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="chapter-autoplug.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Dynamic pipelines</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="part-advanced.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Autoplugging</TD ></TR ></TABLE ></DIV ></BODY ></HTML >