gstpay/depay: add generic gstreamer payloader
authorWim Taymans <wim.taymans@collabora.co.uk>
Sun, 12 Dec 2010 04:10:01 +0000 (05:10 +0100)
committerWim Taymans <wim.taymans@collabora.co.uk>
Thu, 23 Dec 2010 17:39:52 +0000 (18:39 +0100)
Add the beginnings of a generic GStreamer buffers payloader.

gst/rtp/Makefile.am
gst/rtp/gstrtp.c
gst/rtp/gstrtpgstdepay.c [new file with mode: 0644]
gst/rtp/gstrtpgstdepay.h [new file with mode: 0644]
gst/rtp/gstrtpgstpay.c [new file with mode: 0644]
gst/rtp/gstrtpgstpay.h [new file with mode: 0644]

index 9d43108..82ccd5f 100644 (file)
@@ -13,6 +13,8 @@ libgstrtp_la_SOURCES = \
        gstrtpceltpay.c \
        gstrtpdvdepay.c \
        gstrtpdvpay.c \
+       gstrtpgstdepay.c \
+       gstrtpgstpay.c \
        gstrtpilbcdepay.c \
        gstrtpilbcpay.c \
        gstrtpmpadepay.c \
@@ -104,6 +106,8 @@ noinst_HEADERS =                    \
                 gstrtpdvpay.h          \
                 gstrtpamrdepay.h       \
                 gstrtpamrpay.h         \
+                gstrtpgstdepay.h       \
+                gstrtpgstpay.h         \
                 gstrtpilbcdepay.h      \
                 gstrtpilbcpay.h        \
                 gstrtppcmadepay.h      \
index e2fa2ed..721cee3 100644 (file)
@@ -30,6 +30,8 @@
 #include "gstrtpceltpay.h"
 #include "gstrtpdvdepay.h"
 #include "gstrtpdvpay.h"
+#include "gstrtpgstdepay.h"
+#include "gstrtpgstpay.h"
 #include "gstrtpilbcdepay.h"
 #include "gstrtpilbcpay.h"
 #include "gstrtppcmupay.h"
@@ -119,6 +121,12 @@ plugin_init (GstPlugin * plugin)
   if (!gst_rtp_dv_pay_plugin_init (plugin))
     return FALSE;
 
+  if (!gst_rtp_gst_depay_plugin_init (plugin))
+    return FALSE;
+
+  if (!gst_rtp_gst_pay_plugin_init (plugin))
+    return FALSE;
+
   if (!gst_rtp_ilbc_pay_plugin_init (plugin))
     return FALSE;
 
diff --git a/gst/rtp/gstrtpgstdepay.c b/gst/rtp/gstrtpgstdepay.c
new file mode 100644 (file)
index 0000000..633971d
--- /dev/null
@@ -0,0 +1,302 @@
+/* GStreamer
+ * Copyright (C) <2010> Wim Taymans <wim.taymans@gmail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#endif
+
+#include <gst/rtp/gstrtpbuffer.h>
+
+#include <string.h>
+#include "gstrtpgstdepay.h"
+
+GST_DEBUG_CATEGORY_STATIC (rtpgstdepay_debug);
+#define GST_CAT_DEFAULT (rtpgstdepay_debug)
+
+static GstStaticPadTemplate gst_rtp_gst_depay_src_template =
+GST_STATIC_PAD_TEMPLATE ("src",
+    GST_PAD_SRC,
+    GST_PAD_ALWAYS,
+    GST_STATIC_CAPS_ANY);
+
+static GstStaticPadTemplate gst_rtp_gst_depay_sink_template =
+GST_STATIC_PAD_TEMPLATE ("sink",
+    GST_PAD_SINK,
+    GST_PAD_ALWAYS,
+    GST_STATIC_CAPS ("application/x-rtp, "
+        "media = (string) \"application\", "
+        "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
+        "clock-rate = (int) 90000, " "encoding-name = (string) \"X-GST\"")
+    );
+
+GST_BOILERPLATE (GstRtpGSTDepay, gst_rtp_gst_depay, GstBaseRTPDepayload,
+    GST_TYPE_BASE_RTP_DEPAYLOAD);
+
+static void gst_rtp_gst_depay_finalize (GObject * object);
+
+static gboolean gst_rtp_gst_depay_setcaps (GstBaseRTPDepayload * depayload,
+    GstCaps * caps);
+static GstBuffer *gst_rtp_gst_depay_process (GstBaseRTPDepayload * depayload,
+    GstBuffer * buf);
+
+static void
+gst_rtp_gst_depay_base_init (gpointer klass)
+{
+  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
+
+  gst_element_class_add_pad_template (element_class,
+      gst_static_pad_template_get (&gst_rtp_gst_depay_src_template));
+  gst_element_class_add_pad_template (element_class,
+      gst_static_pad_template_get (&gst_rtp_gst_depay_sink_template));
+
+  gst_element_class_set_details_simple (element_class,
+      "GStreamer depayloader", "Codec/Depayloader/Network",
+      "Extracts GStreamer buffers from RTP packets",
+      "Wim Taymans <wim.taymans@gmail.com>");
+}
+
+static void
+gst_rtp_gst_depay_class_init (GstRtpGSTDepayClass * klass)
+{
+  GObjectClass *gobject_class;
+  GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
+
+  gobject_class = (GObjectClass *) klass;
+  gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
+
+  gobject_class->finalize = gst_rtp_gst_depay_finalize;
+
+  gstbasertpdepayload_class->set_caps = gst_rtp_gst_depay_setcaps;
+  gstbasertpdepayload_class->process = gst_rtp_gst_depay_process;
+
+  GST_DEBUG_CATEGORY_INIT (rtpgstdepay_debug, "rtpgstdepay", 0,
+      "Gstreamer RTP Depayloader");
+}
+
+static void
+gst_rtp_gst_depay_init (GstRtpGSTDepay * rtpgstdepay,
+    GstRtpGSTDepayClass * klass)
+{
+  rtpgstdepay->adapter = gst_adapter_new ();
+}
+
+static void
+gst_rtp_gst_depay_finalize (GObject * object)
+{
+  GstRtpGSTDepay *rtpgstdepay;
+
+  rtpgstdepay = GST_RTP_GST_DEPAY (object);
+
+  g_object_unref (rtpgstdepay->adapter);
+
+  G_OBJECT_CLASS (parent_class)->finalize (object);
+}
+
+static gboolean
+gst_rtp_gst_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
+{
+  GstRtpGSTDepay *rtpgstdepay;
+  GstStructure *structure;
+  GstCaps *outcaps;
+  gint clock_rate;
+  gboolean res;
+  const gchar *capsenc;
+  gchar *capsstr;
+
+  rtpgstdepay = GST_RTP_GST_DEPAY (depayload);
+
+  structure = gst_caps_get_structure (caps, 0);
+
+  if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
+    clock_rate = 90000;
+  depayload->clock_rate = clock_rate;
+
+  capsenc = gst_structure_get_string (structure, "caps");
+  if (capsenc) {
+    gsize out_len;
+
+    capsstr = (gchar *) g_base64_decode (capsenc, &out_len);
+    outcaps = gst_caps_from_string (capsstr);
+    g_free (capsstr);
+
+    /* we have the SDP caps as output caps */
+    rtpgstdepay->current_CV = 0;
+  } else {
+    outcaps = gst_caps_new_any ();
+  }
+  res = gst_pad_set_caps (depayload->srcpad, outcaps);
+  gst_caps_unref (outcaps);
+
+  return res;
+}
+
+static GstBuffer *
+gst_rtp_gst_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
+{
+  GstRtpGSTDepay *rtpgstdepay;
+  GstBuffer *subbuf, *outbuf = NULL;
+  gint payload_len;
+  guint8 *payload;
+  guint16 frag_offset;
+  guint CV;
+
+  rtpgstdepay = GST_RTP_GST_DEPAY (depayload);
+
+  payload_len = gst_rtp_buffer_get_payload_len (buf);
+
+  if (payload_len <= 8)
+    goto empty_packet;
+
+  if (GST_BUFFER_IS_DISCONT (buf)) {
+    GST_WARNING_OBJECT (rtpgstdepay, "DISCONT, clear adapter");
+    gst_adapter_clear (rtpgstdepay->adapter);
+  }
+
+  payload = gst_rtp_buffer_get_payload (buf);
+
+  /* strip off header
+   *
+   *  0                   1                   2                   3
+   *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+   * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+   * |C| CV  |D|X|Y|Z|                  MBZ                          |
+   * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+   * |                          Frag_offset                          |
+   * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+   */
+  frag_offset =
+      (payload[4] << 24) | (payload[5] << 16) | (payload[6] << 8) | payload[7];
+
+  /* subbuffer skipping the 8 header bytes */
+  subbuf = gst_rtp_buffer_get_payload_subbuffer (buf, 8, -1);
+  gst_adapter_push (rtpgstdepay->adapter, subbuf);
+
+  if (gst_rtp_buffer_get_marker (buf)) {
+    guint avail;
+    GstCaps *outcaps;
+
+    /* take the buffer */
+    avail = gst_adapter_available (rtpgstdepay->adapter);
+    outbuf = gst_adapter_take_buffer (rtpgstdepay->adapter, avail);
+
+    CV = (payload[0] >> 4) & 0x7;
+
+    if (payload[0] & 0x80) {
+      guint b, csize, size, offset;
+      guint8 *data;
+      GstBuffer *subbuf;
+
+      /* C bit, we have inline caps */
+      data = GST_BUFFER_DATA (outbuf);
+      size = GST_BUFFER_SIZE (outbuf);
+
+      /* start reading the length, we need this to skip to the data later */
+      csize = offset = 0;
+      do {
+        if (offset >= size)
+          goto too_small;
+        b = data[offset++];
+        csize = (csize << 7) | (b & 0x7f);
+      } while (b & 0x80);
+
+      if (size < csize)
+        goto too_small;
+
+      /* parse and store in cache */
+      outcaps = gst_caps_from_string ((gchar *) & data[offset]);
+      if (rtpgstdepay->CV_cache[CV])
+        gst_caps_unref (rtpgstdepay->CV_cache[CV]);
+      rtpgstdepay->CV_cache[CV] = outcaps;
+
+      /* skip caps */
+      offset += csize;
+      size -= csize;
+
+      GST_DEBUG_OBJECT (rtpgstdepay,
+          "inline caps %u, length %u, %" GST_PTR_FORMAT, CV, csize, outcaps);
+
+      /* create real data buffer when needed */
+      if (size)
+        subbuf = gst_buffer_create_sub (outbuf, offset, size);
+      else
+        subbuf = NULL;
+
+      gst_buffer_unref (outbuf);
+      outbuf = subbuf;
+    }
+
+    /* see what caps we need */
+    if (CV != rtpgstdepay->current_CV) {
+      /* we need to switch caps, check if we have the caps */
+      if ((outcaps = rtpgstdepay->CV_cache[CV]) == NULL)
+        goto missing_caps;
+
+      GST_DEBUG_OBJECT (rtpgstdepay,
+          "need caps switch from %u to %u, %" GST_PTR_FORMAT,
+          rtpgstdepay->current_CV, CV, outcaps);
+
+      /* and set caps */
+      if (gst_pad_set_caps (depayload->srcpad, outcaps))
+        rtpgstdepay->current_CV = CV;
+    }
+
+    if (outbuf) {
+      if (payload[0] & 0x8)
+        GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
+      if (payload[0] & 0x4)
+        GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_MEDIA1);
+      if (payload[0] & 0x2)
+        GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_MEDIA2);
+      if (payload[0] & 0x1)
+        GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_MEDIA3);
+    }
+  }
+  return outbuf;
+
+  /* ERRORS */
+empty_packet:
+  {
+    GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
+        ("Empty Payload."), (NULL));
+    return NULL;
+  }
+too_small:
+  {
+    GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
+        ("Buffer too small."), (NULL));
+    if (outbuf)
+      gst_buffer_unref (outbuf);
+    return NULL;
+  }
+missing_caps:
+  {
+    GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
+        ("Missing caps %u.", CV), (NULL));
+    if (outbuf)
+      gst_buffer_unref (outbuf);
+    return NULL;
+  }
+}
+
+gboolean
+gst_rtp_gst_depay_plugin_init (GstPlugin * plugin)
+{
+  return gst_element_register (plugin, "rtpgstdepay",
+      GST_RANK_MARGINAL, GST_TYPE_RTP_GST_DEPAY);
+}
diff --git a/gst/rtp/gstrtpgstdepay.h b/gst/rtp/gstrtpgstdepay.h
new file mode 100644 (file)
index 0000000..7a50856
--- /dev/null
@@ -0,0 +1,63 @@
+/* GStreamer
+ * Copyright (C) <2010> Wim Taymans <wim.taymans@gmail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __GST_RTP_GST_DEPAY_H__
+#define __GST_RTP_GST_DEPAY_H__
+
+#include <gst/gst.h>
+#include <gst/base/gstadapter.h>
+#include <gst/rtp/gstbasertpdepayload.h>
+
+G_BEGIN_DECLS
+
+#define GST_TYPE_RTP_GST_DEPAY \
+  (gst_rtp_gst_depay_get_type())
+#define GST_RTP_GST_DEPAY(obj) \
+  (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_RTP_GST_DEPAY,GstRtpGSTDepay))
+#define GST_RTP_GST_DEPAY_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_RTP_GST_DEPAY,GstRtpGSTDepayClass))
+#define GST_IS_RTP_GST_DEPAY(obj) \
+  (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_RTP_GST_DEPAY))
+#define GST_IS_RTP_GST_DEPAY_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_RTP_GST_DEPAY))
+
+typedef struct _GstRtpGSTDepay GstRtpGSTDepay;
+typedef struct _GstRtpGSTDepayClass GstRtpGSTDepayClass;
+
+struct _GstRtpGSTDepay
+{
+  GstBaseRTPDepayload depayload;
+
+  GstAdapter *adapter;
+  guint current_CV;
+  GstCaps *CV_cache[8];
+};
+
+struct _GstRtpGSTDepayClass
+{
+  GstBaseRTPDepayloadClass parent_class;
+};
+
+GType gst_rtp_gst_depay_get_type (void);
+
+gboolean gst_rtp_gst_depay_plugin_init (GstPlugin * plugin);
+
+G_END_DECLS
+
+#endif /* __GST_RTP_GST_DEPAY_H__ */
diff --git a/gst/rtp/gstrtpgstpay.c b/gst/rtp/gstrtpgstpay.c
new file mode 100644 (file)
index 0000000..5fa315f
--- /dev/null
@@ -0,0 +1,239 @@
+/* GStreamer
+ * Copyright (C) <2010> Wim Taymans <wim.taymans@gmail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#endif
+
+#include <string.h>
+
+#include <gst/rtp/gstrtpbuffer.h>
+
+#include "gstrtpgstpay.h"
+
+/*
+ *  0                   1                   2                   3
+ *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * |C| CV  |D|X|Y|Z|                  MBZ                          |
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * |                          Frag_offset                          |
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ *
+ * C: caps inlined flag 
+ *   When C set, first part of payload contains caps definition. Caps definition
+ *   starts with variable length length prefix and then a string of that length.
+ *   the length is encoded in big endian 7 bit chunks, the top 1 bit of a byte
+ *   is the continuation marker and the 7 next bits the data. A continuation
+ *   marker of 1 means that the next byte contains more data. 
+ *
+ * CV: caps version, 0 = caps from SDP, 1 - 7 inlined caps
+ * D: delta unit buffer
+ * X: media 1 flag
+ * Y: media 2 flag
+ * Z: media 3 flag
+ *
+ *
+ */
+
+static GstStaticPadTemplate gst_rtp_gst_pay_sink_template =
+GST_STATIC_PAD_TEMPLATE ("sink",
+    GST_PAD_SINK,
+    GST_PAD_ALWAYS,
+    GST_STATIC_CAPS_ANY);
+
+static GstStaticPadTemplate gst_rtp_gst_pay_src_template =
+GST_STATIC_PAD_TEMPLATE ("src",
+    GST_PAD_SRC,
+    GST_PAD_ALWAYS,
+    GST_STATIC_CAPS ("application/x-rtp, "
+        "media = (string) \"application\", "
+        "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
+        "clock-rate = (int) 90000, " "encoding-name = (string) \"X-GST\"")
+    );
+
+static void gst_rtp_gst_pay_finalize (GObject * object);
+
+static gboolean gst_rtp_gst_pay_setcaps (GstBaseRTPPayload * payload,
+    GstCaps * caps);
+static GstFlowReturn gst_rtp_gst_pay_handle_buffer (GstBaseRTPPayload * payload,
+    GstBuffer * buffer);
+
+GST_BOILERPLATE (GstRtpGSTPay, gst_rtp_gst_pay, GstBaseRTPPayload,
+    GST_TYPE_BASE_RTP_PAYLOAD)
+
+     static void gst_rtp_gst_pay_base_init (gpointer klass)
+{
+  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
+
+  gst_element_class_add_pad_template (element_class,
+      gst_static_pad_template_get (&gst_rtp_gst_pay_src_template));
+  gst_element_class_add_pad_template (element_class,
+      gst_static_pad_template_get (&gst_rtp_gst_pay_sink_template));
+
+  gst_element_class_set_details_simple (element_class,
+      "RTP GStreamer payloader", "Codec/Payloader/Network",
+      "Payload GStreamer buffers as RTP packets",
+      "Wim Taymans <wim.taymans@gmail.com>");
+}
+
+static void
+gst_rtp_gst_pay_class_init (GstRtpGSTPayClass * klass)
+{
+  GObjectClass *gobject_class;
+  GstBaseRTPPayloadClass *gstbasertppayload_class;
+
+  gobject_class = (GObjectClass *) klass;
+  gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
+
+  gobject_class->finalize = gst_rtp_gst_pay_finalize;
+
+  gstbasertppayload_class->set_caps = gst_rtp_gst_pay_setcaps;
+  gstbasertppayload_class->handle_buffer = gst_rtp_gst_pay_handle_buffer;
+}
+
+static void
+gst_rtp_gst_pay_init (GstRtpGSTPay * rtpgstpay, GstRtpGSTPayClass * klass)
+{
+}
+
+static void
+gst_rtp_gst_pay_finalize (GObject * object)
+{
+  GstRtpGSTPay *rtpgstpay;
+
+  rtpgstpay = GST_RTP_GST_PAY (object);
+
+  G_OBJECT_CLASS (parent_class)->finalize (object);
+}
+
+static gboolean
+gst_rtp_gst_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
+{
+  gboolean res;
+  gchar *capsstr, *capsenc;
+
+  capsstr = gst_caps_to_string (caps);
+  capsenc = g_base64_encode ((guchar *) capsstr, strlen (capsstr));
+  g_free (capsstr);
+
+  gst_basertppayload_set_options (payload, "application", TRUE, "X-GST", 90000);
+  res =
+      gst_basertppayload_set_outcaps (payload, "caps", G_TYPE_STRING, capsenc,
+      NULL);
+  g_free (capsenc);
+
+  return res;
+}
+
+static GstFlowReturn
+gst_rtp_gst_pay_handle_buffer (GstBaseRTPPayload * basepayload,
+    GstBuffer * buffer)
+{
+  GstRtpGSTPay *rtpgstpay;
+  guint8 *data;
+  guint size;
+  GstBuffer *outbuf;
+  GstFlowReturn ret;
+  GstClockTime timestamp;
+  guint16 frag_offset;
+  guint flags;
+
+  rtpgstpay = GST_RTP_GST_PAY (basepayload);
+
+  size = GST_BUFFER_SIZE (buffer);
+  data = GST_BUFFER_DATA (buffer);
+  timestamp = GST_BUFFER_TIMESTAMP (buffer);
+
+  ret = GST_FLOW_OK;
+
+  /* caps always from SDP for now */
+  flags = 0;
+  if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT))
+    flags |= (1 << 3);
+  if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_MEDIA1))
+    flags |= (1 << 2);
+  if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_MEDIA2))
+    flags |= (1 << 1);
+  if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_MEDIA3))
+    flags |= (1 << 0);
+
+  /*
+   *  0                   1                   2                   3
+   *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+   * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+   * |C| CV  |D|X|Y|Z|                  MBZ                          |
+   * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+   * |                          Frag_offset                          |
+   * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+   */
+  frag_offset = 0;
+
+  while (size > 0) {
+    guint towrite;
+    guint8 *payload;
+    guint payload_len;
+    guint packet_len;
+
+    /* this will be the total lenght of the packet */
+    packet_len = gst_rtp_buffer_calc_packet_len (8 + size, 0, 0);
+
+    /* fill one MTU or all available bytes */
+    towrite = MIN (packet_len, GST_BASE_RTP_PAYLOAD_MTU (rtpgstpay));
+
+    /* this is the payload length */
+    payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
+
+    /* create buffer to hold the payload */
+    outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
+    payload = gst_rtp_buffer_get_payload (outbuf);
+
+    payload[0] = flags;
+    payload[1] = payload[2] = payload[3] = 0;
+    payload[4] = frag_offset >> 24;
+    payload[5] = frag_offset >> 16;
+    payload[6] = frag_offset >> 8;
+    payload[7] = frag_offset & 0xff;
+
+    payload += 8;
+    payload_len -= 8;
+
+    memcpy (payload, data, payload_len);
+
+    data += payload_len;
+    size -= payload_len;
+    frag_offset += payload_len;
+
+    if (size == 0)
+      gst_rtp_buffer_set_marker (outbuf, TRUE);
+
+    GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
+
+    ret = gst_basertppayload_push (basepayload, outbuf);
+  }
+
+  return ret;
+}
+
+gboolean
+gst_rtp_gst_pay_plugin_init (GstPlugin * plugin)
+{
+  return gst_element_register (plugin, "rtpgstpay",
+      GST_RANK_NONE, GST_TYPE_RTP_GST_PAY);
+}
diff --git a/gst/rtp/gstrtpgstpay.h b/gst/rtp/gstrtpgstpay.h
new file mode 100644 (file)
index 0000000..c7a7c02
--- /dev/null
@@ -0,0 +1,58 @@
+/* GStreamer
+ * Copyright (C) <2010> Wim Taymans <wim.taymans@gmail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __GST_RTP_GST_PAY_H__
+#define __GST_RTP_GST_PAY_H__
+
+#include <gst/gst.h>
+#include <gst/rtp/gstbasertppayload.h>
+
+G_BEGIN_DECLS
+
+#define GST_TYPE_RTP_GST_PAY \
+  (gst_rtp_gst_pay_get_type())
+#define GST_RTP_GST_PAY(obj) \
+  (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_RTP_GST_PAY,GstRtpGSTPay))
+#define GST_RTP_GST_PAY_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_RTP_GST_PAY,GstRtpGSTPayClass))
+#define GST_IS_RTP_GST_PAY(obj) \
+  (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_RTP_GST_PAY))
+#define GST_IS_RTP_GST_PAY_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_RTP_GST_PAY))
+
+typedef struct _GstRtpGSTPay GstRtpGSTPay;
+typedef struct _GstRtpGSTPayClass GstRtpGSTPayClass;
+
+struct _GstRtpGSTPay
+{
+  GstBaseRTPPayload payload;
+};
+
+struct _GstRtpGSTPayClass
+{
+  GstBaseRTPPayloadClass parent_class;
+};
+
+GType gst_rtp_gst_pay_get_type (void);
+
+gboolean gst_rtp_gst_pay_plugin_init (GstPlugin * plugin);
+
+G_END_DECLS
+
+#endif /* __GST_RTP_GST_PAY_H__ */