Sophie

Sophie

distrib > Mandriva > 10.0-com > i586 > by-pkgid > f0a9f2b9c81d34eadc43f527947c0b70 > files > 193

libgstreamer0.7-devel-0.7.4-2mdk.i586.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Request and Sometimes pads</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="GStreamer Plugin Writer's Guide"
HREF="index.html"><LINK
REL="UP"
TITLE="Advanced Filter Concepts"
HREF="part-advanced.html"><LINK
REL="PREVIOUS"
TITLE="List of Defined Types"
HREF="section-types-definitions.html"><LINK
REL="NEXT"
TITLE="Request pads"
HREF="section-reqpad-request.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
> Plugin Writer's Guide</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="section-types-definitions.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="section-reqpad-request.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="chapter"
><H1
><A
NAME="chapter-advanced-request"
></A
>Chapter 14. Request and Sometimes pads</H1
><P
>&#13;    Until now, we've only dealt with pads that are always available. However,
    there's also pads that are only being created in some cases, or only if
    the application requests the pad. The first is called a
    <SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>sometimes</I
></SPAN
>; the second is called a
    <SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>request</I
></SPAN
> pad. The availability of a pad (always,
    sometimes or request) can be seen in a pad's template. This chapted will
    discuss when each of the two is useful, how they are created and when
    they should be disposed.
  </P
><DIV
CLASS="sect1"
><H1
CLASS="sect1"
><A
NAME="section-reqpad-sometimes"
>14.1. Sometimes pads</A
></H1
><P
>&#13;      A <SPAN
CLASS="QUOTE"
>"sometimes"</SPAN
> pad is a pad that is created under certain
      conditions, but not in all cases. This mostly depends on stream content:
      demuxers will generally parse the stream header, decide what elementary
      (video, audio, subtitle, etc.) streams are embedded inside the system
      stream, and will then create a sometimes pad for each of those elementary
      streams. At its own choice, it can also create more than one instance of
      each of those per element instance. The only limitation is that each
      newly created pad should have a unique name. Sometimes pads are disposed
      when the stream data is disposed, too (i.e. when going from PAUSED to the
      READY state). You should <SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>not</I
></SPAN
> dispose the pad on EOS,
      because someone might re-activate the pipeline and seek back to before
      the end-of-stream point. The stream should still stay valid after EOS, at
      least until the stream data is disposed. In any case, the element is
      always the owner of such a pad.
    </P
><P
>&#13;      The example code below will parse a text file, where the first line is
      a number (n). The next lines all start with a number (0 to n-1), which
      is the number of the source pad over which the data should be sent.
    </P
><PRE
CLASS="programlisting"
>&#13;3
0: foo
1: bar
0: boo
2: bye
    </PRE
><P
>&#13;      The code to parse this file and create the dynamic <SPAN
CLASS="QUOTE"
>"sometimes"</SPAN
>
      pads, looks like this:
    </P
><PRE
CLASS="programlisting"
>&#13;
typedef struct _GstMyFilter {
[..]
  gboolean firstrun;
  GList *srcpadlist;
} GstMyFilter;

static void
gst_my_filter_base_init (GstMyFilterClass *klass)
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
  static GstStaticPadTemplate src_factory =
  GST_STATIC_PAD_TEMPLATE (
    "src_%02d",
    GST_PAD_SRC,
    GST_PAD_SOMETIMES,
    GST_STATIC_CAPS ("ANY")
  );
[..]
  gst_element_class_add_pad_template (element_class,
	gst_static_pad_template_get (&#38;src_factory));
[..]
}

static void
gst_my_filter_init (GstMyFilter *filter)
{
[..]
  filter-&#62;firstrun = TRUE;
  filter-&#62;srcpadlist = NULL;
}

/*
 * Get one line of data - without newline.
 */

static GstBuffer *
gst_my_filter_getline (GstMyFilter *filter)
{
  guint8 *data;
  gint n, num;

  /* max. line length is 512 characters - for safety */
  for (n = 0; n &#60; 512; n++) {
    num = gst_bytestream_peek_bytes (filter-&#62;bs, &#38;data, n + 1);
    if (num != n + 1)
      return NULL;

    /* newline? */
    if (data[n] == '\n') {
      GstBuffer *buf = gst_buffer_new_and_alloc (n + 1);

      gst_bytestream_peek_bytes (filter-&#62;bs, &#38;data, n);
      memcpy (GST_BUFFER_DATA (buf), data, n);
      GST_BUFFER_DATA (buf)[n] = '\0';
      gst_bytestream_flush_fast (filter-&#62;bs, n + 1);

      return buf;
    }
  }
}

static void
gst_my_filter_loopfunc (GstElement *element)
{
  GstMyFilter *filter = GST_MY_FILTER (element);
  GstBuffer *buf;
  GstPad *pad;
  gint num, n;

  /* parse header */
  if (filter-&#62;firstrun) {
    GstElementClass *klass;
    GstPadTemplate *templ;
    gchar *padname;

    if (!(buf = gst_my_filter_getline (filter))) {
      gst_element_error (element, STREAM, READ, (NULL),
			 ("Stream contains no header"));
      return;
    }
    num = atoi (GST_BUFFER_DATA (buf));
    gst_buffer_unref (buf);

    /* for each of the streams, create a pad */
    klass = GST_ELEMENT_GET_CLASS (filter);
    templ = gst_element_class_get_pad_template (klass, "src_%02d");
    for (n = 0; n &#60; num; n++) {
      padname = g_strdup_printf ("src_%02d", n);
      pad = gst_pad_new_from_template (templ, padname);
      g_free (padname);

      /* here, you would set _getcaps () and _link () functions */

      gst_element_add_pad (element, pad);
      filter-&#62;srcpadlist = g_list_append (filter-&#62;srcpadlist, pad);
    }
  }

  /* and now, simply parse each line and push over */
  if (!(buf = gst_my_filter_getline (filter))) {
    GstEvent *event = gst_event_new (GST_EVENT_EOS);
    GList *padlist;

    for (padlist = srcpadlist;
         padlist != NULL; padlist = g_list_next (padlist)) {
      pad = GST_PAD (padlist-&#62;data);
      gst_event_ref (event);
      gst_pad_push (pad, GST_DATA (event));
    }
    gst_event_unref (event);
    gst_element_set_eos (element);

    return;
  }

  /* parse stream number and go beyond the ':' in the data */
  num = atoi (GST_BUFFER_DATA (buf));
  if (num &#62;= 0 &#38;&#38; num &#60; g_list_length (filter-&#62;srcpadlist)) {
    pad = GST_PAD (g_list_nth_data (filter-&#62;srcpadlist, num);

    /* magic buffer parsing foo */
    for (n = 0; GST_BUFFER_DATA (buf)[n] != ':' &#38;&#38;
                GST_BUFFER_DATA (buf)[n] != '\0'; n++) ;
    if (GST_BUFFER_DATA (buf)[n] != '\0') {
      GstBuffer *sub;

      /* create subbuffer that starts right past the space. The reason
       * that we don't just forward the data pointer is because the
       * pointer is no longer the start of an allocated block of memory,
       * but just a pointer to a position somewhere in the middle of it.
       * That cannot be freed upon disposal, so we'd either crash or have
       * a memleak. Creating a subbuffer is a simple way to solve that. */
      sub = gst_buffer_create_sub (buf, n + 1, GST_BUFFER_SIZE (buf) - n - 1);
      gst_pad_push (pad, GST_DATA (sub));
    }
  }
  gst_buffer_unref (buf);
}

    </PRE
><P
>&#13;      Note that we use a lot of checks everywhere to make sure that the content
      in the file is valid. This has two purposes: first, the file could be
      erronous, in which case we prevent a crash. The second and most important
      reason is that - in extreme cases - the file could be used maliciously to
      cause undefined behaviour in the plugin, which might lead to security
      issues. <SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>Always</I
></SPAN
> assume that the file could be used to
      do bad things.
    </P
></DIV
></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-types-definitions.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="section-reqpad-request.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>List of Defined Types</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"
>Request pads</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>