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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
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 "clock-rate = (int) 90000, " "encoding-name = (string) \"X-GST\"")
47 #define gst_rtp_gst_depay_parent_class parent_class
48 G_DEFINE_TYPE (GstRtpGSTDepay, gst_rtp_gst_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
50 static void gst_rtp_gst_depay_finalize (GObject * object);
52 static GstStateChangeReturn gst_rtp_gst_depay_change_state (GstElement *
53 element, GstStateChange transition);
55 static void gst_rtp_gst_depay_reset (GstRtpGSTDepay * rtpgstdepay);
56 static gboolean gst_rtp_gst_depay_setcaps (GstRTPBaseDepayload * depayload,
58 static GstBuffer *gst_rtp_gst_depay_process (GstRTPBaseDepayload * depayload,
62 gst_rtp_gst_depay_class_init (GstRtpGSTDepayClass * klass)
64 GObjectClass *gobject_class;
65 GstElementClass *gstelement_class;
66 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
68 GST_DEBUG_CATEGORY_INIT (rtpgstdepay_debug, "rtpgstdepay", 0,
69 "Gstreamer RTP Depayloader");
71 gobject_class = (GObjectClass *) klass;
72 gstelement_class = (GstElementClass *) klass;
73 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
75 gobject_class->finalize = gst_rtp_gst_depay_finalize;
77 gstelement_class->change_state = gst_rtp_gst_depay_change_state;
79 gst_element_class_add_pad_template (gstelement_class,
80 gst_static_pad_template_get (&gst_rtp_gst_depay_src_template));
81 gst_element_class_add_pad_template (gstelement_class,
82 gst_static_pad_template_get (&gst_rtp_gst_depay_sink_template));
84 gst_element_class_set_static_metadata (gstelement_class,
85 "GStreamer depayloader", "Codec/Depayloader/Network",
86 "Extracts GStreamer buffers from RTP packets",
87 "Wim Taymans <wim.taymans@gmail.com>");
89 gstrtpbasedepayload_class->set_caps = gst_rtp_gst_depay_setcaps;
90 gstrtpbasedepayload_class->process = gst_rtp_gst_depay_process;
94 gst_rtp_gst_depay_init (GstRtpGSTDepay * rtpgstdepay)
96 rtpgstdepay->adapter = gst_adapter_new ();
100 gst_rtp_gst_depay_finalize (GObject * object)
102 GstRtpGSTDepay *rtpgstdepay;
104 rtpgstdepay = GST_RTP_GST_DEPAY (object);
106 gst_rtp_gst_depay_reset (rtpgstdepay);
107 g_object_unref (rtpgstdepay->adapter);
109 G_OBJECT_CLASS (parent_class)->finalize (object);
113 store_cache (GstRtpGSTDepay * rtpgstdepay, guint CV, GstCaps * caps)
115 if (rtpgstdepay->CV_cache[CV])
116 gst_caps_unref (rtpgstdepay->CV_cache[CV]);
117 rtpgstdepay->CV_cache[CV] = caps;
121 gst_rtp_gst_depay_reset (GstRtpGSTDepay * rtpgstdepay)
125 gst_adapter_clear (rtpgstdepay->adapter);
126 rtpgstdepay->current_CV = 0;
127 for (i = 0; i < 8; i++)
128 store_cache (rtpgstdepay, i, NULL);
132 gst_rtp_gst_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
134 GstRtpGSTDepay *rtpgstdepay;
135 GstStructure *structure;
138 const gchar *capsenc;
140 rtpgstdepay = GST_RTP_GST_DEPAY (depayload);
142 structure = gst_caps_get_structure (caps, 0);
144 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
146 depayload->clock_rate = clock_rate;
148 capsenc = gst_structure_get_string (structure, "caps");
153 const gchar *capsver;
157 capsstr = (gchar *) g_base64_decode (capsenc, &out_len);
158 outcaps = gst_caps_from_string (capsstr);
162 capsver = gst_structure_get_string (structure, "capsversion");
166 /* no version, assume 0 */
170 rtpgstdepay->current_CV = CV;
171 gst_caps_ref (outcaps);
172 store_cache (rtpgstdepay, CV, outcaps);
174 res = gst_pad_set_caps (depayload->srcpad, outcaps);
175 gst_caps_unref (outcaps);
177 GST_WARNING_OBJECT (depayload, "no caps given");
178 rtpgstdepay->current_CV = -1;
186 read_length (GstRtpGSTDepay * rtpgstdepay, guint8 * data, guint size,
187 guint * length, guint * skip)
189 guint b, len, offset;
191 /* start reading the length, we need this to skip to the data later */
197 len = (len << 7) | (b & 0x7f);
200 /* check remaining buffer size */
201 if (size - offset < len)
211 read_caps (GstRtpGSTDepay * rtpgstdepay, GstBuffer * buf, guint * skip)
213 guint offset, length;
217 gst_buffer_map (buf, &map, GST_MAP_READ);
219 GST_DEBUG_OBJECT (rtpgstdepay, "buffer size %" G_GSIZE_FORMAT, map.size);
221 if (!read_length (rtpgstdepay, map.data, map.size, &length, &offset))
224 GST_DEBUG_OBJECT (rtpgstdepay, "parsing caps %s", &map.data[offset]);
226 /* parse and store in cache */
227 caps = gst_caps_from_string ((gchar *) & map.data[offset]);
228 gst_buffer_unmap (buf, &map);
230 *skip = length + offset;
236 GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
237 ("Buffer too small."), (NULL));
238 gst_buffer_unmap (buf, &map);
244 read_event (GstRtpGSTDepay * rtpgstdepay, guint type,
245 GstBuffer * buf, guint * skip)
247 guint offset, length;
254 gst_buffer_map (buf, &map, GST_MAP_READ);
256 GST_DEBUG_OBJECT (rtpgstdepay, "buffer size %" G_GSIZE_FORMAT, map.size);
258 if (!read_length (rtpgstdepay, map.data, map.size, &length, &offset))
261 GST_DEBUG_OBJECT (rtpgstdepay, "parsing event %s", &map.data[offset]);
264 s = gst_structure_from_string ((gchar *) & map.data[offset], &end);
265 gst_buffer_unmap (buf, &map);
269 etype = GST_EVENT_TAG;
272 etype = GST_EVENT_CUSTOM_DOWNSTREAM;
275 etype = GST_EVENT_CUSTOM_BOTH;
280 event = gst_event_new_custom (etype, s);
282 *skip = length + offset;
288 GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
289 ("Buffer too small."), (NULL));
290 gst_buffer_unmap (buf, &map);
295 GST_DEBUG_OBJECT (rtpgstdepay, "unknown event type");
296 gst_structure_free (s);
302 gst_rtp_gst_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
304 GstRtpGSTDepay *rtpgstdepay;
305 GstBuffer *subbuf, *outbuf = NULL;
308 guint CV, frag_offset, avail, offset;
309 GstRTPBuffer rtp = { NULL };
311 rtpgstdepay = GST_RTP_GST_DEPAY (depayload);
313 gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
315 payload_len = gst_rtp_buffer_get_payload_len (&rtp);
317 if (payload_len <= 8)
320 if (GST_BUFFER_IS_DISCONT (buf)) {
321 GST_WARNING_OBJECT (rtpgstdepay, "DISCONT, clear adapter");
322 gst_adapter_clear (rtpgstdepay->adapter);
325 payload = gst_rtp_buffer_get_payload (&rtp);
330 * 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
331 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
332 * |C| CV |D|0|0|0| ETYPE | MBZ |
333 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
335 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
338 (payload[4] << 24) | (payload[5] << 16) | (payload[6] << 8) | payload[7];
340 avail = gst_adapter_available (rtpgstdepay->adapter);
341 if (avail != frag_offset)
344 /* subbuffer skipping the 8 header bytes */
345 subbuf = gst_rtp_buffer_get_payload_subbuffer (&rtp, 8, -1);
346 gst_adapter_push (rtpgstdepay->adapter, subbuf);
349 if (gst_rtp_buffer_get_marker (&rtp)) {
353 /* take the buffer */
354 avail = gst_adapter_available (rtpgstdepay->adapter);
355 outbuf = gst_adapter_take_buffer (rtpgstdepay->adapter, avail);
357 CV = (payload[0] >> 4) & 0x7;
359 if (payload[0] & 0x80) {
362 /* C bit, we have inline caps */
363 outcaps = read_caps (rtpgstdepay, outbuf, &size);
367 GST_DEBUG_OBJECT (rtpgstdepay,
368 "inline caps %u, length %u, %" GST_PTR_FORMAT, CV, size, outcaps);
370 store_cache (rtpgstdepay, CV, outcaps);
380 /* we have an event */
381 event = read_event (rtpgstdepay, payload[1], outbuf, &size);
385 GST_DEBUG_OBJECT (rtpgstdepay,
386 "inline event, length %u, %" GST_PTR_FORMAT, size, event);
388 gst_pad_push_event (depayload->srcpad, event);
390 /* no buffer after event */
398 GST_DEBUG_OBJECT (rtpgstdepay, "sub buffer: offset %u, size %u", offset,
402 gst_buffer_copy_region (outbuf, GST_BUFFER_COPY_ALL, offset, avail);
404 gst_buffer_unref (outbuf);
408 /* see what caps we need */
409 if (CV != rtpgstdepay->current_CV) {
410 /* we need to switch caps, check if we have the caps */
411 if ((outcaps = rtpgstdepay->CV_cache[CV]) == NULL)
414 GST_DEBUG_OBJECT (rtpgstdepay,
415 "need caps switch from %u to %u, %" GST_PTR_FORMAT,
416 rtpgstdepay->current_CV, CV, outcaps);
419 if (gst_pad_set_caps (depayload->srcpad, outcaps))
420 rtpgstdepay->current_CV = CV;
423 if (payload[0] & 0x8)
424 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
426 gst_buffer_unref (outbuf);
430 gst_rtp_buffer_unmap (&rtp);
437 GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
438 ("Empty Payload."), (NULL));
439 gst_rtp_buffer_unmap (&rtp);
444 gst_adapter_clear (rtpgstdepay->adapter);
445 gst_rtp_buffer_unmap (&rtp);
446 GST_LOG_OBJECT (rtpgstdepay, "wrong fragment, skipping");
451 GST_WARNING_OBJECT (rtpgstdepay, "failed to parse caps");
452 gst_buffer_unref (outbuf);
453 gst_rtp_buffer_unmap (&rtp);
458 GST_WARNING_OBJECT (rtpgstdepay, "failed to parse event");
459 gst_buffer_unref (outbuf);
460 gst_rtp_buffer_unmap (&rtp);
465 GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
466 ("Missing caps %u.", CV), (NULL));
467 gst_buffer_unref (outbuf);
468 gst_rtp_buffer_unmap (&rtp);
473 static GstStateChangeReturn
474 gst_rtp_gst_depay_change_state (GstElement * element, GstStateChange transition)
476 GstRtpGSTDepay *rtpgstdepay;
477 GstStateChangeReturn ret;
479 rtpgstdepay = GST_RTP_GST_DEPAY (element);
481 switch (transition) {
482 case GST_STATE_CHANGE_READY_TO_PAUSED:
483 gst_rtp_gst_depay_reset (rtpgstdepay);
489 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
491 switch (transition) {
492 case GST_STATE_CHANGE_PAUSED_TO_READY:
493 gst_rtp_gst_depay_reset (rtpgstdepay);
503 gst_rtp_gst_depay_plugin_init (GstPlugin * plugin)
505 return gst_element_register (plugin, "rtpgstdepay",
506 GST_RANK_MARGINAL, GST_TYPE_RTP_GST_DEPAY);