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 gboolean gst_rtp_gst_depay_handle_event (GstRTPBaseDepayload * depay,
54 static GstStateChangeReturn gst_rtp_gst_depay_change_state (GstElement *
55 element, GstStateChange transition);
57 static void gst_rtp_gst_depay_reset (GstRtpGSTDepay * rtpgstdepay, gboolean
59 static gboolean gst_rtp_gst_depay_setcaps (GstRTPBaseDepayload * depayload,
61 static GstBuffer *gst_rtp_gst_depay_process (GstRTPBaseDepayload * depayload,
65 gst_rtp_gst_depay_class_init (GstRtpGSTDepayClass * klass)
67 GObjectClass *gobject_class;
68 GstElementClass *gstelement_class;
69 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
71 GST_DEBUG_CATEGORY_INIT (rtpgstdepay_debug, "rtpgstdepay", 0,
72 "Gstreamer RTP Depayloader");
74 gobject_class = (GObjectClass *) klass;
75 gstelement_class = (GstElementClass *) klass;
76 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
78 gobject_class->finalize = gst_rtp_gst_depay_finalize;
80 gstelement_class->change_state = gst_rtp_gst_depay_change_state;
82 gst_element_class_add_pad_template (gstelement_class,
83 gst_static_pad_template_get (&gst_rtp_gst_depay_src_template));
84 gst_element_class_add_pad_template (gstelement_class,
85 gst_static_pad_template_get (&gst_rtp_gst_depay_sink_template));
87 gst_element_class_set_static_metadata (gstelement_class,
88 "GStreamer depayloader", "Codec/Depayloader/Network",
89 "Extracts GStreamer buffers from RTP packets",
90 "Wim Taymans <wim.taymans@gmail.com>");
92 gstrtpbasedepayload_class->handle_event = gst_rtp_gst_depay_handle_event;
93 gstrtpbasedepayload_class->set_caps = gst_rtp_gst_depay_setcaps;
94 gstrtpbasedepayload_class->process = gst_rtp_gst_depay_process;
98 gst_rtp_gst_depay_init (GstRtpGSTDepay * rtpgstdepay)
100 rtpgstdepay->adapter = gst_adapter_new ();
104 gst_rtp_gst_depay_finalize (GObject * object)
106 GstRtpGSTDepay *rtpgstdepay;
108 rtpgstdepay = GST_RTP_GST_DEPAY (object);
110 gst_rtp_gst_depay_reset (rtpgstdepay, TRUE);
111 g_object_unref (rtpgstdepay->adapter);
113 G_OBJECT_CLASS (parent_class)->finalize (object);
117 store_cache (GstRtpGSTDepay * rtpgstdepay, guint CV, GstCaps * caps)
119 if (rtpgstdepay->CV_cache[CV])
120 gst_caps_unref (rtpgstdepay->CV_cache[CV]);
121 rtpgstdepay->CV_cache[CV] = caps;
125 gst_rtp_gst_depay_reset (GstRtpGSTDepay * rtpgstdepay, gboolean full)
129 gst_adapter_clear (rtpgstdepay->adapter);
131 rtpgstdepay->current_CV = 0;
132 for (i = 0; i < 8; i++)
133 store_cache (rtpgstdepay, i, NULL);
134 g_free (rtpgstdepay->stream_id);
135 rtpgstdepay->stream_id = NULL;
136 if (rtpgstdepay->tags)
137 gst_tag_list_unref (rtpgstdepay->tags);
138 rtpgstdepay->tags = NULL;
143 gst_rtp_gst_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
145 GstRtpGSTDepay *rtpgstdepay;
146 GstStructure *structure;
149 const gchar *capsenc;
151 rtpgstdepay = GST_RTP_GST_DEPAY (depayload);
153 structure = gst_caps_get_structure (caps, 0);
155 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
157 depayload->clock_rate = clock_rate;
159 capsenc = gst_structure_get_string (structure, "caps");
164 const gchar *capsver;
168 capsstr = (gchar *) g_base64_decode (capsenc, &out_len);
169 outcaps = gst_caps_from_string (capsstr);
173 capsver = gst_structure_get_string (structure, "capsversion");
177 /* no version, assume 0 */
181 rtpgstdepay->current_CV = CV;
182 gst_caps_ref (outcaps);
183 store_cache (rtpgstdepay, CV, outcaps);
185 res = gst_pad_set_caps (depayload->srcpad, outcaps);
186 gst_caps_unref (outcaps);
188 GST_WARNING_OBJECT (depayload, "no caps given");
189 rtpgstdepay->current_CV = -1;
197 read_length (GstRtpGSTDepay * rtpgstdepay, guint8 * data, guint size,
198 guint * length, guint * skip)
200 guint b, len, offset;
202 /* start reading the length, we need this to skip to the data later */
208 len = (len << 7) | (b & 0x7f);
211 /* check remaining buffer size */
212 if (size - offset < len)
222 read_caps (GstRtpGSTDepay * rtpgstdepay, GstBuffer * buf, guint * skip)
224 guint offset, length;
228 gst_buffer_map (buf, &map, GST_MAP_READ);
230 GST_DEBUG_OBJECT (rtpgstdepay, "buffer size %" G_GSIZE_FORMAT, map.size);
232 if (!read_length (rtpgstdepay, map.data, map.size, &length, &offset))
235 GST_DEBUG_OBJECT (rtpgstdepay, "parsing caps %s", &map.data[offset]);
237 /* parse and store in cache */
238 caps = gst_caps_from_string ((gchar *) & map.data[offset]);
239 gst_buffer_unmap (buf, &map);
241 *skip = length + offset;
247 GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
248 ("Buffer too small."), (NULL));
249 gst_buffer_unmap (buf, &map);
255 read_event (GstRtpGSTDepay * rtpgstdepay, guint type,
256 GstBuffer * buf, guint * skip)
258 guint offset, length;
265 gst_buffer_map (buf, &map, GST_MAP_READ);
267 GST_DEBUG_OBJECT (rtpgstdepay, "buffer size %" G_GSIZE_FORMAT, map.size);
269 if (!read_length (rtpgstdepay, map.data, map.size, &length, &offset))
272 GST_DEBUG_OBJECT (rtpgstdepay, "parsing event %s", &map.data[offset]);
275 s = gst_structure_from_string ((gchar *) & map.data[offset], &end);
276 gst_buffer_unmap (buf, &map);
283 etype = GST_EVENT_TAG;
286 etype = GST_EVENT_CUSTOM_DOWNSTREAM;
289 etype = GST_EVENT_CUSTOM_BOTH;
292 etype = GST_EVENT_STREAM_START;
297 event = gst_event_new_custom (etype, s);
299 *skip = length + offset;
305 GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
306 ("Buffer too small."), (NULL));
307 gst_buffer_unmap (buf, &map);
312 GST_WARNING_OBJECT (rtpgstdepay, "could not parse event");
317 GST_DEBUG_OBJECT (rtpgstdepay, "unknown event type");
318 gst_structure_free (s);
324 store_event (GstRtpGSTDepay * rtpgstdepay, GstEvent * event)
326 gboolean do_push = FALSE;
328 switch (GST_EVENT_TYPE (event)) {
331 GstTagList *old, *tags;
333 gst_event_parse_tag (event, &tags);
335 old = rtpgstdepay->tags;
336 if (!old || !gst_tag_list_is_equal (old, tags)) {
339 gst_tag_list_unref (old);
340 rtpgstdepay->tags = gst_tag_list_ref (tags);
344 case GST_EVENT_CUSTOM_DOWNSTREAM:
345 case GST_EVENT_CUSTOM_BOTH:
346 /* always push custom events */
349 case GST_EVENT_STREAM_START:
352 const gchar *stream_id = NULL;
354 gst_event_parse_stream_start (event, &stream_id);
356 old = rtpgstdepay->stream_id;
357 if (!old || g_strcmp0 (old, stream_id)) {
360 rtpgstdepay->stream_id = g_strdup (stream_id);
365 /* unknown event, don't push */
369 gst_pad_push_event (GST_RTP_BASE_DEPAYLOAD (rtpgstdepay)->srcpad, event);
371 gst_event_unref (event);
375 gst_rtp_gst_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
377 GstRtpGSTDepay *rtpgstdepay;
378 GstBuffer *subbuf, *outbuf = NULL;
381 guint CV, frag_offset, avail, offset;
382 GstRTPBuffer rtp = { NULL };
384 rtpgstdepay = GST_RTP_GST_DEPAY (depayload);
386 gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
388 payload_len = gst_rtp_buffer_get_payload_len (&rtp);
390 if (payload_len <= 8)
393 if (GST_BUFFER_IS_DISCONT (buf)) {
394 GST_WARNING_OBJECT (rtpgstdepay, "DISCONT, clear adapter");
395 gst_adapter_clear (rtpgstdepay->adapter);
398 payload = gst_rtp_buffer_get_payload (&rtp);
403 * 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
404 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
405 * |C| CV |D|0|0|0| ETYPE | MBZ |
406 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
408 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
411 (payload[4] << 24) | (payload[5] << 16) | (payload[6] << 8) | payload[7];
413 avail = gst_adapter_available (rtpgstdepay->adapter);
414 if (avail != frag_offset)
417 /* subbuffer skipping the 8 header bytes */
418 subbuf = gst_rtp_buffer_get_payload_subbuffer (&rtp, 8, -1);
419 gst_adapter_push (rtpgstdepay->adapter, subbuf);
422 if (gst_rtp_buffer_get_marker (&rtp)) {
426 /* take the buffer */
427 avail = gst_adapter_available (rtpgstdepay->adapter);
428 outbuf = gst_adapter_take_buffer (rtpgstdepay->adapter, avail);
430 CV = (payload[0] >> 4) & 0x7;
432 if (payload[0] & 0x80) {
435 /* C bit, we have inline caps */
436 outcaps = read_caps (rtpgstdepay, outbuf, &size);
440 GST_DEBUG_OBJECT (rtpgstdepay,
441 "inline caps %u, length %u, %" GST_PTR_FORMAT, CV, size, outcaps);
443 store_cache (rtpgstdepay, CV, outcaps);
453 /* we have an event */
454 event = read_event (rtpgstdepay, payload[1], outbuf, &size);
458 GST_DEBUG_OBJECT (rtpgstdepay,
459 "inline event, length %u, %" GST_PTR_FORMAT, size, event);
461 store_event (rtpgstdepay, event);
463 /* no buffer after event */
471 GST_DEBUG_OBJECT (rtpgstdepay, "sub buffer: offset %u, size %u", offset,
475 gst_buffer_copy_region (outbuf, GST_BUFFER_COPY_ALL, offset, avail);
477 gst_buffer_unref (outbuf);
481 /* see what caps we need */
482 if (CV != rtpgstdepay->current_CV) {
483 /* we need to switch caps, check if we have the caps */
484 if ((outcaps = rtpgstdepay->CV_cache[CV]) == NULL)
487 GST_DEBUG_OBJECT (rtpgstdepay,
488 "need caps switch from %u to %u, %" GST_PTR_FORMAT,
489 rtpgstdepay->current_CV, CV, outcaps);
492 if (gst_pad_set_caps (depayload->srcpad, outcaps))
493 rtpgstdepay->current_CV = CV;
496 if (payload[0] & 0x8)
497 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
499 gst_buffer_unref (outbuf);
503 gst_rtp_buffer_unmap (&rtp);
510 GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
511 ("Empty Payload."), (NULL));
512 gst_rtp_buffer_unmap (&rtp);
517 gst_adapter_clear (rtpgstdepay->adapter);
518 gst_rtp_buffer_unmap (&rtp);
519 GST_LOG_OBJECT (rtpgstdepay, "wrong fragment, skipping");
524 GST_WARNING_OBJECT (rtpgstdepay, "failed to parse caps");
525 gst_buffer_unref (outbuf);
526 gst_rtp_buffer_unmap (&rtp);
531 GST_WARNING_OBJECT (rtpgstdepay, "failed to parse event");
532 gst_buffer_unref (outbuf);
533 gst_rtp_buffer_unmap (&rtp);
538 GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
539 ("Missing caps %u.", CV), (NULL));
540 gst_buffer_unref (outbuf);
541 gst_rtp_buffer_unmap (&rtp);
547 gst_rtp_gst_depay_handle_event (GstRTPBaseDepayload * depay, GstEvent * event)
549 GstRtpGSTDepay *rtpgstdepay;
551 rtpgstdepay = GST_RTP_GST_DEPAY (depay);
553 switch (GST_EVENT_TYPE (event)) {
554 case GST_EVENT_FLUSH_STOP:
555 gst_rtp_gst_depay_reset (rtpgstdepay, FALSE);
562 GST_RTP_BASE_DEPAYLOAD_CLASS (parent_class)->handle_event (depay, event);
566 static GstStateChangeReturn
567 gst_rtp_gst_depay_change_state (GstElement * element, GstStateChange transition)
569 GstRtpGSTDepay *rtpgstdepay;
570 GstStateChangeReturn ret;
572 rtpgstdepay = GST_RTP_GST_DEPAY (element);
574 switch (transition) {
575 case GST_STATE_CHANGE_READY_TO_PAUSED:
576 gst_rtp_gst_depay_reset (rtpgstdepay, TRUE);
582 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
584 switch (transition) {
585 case GST_STATE_CHANGE_PAUSED_TO_READY:
586 gst_rtp_gst_depay_reset (rtpgstdepay, TRUE);
596 gst_rtp_gst_depay_plugin_init (GstPlugin * plugin)
598 return gst_element_register (plugin, "rtpgstdepay",
599 GST_RANK_MARGINAL, GST_TYPE_RTP_GST_DEPAY);