<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >Plugging together dynamic pipelines</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79;charset=UTF-8"><LINK REL="HOME" TITLE="GStreamer Application Development Manual (0.10.36)" HREF="index.html"><LINK REL="UP" TITLE="Autoplugging" HREF="chapter-autoplugging.html"><LINK REL="PREVIOUS" TITLE="Media stream type detection" HREF="section-typefinding.html"><LINK REL="NEXT" TITLE="Pipeline manipulation" HREF="chapter-dataaccess.html"></HEAD ><BODY CLASS="sect1" 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 (0.10.36)</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="section-typefinding.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Chapter 17. Autoplugging</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="chapter-dataaccess.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="section-dynamic" >17.3. Plugging together dynamic pipelines</A ></H1 ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" WIDTH="100%" BORDER="0" ><TR ><TD WIDTH="25" ALIGN="CENTER" VALIGN="TOP" ><IMG SRC="../images/warning.gif" HSPACE="5" ALT="Warning"></TD ><TD ALIGN="LEFT" VALIGN="TOP" ><P > The code in this section is broken, outdated and overly complicated. Also, you should use decodebin, playbin or uridecodebin to get decoders plugged automatically. </P ></TD ></TR ></TABLE ></DIV ><P > In this chapter we will see how you can create a dynamic pipeline. A dynamic pipeline is a pipeline that is updated or created while data is flowing through it. We will create a partial pipeline first and add more elements while the pipeline is playing. The basis of this player will be the application that we wrote in the previous section (<A HREF="section-typefinding.html" >Section 17.2</A >) to identify unknown media streams. </P ><P > Once the type of the media has been found, we will find elements in the registry that can decode this streamtype. For this, we will get all element factories (which we've seen before in <A HREF="section-elements-create.html" >Section 5.2</A >) and find the ones with the given MIME-type and capabilities on their sinkpad. Note that we will only use parsers, demuxers and decoders. We will not use factories for any other element types, or we might get into a loop of encoders and decoders. For this, we will want to build a list of <SPAN CLASS="QUOTE" >"allowed"</SPAN > factories right after initializing <SPAN CLASS="application" >GStreamer</SPAN >. </P ><PRE CLASS="programlisting" > static GList *factories; /* * This function is called by the registry loader. Its return value * (TRUE or FALSE) decides whether the given feature will be included * in the list that we're generating further down. */ static gboolean cb_feature_filter (GstPluginFeature *feature, gpointer data) { const gchar *klass; guint rank; /* we only care about element factories */ if (!GST_IS_ELEMENT_FACTORY (feature)) return FALSE; /* only parsers, demuxers and decoders */ klass = gst_element_factory_get_klass (GST_ELEMENT_FACTORY (feature)); if (g_strrstr (klass, "Demux") == NULL && g_strrstr (klass, "Decoder") == NULL && g_strrstr (klass, "Parse") == NULL) return FALSE; /* only select elements with autoplugging rank */ rank = gst_plugin_feature_get_rank (feature); if (rank < GST_RANK_MARGINAL) return FALSE; return TRUE; } /* * This function is called to sort features by rank. */ static gint cb_compare_ranks (GstPluginFeature *f1, GstPluginFeature *f2) { return gst_plugin_feature_get_rank (f2) - gst_plugin_feature_get_rank (f1); } static void init_factories (void) { /* first filter out the interesting element factories */ factories = gst_registry_feature_filter ( gst_registry_get_default (), (GstPluginFeatureFilter) cb_feature_filter, FALSE, NULL); /* sort them according to their ranks */ factories = g_list_sort (factories, (GCompareFunc) cb_compare_ranks); } </PRE ><P > From this list of element factories, we will select the one that most likely will help us decoding a media stream to a given output type. For each newly created element, we will again try to autoplug new elements to its source pad(s). Also, if the element has dynamic pads (which we've seen before in <A HREF="chapter-pads.html#section-pads-dynamic" >Section 8.1.1</A >), we will listen for newly created source pads and handle those, too. The following code replaces the <CODE CLASS="function" >cb_type_found</CODE > from the previous section with a function to initiate autoplugging, which will continue with the above approach. </P ><PRE CLASS="programlisting" > static void try_to_plug (GstPad *pad, const GstCaps *caps); static GstElement *audiosink; static void cb_newpad (GstElement *element, GstPad *pad, gpointer data) { GstCaps *caps; caps = gst_pad_get_caps (pad); try_to_plug (pad, caps); gst_caps_unref (caps); } static void close_link (GstPad *srcpad, GstElement *sinkelement, const gchar *padname, const GList *templlist) { GstPad *pad; gboolean has_dynamic_pads = FALSE; g_print ("Plugging pad %s:%s to newly created %s:%s\n", gst_object_get_name (GST_OBJECT (gst_pad_get_parent (srcpad))), gst_pad_get_name (srcpad), gst_object_get_name (GST_OBJECT (sinkelement)), padname); /* add the element to the pipeline and set correct state */ if (sinkelement != audiosink) { gst_bin_add (GST_BIN (pipeline), sinkelement); gst_element_set_state (sinkelement, GST_STATE_READY); } pad = gst_element_get_static_pad (sinkelement, padname); gst_pad_link (srcpad, pad); if (sinkelement != audiosink) { gst_element_set_state (sinkelement, GST_STATE_PAUSED); } gst_object_unref (GST_OBJECT (pad)); /* if we have static source pads, link those. If we have dynamic * source pads, listen for pad-added signals on the element */ for ( ; templlist != NULL; templlist = templlist->next) { GstStaticPadTemplate *templ = templlist->data; /* only sourcepads, no request pads */ if (templ->direction != GST_PAD_SRC || templ->presence == GST_PAD_REQUEST) { continue; } switch (templ->presence) { case GST_PAD_ALWAYS: { GstPad *pad = gst_element_get_static_pad (sinkelement, templ->name_template); GstCaps *caps = gst_pad_get_caps (pad); /* link */ try_to_plug (pad, caps); gst_object_unref (GST_OBJECT (pad)); gst_caps_unref (caps); break; } case GST_PAD_SOMETIMES: has_dynamic_pads = TRUE; break; default: break; } } /* listen for newly created pads if this element supports that */ if (has_dynamic_pads) { g_signal_connect (sinkelement, "pad-added", G_CALLBACK (cb_newpad), NULL); } } static void try_to_plug (GstPad *pad, const GstCaps *caps) { GstObject *parent = GST_OBJECT (GST_OBJECT_PARENT (pad)); const gchar *mime; const GList *item; GstCaps *res, *audiocaps; /* don't plug if we're already plugged - FIXME: memleak for pad */ if (GST_PAD_IS_LINKED (gst_element_get_static_pad (audiosink, "sink"))) { g_print ("Omitting link for pad %s:%s because we're already linked\n", GST_OBJECT_NAME (parent), GST_OBJECT_NAME (pad)); return; } /* as said above, we only try to plug audio... Omit video */ mime = gst_structure_get_name (gst_caps_get_structure (caps, 0)); if (g_strrstr (mime, "video")) { g_print ("Omitting link for pad %s:%s because mimetype %s is non-audio\n", GST_OBJECT_NAME (parent), GST_OBJECT_NAME (pad), mime); return; } /* can it link to the audiopad? */ audiocaps = gst_pad_get_caps (gst_element_get_static_pad (audiosink, "sink")); res = gst_caps_intersect (caps, audiocaps); if (res && !gst_caps_is_empty (res)) { g_print ("Found pad to link to audiosink - plugging is now done\n"); close_link (pad, audiosink, "sink", NULL); gst_caps_unref (audiocaps); gst_caps_unref (res); return; } gst_caps_unref (audiocaps); gst_caps_unref (res); /* try to plug from our list */ for (item = factories; item != NULL; item = item->next) { GstElementFactory *factory = GST_ELEMENT_FACTORY (item->data); const GList *pads; for (pads = gst_element_factory_get_static_pad_templates (factory); pads != NULL; pads = pads->next) { GstStaticPadTemplate *templ = pads->data; /* find the sink template - need an always pad*/ if (templ->direction != GST_PAD_SINK || templ->presence != GST_PAD_ALWAYS) { continue; } /* can it link? */ res = gst_caps_intersect (caps, gst_static_caps_get (&templ->static_caps)); if (res && !gst_caps_is_empty (res)) { GstElement *element; gchar *name_template = g_strdup (templ->name_template); /* close link and return */ gst_caps_unref (res); element = gst_element_factory_create (factory, NULL); close_link (pad, element, name_template, gst_element_factory_get_static_pad_templates (factory)); g_free (name_template); return; } gst_caps_unref (res); /* we only check one sink template per factory, so move on to the * next factory now */ break; } } /* if we get here, no item was found */ g_print ("No compatible pad found to decode %s on %s:%s\n", mime, GST_OBJECT_NAME (parent), GST_OBJECT_NAME (pad)); } static void cb_typefound (GstElement *typefind, guint probability, GstCaps *caps, gpointer data) { gchar *s; GstPad *pad; s = gst_caps_to_string (caps); g_print ("Detected media type %s\n", s); g_free (s); /* actually plug now */ pad = gst_element_get_static_pad (typefind, "src"); try_to_plug (pad, caps); gst_object_unref (GST_OBJECT (pad)); } </PRE ><P > By doing all this, we will be able to make a simple autoplugger that can automatically setup a pipeline for any media type. In the example above, we did this for audio only. However, we can also do this for video to create a player that plays both audio and video. </P ><P > The example above is a good first try for an autoplugger. Next steps would be to listen for <SPAN CLASS="QUOTE" >"pad-removed"</SPAN > signals, so we can dynamically change the plugged pipeline if the stream changes (this happens for DVB or Ogg radio). Also, you might want special-case code for input with known content (such as a DVD or an audio-CD), and much, much more. Moreover, you'll want many checks to prevent infinite loops during autoplugging, maybe you'll want to implement shortest-path-finding to make sure the most optimal pipeline is chosen, and so on. Basically, the features that you implement in an autoplugger depend on what you want to use it for. For full-blown implementations, see the <SPAN CLASS="QUOTE" >"playbin"</SPAN > and <SPAN CLASS="QUOTE" >"decodebin"</SPAN > elements in <A HREF="chapter-components.html" >Chapter 19</A >. </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="section-typefinding.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-dataaccess.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Media stream type detection</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="chapter-autoplugging.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Pipeline manipulation</TD ></TR ></TABLE ></DIV ></BODY ></HTML >