gst/rtp/: Fix speex (de)payloader. Fixes #358040.
authorWim Taymans <wim.taymans@gmail.com>
Mon, 5 Mar 2007 16:39:29 +0000 (16:39 +0000)
committerWim Taymans <wim.taymans@gmail.com>
Mon, 5 Mar 2007 16:39:29 +0000 (16:39 +0000)
Original commit message from CVS:
* gst/rtp/gstrtpspeexdepay.c: (gst_rtp_speex_depay_class_init),
(gst_rtp_speex_depay_get_mode), (gst_rtp_speex_depay_setcaps),
(gst_rtp_speex_depay_process):
* gst/rtp/gstrtpspeexpay.c: (gst_rtp_speex_pay_base_init),
(gst_rtp_speex_pay_class_init), (gst_rtp_speex_pay_setcaps),
(gst_rtp_speex_pay_parse_ident), (gst_rtp_speex_pay_handle_buffer),
(gst_rtp_speex_pay_change_state):
* gst/rtp/gstrtpspeexpay.h:
Fix speex (de)payloader. Fixes #358040.

ChangeLog
gst/rtp/gstrtpspeexdepay.c
gst/rtp/gstrtpspeexpay.c
gst/rtp/gstrtpspeexpay.h

index 3e3eab8..2f65b29 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2007-03-05  Wim Taymans  <wim@fluendo.com>
+
+       * gst/rtp/gstrtpspeexdepay.c: (gst_rtp_speex_depay_class_init),
+       (gst_rtp_speex_depay_get_mode), (gst_rtp_speex_depay_setcaps),
+       (gst_rtp_speex_depay_process):
+       * gst/rtp/gstrtpspeexpay.c: (gst_rtp_speex_pay_base_init),
+       (gst_rtp_speex_pay_class_init), (gst_rtp_speex_pay_setcaps),
+       (gst_rtp_speex_pay_parse_ident), (gst_rtp_speex_pay_handle_buffer),
+       (gst_rtp_speex_pay_change_state):
+       * gst/rtp/gstrtpspeexpay.h:
+       Fix speex (de)payloader. Fixes #358040.
+
 2007-03-05  Jan Schmidt  <thaytan@mad.scientist.com>
 
        * ext/gconf/gstswitchsink.c: (gst_switch_sink_reset),
index e0d7729..833b029 100644 (file)
@@ -94,8 +94,6 @@ gst_rtp_speex_depay_class_init (GstRtpSPEEXDepayClass * klass)
   gstelement_class = (GstElementClass *) klass;
   gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
 
-  parent_class = g_type_class_peek_parent (klass);
-
   gstbasertpdepayload_class->process = gst_rtp_speex_depay_process;
   gstbasertpdepayload_class->set_caps = gst_rtp_speex_depay_setcaps;
 }
@@ -107,37 +105,104 @@ gst_rtp_speex_depay_init (GstRtpSPEEXDepay * rtpspeexdepay,
   GST_BASE_RTP_DEPAYLOAD (rtpspeexdepay)->clock_rate = 8000;
 }
 
-static gboolean
-gst_rtp_speex_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
+static gint
+gst_rtp_speex_depay_get_mode (gint rate)
 {
-  GstCaps *srccaps;
-  gboolean ret;
+  if (rate > 25000)
+    return 2;
+  else if (rate > 12500)
+    return 1;
+  else
+    return 0;
+}
 
-  srccaps =
-      gst_static_pad_template_get_caps (&gst_rtp_speex_depay_src_template);
-  ret = gst_pad_set_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload), srccaps);
+/* len 4 bytes LE,
+ * vendor string (len bytes),
+ * user_len 4 (0) bytes LE
+ */
+static const gchar gst_rtp_speex_comment[] =
+    "\045\0\0\0Depayloaded with GStreamer speexdepay\0\0\0\0";
 
-  gst_caps_unref (srccaps);
-  return ret;
+static gboolean
+gst_rtp_speex_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
+{
+  GstStructure *structure;
+  GstRtpSPEEXDepay *rtpspeexdepay;
+  gint clock_rate, nb_channels;
+  GstBuffer *buf;
+  guint8 *data;
+  const gchar *params;
+
+  rtpspeexdepay = GST_RTP_SPEEX_DEPAY (depayload);
+
+  structure = gst_caps_get_structure (caps, 0);
+
+  gst_structure_get_int (structure, "clock-rate", &clock_rate);
+  depayload->clock_rate = clock_rate;
+
+  if (!(params = gst_structure_get_string (structure, "encoding-params")))
+    nb_channels = 1;
+  else {
+    nb_channels = atoi (params);
+  }
+
+  /* construct minimal header and comment packet for the decoder */
+  buf = gst_buffer_new_and_alloc (80);
+  data = GST_BUFFER_DATA (buf);
+  memcpy (data, "Speex   ", 8);
+  data += 8;
+  memcpy (data, "1.1.12", 7);
+  data += 20;
+  GST_WRITE_UINT32_LE (data, 1);        /* version */
+  data += 4;
+  GST_WRITE_UINT32_LE (data, 80);       /* header_size */
+  data += 4;
+  GST_WRITE_UINT32_LE (data, clock_rate);       /* rate */
+  data += 4;
+  GST_WRITE_UINT32_LE (data, gst_rtp_speex_depay_get_mode (clock_rate));        /* mode */
+  data += 4;
+  GST_WRITE_UINT32_LE (data, 4);        /* mode_bitstream_version */
+  data += 4;
+  GST_WRITE_UINT32_LE (data, nb_channels);      /* nb_channels */
+  data += 4;
+  GST_WRITE_UINT32_LE (data, -1);       /* bitrate */
+  data += 4;
+  GST_WRITE_UINT32_LE (data, 0xa0);     /* frame_size */
+  data += 4;
+  GST_WRITE_UINT32_LE (data, 0);        /* VBR */
+  data += 4;
+  GST_WRITE_UINT32_LE (data, 1);        /* frames_per_packet */
+  data += 4;
+  GST_WRITE_UINT32_LE (data, 0);        /* extra_headers */
+  data += 4;
+  GST_WRITE_UINT32_LE (data, 0);        /* reserved1 */
+  data += 4;
+  GST_WRITE_UINT32_LE (data, 0);        /* reserved2 */
+
+  gst_base_rtp_depayload_push (GST_BASE_RTP_DEPAYLOAD (rtpspeexdepay), buf);
+
+  buf = gst_buffer_new_and_alloc (sizeof (gst_rtp_speex_comment));
+  memcpy (GST_BUFFER_DATA (buf), gst_rtp_speex_comment,
+      sizeof (gst_rtp_speex_comment));
+
+  gst_base_rtp_depayload_push (GST_BASE_RTP_DEPAYLOAD (rtpspeexdepay), buf);
+
+  return TRUE;
 }
 
 static GstBuffer *
 gst_rtp_speex_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
 {
   GstBuffer *outbuf = NULL;
-  gint payload_len;
-  guint8 *payload;
 
   GST_DEBUG ("process : got %d bytes, mark %d ts %u seqn %d",
       GST_BUFFER_SIZE (buf),
       gst_rtp_buffer_get_marker (buf),
       gst_rtp_buffer_get_timestamp (buf), gst_rtp_buffer_get_seq (buf));
 
-  payload_len = gst_rtp_buffer_get_payload_len (buf);
-  payload = gst_rtp_buffer_get_payload (buf);
+  /* nothing special to be done */
+  outbuf = gst_rtp_buffer_get_payload_buffer (buf);
 
-  outbuf = gst_buffer_new_and_alloc (payload_len);
-  memcpy (GST_BUFFER_DATA (outbuf), payload, payload_len);
   return outbuf;
 }
 
index a102ba7..2ca7240 100644 (file)
@@ -27,6 +27,9 @@
 
 #include "gstrtpspeexpay.h"
 
+GST_DEBUG_CATEGORY_STATIC (rtpspeexpay_debug);
+#define GST_CAT_DEFAULT (rtpspeexpay_debug)
+
 /* elementfactory information */
 static const GstElementDetails gst_rtp_speex_pay_details =
 GST_ELEMENT_DETAILS ("RTP packet payloader",
@@ -48,11 +51,14 @@ GST_STATIC_PAD_TEMPLATE ("src",
     GST_STATIC_CAPS ("application/x-rtp, "
         "media = (string) \"audio\", "
         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
-        "clock-rate =  (int) 8000, "
+        "clock-rate =  (int) [ 6000, 48000 ], "
         "encoding-name = (string) \"SPEEX\", "
         "encoding-params = (string) \"1\"")
     );
 
+static GstStateChangeReturn gst_rtp_speex_pay_change_state (GstElement *
+    element, GstStateChange transition);
+
 static gboolean gst_rtp_speex_pay_setcaps (GstBaseRTPPayload * payload,
     GstCaps * caps);
 static GstFlowReturn gst_rtp_speex_pay_handle_buffer (GstBaseRTPPayload *
@@ -71,6 +77,9 @@ gst_rtp_speex_pay_base_init (gpointer klass)
   gst_element_class_add_pad_template (element_class,
       gst_static_pad_template_get (&gst_rtp_speex_pay_src_template));
   gst_element_class_set_details (element_class, &gst_rtp_speex_pay_details);
+
+  GST_DEBUG_CATEGORY_INIT (rtpspeexpay_debug, "rtpspeexpay", 0,
+      "Speex RTP Payloader");
 }
 
 static void
@@ -84,7 +93,7 @@ gst_rtp_speex_pay_class_init (GstRtpSPEEXPayClass * klass)
   gstelement_class = (GstElementClass *) klass;
   gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
 
-  parent_class = g_type_class_peek_parent (klass);
+  gstelement_class->change_state = gst_rtp_speex_pay_change_state;
 
   gstbasertppayload_class->set_caps = gst_rtp_speex_pay_setcaps;
   gstbasertppayload_class->handle_buffer = gst_rtp_speex_pay_handle_buffer;
@@ -101,10 +110,95 @@ gst_rtp_speex_pay_init (GstRtpSPEEXPay * rtpspeexpay,
 static gboolean
 gst_rtp_speex_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
 {
-  gst_basertppayload_set_options (payload, "audio", FALSE, "speex", 8000);
-  gst_basertppayload_set_outcaps (payload, NULL);
+  /* don't configure yet, we wait for the ident packet */
+  return TRUE;
+}
+
+static gboolean
+gst_rtp_speex_pay_parse_ident (GstRtpSPEEXPay * rtpspeexpay,
+    const guint8 * data, guint size)
+{
+  guint32 version, header_size, rate, mode, nb_channels;
+  GstBaseRTPPayload *payload;
+  gchar *cstr;
+
+  /* we need the header string (8), the version string (20), the version
+   * and the header length. */
+  if (size < 36)
+    goto too_small;
+
+  if (!g_str_has_prefix ((const gchar *) data, "Speex   "))
+    goto wrong_header;
+
+  /* skip header and version string */
+  data += 28;
+
+  version = GST_READ_UINT32_LE (data);
+  if (version != 1)
+    goto wrong_version;
+
+  data += 4;
+  /* ensure sizes */
+  header_size = GST_READ_UINT32_LE (data);
+  if (header_size < 80)
+    goto header_too_small;
+
+  if (size < header_size)
+    goto payload_too_small;
+
+  data += 4;
+  rate = GST_READ_UINT32_LE (data);
+  data += 4;
+  mode = GST_READ_UINT32_LE (data);
+  data += 8;
+  nb_channels = GST_READ_UINT32_LE (data);
+
+  GST_DEBUG_OBJECT (rtpspeexpay, "rate %d, mode %d, nb_channels %d",
+      rate, mode, nb_channels);
+
+  payload = GST_BASE_RTP_PAYLOAD (rtpspeexpay);
+
+  gst_basertppayload_set_options (payload, "audio", FALSE, "SPEEX", rate);
+  cstr = g_strdup_printf ("%d", nb_channels);
+  gst_basertppayload_set_outcaps (payload, "encoding-params",
+      G_TYPE_STRING, cstr, NULL);
+  g_free (cstr);
 
   return TRUE;
+
+  /* ERRORS */
+too_small:
+  {
+    GST_DEBUG_OBJECT (rtpspeexpay,
+        "ident packet too small, need at least 32 bytes");
+    return FALSE;
+  }
+wrong_header:
+  {
+    GST_DEBUG_OBJECT (rtpspeexpay,
+        "ident packet does not start with \"Speex   \"");
+    return FALSE;
+  }
+wrong_version:
+  {
+    GST_DEBUG_OBJECT (rtpspeexpay, "can only handle version 1, have version %d",
+        version);
+    return FALSE;
+  }
+header_too_small:
+  {
+    GST_DEBUG_OBJECT (rtpspeexpay,
+        "header size too small, need at least 80 bytes, " "got only %d",
+        header_size);
+    return FALSE;
+  }
+payload_too_small:
+  {
+    GST_DEBUG_OBJECT (rtpspeexpay,
+        "payload too small, need at least %d bytes, got only %d", header_size,
+        size);
+    return FALSE;
+  }
 }
 
 static GstFlowReturn
@@ -121,6 +215,26 @@ gst_rtp_speex_pay_handle_buffer (GstBaseRTPPayload * basepayload,
   rtpspeexpay = GST_RTP_SPEEX_PAY (basepayload);
 
   size = GST_BUFFER_SIZE (buffer);
+  data = GST_BUFFER_DATA (buffer);
+
+  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))
+        goto parse_error;
+
+      ret = GST_FLOW_OK;
+      goto done;
+    case 1:
+      /* comment packet, we ignore it */
+      ret = GST_FLOW_OK;
+      goto done;
+    default:
+      /* other packets go in the payload */
+      break;
+  }
+
   timestamp = GST_BUFFER_TIMESTAMP (buffer);
 
   /* FIXME, only one SPEEX frame per RTP packet for now */
@@ -135,8 +249,6 @@ gst_rtp_speex_pay_handle_buffer (GstBaseRTPPayload * basepayload,
   /* get payload */
   payload = gst_rtp_buffer_get_payload (outbuf);
 
-  data = GST_BUFFER_DATA (buffer);
-
   /* copy data in payload */
   memcpy (&payload[0], data, size);
 
@@ -144,6 +256,46 @@ gst_rtp_speex_pay_handle_buffer (GstBaseRTPPayload * basepayload,
 
   ret = gst_basertppayload_push (basepayload, outbuf);
 
+done:
+  rtpspeexpay->packet++;
+
+  return ret;
+
+  /* ERRORS */
+parse_error:
+  {
+    GST_ELEMENT_ERROR (rtpspeexpay, STREAM, DECODE, (NULL),
+        ("Error parsing first identification packet."));
+    return GST_FLOW_ERROR;
+  }
+}
+
+static GstStateChangeReturn
+gst_rtp_speex_pay_change_state (GstElement * element, GstStateChange transition)
+{
+  GstRtpSPEEXPay *rtpspeexpay;
+  GstStateChangeReturn ret;
+
+  rtpspeexpay = GST_RTP_SPEEX_PAY (element);
+
+  switch (transition) {
+    case GST_STATE_CHANGE_NULL_TO_READY:
+      break;
+    case GST_STATE_CHANGE_READY_TO_PAUSED:
+      rtpspeexpay->packet = 0;
+      break;
+    default:
+      break;
+  }
+
+  ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
+
+  switch (transition) {
+    case GST_STATE_CHANGE_READY_TO_NULL:
+      break;
+    default:
+      break;
+  }
   return ret;
 }
 
index faea707..b50f750 100644 (file)
@@ -38,6 +38,8 @@ typedef struct _GstRtpSPEEXPayClass GstRtpSPEEXPayClass;
 struct _GstRtpSPEEXPay
 {
   GstBaseRTPPayload payload;
+
+  guint64 packet;
 };
 
 struct _GstRtpSPEEXPayClass