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"
28 #include "gstrtputils.h"
30 GST_DEBUG_CATEGORY_STATIC (rtpgstdepay_debug);
31 #define GST_CAT_DEFAULT (rtpgstdepay_debug)
33 static GstStaticPadTemplate gst_rtp_gst_depay_src_template =
34 GST_STATIC_PAD_TEMPLATE ("src",
39 static GstStaticPadTemplate gst_rtp_gst_depay_sink_template =
40 GST_STATIC_PAD_TEMPLATE ("sink",
43 GST_STATIC_CAPS ("application/x-rtp, "
44 "media = (string) \"application\", "
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_RTP_BASE_DEPAYLOAD);
51 static void gst_rtp_gst_depay_finalize (GObject * object);
53 static gboolean gst_rtp_gst_depay_handle_event (GstRTPBaseDepayload * depay,
55 static GstStateChangeReturn gst_rtp_gst_depay_change_state (GstElement *
56 element, GstStateChange transition);
58 static void gst_rtp_gst_depay_reset (GstRtpGSTDepay * rtpgstdepay, gboolean
60 static gboolean gst_rtp_gst_depay_setcaps (GstRTPBaseDepayload * depayload,
62 static GstBuffer *gst_rtp_gst_depay_process (GstRTPBaseDepayload * depayload,
66 gst_rtp_gst_depay_class_init (GstRtpGSTDepayClass * klass)
68 GObjectClass *gobject_class;
69 GstElementClass *gstelement_class;
70 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
72 GST_DEBUG_CATEGORY_INIT (rtpgstdepay_debug, "rtpgstdepay", 0,
73 "Gstreamer RTP Depayloader");
75 gobject_class = (GObjectClass *) klass;
76 gstelement_class = (GstElementClass *) klass;
77 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
79 gobject_class->finalize = gst_rtp_gst_depay_finalize;
81 gstelement_class->change_state = gst_rtp_gst_depay_change_state;
83 gst_element_class_add_static_pad_template (gstelement_class,
84 &gst_rtp_gst_depay_src_template);
85 gst_element_class_add_static_pad_template (gstelement_class,
86 &gst_rtp_gst_depay_sink_template);
88 gst_element_class_set_static_metadata (gstelement_class,
89 "GStreamer depayloader", "Codec/Depayloader/Network",
90 "Extracts GStreamer buffers from RTP packets",
91 "Wim Taymans <wim.taymans@gmail.com>");
93 gstrtpbasedepayload_class->handle_event = gst_rtp_gst_depay_handle_event;
94 gstrtpbasedepayload_class->set_caps = gst_rtp_gst_depay_setcaps;
95 gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_gst_depay_process;
99 gst_rtp_gst_depay_init (GstRtpGSTDepay * rtpgstdepay)
101 rtpgstdepay->adapter = gst_adapter_new ();
105 gst_rtp_gst_depay_finalize (GObject * object)
107 GstRtpGSTDepay *rtpgstdepay;
109 rtpgstdepay = GST_RTP_GST_DEPAY (object);
111 gst_rtp_gst_depay_reset (rtpgstdepay, TRUE);
112 g_object_unref (rtpgstdepay->adapter);
114 G_OBJECT_CLASS (parent_class)->finalize (object);
118 store_cache (GstRtpGSTDepay * rtpgstdepay, guint CV, GstCaps * caps)
120 if (rtpgstdepay->CV_cache[CV])
121 gst_caps_unref (rtpgstdepay->CV_cache[CV]);
122 rtpgstdepay->CV_cache[CV] = caps;
126 gst_rtp_gst_depay_reset (GstRtpGSTDepay * rtpgstdepay, gboolean full)
130 gst_adapter_clear (rtpgstdepay->adapter);
132 rtpgstdepay->current_CV = 0;
133 for (i = 0; i < 8; i++)
134 store_cache (rtpgstdepay, i, NULL);
135 g_free (rtpgstdepay->stream_id);
136 rtpgstdepay->stream_id = NULL;
137 if (rtpgstdepay->tags)
138 gst_tag_list_unref (rtpgstdepay->tags);
139 rtpgstdepay->tags = NULL;
144 gst_rtp_gst_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
146 GstRtpGSTDepay *rtpgstdepay;
147 GstStructure *structure;
150 const gchar *capsenc;
152 rtpgstdepay = GST_RTP_GST_DEPAY (depayload);
154 structure = gst_caps_get_structure (caps, 0);
156 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
158 depayload->clock_rate = clock_rate;
160 capsenc = gst_structure_get_string (structure, "caps");
165 const gchar *capsver;
169 capsstr = (gchar *) g_base64_decode (capsenc, &out_len);
170 outcaps = gst_caps_from_string (capsstr);
174 capsver = gst_structure_get_string (structure, "capsversion");
178 /* no version, assume 0 */
182 rtpgstdepay->current_CV = CV;
183 gst_caps_ref (outcaps);
184 store_cache (rtpgstdepay, CV, outcaps);
186 res = gst_pad_set_caps (depayload->srcpad, outcaps);
187 gst_caps_unref (outcaps);
189 GST_WARNING_OBJECT (depayload, "no caps given");
190 rtpgstdepay->current_CV = -1;
198 read_length (GstRtpGSTDepay * rtpgstdepay, guint8 * data, guint size,
199 guint * length, guint * skip)
201 guint b, len, offset;
203 /* start reading the length, we need this to skip to the data later */
209 len = (len << 7) | (b & 0x7f);
212 /* check remaining buffer size */
213 if (size - offset < len)
223 read_caps (GstRtpGSTDepay * rtpgstdepay, GstBuffer * buf, guint * skip)
225 guint offset, length;
229 gst_buffer_map (buf, &map, GST_MAP_READ);
231 GST_DEBUG_OBJECT (rtpgstdepay, "buffer size %" G_GSIZE_FORMAT, map.size);
233 if (!read_length (rtpgstdepay, map.data, map.size, &length, &offset))
236 if (length == 0 || map.data[offset + length - 1] != '\0')
239 GST_DEBUG_OBJECT (rtpgstdepay, "parsing caps %s", &map.data[offset]);
241 /* parse and store in cache */
242 caps = gst_caps_from_string ((gchar *) & map.data[offset]);
243 gst_buffer_unmap (buf, &map);
245 *skip = length + offset;
251 GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
252 ("Buffer too small."), (NULL));
253 gst_buffer_unmap (buf, &map);
258 GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
259 ("caps string not 0-terminated."), (NULL));
260 gst_buffer_unmap (buf, &map);
266 read_event (GstRtpGSTDepay * rtpgstdepay, guint type,
267 GstBuffer * buf, guint * skip)
269 guint offset, length;
276 gst_buffer_map (buf, &map, GST_MAP_READ);
278 GST_DEBUG_OBJECT (rtpgstdepay, "buffer size %" G_GSIZE_FORMAT, map.size);
280 if (!read_length (rtpgstdepay, map.data, map.size, &length, &offset))
285 /* backward compat, old payloader did not put 0-byte at the end */
286 if (map.data[offset + length - 1] != '\0'
287 && map.data[offset + length - 1] != ';')
290 GST_DEBUG_OBJECT (rtpgstdepay, "parsing event %s", &map.data[offset]);
293 s = gst_structure_from_string ((gchar *) & map.data[offset], &end);
294 gst_buffer_unmap (buf, &map);
301 etype = GST_EVENT_TAG;
304 etype = GST_EVENT_CUSTOM_DOWNSTREAM;
307 etype = GST_EVENT_CUSTOM_BOTH;
310 etype = GST_EVENT_STREAM_START;
315 event = gst_event_new_custom (etype, s);
317 *skip = length + offset;
323 GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
324 ("Buffer too small."), (NULL));
325 gst_buffer_unmap (buf, &map);
330 GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
331 ("event string not 0-terminated."), (NULL));
332 gst_buffer_unmap (buf, &map);
337 GST_WARNING_OBJECT (rtpgstdepay, "could not parse event");
342 GST_DEBUG_OBJECT (rtpgstdepay, "unknown event type");
343 gst_structure_free (s);
349 store_event (GstRtpGSTDepay * rtpgstdepay, GstEvent * event)
351 gboolean do_push = FALSE;
353 switch (GST_EVENT_TYPE (event)) {
356 GstTagList *old, *tags;
358 gst_event_parse_tag (event, &tags);
360 old = rtpgstdepay->tags;
361 if (!old || !gst_tag_list_is_equal (old, tags)) {
364 gst_tag_list_unref (old);
365 rtpgstdepay->tags = gst_tag_list_ref (tags);
369 case GST_EVENT_CUSTOM_DOWNSTREAM:
370 case GST_EVENT_CUSTOM_BOTH:
371 /* always push custom events */
374 case GST_EVENT_STREAM_START:
377 const gchar *stream_id = NULL;
379 gst_event_parse_stream_start (event, &stream_id);
381 old = rtpgstdepay->stream_id;
382 if (!old || g_strcmp0 (old, stream_id)) {
385 rtpgstdepay->stream_id = g_strdup (stream_id);
390 /* unknown event, don't push */
394 gst_pad_push_event (GST_RTP_BASE_DEPAYLOAD (rtpgstdepay)->srcpad, event);
396 gst_event_unref (event);
400 gst_rtp_gst_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
402 GstRtpGSTDepay *rtpgstdepay;
403 GstBuffer *subbuf, *outbuf = NULL;
406 guint CV, frag_offset, avail, offset;
408 rtpgstdepay = GST_RTP_GST_DEPAY (depayload);
410 payload_len = gst_rtp_buffer_get_payload_len (rtp);
412 if (payload_len <= 8)
415 if (GST_BUFFER_IS_DISCONT (rtp->buffer)) {
416 GST_WARNING_OBJECT (rtpgstdepay, "DISCONT, clear adapter");
417 gst_adapter_clear (rtpgstdepay->adapter);
420 payload = gst_rtp_buffer_get_payload (rtp);
425 * 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
426 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
427 * |C| CV |D|0|0|0| ETYPE | MBZ |
428 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
430 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
433 (payload[4] << 24) | (payload[5] << 16) | (payload[6] << 8) | payload[7];
435 avail = gst_adapter_available (rtpgstdepay->adapter);
436 if (avail != frag_offset)
439 /* subbuffer skipping the 8 header bytes */
440 subbuf = gst_rtp_buffer_get_payload_subbuffer (rtp, 8, -1);
441 gst_adapter_push (rtpgstdepay->adapter, subbuf);
444 if (gst_rtp_buffer_get_marker (rtp)) {
448 /* take the buffer */
449 avail = gst_adapter_available (rtpgstdepay->adapter);
450 outbuf = gst_adapter_take_buffer (rtpgstdepay->adapter, avail);
452 CV = (payload[0] >> 4) & 0x7;
454 if (payload[0] & 0x80) {
457 /* C bit, we have inline caps */
458 outcaps = read_caps (rtpgstdepay, outbuf, &size);
462 GST_DEBUG_OBJECT (rtpgstdepay,
463 "inline caps %u, length %u, %" GST_PTR_FORMAT, CV, size, outcaps);
465 store_cache (rtpgstdepay, CV, outcaps);
475 /* we have an event */
476 event = read_event (rtpgstdepay, payload[1], outbuf, &size);
480 GST_DEBUG_OBJECT (rtpgstdepay,
481 "inline event, length %u, %" GST_PTR_FORMAT, size, event);
483 store_event (rtpgstdepay, event);
485 /* no buffer after event */
493 GST_DEBUG_OBJECT (rtpgstdepay, "sub buffer: offset %u, size %u", offset,
497 gst_buffer_copy_region (outbuf, GST_BUFFER_COPY_ALL, offset, avail);
499 gst_buffer_unref (outbuf);
503 /* see what caps we need */
504 if (CV != rtpgstdepay->current_CV) {
505 /* we need to switch caps, check if we have the caps */
506 if ((outcaps = rtpgstdepay->CV_cache[CV]) == NULL)
509 GST_DEBUG_OBJECT (rtpgstdepay,
510 "need caps switch from %u to %u, %" GST_PTR_FORMAT,
511 rtpgstdepay->current_CV, CV, outcaps);
514 if (gst_pad_set_caps (depayload->srcpad, outcaps))
515 rtpgstdepay->current_CV = CV;
518 if (payload[0] & 0x8)
519 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
521 gst_buffer_unref (outbuf);
527 gst_rtp_drop_meta (GST_ELEMENT_CAST (rtpgstdepay), outbuf, 0);
535 GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
536 ("Empty Payload."), (NULL));
541 gst_adapter_clear (rtpgstdepay->adapter);
542 GST_LOG_OBJECT (rtpgstdepay, "wrong fragment, skipping");
547 GST_WARNING_OBJECT (rtpgstdepay, "failed to parse caps");
548 gst_buffer_unref (outbuf);
553 GST_WARNING_OBJECT (rtpgstdepay, "failed to parse event");
554 gst_buffer_unref (outbuf);
559 GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
560 ("Missing caps %u.", CV), (NULL));
561 gst_buffer_unref (outbuf);
567 gst_rtp_gst_depay_handle_event (GstRTPBaseDepayload * depay, GstEvent * event)
569 GstRtpGSTDepay *rtpgstdepay;
571 rtpgstdepay = GST_RTP_GST_DEPAY (depay);
573 switch (GST_EVENT_TYPE (event)) {
574 case GST_EVENT_FLUSH_STOP:
575 gst_rtp_gst_depay_reset (rtpgstdepay, FALSE);
582 GST_RTP_BASE_DEPAYLOAD_CLASS (parent_class)->handle_event (depay, event);
586 static GstStateChangeReturn
587 gst_rtp_gst_depay_change_state (GstElement * element, GstStateChange transition)
589 GstRtpGSTDepay *rtpgstdepay;
590 GstStateChangeReturn ret;
592 rtpgstdepay = GST_RTP_GST_DEPAY (element);
594 switch (transition) {
595 case GST_STATE_CHANGE_READY_TO_PAUSED:
596 gst_rtp_gst_depay_reset (rtpgstdepay, TRUE);
602 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
604 switch (transition) {
605 case GST_STATE_CHANGE_PAUSED_TO_READY:
606 gst_rtp_gst_depay_reset (rtpgstdepay, TRUE);
616 gst_rtp_gst_depay_plugin_init (GstPlugin * plugin)
618 return gst_element_register (plugin, "rtpgstdepay",
619 GST_RANK_MARGINAL, GST_TYPE_RTP_GST_DEPAY);