2 * Copyright (C) <2010> Wim Taymans <wim.taymans@gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
24 #include <gst/rtp/gstrtpbuffer.h>
27 #include "gstrtpgstdepay.h"
29 GST_DEBUG_CATEGORY_STATIC (rtpgstdepay_debug);
30 #define GST_CAT_DEFAULT (rtpgstdepay_debug)
32 static GstStaticPadTemplate gst_rtp_gst_depay_src_template =
33 GST_STATIC_PAD_TEMPLATE ("src",
38 static GstStaticPadTemplate gst_rtp_gst_depay_sink_template =
39 GST_STATIC_PAD_TEMPLATE ("sink",
42 GST_STATIC_CAPS ("application/x-rtp, "
43 "media = (string) \"application\", "
44 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
45 "clock-rate = (int) 90000, " "encoding-name = (string) \"X-GST\"")
48 #define gst_rtp_gst_depay_parent_class parent_class
49 G_DEFINE_TYPE (GstRtpGSTDepay, gst_rtp_gst_depay, GST_TYPE_BASE_RTP_DEPAYLOAD);
51 static void gst_rtp_gst_depay_finalize (GObject * object);
53 static GstStateChangeReturn gst_rtp_gst_depay_change_state (GstElement *
54 element, GstStateChange transition);
56 static void gst_rtp_gst_depay_reset (GstRtpGSTDepay * rtpgstdepay);
57 static gboolean gst_rtp_gst_depay_setcaps (GstBaseRTPDepayload * depayload,
59 static GstBuffer *gst_rtp_gst_depay_process (GstBaseRTPDepayload * depayload,
63 gst_rtp_gst_depay_class_init (GstRtpGSTDepayClass * klass)
65 GObjectClass *gobject_class;
66 GstElementClass *gstelement_class;
67 GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
69 GST_DEBUG_CATEGORY_INIT (rtpgstdepay_debug, "rtpgstdepay", 0,
70 "Gstreamer RTP Depayloader");
72 gobject_class = (GObjectClass *) klass;
73 gstelement_class = (GstElementClass *) klass;
74 gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
76 gobject_class->finalize = gst_rtp_gst_depay_finalize;
78 gstelement_class->change_state = gst_rtp_gst_depay_change_state;
80 gst_element_class_add_pad_template (gstelement_class,
81 gst_static_pad_template_get (&gst_rtp_gst_depay_src_template));
82 gst_element_class_add_pad_template (gstelement_class,
83 gst_static_pad_template_get (&gst_rtp_gst_depay_sink_template));
85 gst_element_class_set_details_simple (gstelement_class,
86 "GStreamer depayloader", "Codec/Depayloader/Network",
87 "Extracts GStreamer buffers from RTP packets",
88 "Wim Taymans <wim.taymans@gmail.com>");
90 gstbasertpdepayload_class->set_caps = gst_rtp_gst_depay_setcaps;
91 gstbasertpdepayload_class->process = gst_rtp_gst_depay_process;
95 gst_rtp_gst_depay_init (GstRtpGSTDepay * rtpgstdepay)
97 rtpgstdepay->adapter = gst_adapter_new ();
101 gst_rtp_gst_depay_finalize (GObject * object)
103 GstRtpGSTDepay *rtpgstdepay;
105 rtpgstdepay = GST_RTP_GST_DEPAY (object);
107 gst_rtp_gst_depay_reset (rtpgstdepay);
108 g_object_unref (rtpgstdepay->adapter);
110 G_OBJECT_CLASS (parent_class)->finalize (object);
114 store_cache (GstRtpGSTDepay * rtpgstdepay, guint CV, GstCaps * caps)
116 if (rtpgstdepay->CV_cache[CV])
117 gst_caps_unref (rtpgstdepay->CV_cache[CV]);
118 rtpgstdepay->CV_cache[CV] = caps;
122 gst_rtp_gst_depay_reset (GstRtpGSTDepay * rtpgstdepay)
126 gst_adapter_clear (rtpgstdepay->adapter);
127 rtpgstdepay->current_CV = 0;
128 for (i = 0; i < 8; i++)
129 store_cache (rtpgstdepay, i, NULL);
133 gst_rtp_gst_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
135 GstRtpGSTDepay *rtpgstdepay;
136 GstStructure *structure;
140 const gchar *capsenc;
143 rtpgstdepay = GST_RTP_GST_DEPAY (depayload);
145 structure = gst_caps_get_structure (caps, 0);
147 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
149 depayload->clock_rate = clock_rate;
151 capsenc = gst_structure_get_string (structure, "caps");
155 capsstr = (gchar *) g_base64_decode (capsenc, &out_len);
156 outcaps = gst_caps_from_string (capsstr);
159 /* we have the SDP caps as output caps */
160 rtpgstdepay->current_CV = 0;
161 gst_caps_ref (outcaps);
162 store_cache (rtpgstdepay, 0, outcaps);
164 outcaps = gst_caps_new_any ();
166 res = gst_pad_set_caps (depayload->srcpad, outcaps);
167 gst_caps_unref (outcaps);
173 gst_rtp_gst_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
175 GstRtpGSTDepay *rtpgstdepay;
176 GstBuffer *subbuf, *outbuf = NULL;
180 GstRTPBuffer rtp = { NULL };
182 rtpgstdepay = GST_RTP_GST_DEPAY (depayload);
184 gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
186 payload_len = gst_rtp_buffer_get_payload_len (&rtp);
188 if (payload_len <= 8)
191 if (GST_BUFFER_IS_DISCONT (buf)) {
192 GST_WARNING_OBJECT (rtpgstdepay, "DISCONT, clear adapter");
193 gst_adapter_clear (rtpgstdepay->adapter);
196 payload = gst_rtp_buffer_get_payload (&rtp);
201 * 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
202 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
203 * |C| CV |D|0|0|0| MBZ |
204 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
206 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
209 * (payload[4] << 24) | (payload[5] << 16) | (payload[6] << 8) | payload[7];
212 /* subbuffer skipping the 8 header bytes */
213 subbuf = gst_rtp_buffer_get_payload_subbuffer (&rtp, 8, -1);
214 gst_adapter_push (rtpgstdepay->adapter, subbuf);
216 if (gst_rtp_buffer_get_marker (&rtp)) {
220 /* take the buffer */
221 avail = gst_adapter_available (rtpgstdepay->adapter);
222 outbuf = gst_adapter_take_buffer (rtpgstdepay->adapter, avail);
224 CV = (payload[0] >> 4) & 0x7;
226 if (payload[0] & 0x80) {
227 guint b, csize, left, offset;
232 /* C bit, we have inline caps */
233 data = gst_buffer_map (outbuf, &size, NULL, GST_MAP_READ);
235 /* start reading the length, we need this to skip to the data later */
239 if (offset >= left) {
240 gst_buffer_unmap (outbuf, data, size);
244 csize = (csize << 7) | (b & 0x7f);
248 gst_buffer_unmap (outbuf, data, size);
252 /* parse and store in cache */
253 outcaps = gst_caps_from_string ((gchar *) & data[offset]);
254 store_cache (rtpgstdepay, CV, outcaps);
260 GST_DEBUG_OBJECT (rtpgstdepay,
261 "inline caps %u, length %u, %" GST_PTR_FORMAT, CV, csize, outcaps);
263 /* create real data buffer when needed */
266 gst_buffer_copy_region (outbuf, GST_BUFFER_COPY_ALL, offset, left);
270 gst_buffer_unmap (outbuf, data, size);
271 gst_buffer_unref (outbuf);
275 /* see what caps we need */
276 if (CV != rtpgstdepay->current_CV) {
277 /* we need to switch caps, check if we have the caps */
278 if ((outcaps = rtpgstdepay->CV_cache[CV]) == NULL)
281 GST_DEBUG_OBJECT (rtpgstdepay,
282 "need caps switch from %u to %u, %" GST_PTR_FORMAT,
283 rtpgstdepay->current_CV, CV, outcaps);
286 if (gst_pad_set_caps (depayload->srcpad, outcaps))
287 rtpgstdepay->current_CV = CV;
291 if (payload[0] & 0x8)
292 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
300 GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
301 ("Empty Payload."), (NULL));
302 gst_rtp_buffer_unmap (&rtp);
307 GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
308 ("Buffer too small."), (NULL));
310 gst_buffer_unref (outbuf);
311 gst_rtp_buffer_unmap (&rtp);
316 GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
317 ("Missing caps %u.", CV), (NULL));
319 gst_buffer_unref (outbuf);
320 gst_rtp_buffer_unmap (&rtp);
325 static GstStateChangeReturn
326 gst_rtp_gst_depay_change_state (GstElement * element, GstStateChange transition)
328 GstRtpGSTDepay *rtpgstdepay;
329 GstStateChangeReturn ret;
331 rtpgstdepay = GST_RTP_GST_DEPAY (element);
333 switch (transition) {
334 case GST_STATE_CHANGE_READY_TO_PAUSED:
335 gst_rtp_gst_depay_reset (rtpgstdepay);
341 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
343 switch (transition) {
344 case GST_STATE_CHANGE_PAUSED_TO_READY:
345 gst_rtp_gst_depay_reset (rtpgstdepay);
355 gst_rtp_gst_depay_plugin_init (GstPlugin * plugin)
357 return gst_element_register (plugin, "rtpgstdepay",
358 GST_RANK_MARGINAL, GST_TYPE_RTP_GST_DEPAY);