diff options
author | Christian Schaller <uraeus@gnome.org> | 2006-11-08 01:28:00 +0000 |
---|---|---|
committer | Christian Schaller <uraeus@gnome.org> | 2006-11-08 01:28:00 +0000 |
commit | 2ed1cb8ac258a5fcdf89b2f29e72fcf977b79c75 (patch) | |
tree | 08ed98c131482917150e5c131008414f73a9b60d /gst | |
parent | e0e75f715e4d6594a9804fac7a660f35f3107fd0 (diff) |
gst/rtp/: Add theora pay/depayloaders.
Original commit message from CVS:
* gst/rtp/Makefile.am:
* gst/rtp/gstrtp.c: (plugin_init):
* gst/rtp/gstrtptheoradepay.c: (gst_rtp_theora_depay_base_init),
(gst_rtp_theora_depay_class_init), (gst_rtp_theora_depay_init),
(gst_rtp_theora_depay_finalize),
(gst_rtp_theora_depay_parse_configuration),
(gst_rtp_theora_depay_setcaps),
(gst_rtp_theora_depay_switch_codebook),
(gst_rtp_theora_depay_process),
(gst_rtp_theora_depay_set_property),
(gst_rtp_theora_depay_get_property),
(gst_rtp_theora_depay_change_state),
(gst_rtp_theora_depay_plugin_init):
* gst/rtp/gstrtptheoradepay.h:
* gst/rtp/gstrtptheorapay.c: (gst_rtp_theora_pay_base_init),
(gst_rtp_theora_pay_class_init), (gst_rtp_theora_pay_init),
(gst_rtp_theora_pay_setcaps), (gst_rtp_theora_pay_reset_packet),
(gst_rtp_theora_pay_init_packet),
(gst_rtp_theora_pay_flush_packet),
(gst_rtp_theora_pay_finish_headers), (gst_rtp_theora_pay_parse_id),
(gst_rtp_theora_pay_handle_buffer),
(gst_rtp_theora_pay_plugin_init):
* gst/rtp/gstrtptheorapay.h:
Add theora pay/depayloaders.
Diffstat (limited to 'gst')
-rw-r--r-- | gst/rtp/Makefile.am | 4 | ||||
-rw-r--r-- | gst/rtp/gstrtp.c | 8 | ||||
-rw-r--r-- | gst/rtp/gstrtptheoradepay.c | 673 | ||||
-rw-r--r-- | gst/rtp/gstrtptheoradepay.h | 68 | ||||
-rw-r--r-- | gst/rtp/gstrtptheorapay.c | 581 | ||||
-rw-r--r-- | gst/rtp/gstrtptheorapay.h | 75 |
6 files changed, 1409 insertions, 0 deletions
diff --git a/gst/rtp/Makefile.am b/gst/rtp/Makefile.am index 70b8e183..0e903a7e 100644 --- a/gst/rtp/Makefile.am +++ b/gst/rtp/Makefile.am @@ -28,6 +28,8 @@ libgstrtp_la_SOURCES = \ gstrtpspeexdepay.c \ gstrtpspeexpay.c \ gstrtpsv3vdepay.c \ + gstrtptheoradepay.c \ + gstrtptheorapay.c \ gstrtpvorbisdepay.c \ gstrtpvorbispay.c @@ -74,5 +76,7 @@ noinst_HEADERS = \ gstrtpspeexdepay.h \ gstrtpspeexpay.h \ gstrtpsv3vdepay.h \ + gstrtptheoradepay.h \ + gstrtptheorapay.h \ gstrtpvorbisdepay.h \ gstrtpvorbispay.h diff --git a/gst/rtp/gstrtp.c b/gst/rtp/gstrtp.c index 3d4aa841..01ead5d0 100644 --- a/gst/rtp/gstrtp.c +++ b/gst/rtp/gstrtp.c @@ -47,6 +47,8 @@ #include "gstrtpspeexpay.h" #include "gstrtpspeexdepay.h" #include "gstrtpsv3vdepay.h" +#include "gstrtptheoradepay.h" +#include "gstrtptheorapay.h" #include "gstrtpvorbisdepay.h" #include "gstrtpvorbispay.h" @@ -131,6 +133,12 @@ plugin_init (GstPlugin * plugin) if (!gst_rtp_sv3v_depay_plugin_init (plugin)) return FALSE; + if (!gst_rtp_theora_depay_plugin_init (plugin)) + return FALSE; + + if (!gst_rtp_theora_pay_plugin_init (plugin)) + return FALSE; + if (!gst_rtp_vorbis_depay_plugin_init (plugin)) return FALSE; diff --git a/gst/rtp/gstrtptheoradepay.c b/gst/rtp/gstrtptheoradepay.c new file mode 100644 index 00000000..3f027ed0 --- /dev/null +++ b/gst/rtp/gstrtptheoradepay.c @@ -0,0 +1,673 @@ +/* GStreamer + * Copyright (C) <2006> Wim Taymans <wim@fluendo.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/tag/tag.h> +#include <gst/rtp/gstrtpbuffer.h> + +#include <string.h> +#include "gstrtptheoradepay.h" + +GST_DEBUG_CATEGORY_STATIC (rtptheoradepay_debug); +#define GST_CAT_DEFAULT (rtptheoradepay_debug) + +/* elementfactory information */ +static const GstElementDetails gst_rtp_theora_depay_details = +GST_ELEMENT_DETAILS ("RTP packet depayloader", + "Codec/Depayloader/Network", + "Extracts Theora video from RTP packets (draft-01 of RFC XXXX)", + "Wim Taymans <wim@fluendo.com>"); + +/* RtpTheoraDepay signals and args */ +enum +{ + /* FILL ME */ + LAST_SIGNAL +}; + +enum +{ + ARG_0, +}; + +static GstStaticPadTemplate gst_rtp_theora_depay_sink_template = +GST_STATIC_PAD_TEMPLATE ("sink", + GST_PAD_SINK, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("application/x-rtp, " + "media = (string) \"video\", " + "clock-rate = (int) [1, MAX ], " "encoding-name = (string) \"theora\"" + /* All required parameters + * + * "encoding-params = (string) <num channels>" + * "delivery-method = (string) { inline, in_band, out_band/<specific_name> } " + * "configuration = (string) ANY" + */ + /* All optional parameters + * + * "configuration-uri =" + */ + ) + ); + +static GstStaticPadTemplate gst_rtp_theora_depay_src_template = +GST_STATIC_PAD_TEMPLATE ("src", + GST_PAD_SRC, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("video/x-theora") + ); + +/* 42 bytes for the theora identification packet length */ +#define THEORA_ID_LEN 42 + +GST_BOILERPLATE (GstRtpTheoraDepay, gst_rtp_theora_depay, GstBaseRTPDepayload, + GST_TYPE_BASE_RTP_DEPAYLOAD); + +static gboolean gst_rtp_theora_depay_setcaps (GstBaseRTPDepayload * depayload, + GstCaps * caps); +static GstBuffer *gst_rtp_theora_depay_process (GstBaseRTPDepayload * depayload, + GstBuffer * buf); + +static void gst_rtp_theora_depay_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * pspec); +static void gst_rtp_theora_depay_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * pspec); +static void gst_rtp_theora_depay_finalize (GObject * object); + +static GstStateChangeReturn gst_rtp_theora_depay_change_state (GstElement * + element, GstStateChange transition); + + +static void +gst_rtp_theora_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_theora_depay_sink_template)); + gst_element_class_add_pad_template (element_class, + gst_static_pad_template_get (&gst_rtp_theora_depay_src_template)); + + gst_element_class_set_details (element_class, &gst_rtp_theora_depay_details); +} + +static void +gst_rtp_theora_depay_class_init (GstRtpTheoraDepayClass * klass) +{ + GObjectClass *gobject_class; + GstElementClass *gstelement_class; + GstBaseRTPDepayloadClass *gstbasertpdepayload_class; + + gobject_class = (GObjectClass *) klass; + gstelement_class = (GstElementClass *) klass; + gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass; + + gobject_class->set_property = gst_rtp_theora_depay_set_property; + gobject_class->get_property = gst_rtp_theora_depay_get_property; + gobject_class->finalize = gst_rtp_theora_depay_finalize; + + gstelement_class->change_state = gst_rtp_theora_depay_change_state; + + gstbasertpdepayload_class->process = gst_rtp_theora_depay_process; + gstbasertpdepayload_class->set_caps = gst_rtp_theora_depay_setcaps; + + GST_DEBUG_CATEGORY_INIT (rtptheoradepay_debug, "rtptheoradepay", 0, + "Theora RTP Depayloader"); +} + +static void +gst_rtp_theora_depay_init (GstRtpTheoraDepay * rtptheoradepay, + GstRtpTheoraDepayClass * klass) +{ + rtptheoradepay->adapter = gst_adapter_new (); +} +static void +gst_rtp_theora_depay_finalize (GObject * object) +{ + GstRtpTheoraDepay *rtptheoradepay = GST_RTP_THEORA_DEPAY (object); + + g_object_unref (rtptheoradepay->adapter); + + G_OBJECT_CLASS (parent_class)->finalize (object); +} + +static gboolean +gst_rtp_theora_depay_parse_configuration (GstRtpTheoraDepay * rtptheoradepay, + const gchar * configuration) +{ + GValue v = { 0 }; + GstBuffer *buf; + guint32 num_headers; + guint8 *data; + guint size; + gint i; + + /* deserialize base16 to buffer */ + g_value_init (&v, GST_TYPE_BUFFER); + if (!gst_value_deserialize (&v, configuration)) + goto wrong_configuration; + + buf = gst_value_get_buffer (&v); + gst_buffer_ref (buf); + g_value_unset (&v); + + data = GST_BUFFER_DATA (buf); + size = GST_BUFFER_SIZE (buf); + + GST_DEBUG_OBJECT (rtptheoradepay, "config size %u", size); + + /* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | Number of packed headers | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | Packed header | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | Packed header | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | .... | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + */ + if (size < 4) + goto too_small; + + num_headers = GST_READ_UINT32_BE (data); + size -= 4; + data += 4; + + GST_DEBUG_OBJECT (rtptheoradepay, "have %u headers", num_headers); + + /* 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 + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | Ident | .. + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * .. length | Identification Header .. + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * .. Identification Header | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | Setup Header .. + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * .. Setup Header | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * + */ + for (i = 0; i < num_headers; i++) { + guint32 ident; + guint16 length; + GstRtpTheoraConfig *conf; + GstTagList *list; + + if (size < 5) + goto too_small; + + ident = (data[0] << 16) | (data[1] << 8) | data[2]; + length = (data[3] << 8) | data[4]; + size -= 5; + data += 5; + + GST_DEBUG_OBJECT (rtptheoradepay, "header %d, ident %08x, length %u", i, + ident, length); + + if (size < length + THEORA_ID_LEN) + goto too_small; + + GST_DEBUG_OBJECT (rtptheoradepay, "preparing headers"); + + conf = g_new0 (GstRtpTheoraConfig, 1); + conf->ident = ident; + + buf = gst_buffer_new_and_alloc (THEORA_ID_LEN); + memcpy (GST_BUFFER_DATA (buf), data, THEORA_ID_LEN); + conf->headers = g_list_append (conf->headers, buf); + data += THEORA_ID_LEN; + size -= THEORA_ID_LEN; + + /* create a dummy comment */ + list = gst_tag_list_new (); + buf = + gst_tag_list_to_vorbiscomment_buffer (list, (guint8 *) "\201theora", 7, + "Theora RTP depayloader"); + conf->headers = g_list_append (conf->headers, buf); + gst_tag_list_free (list); + + buf = gst_buffer_new_and_alloc (length); + memcpy (GST_BUFFER_DATA (buf), data, length); + conf->headers = g_list_append (conf->headers, buf); + data += length; + size -= length; + + rtptheoradepay->configs = g_list_append (rtptheoradepay->configs, conf); + } + + return TRUE; + + /* ERRORS */ +wrong_configuration: + { + GST_DEBUG_OBJECT (rtptheoradepay, "error parsing configuration"); + return FALSE; + } +too_small: + { + GST_DEBUG_OBJECT (rtptheoradepay, "configuration too small"); + return FALSE; + } +} + +static gboolean +gst_rtp_theora_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps) +{ + GstStructure *structure; + GstRtpTheoraDepay *rtptheoradepay; + GstCaps *srccaps; + const gchar *delivery_method; + const gchar *configuration; + gint clock_rate; + + rtptheoradepay = GST_RTP_THEORA_DEPAY (depayload); + + structure = gst_caps_get_structure (caps, 0); + + /* get clockrate */ + if (!gst_structure_get_int (structure, "clock-rate", &clock_rate)) + goto no_rate; + + /* see how the configuration parameters will be transmitted */ + delivery_method = gst_structure_get_string (structure, "delivery-method"); + if (delivery_method == NULL) + goto no_delivery_method; + + if (g_strcasecmp (delivery_method, "inline")) { + /* configure string is in the caps */ + } else if (g_strcasecmp (delivery_method, "in_band")) { + /* headers will (also) be transmitted in the RTP packets */ + } else if (g_str_has_prefix (delivery_method, "out_band/")) { + /* some other method of header delivery. */ + goto unsupported_delivery_method; + } else + goto unsupported_delivery_method; + + /* read and parse configuration string */ + configuration = gst_structure_get_string (structure, "configuration"); + if (configuration == NULL) + goto no_configuration; + + if (!gst_rtp_theora_depay_parse_configuration (rtptheoradepay, configuration)) + goto invalid_configuration; + + /* caps seem good, configure element */ + depayload->clock_rate = clock_rate; + + /* set caps on pad and on header */ + srccaps = gst_caps_new_simple ("video/x-theora", NULL); + gst_pad_set_caps (depayload->srcpad, srccaps); + gst_caps_unref (srccaps); + + return TRUE; + + /* ERRORS */ +unsupported_delivery_method: + { + GST_ERROR_OBJECT (rtptheoradepay, + "unsupported delivery-method \"%s\" specified", delivery_method); + return FALSE; + } +no_delivery_method: + { + GST_ERROR_OBJECT (rtptheoradepay, "no delivery-method specified"); + return FALSE; + } +no_configuration: + { + GST_ERROR_OBJECT (rtptheoradepay, "no configuration specified"); + return FALSE; + } +invalid_configuration: + { + GST_ERROR_OBJECT (rtptheoradepay, "invalid configuration specified"); + return FALSE; + } +no_rate: + { + GST_ERROR_OBJECT (rtptheoradepay, "no clock-rate specified"); + return FALSE; + } +} + +static gboolean +gst_rtp_theora_depay_switch_codebook (GstRtpTheoraDepay * rtptheoradepay, + guint32 ident) +{ + GList *walk; + gboolean res = FALSE; + + for (walk = rtptheoradepay->configs; walk; walk = g_list_next (walk)) { + GstRtpTheoraConfig *conf = (GstRtpTheoraConfig *) walk->data; + + if (conf->ident == ident) { + GList *headers; + + /* FIXME, remove pads, create new pad.. */ + + /* push out all the headers */ + for (headers = conf->headers; headers; headers = g_list_next (headers)) { + GstBuffer *header = GST_BUFFER_CAST (headers->data); + + gst_buffer_ref (header); + gst_base_rtp_depayload_push (GST_BASE_RTP_DEPAYLOAD (rtptheoradepay), + header); + } + /* remember the current config */ + rtptheoradepay->config = conf; + res = TRUE; + } + } + if (!res) { + /* we don't know about the headers, figure out an alternative method for + * getting the codebooks. FIXME, fail for now. */ + } + return res; +} + +static GstBuffer * +gst_rtp_theora_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf) +{ + GstRtpTheoraDepay *rtptheoradepay; + GstBuffer *outbuf; + GstFlowReturn ret; + gint payload_len; + guint8 *payload, *to_free = NULL; + guint32 timestamp; + guint32 header, ident; + guint8 F, TDT, packets; + gboolean free_payload; + + rtptheoradepay = GST_RTP_THEORA_DEPAY (depayload); + + if (!gst_rtp_buffer_validate (buf)) + goto bad_packet; + + payload_len = gst_rtp_buffer_get_payload_len (buf); + + GST_DEBUG_OBJECT (depayload, "got RTP packet of size %d", payload_len); + + /* we need at least 4 bytes for the packet header */ + if (G_UNLIKELY (payload_len < 4)) + goto packet_short; + + payload = gst_rtp_buffer_get_payload (buf); + free_payload = FALSE; + + header = GST_READ_UINT32_BE (payload); + /* + * 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 + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | Ident | F |TDT|# pkts.| + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * + * F: Fragment type (0=none, 1=start, 2=cont, 3=end) + * TDT: Theora data type (0=theora, 1=config, 2=comment, 3=reserved) + * pkts: number of packets. + */ + TDT = (header & 0x30) >> 4; + if (G_UNLIKELY (TDT == 3)) + goto ignore_reserved; + + ident = (header >> 8) & 0xffffff; + F = (header & 0xc0) >> 6; + packets = (header & 0xf); + + if (TDT == 0) { + gboolean do_switch = FALSE; + + /* we have a raw payload, find the codebook for the ident */ + if (!rtptheoradepay->config) { + /* we don't have an active codebook, find the codebook and + * activate it */ + do_switch = TRUE; + } else if (rtptheoradepay->config->ident != ident) { + /* codebook changed */ + do_switch = TRUE; + } + if (do_switch) { + if (!gst_rtp_theora_depay_switch_codebook (rtptheoradepay, ident)) + goto switch_failed; + } + } + + /* skip header */ + payload += 4; + payload_len -= 4; + + GST_DEBUG_OBJECT (depayload, "ident: %u, F: %d, TDT: %d, packets: %d", ident, + F, TDT, packets); + + /* fragmented packets, assemble */ + if (F != 0) { + GstBuffer *vdata; + guint headerskip; + + if (F == 1) { + /* if we start a packet, clear adapter and start assembling. */ + gst_adapter_clear (rtptheoradepay->adapter); + GST_DEBUG_OBJECT (depayload, "start assemble"); + rtptheoradepay->assembling = TRUE; + } + + if (!rtptheoradepay->assembling) + goto no_output; + + /* first assembled packet, reuse 2 bytes to store the length */ + headerskip = (F == 1 ? 4 : 6); + /* skip header and length. */ + vdata = gst_rtp_buffer_get_payload_subbuffer (buf, headerskip, -1); + + GST_DEBUG_OBJECT (depayload, "assemble theora packet"); + gst_adapter_push (rtptheoradepay->adapter, vdata); + + /* packet is not complete, we are done */ + if (F != 3) + goto no_output; + + /* construct assembled buffer */ + payload_len = gst_adapter_available (rtptheoradepay->adapter); + payload = gst_adapter_take (rtptheoradepay->adapter, payload_len); + /* fix the length */ + payload[0] = ((payload_len - 2) >> 8) & 0xff; + payload[1] = (payload_len - 2) & 0xff; + to_free = payload; + } + + GST_DEBUG_OBJECT (depayload, "assemble done"); + + /* we not assembling anymore now */ + rtptheoradepay->assembling = FALSE; + gst_adapter_clear (rtptheoradepay->adapter); + + /* payload now points to a length with that many theora data bytes. + * Iterate over the packets and send them out. + * + * 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 + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | length | theora data .. + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * .. theora data | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | length | next theora packet data .. + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * .. theora data | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+* + */ + timestamp = gst_rtp_buffer_get_timestamp (buf); + + while (payload_len > 2) { + guint16 length; + + length = GST_READ_UINT16_BE (payload); + payload += 2; + payload_len -= 2; + + GST_DEBUG_OBJECT (depayload, "read length %u, avail: %d", length, + payload_len); + + /* skip packet if something odd happens */ + if (G_UNLIKELY (length > payload_len)) + goto length_short; + + /* create buffer for packet */ + if (G_UNLIKELY (to_free)) { + outbuf = gst_buffer_new (); + GST_BUFFER_DATA (outbuf) = payload; + GST_BUFFER_MALLOCDATA (outbuf) = to_free; + GST_BUFFER_SIZE (outbuf) = length; + to_free = NULL; + } else { + outbuf = gst_buffer_new_and_alloc (length); + memcpy (GST_BUFFER_DATA (outbuf), payload, length); + } + + payload += length; + payload_len -= length; + + if (timestamp != -1) + /* push with timestamp of the last packet, which is the same timestamp that + * should apply to the first assembled packet. */ + ret = gst_base_rtp_depayload_push_ts (depayload, timestamp, outbuf); + else + ret = gst_base_rtp_depayload_push (depayload, outbuf); + + if (ret != GST_FLOW_OK) + break; + + /* make sure we don't set a timestamp on next buffers */ + timestamp = -1; + } + + g_free (to_free); + + return NULL; + +no_output: + { + return NULL; + } + /* ERORRS */ +bad_packet: + { + GST_ELEMENT_WARNING (rtptheoradepay, STREAM, DECODE, + (NULL), ("Packet did not validate")); + return NULL; + } +switch_failed: + { + GST_ELEMENT_ERROR (rtptheoradepay, STREAM, DECODE, + (NULL), ("Could not switch codebooks")); + return NULL; + } +packet_short: + { + GST_ELEMENT_WARNING (rtptheoradepay, STREAM, DECODE, + (NULL), ("Packet was too short (%d < 4)", payload_len)); + return NULL; + } +ignore_reserved: + { + GST_WARNING_OBJECT (rtptheoradepay, "reserved TDT ignored"); + return NULL; + } +length_short: + { + GST_ELEMENT_WARNING (rtptheoradepay, STREAM, DECODE, + (NULL), ("Packet contains invalid data")); + return NULL; + } +} + +static void +gst_rtp_theora_depay_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * pspec) +{ + GstRtpTheoraDepay *rtptheoradepay; + + rtptheoradepay = GST_RTP_THEORA_DEPAY (object); + + switch (prop_id) { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +gst_rtp_theora_depay_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * pspec) +{ + GstRtpTheoraDepay *rtptheoradepay; + + rtptheoradepay = GST_RTP_THEORA_DEPAY (object); + + switch (prop_id) { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static GstStateChangeReturn +gst_rtp_theora_depay_change_state (GstElement * element, + GstStateChange transition) +{ + GstRtpTheoraDepay *rtptheoradepay; + GstStateChangeReturn ret; + + rtptheoradepay = GST_RTP_THEORA_DEPAY (element); + + switch (transition) { + case GST_STATE_CHANGE_NULL_TO_READY: + break; + case GST_STATE_CHANGE_READY_TO_PAUSED: + 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; +} + +gboolean +gst_rtp_theora_depay_plugin_init (GstPlugin * plugin) +{ + return gst_element_register (plugin, "rtptheoradepay", + GST_RANK_MARGINAL, GST_TYPE_RTP_THEORA_DEPAY); +} diff --git a/gst/rtp/gstrtptheoradepay.h b/gst/rtp/gstrtptheoradepay.h new file mode 100644 index 00000000..38737e0f --- /dev/null +++ b/gst/rtp/gstrtptheoradepay.h @@ -0,0 +1,68 @@ +/* GStreamer + * Copyright (C) <2006> Wim Taymans <wim@fluendo.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_THEORA_DEPAY_H__ +#define __GST_RTP_THEORA_DEPAY_H__ + +#include <gst/gst.h> +#include <gst/base/gstadapter.h> +#include <gst/rtp/gstbasertpdepayload.h> + +G_BEGIN_DECLS + +#define GST_TYPE_RTP_THEORA_DEPAY \ + (gst_rtp_theora_depay_get_type()) +#define GST_RTP_THEORA_DEPAY(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_RTP_THEORA_DEPAY,GstRtpTheoraDepay)) +#define GST_RTP_THEORA_DEPAY_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_RTP_THEORA_DEPAY,GstRtpTheoraDepayClass)) +#define GST_IS_RTP_THEORA_DEPAY(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_RTP_THEORA_DEPAY)) +#define GST_IS_RTP_THEORA_DEPAY_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_RTP_THEORA_DEPAY)) + +typedef struct _GstRtpTheoraDepay GstRtpTheoraDepay; +typedef struct _GstRtpTheoraDepayClass GstRtpTheoraDepayClass; + +typedef struct _GstRtpTheoraConfig { + guint32 ident; + GList *headers; +} GstRtpTheoraConfig; + +struct _GstRtpTheoraDepay +{ + GstBaseRTPDepayload parent; + + GList *configs; + GstRtpTheoraConfig *config; + + GstAdapter *adapter; + gboolean assembling; +}; + +struct _GstRtpTheoraDepayClass +{ + GstBaseRTPDepayloadClass parent_class; +}; + +gboolean gst_rtp_theora_depay_plugin_init (GstPlugin * plugin); + +G_END_DECLS + +#endif /* __GST_RTP_THEORA_DEPAY_H__ */ diff --git a/gst/rtp/gstrtptheorapay.c b/gst/rtp/gstrtptheorapay.c new file mode 100644 index 00000000..d9bd81e6 --- /dev/null +++ b/gst/rtp/gstrtptheorapay.c @@ -0,0 +1,581 @@ +/* GStreamer + * Copyright (C) <2006> Wim Taymans <wim@fluendo.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 "gstrtptheorapay.h" + +#define THEORA_ID_LEN 42 + +GST_DEBUG_CATEGORY_STATIC (rtptheorapay_debug); +#define GST_CAT_DEFAULT (rtptheorapay_debug) + +/* references: + * http://svn.xiph.org/trunk/theora/doc/draft-ietf-avt-rtp-theora-01.txt + */ + +/* elementfactory information */ +static const GstElementDetails gst_rtp_theorapay_details = +GST_ELEMENT_DETAILS ("RTP packet depayloader", + "Codec/Payloader/Network", + "Payload-encode Theora video into RTP packets (draft-01 RFC XXXX)", + "Wim Taymans <wim@fluendo.com>"); + +static GstStaticPadTemplate gst_rtp_theora_pay_src_template = +GST_STATIC_PAD_TEMPLATE ("src", + GST_PAD_SRC, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("application/x-rtp, " + "media = (string) \"video\", " + "payload = (int) [ 96, 127 ], " + "clock-rate = (int) [1, MAX ], " "encoding-name = (string) \"theora\"" + /* All required parameters + * + * "sampling = (string) { "YCbCr-4:2:0", "YCbCr-4:2:2", "YCbCr-4:4:4" } " + * "width = (string) [1, 1048561] (multiples of 16) " + * "height = (string) [1, 1048561] (multiples of 16) " + * "delivery-method = (string) { inline, in_band, out_band/<specific_name> } " + * "configuration = (string) ANY" + */ + /* All optional parameters + * + * "configuration-uri =" + */ + ) + ); + +static GstStaticPadTemplate gst_rtp_theora_pay_sink_template = +GST_STATIC_PAD_TEMPLATE ("sink", + GST_PAD_SINK, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("video/x-theora") + ); + +GST_BOILERPLATE (GstRtpTheoraPay, gst_rtp_theora_pay, GstBaseRTPPayload, + GST_TYPE_BASE_RTP_PAYLOAD); + +static gboolean gst_rtp_theora_pay_setcaps (GstBaseRTPPayload * basepayload, + GstCaps * caps); +static GstFlowReturn gst_rtp_theora_pay_handle_buffer (GstBaseRTPPayload * pad, + GstBuffer * buffer); + +static void +gst_rtp_theora_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_theora_pay_src_template)); + gst_element_class_add_pad_template (element_class, + gst_static_pad_template_get (&gst_rtp_theora_pay_sink_template)); + + gst_element_class_set_details (element_class, &gst_rtp_theorapay_details); +} + +static void +gst_rtp_theora_pay_class_init (GstRtpTheoraPayClass * klass) +{ + GObjectClass *gobject_class; + GstElementClass *gstelement_class; + GstBaseRTPPayloadClass *gstbasertppayload_class; + + gobject_class = (GObjectClass *) klass; + gstelement_class = (GstElementClass *) klass; + gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass; + + parent_class = g_type_class_peek_parent (klass); + + gstbasertppayload_class->set_caps = gst_rtp_theora_pay_setcaps; + gstbasertppayload_class->handle_buffer = gst_rtp_theora_pay_handle_buffer; + + GST_DEBUG_CATEGORY_INIT (rtptheorapay_debug, "rtptheorapay", 0, + "Theora RTP Payloader"); +} + +static void +gst_rtp_theora_pay_init (GstRtpTheoraPay * rtptheorapay, + GstRtpTheoraPayClass * klass) +{ +} + +static gboolean +gst_rtp_theora_pay_setcaps (GstBaseRTPPayload * basepayload, GstCaps * caps) +{ + GstRtpTheoraPay *rtptheorapay; + + rtptheorapay = GST_RTP_THEORA_PAY (basepayload); + + rtptheorapay->need_headers = TRUE; + + return TRUE; +} + +static void +gst_rtp_theora_pay_reset_packet (GstRtpTheoraPay * rtptheorapay, guint8 TDT) +{ + guint payload_len; + + GST_DEBUG_OBJECT (rtptheorapay, "reset packet"); + + rtptheorapay->payload_pos = 4; + payload_len = gst_rtp_buffer_get_payload_len (rtptheorapay->packet); + rtptheorapay->payload_left = payload_len - 4; + rtptheorapay->payload_duration = 0; + rtptheorapay->payload_F = 0; + rtptheorapay->payload_TDT = TDT; + rtptheorapay->payload_pkts = 0; +} + +static void +gst_rtp_theora_pay_init_packet (GstRtpTheoraPay * rtptheorapay, guint8 TDT, + GstClockTime timestamp) +{ + GST_DEBUG_OBJECT (rtptheorapay, "starting new packet, TDT: %d", TDT); + + if (rtptheorapay->packet) + gst_buffer_unref (rtptheorapay->packet); + + /* new packet allocate max packet size */ + rtptheorapay->packet = + gst_rtp_buffer_new_allocate_len (GST_BASE_RTP_PAYLOAD_MTU + (rtptheorapay), 0, 0); + gst_rtp_theora_pay_reset_packet (rtptheorapay, TDT); + GST_BUFFER_TIMESTAMP (rtptheorapay->packet) = timestamp; +} + +static GstFlowReturn +gst_rtp_theora_pay_flush_packet (GstRtpTheoraPay * rtptheorapay) +{ + GstFlowReturn ret; + guint8 *payload; + guint hlen; + + /* check for empty packet */ + if (!rtptheorapay->packet || rtptheorapay->payload_pos <= 4) + return GST_FLOW_OK; + + GST_DEBUG_OBJECT (rtptheorapay, "flushing packet"); + + /* fix header */ + payload = gst_rtp_buffer_get_payload (rtptheorapay->packet); + /* + * 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 + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | Ident | F |TDT|# pkts.| + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * + * F: Fragment type (0=none, 1=start, 2=cont, 3=end) + * TDT: Theora data type (0=theora, 1=config, 2=comment, 3=reserved) + * pkts: number of packets. + */ + payload[0] = (rtptheorapay->payload_ident >> 16) & 0xff; + payload[1] = (rtptheorapay->payload_ident >> 8) & 0xff; + payload[2] = (rtptheorapay->payload_ident) & 0xff; + payload[3] = (rtptheorapay->payload_F & 0x3) << 6 | + (rtptheorapay->payload_TDT & 0x3) << 4 | + (rtptheorapay->payload_pkts & 0xf); + + /* shrink the buffer size to the last written byte */ + hlen = gst_rtp_buffer_calc_header_len (0); + GST_BUFFER_SIZE (rtptheorapay->packet) = hlen + rtptheorapay->payload_pos; + + /* push, this gives away our ref to the packet, so clear it. */ + ret = + gst_basertppayload_push (GST_BASE_RTP_PAYLOAD (rtptheorapay), + rtptheorapay->packet); + rtptheorapay->packet = NULL; + + return ret; +} + +static gboolean +gst_rtp_theora_pay_finish_headers (GstBaseRTPPayload * basepayload) +{ + GstRtpTheoraPay *rtptheorapay = GST_RTP_THEORA_PAY (basepayload); + GList *walk; + guint length; + gchar *configuration; + gchar *wstr, *hstr; + guint8 *data; + guint32 ident; + GValue v = { 0 }; + GstBuffer *config; + + GST_DEBUG_OBJECT (rtptheorapay, "finish headers"); + + if (!rtptheorapay->headers) + goto no_headers; + + /* we need exactly 2 header packets */ + if (g_list_length (rtptheorapay->headers) != 2) + goto no_headers; + + /* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | Number of packed headers | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | Packed header | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | Packed header | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | .... | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * + * We only construct a config containing 1 packed header like this: + * + * 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 + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | Ident | .. + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * .. length | Identification Header .. + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * .. Identification Header | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | Setup Header .. + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * .. Setup Header | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * + */ + + /* count the size of the headers first */ + length = 0; + for (walk = rtptheorapay->headers; walk; walk = g_list_next (walk)) { + GstBuffer *buf = GST_BUFFER_CAST (walk->data); + + length += GST_BUFFER_SIZE (buf); + } + + /* total config length is 4 bytes header number + size of the + * headers + 2 bytes length + 3 bytes for the ident */ + config = gst_buffer_new_and_alloc (length + 4 + 2 + 3); + data = GST_BUFFER_DATA (config); + + /* number of packed headers, we only pack 1 header */ + data[0] = 0; + data[1] = 0; + data[2] = 0; + data[3] = 1; + + /* we generate a random ident for this configuration */ + ident = rtptheorapay->payload_ident = g_random_int (); + + /* take lower 3 bytes */ + data[4] = (ident >> 16) & 0xff; + data[5] = (ident >> 8) & 0xff; + data[6] = ident & 0xff; + + /* store length minus the length of the fixed theora header */ + data[7] = ((length - THEORA_ID_LEN) >> 8) & 0xff; + data[8] = (length - THEORA_ID_LEN) & 0xff; + + /* copy header data */ + data += 9; + for (walk = rtptheorapay->headers; walk; walk = g_list_next (walk)) { + GstBuffer *buf = GST_BUFFER_CAST (walk->data); + + memcpy (data, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf)); + data += GST_BUFFER_SIZE (buf); + } + + /* serialize buffer to base16 */ + g_value_init (&v, GST_TYPE_BUFFER); + gst_value_take_buffer (&v, config); + configuration = gst_value_serialize (&v); + + /* configure payloader settings */ + wstr = g_strdup_printf ("%d", rtptheorapay->width); + hstr = g_strdup_printf ("%d", rtptheorapay->height); + gst_basertppayload_set_options (basepayload, "video", TRUE, "theora", 90000); + gst_basertppayload_set_outcaps (basepayload, + "sampling", G_TYPE_STRING, "YCbCr-4:2:0", + "width", G_TYPE_STRING, wstr, + "height", G_TYPE_STRING, hstr, + "configuration", G_TYPE_STRING, configuration, + "delivery-method", G_TYPE_STRING, "inline", + /* don't set the other defaults + */ + NULL); + g_free (wstr); + g_free (hstr); + g_free (configuration); + g_value_unset (&v); + + return TRUE; + + /* ERRORS */ +no_headers: + { + GST_DEBUG_OBJECT (rtptheorapay, "finish headers"); + return FALSE; + } +} + +static gboolean +gst_rtp_theora_pay_parse_id (GstBaseRTPPayload * basepayload, guint8 * data, + guint size) +{ + GstRtpTheoraPay *rtptheorapay; + gint width, height; + + rtptheorapay = GST_RTP_THEORA_PAY (basepayload); + + if (G_UNLIKELY (size < 42)) + goto too_short; + + if (G_UNLIKELY (memcmp (data, "\200theora", 7))) + goto invalid_start; + data += 7; + + if (G_UNLIKELY (data[0] != 3)) + goto invalid_version; + if (G_UNLIKELY (data[1] != 2)) + goto invalid_version; + data += 3; + + width = GST_READ_UINT16_BE (data) << 4; + data += 2; + height = GST_READ_UINT16_BE (data) << 4; + data += 2; + + /* FIXME, parse pixel format */ + + /* store values */ + rtptheorapay->width = width; + rtptheorapay->height = height; + + return TRUE; + + /* ERRORS */ +too_short: + { + GST_ELEMENT_ERROR (basepayload, STREAM, DECODE, + (NULL), + ("Identification packet is too short, need at least 42, got %d", size)); + return FALSE; + } +invalid_start: + { + GST_ELEMENT_ERROR (basepayload, STREAM, DECODE, + (NULL), ("Invalid header start in identification packet")); + return FALSE; + } +invalid_version: + { + GST_ELEMENT_ERROR (basepayload, STREAM, DECODE, + (NULL), ("Invalid version")); + return FALSE; + } +} + +static GstFlowReturn +gst_rtp_theora_pay_handle_buffer (GstBaseRTPPayload * basepayload, + GstBuffer * buffer) +{ + GstRtpTheoraPay *rtptheorapay; + GstFlowReturn ret; + guint size, newsize; + guint8 *data; + guint packet_len; + GstClockTime duration, newduration, timestamp; + gboolean flush; + guint8 TDT; + guint plen; + guint8 *ppos, *payload; + gboolean fragmented; + + rtptheorapay = GST_RTP_THEORA_PAY (basepayload); + + size = GST_BUFFER_SIZE (buffer); + data = GST_BUFFER_DATA (buffer); + duration = GST_BUFFER_DURATION (buffer); + timestamp = GST_BUFFER_TIMESTAMP (buffer); + + GST_DEBUG_OBJECT (rtptheorapay, "size %u, duration %" GST_TIME_FORMAT, + size, GST_TIME_ARGS (duration)); + + if (G_UNLIKELY (size < 1 || size > 0xffff)) + goto wrong_size; + + /* find packet type */ + if (data[0] & 0x80) { + /* header */ + if (data[0] == 0x80) { + /* identification, we need to parse this in order to get the clock rate. */ + if (G_UNLIKELY (!gst_rtp_theora_pay_parse_id (basepayload, data, size))) + goto parse_id_failed; + TDT = 1; + } else if (data[0] == 0x81) { + /* comment */ + TDT = 2; + } else if (data[0] == 0x82) { + /* setup */ + TDT = 1; + } else + goto unknown_header; + } else + /* data */ + TDT = 0; + + if (rtptheorapay->need_headers) { + /* we need to collect the headers and construct a config string from them */ + if (TDT == 2) { + GST_DEBUG_OBJECT (rtptheorapay, + "discard comment packet while collecting headers"); + ret = GST_FLOW_OK; + goto done; + } else if (TDT != 0) { + GST_DEBUG_OBJECT (rtptheorapay, "collecting header"); + /* append header to the list of headers */ + rtptheorapay->headers = g_list_append (rtptheorapay->headers, buffer); + ret = GST_FLOW_OK; + goto done; + } else { + if (!gst_rtp_theora_pay_finish_headers (basepayload)) + goto header_error; + rtptheorapay->need_headers = FALSE; + } + } + + /* size increases with packet length and 2 bytes size eader. */ + newduration = rtptheorapay->payload_duration; + if (duration != GST_CLOCK_TIME_NONE) + newduration += duration; + + newsize = rtptheorapay->payload_pos + 2 + size; + packet_len = gst_rtp_buffer_calc_packet_len (newsize, 0, 0); + + /* check buffer filled against length and max latency */ + flush = gst_basertppayload_is_filled (basepayload, packet_len, newduration); + /* we can store up to 15 theora packets in one RTP packet. */ + flush |= (rtptheorapay->payload_pkts == 15); + /* flush if we have a new TDT */ + if (rtptheorapay->packet) + flush |= (rtptheorapay->payload_TDT != TDT); + if (flush) + ret = gst_rtp_theora_pay_flush_packet (rtptheorapay); + + /* create new packet if we must */ + if (!rtptheorapay->packet) { + gst_rtp_theora_pay_init_packet (rtptheorapay, TDT, timestamp); + } + + payload = gst_rtp_buffer_get_payload (rtptheorapay->packet); + ppos = payload + rtptheorapay->payload_pos; + fragmented = FALSE; + + ret = GST_FLOW_OK; + + /* put buffer in packet, it either fits completely or needs to be fragmented + * over multiple RTP packets. */ + while (size) { + plen = MIN (rtptheorapay->payload_left - 2, size); + + GST_DEBUG_OBJECT (rtptheorapay, "append %u bytes", plen); + + /* data is copied in the payload with a 2 byte length header */ + ppos[0] = (plen >> 8) & 0xff; + ppos[1] = (plen & 0xff); + memcpy (&ppos[2], data, plen); + + size -= plen; + data += plen; + + rtptheorapay->payload_pos += plen + 2; + rtptheorapay->payload_left -= plen + 2; + + if (fragmented) { + if (size == 0) + /* last fragment, set F to 0x3. */ + rtptheorapay->payload_F = 0x3; + else + /* fragment continues, set F to 0x2. */ + rtptheorapay->payload_F = 0x2; + } else { + if (size > 0) { + /* fragmented packet starts, set F to 0x1, mark ourselves as + * fragmented. */ + rtptheorapay->payload_F = 0x1; + fragmented = TRUE; + } + } + if (fragmented) { + /* fragmented packets are always flushed and have ptks of 0 */ + rtptheorapay->payload_pkts = 0; + ret = gst_rtp_theora_pay_flush_packet (rtptheorapay); + + if (size > 0) { + /* start new packet and get pointers. TDT stays the same. */ + gst_rtp_theora_pay_init_packet (rtptheorapay, + rtptheorapay->payload_TDT, timestamp); + payload = gst_rtp_buffer_get_payload (rtptheorapay->packet); + ppos = payload + rtptheorapay->payload_pos; + } + } else { + /* unfragmented packet, update stats for next packet, size == 0 and we + * exit the while loop */ + rtptheorapay->payload_pkts++; + if (duration != GST_CLOCK_TIME_NONE) + rtptheorapay->payload_duration += duration; + } + } +done: + return ret; + + /* ERRORS */ +wrong_size: + { + GST_ELEMENT_WARNING (rtptheorapay, STREAM, DECODE, + ("Invalid packet size (1 < %d <= 0xffff)", size), (NULL)); + return GST_FLOW_OK; + } +parse_id_failed: + { + return GST_FLOW_ERROR; + } +unknown_header: + { + GST_ELEMENT_WARNING (rtptheorapay, STREAM, DECODE, + (NULL), ("Ignoring unknown header received")); + return GST_FLOW_OK; + } +header_error: + { + GST_ELEMENT_WARNING (rtptheorapay, STREAM, DECODE, + (NULL), ("Error initializing header config")); + return GST_FLOW_OK; + } +} + +gboolean +gst_rtp_theora_pay_plugin_init (GstPlugin * plugin) +{ + return gst_element_register (plugin, "rtptheorapay", + GST_RANK_NONE, GST_TYPE_RTP_THEORA_PAY); +} diff --git a/gst/rtp/gstrtptheorapay.h b/gst/rtp/gstrtptheorapay.h new file mode 100644 index 00000000..95076d3f --- /dev/null +++ b/gst/rtp/gstrtptheorapay.h @@ -0,0 +1,75 @@ +/* GStreamer + * Copyright (C) <2006> Wim Taymans <wim@fluendo.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_THEORA_PAY_H__ +#define __GST_RTP_THEORA_PAY_H__ + +#include <gst/gst.h> +#include <gst/rtp/gstbasertppayload.h> +#include <gst/base/gstadapter.h> + +G_BEGIN_DECLS + +#define GST_TYPE_RTP_THEORA_PAY \ + (gst_rtp_theora_pay_get_type()) +#define GST_RTP_THEORA_PAY(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_RTP_THEORA_PAY,GstRtpTheoraPay)) +#define GST_RTP_THEORA_PAY_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_RTP_THEORA_PAY,GstRtpTheoraPayClass)) +#define GST_IS_RTP_THEORA_PAY(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_RTP_THEORA_PAY)) +#define GST_IS_RTP_THEORA_PAY_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_RTP_THEORA_PAY)) + +typedef struct _GstRtpTheoraPay GstRtpTheoraPay; +typedef struct _GstRtpTheoraPayClass GstRtpTheoraPayClass; + +struct _GstRtpTheoraPay +{ + GstBaseRTPPayload payload; + + /* the headers */ + gboolean need_headers; + GList *headers; + + /* queues of buffers along with some stats. */ + GstBuffer *packet; + guint payload_pos; + guint payload_left; + guint32 payload_ident; + guint8 payload_F; + guint8 payload_TDT; + guint payload_pkts; + GstClockTime payload_timestamp; + GstClockTime payload_duration; + + gint width; + gint height; +}; + +struct _GstRtpTheoraPayClass +{ + GstBaseRTPPayloadClass parent_class; +}; + +gboolean gst_rtp_theora_pay_plugin_init (GstPlugin * plugin); + +G_END_DECLS + +#endif /* __GST_RTP_THEORA_PAY_H__ */ |