2002-09-27 18:34:33 +00:00
|
|
|
|
|
|
|
<!-- ############ chapter ############# -->
|
|
|
|
|
2004-01-28 15:51:14 +00:00
|
|
|
<chapter id="chapter-building-pads">
|
2002-09-27 18:34:33 +00:00
|
|
|
<title>Specifying the pads</title>
|
|
|
|
<para>
|
2004-01-27 13:33:39 +00:00
|
|
|
As explained before, pads are the port through which data goes in and out
|
|
|
|
of your element, and that makes them a very important item in the process
|
|
|
|
of element creation. In the boilerplate code, we have seen how static pad
|
|
|
|
templates take care of registering pad templates with the element class.
|
2005-07-06 12:18:00 +00:00
|
|
|
Here, we will see how to create actual elements, use a <function>_setcaps
|
|
|
|
()</function>-functions to configure for a particular format and how to
|
|
|
|
register functions to let data flow through the element.
|
2002-09-27 18:34:33 +00:00
|
|
|
</para>
|
2004-01-27 13:33:39 +00:00
|
|
|
<para>
|
|
|
|
In the element <function>_init ()</function> function, you create the pad
|
|
|
|
from the pad template that has been registered with the element class in
|
|
|
|
the <function>_base_init ()</function> function. After creating the pad,
|
2005-07-06 12:18:00 +00:00
|
|
|
you have to set a <function>_setcaps ()</function> function pointer and
|
|
|
|
optionally a <function>_getcaps ()</function> function pointer. Also, you
|
|
|
|
have to set a <function>_chain ()</function> function pointer.
|
2010-02-08 03:42:01 +00:00
|
|
|
Alternatively, pads can also operate in looping mode, which means that they
|
2005-07-06 12:18:00 +00:00
|
|
|
can pull data themselves. More on this topic later. After that, you have
|
|
|
|
to register the pad with the element. This happens like this:
|
2004-01-27 13:33:39 +00:00
|
|
|
</para>
|
2005-07-01 12:43:03 +00:00
|
|
|
<programlisting><!-- example-begin init.func a --><!--
|
|
|
|
#include "filter.h"
|
|
|
|
#include <string.h>
|
|
|
|
|
2005-12-11 18:24:27 +00:00
|
|
|
static GstStateChangeReturn
|
|
|
|
gst_my_filter_change_state (GstElement * element, GstStateChange transition);
|
2005-07-01 12:43:03 +00:00
|
|
|
|
|
|
|
GST_BOILERPLATE (GstMyFilter, gst_my_filter, GstElement, GST_TYPE_ELEMENT);
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_my_filter_base_init (gpointer klass)
|
|
|
|
{
|
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
|
|
|
|
static GstElementDetails my_filter_details = {
|
|
|
|
"An example plugin",
|
|
|
|
"Example/FirstExample",
|
|
|
|
"Shows the basic structure of a plugin",
|
|
|
|
"your name <your.name@your.isp>"
|
|
|
|
};
|
|
|
|
static GstStaticPadTemplate sink_factory =
|
|
|
|
GST_STATIC_PAD_TEMPLATE (
|
|
|
|
"sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS ("ANY")
|
|
|
|
);
|
|
|
|
static GstStaticPadTemplate src_factory =
|
|
|
|
GST_STATIC_PAD_TEMPLATE (
|
|
|
|
"src",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS ("ANY")
|
|
|
|
);
|
|
|
|
|
|
|
|
gst_element_class_set_details (element_class, &my_filter_details);
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_static_pad_template_get (&src_factory));
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_static_pad_template_get (&sink_factory));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_my_filter_class_init (GstMyFilterClass * klass)
|
|
|
|
{
|
|
|
|
GST_ELEMENT_CLASS (klass)->change_state = gst_my_filter_change_state;
|
|
|
|
}
|
|
|
|
--><!-- example-end init.func a -->
|
|
|
|
<!-- example-begin init.func b -->
|
|
|
|
static gboolean gst_my_filter_setcaps (GstPad *pad,
|
|
|
|
GstCaps *caps);
|
|
|
|
static GstFlowReturn gst_my_filter_chain (GstPad *pad,
|
|
|
|
GstBuffer *buf);
|
|
|
|
<!-- example-end init.func b -->
|
|
|
|
<!-- example-begin init.func c --><!--
|
2005-07-06 12:18:00 +00:00
|
|
|
static GstCaps * gst_my_filter_getcaps (GstPad *pad);
|
2005-07-01 12:43:03 +00:00
|
|
|
static gboolean gst_my_filter_event (GstPad *pad,
|
|
|
|
GstEvent *event);
|
|
|
|
--><!-- example-end init.func c -->
|
|
|
|
<!-- example-begin init.func d -->
|
2004-01-27 13:33:39 +00:00
|
|
|
|
|
|
|
static void
|
2006-03-07 17:06:53 +00:00
|
|
|
gst_my_filter_init (GstMyFilter *filter, GstMyFilterClass *filter_klass)
|
2004-01-27 13:33:39 +00:00
|
|
|
{
|
2006-03-07 17:06:53 +00:00
|
|
|
GstElementClass *klass = GST_ELEMENT_CLASS (filter_klass);
|
2004-01-27 13:33:39 +00:00
|
|
|
|
|
|
|
/* pad through which data comes in to the element */
|
2004-01-27 23:08:18 +00:00
|
|
|
filter->sinkpad = gst_pad_new_from_template (
|
2004-01-27 13:33:39 +00:00
|
|
|
gst_element_class_get_pad_template (klass, "sink"), "sink");
|
2005-07-01 12:43:03 +00:00
|
|
|
gst_pad_set_setcaps_function (filter->sinkpad, gst_my_filter_setcaps);
|
2004-01-27 23:08:18 +00:00
|
|
|
gst_pad_set_chain_function (filter->sinkpad, gst_my_filter_chain);
|
2005-07-01 12:43:03 +00:00
|
|
|
<!-- example-end init.func d -->
|
|
|
|
<!-- example-begin init.func e --><!--
|
2005-07-06 12:18:00 +00:00
|
|
|
gst_pad_set_getcaps_function (filter->sinkpad, gst_my_filter_getcaps);
|
2005-07-01 12:43:03 +00:00
|
|
|
gst_pad_set_event_function (filter->sinkpad, gst_my_filter_event);
|
|
|
|
--><!-- example-end init.func e -->
|
|
|
|
<!-- example-begin init.func f -->
|
2004-01-27 23:08:18 +00:00
|
|
|
gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
|
2004-01-27 13:33:39 +00:00
|
|
|
|
|
|
|
/* pad through which data goes out of the element */
|
2004-01-27 23:08:18 +00:00
|
|
|
filter->srcpad = gst_pad_new_from_template (
|
2004-01-27 13:33:39 +00:00
|
|
|
gst_element_class_get_pad_template (klass, "src"), "src");
|
2005-07-06 12:18:00 +00:00
|
|
|
<!-- example-end init.func f -->
|
|
|
|
<!-- example-begin init.func g --><!--
|
2004-01-27 23:08:18 +00:00
|
|
|
gst_pad_set_getcaps_function (filter->srcpad, gst_my_filter_getcaps);
|
2005-07-06 12:18:00 +00:00
|
|
|
--><!-- example-end init.func g -->
|
|
|
|
<!-- example-begin init.func h -->
|
2004-01-27 23:08:18 +00:00
|
|
|
gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
|
2005-07-01 12:43:03 +00:00
|
|
|
|
|
|
|
/* properties initial value */
|
|
|
|
filter->silent = FALSE;
|
2004-01-27 13:33:39 +00:00
|
|
|
}
|
2005-07-06 12:18:00 +00:00
|
|
|
<!-- example-end init.func h --></programlisting>
|
2004-01-28 10:03:51 +00:00
|
|
|
|
2004-01-28 15:51:14 +00:00
|
|
|
<sect1 id="section-pads-linkfn" xreflabel="The link function">
|
2005-07-06 12:18:00 +00:00
|
|
|
<title>The setcaps-function</title>
|
2004-01-27 13:33:39 +00:00
|
|
|
<para>
|
2005-07-06 12:18:00 +00:00
|
|
|
The <function>_setcaps ()</function>-function is called during caps
|
|
|
|
negotiation, which is discussed in great detail in <xref
|
|
|
|
linkend="chapter-negotiation"/>. This is the process where the linked
|
|
|
|
pads decide on the streamtype that will transfer between them. A full
|
|
|
|
list of type-definitions can be found in <xref
|
|
|
|
linkend="chapter-building-types"/>. A <function>_link ()</function>
|
2004-07-27 15:01:10 +00:00
|
|
|
receives a pointer to a <ulink type="http"
|
2005-07-06 12:18:00 +00:00
|
|
|
url="../../gstreamer/html/gstreamer-GstCaps.html"><classname>GstCaps</classname></ulink>
|
|
|
|
struct that defines the proposed streamtype, and can respond with
|
|
|
|
either <quote>yes</quote> (<symbol>TRUE</symbol>) or <quote>no</quote>
|
|
|
|
(<symbol>FALSE</symbol>). If the element responds positively towards
|
|
|
|
the streamtype, that type will be used on the pad. An example:
|
2004-01-27 13:33:39 +00:00
|
|
|
</para>
|
2005-07-01 12:43:03 +00:00
|
|
|
<programlisting><!-- example-begin caps.func a -->
|
|
|
|
static gboolean
|
|
|
|
gst_my_filter_setcaps (GstPad *pad,
|
|
|
|
GstCaps *caps)
|
2004-01-27 13:33:39 +00:00
|
|
|
{
|
|
|
|
GstStructure *structure = gst_caps_get_structure (caps, 0);
|
2005-07-06 12:18:00 +00:00
|
|
|
GstMyFilter *filter = GST_MY_FILTER (GST_OBJECT_PARENT (pad));
|
2004-01-27 13:33:39 +00:00
|
|
|
const gchar *mime;
|
|
|
|
|
|
|
|
/* Since we're an audio filter, we want to handle raw audio
|
|
|
|
* and from that audio type, we need to get the samplerate and
|
|
|
|
* number of channels. */
|
|
|
|
mime = gst_structure_get_name (structure);
|
|
|
|
if (strcmp (mime, "audio/x-raw-int") != 0) {
|
|
|
|
GST_WARNING ("Wrong mimetype %s provided, we only support %s",
|
|
|
|
mime, "audio/x-raw-int");
|
2005-07-15 12:55:30 +00:00
|
|
|
return FALSE;
|
2004-01-27 13:33:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* we're a filter and don't touch the properties of the data.
|
|
|
|
* That means we can set the given caps unmodified on the next
|
|
|
|
* element, and use that negotiation return value as ours. */
|
2005-07-06 12:18:00 +00:00
|
|
|
if (!gst_pad_set_caps (filter->srcpad, caps))
|
|
|
|
return FALSE;
|
2004-01-27 13:33:39 +00:00
|
|
|
|
|
|
|
/* Capsnego succeeded, get the stream properties for internal
|
|
|
|
* usage and return success. */
|
2004-01-27 23:08:18 +00:00
|
|
|
gst_structure_get_int (structure, "rate", &filter->samplerate);
|
|
|
|
gst_structure_get_int (structure, "channels", &filter->channels);
|
2004-01-27 13:33:39 +00:00
|
|
|
|
|
|
|
g_print ("Caps negotiation succeeded with %d Hz @ %d channels\n",
|
2004-01-27 23:08:18 +00:00
|
|
|
filter->samplerate, filter->channels);
|
2004-01-27 13:33:39 +00:00
|
|
|
|
2005-07-06 12:18:00 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
<!-- example-end caps.func a -->
|
|
|
|
<!-- example-begin caps.func b --><!--
|
|
|
|
static GstCaps *
|
|
|
|
gst_my_filter_getcaps (GstPad * pad)
|
|
|
|
{
|
|
|
|
GstMyFilter *filter = GST_MY_FILTER (GST_OBJECT_PARENT (pad));
|
|
|
|
GstPad *otherpad = (pad == filter->srcpad) ? filter->sinkpad :
|
|
|
|
filter->srcpad;
|
|
|
|
GstCaps *othercaps = gst_pad_get_allowed_caps (otherpad);
|
|
|
|
|
|
|
|
return othercaps;
|
2004-01-27 13:33:39 +00:00
|
|
|
}
|
2005-07-06 12:18:00 +00:00
|
|
|
--><!-- example-end caps.func b --></programlisting>
|
2004-01-28 10:03:51 +00:00
|
|
|
<para>
|
|
|
|
In here, we check the mimetype of the provided caps. Normally, you don't
|
|
|
|
need to do that in your own plugin/element, because the core does that
|
|
|
|
for you. We simply use it to show how to retrieve the mimetype from a
|
2004-07-27 15:01:10 +00:00
|
|
|
provided set of caps. Types are stored in <ulink type="http"
|
|
|
|
url="../../gstreamer/html/gstreamer-GstStructure.html"><classname>GstStructure
|
|
|
|
</classname></ulink> internally. A <ulink
|
|
|
|
type="http" url="../../gstreamer/html/gstreamer-GstCaps.html"><classname>GstCaps
|
|
|
|
</classname></ulink> is nothing more than a small
|
2004-01-28 10:03:51 +00:00
|
|
|
wrapper for 0 or more structures/types. From the structure, you can also
|
|
|
|
retrieve properties, as is shown above with the function
|
|
|
|
<function>gst_structure_get_int ()</function>.
|
|
|
|
</para>
|
2004-01-27 13:33:39 +00:00
|
|
|
<para>
|
|
|
|
If your <function>_link ()</function> function does not need to perform
|
|
|
|
any specific operation (i.e. it will only forward caps), you can set it
|
2005-07-06 12:18:00 +00:00
|
|
|
to <function>gst_pad_proxy_link ()</function>. This is a link forwarding
|
2004-01-27 13:33:39 +00:00
|
|
|
function implementation provided by the core. It is useful for elements
|
|
|
|
such as <classname>identity</classname>.
|
|
|
|
</para>
|
2004-01-28 10:03:51 +00:00
|
|
|
</sect1>
|
2005-07-01 12:43:03 +00:00
|
|
|
<!-- example-begin pads.c --><!--
|
|
|
|
#include "init.func"
|
|
|
|
#include "caps.func"
|
2004-01-28 10:03:51 +00:00
|
|
|
|
2005-07-01 12:43:03 +00:00
|
|
|
static gboolean
|
|
|
|
gst_my_filter_event (GstPad * pad, GstEvent * event)
|
2004-01-27 13:33:39 +00:00
|
|
|
{
|
2005-07-01 12:43:03 +00:00
|
|
|
return gst_pad_event_default (pad, event);
|
2004-01-27 13:33:39 +00:00
|
|
|
}
|
|
|
|
|
2005-07-01 12:43:03 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_my_filter_chain (GstPad * pad, GstBuffer * buf)
|
2004-01-27 13:33:39 +00:00
|
|
|
{
|
2005-07-01 12:43:03 +00:00
|
|
|
return gst_pad_push (GST_MY_FILTER (GST_OBJECT_PARENT (pad))->srcpad, buf);
|
2004-01-27 13:33:39 +00:00
|
|
|
}
|
2005-07-01 12:43:03 +00:00
|
|
|
|
2005-12-11 18:24:27 +00:00
|
|
|
static GstStateChangeReturn
|
|
|
|
gst_my_filter_change_state (GstElement * element, GstStateChange transition)
|
2005-07-01 12:43:03 +00:00
|
|
|
{
|
|
|
|
return GST_CALL_PARENT_WITH_DEFAULT (GST_ELEMENT_CLASS,
|
2005-12-11 18:24:27 +00:00
|
|
|
change_state, (element, transition), GST_STATE_CHANGE_SUCCESS);
|
2005-07-01 12:43:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "register.func"
|
|
|
|
--><!-- example-end pads.c -->
|
2002-09-27 18:34:33 +00:00
|
|
|
</chapter>
|
|
|
|
|