rtp: Update codes based on 1.18.4
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpspeexpay.c
index a551fa9..6f086b0 100644 (file)
@@ -1,5 +1,5 @@
 /* GStreamer
- * Copyright (C) <2005> Edgard Lima <edgard.lima@indt.org.br>
+ * Copyright (C) <2005> Edgard Lima <edgard.lima@gmail.com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
@@ -13,8 +13,8 @@
  *
  * 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.
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
  */
 
 #ifdef HAVE_CONFIG_H
 #include <stdlib.h>
 #include <string.h>
 #include <gst/rtp/gstrtpbuffer.h>
+#include <gst/audio/audio.h>
 
 #include "gstrtpspeexpay.h"
+#include "gstrtputils.h"
 
 GST_DEBUG_CATEGORY_STATIC (rtpspeexpay_debug);
 #define GST_CAT_DEFAULT (rtpspeexpay_debug)
@@ -53,60 +55,53 @@ GST_STATIC_PAD_TEMPLATE ("src",
 static GstStateChangeReturn gst_rtp_speex_pay_change_state (GstElement *
     element, GstStateChange transition);
 
-static gboolean gst_rtp_speex_pay_setcaps (GstBaseRTPPayload * payload,
+static gboolean gst_rtp_speex_pay_setcaps (GstRTPBasePayload * payload,
     GstCaps * caps);
-static GstCaps *gst_rtp_speex_pay_getcaps (GstBaseRTPPayload * payload,
-    GstPad * pad);
-static GstFlowReturn gst_rtp_speex_pay_handle_buffer (GstBaseRTPPayload *
+static GstCaps *gst_rtp_speex_pay_getcaps (GstRTPBasePayload * payload,
+    GstPad * pad, GstCaps * filter);
+static GstFlowReturn gst_rtp_speex_pay_handle_buffer (GstRTPBasePayload *
     payload, GstBuffer * buffer);
 
-GST_BOILERPLATE (GstRtpSPEEXPay, gst_rtp_speex_pay, GstBaseRTPPayload,
-    GST_TYPE_BASE_RTP_PAYLOAD);
-
-static void
-gst_rtp_speex_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_speex_pay_sink_template));
-  gst_element_class_add_pad_template (element_class,
-      gst_static_pad_template_get (&gst_rtp_speex_pay_src_template));
-  gst_element_class_set_details_simple (element_class, "RTP Speex payloader",
-      "Codec/Payloader/Network/RTP",
-      "Payload-encodes Speex audio into a RTP packet",
-      "Edgard Lima <edgard.lima@indt.org.br>");
-
-  GST_DEBUG_CATEGORY_INIT (rtpspeexpay_debug, "rtpspeexpay", 0,
-      "Speex RTP Payloader");
-}
+#define gst_rtp_speex_pay_parent_class parent_class
+G_DEFINE_TYPE (GstRtpSPEEXPay, gst_rtp_speex_pay, GST_TYPE_RTP_BASE_PAYLOAD);
 
 static void
 gst_rtp_speex_pay_class_init (GstRtpSPEEXPayClass * klass)
 {
   GstElementClass *gstelement_class;
-  GstBaseRTPPayloadClass *gstbasertppayload_class;
+  GstRTPBasePayloadClass *gstrtpbasepayload_class;
 
   gstelement_class = (GstElementClass *) klass;
-  gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
+  gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
 
   gstelement_class->change_state = gst_rtp_speex_pay_change_state;
 
-  gstbasertppayload_class->set_caps = gst_rtp_speex_pay_setcaps;
-  gstbasertppayload_class->get_caps = gst_rtp_speex_pay_getcaps;
-  gstbasertppayload_class->handle_buffer = gst_rtp_speex_pay_handle_buffer;
+  gstrtpbasepayload_class->set_caps = gst_rtp_speex_pay_setcaps;
+  gstrtpbasepayload_class->get_caps = gst_rtp_speex_pay_getcaps;
+  gstrtpbasepayload_class->handle_buffer = gst_rtp_speex_pay_handle_buffer;
+
+  gst_element_class_add_static_pad_template (gstelement_class,
+      &gst_rtp_speex_pay_sink_template);
+  gst_element_class_add_static_pad_template (gstelement_class,
+      &gst_rtp_speex_pay_src_template);
+  gst_element_class_set_static_metadata (gstelement_class,
+      "RTP Speex payloader", "Codec/Payloader/Network/RTP",
+      "Payload-encodes Speex audio into a RTP packet",
+      "Edgard Lima <edgard.lima@gmail.com>");
+
+  GST_DEBUG_CATEGORY_INIT (rtpspeexpay_debug, "rtpspeexpay", 0,
+      "Speex RTP Payloader");
 }
 
 static void
-gst_rtp_speex_pay_init (GstRtpSPEEXPay * rtpspeexpay,
-    GstRtpSPEEXPayClass * klass)
+gst_rtp_speex_pay_init (GstRtpSPEEXPay * rtpspeexpay)
 {
-  GST_BASE_RTP_PAYLOAD (rtpspeexpay)->clock_rate = 8000;
-  GST_BASE_RTP_PAYLOAD_PT (rtpspeexpay) = 110;  /* Create String */
+  GST_RTP_BASE_PAYLOAD (rtpspeexpay)->clock_rate = 8000;
+  GST_RTP_BASE_PAYLOAD_PT (rtpspeexpay) = 110;  /* Create String */
 }
 
 static gboolean
-gst_rtp_speex_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
+gst_rtp_speex_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
 {
   /* don't configure yet, we wait for the ident packet */
   return TRUE;
@@ -114,20 +109,25 @@ gst_rtp_speex_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
 
 
 static GstCaps *
-gst_rtp_speex_pay_getcaps (GstBaseRTPPayload * payload, GstPad * pad)
+gst_rtp_speex_pay_getcaps (GstRTPBasePayload * payload, GstPad * pad,
+    GstCaps * filter)
 {
   GstCaps *otherpadcaps;
   GstCaps *caps;
 
   otherpadcaps = gst_pad_get_allowed_caps (payload->srcpad);
-  caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
+  caps = gst_pad_get_pad_template_caps (pad);
 
   if (otherpadcaps) {
     if (!gst_caps_is_empty (otherpadcaps)) {
-      GstStructure *ps = gst_caps_get_structure (otherpadcaps, 0);
-      GstStructure *s = gst_caps_get_structure (caps, 0);
+      GstStructure *ps;
+      GstStructure *s;
       gint clock_rate;
 
+      ps = gst_caps_get_structure (otherpadcaps, 0);
+      caps = gst_caps_make_writable (caps);
+      s = gst_caps_get_structure (caps, 0);
+
       if (gst_structure_get_int (ps, "clock-rate", &clock_rate)) {
         gst_structure_fixate_field_nearest_int (s, "rate", clock_rate);
       }
@@ -135,6 +135,13 @@ gst_rtp_speex_pay_getcaps (GstBaseRTPPayload * payload, GstPad * pad)
     gst_caps_unref (otherpadcaps);
   }
 
+  if (filter) {
+    GstCaps *tcaps = caps;
+
+    caps = gst_caps_intersect_full (filter, tcaps, GST_CAPS_INTERSECT_FIRST);
+    gst_caps_unref (tcaps);
+  }
+
   return caps;
 }
 
@@ -143,7 +150,7 @@ gst_rtp_speex_pay_parse_ident (GstRtpSPEEXPay * rtpspeexpay,
     const guint8 * data, guint size)
 {
   guint32 version, header_size, rate, mode, nb_channels;
-  GstBaseRTPPayload *payload;
+  GstRTPBasePayload *payload;
   gchar *cstr;
   gboolean res;
 
@@ -181,11 +188,11 @@ gst_rtp_speex_pay_parse_ident (GstRtpSPEEXPay * rtpspeexpay,
   GST_DEBUG_OBJECT (rtpspeexpay, "rate %d, mode %d, nb_channels %d",
       rate, mode, nb_channels);
 
-  payload = GST_BASE_RTP_PAYLOAD (rtpspeexpay);
+  payload = GST_RTP_BASE_PAYLOAD (rtpspeexpay);
 
-  gst_basertppayload_set_options (payload, "audio", FALSE, "SPEEX", rate);
+  gst_rtp_base_payload_set_options (payload, "audio", FALSE, "SPEEX", rate);
   cstr = g_strdup_printf ("%d", nb_channels);
-  res = gst_basertppayload_set_outcaps (payload, "encoding-params",
+  res = gst_rtp_base_payload_set_outcaps (payload, "encoding-params",
       G_TYPE_STRING, cstr, NULL);
   g_free (cstr);
 
@@ -227,63 +234,70 @@ payload_too_small:
 }
 
 static GstFlowReturn
-gst_rtp_speex_pay_handle_buffer (GstBaseRTPPayload * basepayload,
+gst_rtp_speex_pay_handle_buffer (GstRTPBasePayload * basepayload,
     GstBuffer * buffer)
 {
   GstRtpSPEEXPay *rtpspeexpay;
-  guint size, payload_len;
+  GstMapInfo map;
   GstBuffer *outbuf;
-  guint8 *payload, *data;
   GstClockTime timestamp, duration;
   GstFlowReturn ret;
 
   rtpspeexpay = GST_RTP_SPEEX_PAY (basepayload);
 
-  size = GST_BUFFER_SIZE (buffer);
-  data = GST_BUFFER_DATA (buffer);
+  gst_buffer_map (buffer, &map, GST_MAP_READ);
 
   switch (rtpspeexpay->packet) {
     case 0:
       /* ident packet. We need to parse the headers to construct the RTP
        * properties. */
-      if (!gst_rtp_speex_pay_parse_ident (rtpspeexpay, data, size))
+      if (!gst_rtp_speex_pay_parse_ident (rtpspeexpay, map.data, map.size)) {
+        gst_buffer_unmap (buffer, &map);
         goto parse_error;
+      }
 
       ret = GST_FLOW_OK;
+      gst_buffer_unmap (buffer, &map);
       goto done;
     case 1:
       /* comment packet, we ignore it */
       ret = GST_FLOW_OK;
+      gst_buffer_unmap (buffer, &map);
       goto done;
     default:
       /* other packets go in the payload */
       break;
   }
+  gst_buffer_unmap (buffer, &map);
+
+  if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_GAP)) {
+    ret = GST_FLOW_OK;
+    goto done;
+  }
 
-  timestamp = GST_BUFFER_TIMESTAMP (buffer);
+  timestamp = GST_BUFFER_PTS (buffer);
   duration = GST_BUFFER_DURATION (buffer);
 
   /* FIXME, only one SPEEX frame per RTP packet for now */
-  payload_len = size;
+  outbuf = gst_rtp_base_payload_allocate_output_buffer (basepayload, 0, 0, 0);
 
-  outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
   /* FIXME, assert for now */
-  g_assert (payload_len <= GST_BASE_RTP_PAYLOAD_MTU (rtpspeexpay));
+  g_assert (gst_buffer_get_size (buffer) <=
+      GST_RTP_BASE_PAYLOAD_MTU (rtpspeexpay));
 
   /* copy timestamp and duration */
-  GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
+  GST_BUFFER_PTS (outbuf) = timestamp;
   GST_BUFFER_DURATION (outbuf) = duration;
 
-  /* get payload */
-  payload = gst_rtp_buffer_get_payload (outbuf);
+  gst_rtp_copy_audio_meta (basepayload, outbuf, buffer);
+  outbuf = gst_buffer_append (outbuf, buffer);
+  buffer = NULL;
 
-  /* copy data in payload */
-  memcpy (&payload[0], data, size);
-
-  ret = gst_basertppayload_push (basepayload, outbuf);
+  ret = gst_rtp_base_payload_push (basepayload, outbuf);
 
 done:
-  gst_buffer_unref (buffer);
+  if (buffer)
+    gst_buffer_unref (buffer);
 
   rtpspeexpay->packet++;