summaryrefslogtreecommitdiffstats
path: root/gst/rtp/gstrtpspeexpay.c
diff options
context:
space:
mode:
Diffstat (limited to 'gst/rtp/gstrtpspeexpay.c')
-rw-r--r--gst/rtp/gstrtpspeexpay.c164
1 files changed, 158 insertions, 6 deletions
diff --git a/gst/rtp/gstrtpspeexpay.c b/gst/rtp/gstrtpspeexpay.c
index a102ba7d..2ca72408 100644
--- a/gst/rtp/gstrtpspeexpay.c
+++ b/gst/rtp/gstrtpspeexpay.c
@@ -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;
}