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 #include <gst/video/video.h>
32 GST_DEBUG_CATEGORY_STATIC (rtpgstdepay_debug);
33 #define GST_CAT_DEFAULT (rtpgstdepay_debug)
35 static GstStaticPadTemplate gst_rtp_gst_depay_src_template =
36 GST_STATIC_PAD_TEMPLATE ("src",
41 static GstStaticPadTemplate gst_rtp_gst_depay_sink_template =
42 GST_STATIC_PAD_TEMPLATE ("sink",
45 GST_STATIC_CAPS ("application/x-rtp, "
46 "media = (string) \"application\", "
47 "clock-rate = (int) 90000, " "encoding-name = (string) \"X-GST\"")
50 #define gst_rtp_gst_depay_parent_class parent_class
51 G_DEFINE_TYPE (GstRtpGSTDepay, gst_rtp_gst_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
53 static void gst_rtp_gst_depay_finalize (GObject * object);
55 static gboolean gst_rtp_gst_depay_handle_event (GstRTPBaseDepayload * depay,
57 static GstStateChangeReturn gst_rtp_gst_depay_change_state (GstElement *
58 element, GstStateChange transition);
60 static void gst_rtp_gst_depay_reset (GstRtpGSTDepay * rtpgstdepay, gboolean
62 static gboolean gst_rtp_gst_depay_setcaps (GstRTPBaseDepayload * depayload,
64 static GstBuffer *gst_rtp_gst_depay_process (GstRTPBaseDepayload * depayload,
68 gst_rtp_gst_depay_class_init (GstRtpGSTDepayClass * klass)
70 GObjectClass *gobject_class;
71 GstElementClass *gstelement_class;
72 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
74 GST_DEBUG_CATEGORY_INIT (rtpgstdepay_debug, "rtpgstdepay", 0,
75 "Gstreamer RTP Depayloader");
77 gobject_class = (GObjectClass *) klass;
78 gstelement_class = (GstElementClass *) klass;
79 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
81 gobject_class->finalize = gst_rtp_gst_depay_finalize;
83 gstelement_class->change_state = gst_rtp_gst_depay_change_state;
85 gst_element_class_add_static_pad_template (gstelement_class,
86 &gst_rtp_gst_depay_src_template);
87 gst_element_class_add_static_pad_template (gstelement_class,
88 &gst_rtp_gst_depay_sink_template);
90 gst_element_class_set_static_metadata (gstelement_class,
91 "GStreamer depayloader", "Codec/Depayloader/Network",
92 "Extracts GStreamer buffers from RTP packets",
93 "Wim Taymans <wim.taymans@gmail.com>");
95 gstrtpbasedepayload_class->handle_event = gst_rtp_gst_depay_handle_event;
96 gstrtpbasedepayload_class->set_caps = gst_rtp_gst_depay_setcaps;
97 gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_gst_depay_process;
101 gst_rtp_gst_depay_init (GstRtpGSTDepay * rtpgstdepay)
103 rtpgstdepay->adapter = gst_adapter_new ();
107 gst_rtp_gst_depay_finalize (GObject * object)
109 GstRtpGSTDepay *rtpgstdepay;
111 rtpgstdepay = GST_RTP_GST_DEPAY (object);
113 gst_rtp_gst_depay_reset (rtpgstdepay, TRUE);
114 g_object_unref (rtpgstdepay->adapter);
116 G_OBJECT_CLASS (parent_class)->finalize (object);
120 gst_rtp_gst_depay_reset (GstRtpGSTDepay * rtpgstdepay, gboolean full)
122 gst_adapter_clear (rtpgstdepay->adapter);
124 rtpgstdepay->current_CV = 0;
125 gst_caps_replace (&rtpgstdepay->current_caps, NULL);
126 g_free (rtpgstdepay->stream_id);
127 rtpgstdepay->stream_id = NULL;
128 if (rtpgstdepay->tags)
129 gst_tag_list_unref (rtpgstdepay->tags);
130 rtpgstdepay->tags = NULL;
135 gst_rtp_gst_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
137 GstRtpGSTDepay *rtpgstdepay;
138 GstStructure *structure;
141 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");
156 const gchar *capsver;
160 capsstr = (gchar *) g_base64_decode (capsenc, &out_len);
161 outcaps = gst_caps_from_string (capsstr);
165 capsver = gst_structure_get_string (structure, "capsversion");
169 /* no version, assume 0 */
173 rtpgstdepay->current_CV = CV;
174 gst_caps_replace (&rtpgstdepay->current_caps, outcaps);
176 res = gst_pad_set_caps (depayload->srcpad, outcaps);
177 gst_caps_unref (outcaps);
179 GST_WARNING_OBJECT (depayload, "no caps given");
180 rtpgstdepay->current_CV = -1;
181 gst_caps_replace (&rtpgstdepay->current_caps, NULL);
189 read_length (GstRtpGSTDepay * rtpgstdepay, guint8 * data, guint size,
190 guint * length, guint * skip)
192 guint b, len, offset;
194 /* start reading the length, we need this to skip to the data later */
200 len = (len << 7) | (b & 0x7f);
203 /* check remaining buffer size */
204 if (size - offset < len)
214 read_caps (GstRtpGSTDepay * rtpgstdepay, GstBuffer * buf, guint * skip)
216 guint offset, length;
220 gst_buffer_map (buf, &map, GST_MAP_READ);
222 GST_DEBUG_OBJECT (rtpgstdepay, "buffer size %" G_GSIZE_FORMAT, map.size);
224 if (!read_length (rtpgstdepay, map.data, map.size, &length, &offset))
227 if (length == 0 || map.data[offset + length - 1] != '\0')
230 GST_DEBUG_OBJECT (rtpgstdepay, "parsing caps %s", &map.data[offset]);
232 /* parse and store in cache */
233 caps = gst_caps_from_string ((gchar *) & map.data[offset]);
234 gst_buffer_unmap (buf, &map);
236 *skip = length + offset;
242 GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
243 ("Buffer too small."), (NULL));
244 gst_buffer_unmap (buf, &map);
249 GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
250 ("caps string not 0-terminated."), (NULL));
251 gst_buffer_unmap (buf, &map);
257 read_event (GstRtpGSTDepay * rtpgstdepay, guint type,
258 GstBuffer * buf, guint * skip)
260 guint offset, length;
267 gst_buffer_map (buf, &map, GST_MAP_READ);
269 GST_DEBUG_OBJECT (rtpgstdepay, "buffer size %" G_GSIZE_FORMAT, map.size);
271 if (!read_length (rtpgstdepay, map.data, map.size, &length, &offset))
276 /* backward compat, old payloader did not put 0-byte at the end */
277 if (map.data[offset + length - 1] != '\0'
278 && map.data[offset + length - 1] != ';')
281 GST_DEBUG_OBJECT (rtpgstdepay, "parsing event %s", &map.data[offset]);
284 s = gst_structure_from_string ((gchar *) & map.data[offset], &end);
285 gst_buffer_unmap (buf, &map);
292 etype = GST_EVENT_TAG;
295 etype = GST_EVENT_CUSTOM_DOWNSTREAM;
298 etype = GST_EVENT_CUSTOM_BOTH;
301 etype = GST_EVENT_STREAM_START;
306 event = gst_event_new_custom (etype, s);
308 *skip = length + offset;
314 GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
315 ("Buffer too small."), (NULL));
316 gst_buffer_unmap (buf, &map);
321 GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
322 ("event string not 0-terminated."), (NULL));
323 gst_buffer_unmap (buf, &map);
328 GST_WARNING_OBJECT (rtpgstdepay, "could not parse event");
333 GST_DEBUG_OBJECT (rtpgstdepay, "unknown event type");
334 gst_structure_free (s);
340 store_event (GstRtpGSTDepay * rtpgstdepay, GstEvent * event)
342 gboolean do_push = FALSE;
344 switch (GST_EVENT_TYPE (event)) {
347 GstTagList *old, *tags;
349 gst_event_parse_tag (event, &tags);
351 old = rtpgstdepay->tags;
352 if (!old || !gst_tag_list_is_equal (old, tags)) {
355 gst_tag_list_unref (old);
356 rtpgstdepay->tags = gst_tag_list_ref (tags);
360 case GST_EVENT_CUSTOM_DOWNSTREAM:
361 case GST_EVENT_CUSTOM_BOTH:
362 /* always push custom events */
365 case GST_EVENT_STREAM_START:
368 const gchar *stream_id = NULL;
370 gst_event_parse_stream_start (event, &stream_id);
372 old = rtpgstdepay->stream_id;
373 if (!old || g_strcmp0 (old, stream_id)) {
376 rtpgstdepay->stream_id = g_strdup (stream_id);
381 /* unknown event, don't push */
385 gst_pad_push_event (GST_RTP_BASE_DEPAYLOAD (rtpgstdepay)->srcpad, event);
387 gst_event_unref (event);
391 gst_rtp_gst_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
393 GstRtpGSTDepay *rtpgstdepay;
394 GstBuffer *subbuf, *outbuf = NULL;
397 guint CV, frag_offset, avail, offset;
399 rtpgstdepay = GST_RTP_GST_DEPAY (depayload);
401 payload_len = gst_rtp_buffer_get_payload_len (rtp);
403 if (payload_len <= 8)
406 if (GST_BUFFER_IS_DISCONT (rtp->buffer)) {
407 GST_WARNING_OBJECT (rtpgstdepay, "DISCONT, clear adapter");
408 gst_adapter_clear (rtpgstdepay->adapter);
411 payload = gst_rtp_buffer_get_payload (rtp);
416 * 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
417 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
418 * |C| CV |D|0|0|0| ETYPE | MBZ |
419 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
421 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
424 (payload[4] << 24) | (payload[5] << 16) | (payload[6] << 8) | payload[7];
426 avail = gst_adapter_available (rtpgstdepay->adapter);
427 if (avail != frag_offset)
430 /* subbuffer skipping the 8 header bytes */
431 subbuf = gst_rtp_buffer_get_payload_subbuffer (rtp, 8, -1);
432 gst_adapter_push (rtpgstdepay->adapter, subbuf);
435 if (gst_rtp_buffer_get_marker (rtp)) {
439 /* take the buffer */
440 avail = gst_adapter_available (rtpgstdepay->adapter);
441 outbuf = gst_adapter_take_buffer (rtpgstdepay->adapter, avail);
443 CV = (payload[0] >> 4) & 0x7;
445 if (payload[0] & 0x80) {
448 /* C bit, we have inline caps */
449 outcaps = read_caps (rtpgstdepay, outbuf, &size);
453 GST_DEBUG_OBJECT (rtpgstdepay,
454 "inline caps %u, length %u, %" GST_PTR_FORMAT, CV, size, outcaps);
456 if (!rtpgstdepay->current_caps
457 || !gst_caps_is_strictly_equal (rtpgstdepay->current_caps, outcaps))
458 gst_pad_set_caps (depayload->srcpad, outcaps);
459 gst_caps_replace (&rtpgstdepay->current_caps, outcaps);
460 gst_caps_unref (outcaps);
461 rtpgstdepay->current_CV = CV;
471 /* we have an event */
472 event = read_event (rtpgstdepay, payload[1], outbuf, &size);
476 GST_DEBUG_OBJECT (rtpgstdepay,
477 "inline event, length %u, %" GST_PTR_FORMAT, size, event);
479 store_event (rtpgstdepay, event);
481 /* no buffer after event */
489 GST_DEBUG_OBJECT (rtpgstdepay, "sub buffer: offset %u, size %u", offset,
493 gst_buffer_copy_region (outbuf, GST_BUFFER_COPY_ALL, offset, avail);
495 gst_buffer_unref (outbuf);
499 /* see what caps we need */
500 if (CV != rtpgstdepay->current_CV) {
501 /* we need to switch caps but didn't receive the new caps yet */
502 gst_caps_replace (&rtpgstdepay->current_caps, NULL);
506 if (payload[0] & 0x8)
507 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
509 gst_buffer_unref (outbuf);
515 gst_rtp_drop_meta (GST_ELEMENT_CAST (rtpgstdepay), outbuf, 0);
523 GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
524 ("Empty Payload."), (NULL));
529 gst_adapter_clear (rtpgstdepay->adapter);
530 GST_LOG_OBJECT (rtpgstdepay, "wrong fragment, skipping");
535 GST_WARNING_OBJECT (rtpgstdepay, "failed to parse caps");
536 gst_buffer_unref (outbuf);
541 GST_WARNING_OBJECT (rtpgstdepay, "failed to parse event");
542 gst_buffer_unref (outbuf);
547 GST_INFO_OBJECT (rtpgstdepay, "No caps received yet %u", CV);
548 gst_buffer_unref (outbuf);
550 gst_pad_push_event (GST_RTP_BASE_DEPAYLOAD_SINKPAD (rtpgstdepay),
551 gst_video_event_new_upstream_force_key_unit (GST_CLOCK_TIME_NONE,
559 gst_rtp_gst_depay_handle_event (GstRTPBaseDepayload * depay, GstEvent * event)
561 GstRtpGSTDepay *rtpgstdepay;
563 rtpgstdepay = GST_RTP_GST_DEPAY (depay);
565 switch (GST_EVENT_TYPE (event)) {
566 case GST_EVENT_FLUSH_STOP:
567 gst_rtp_gst_depay_reset (rtpgstdepay, FALSE);
574 GST_RTP_BASE_DEPAYLOAD_CLASS (parent_class)->handle_event (depay, event);
578 static GstStateChangeReturn
579 gst_rtp_gst_depay_change_state (GstElement * element, GstStateChange transition)
581 GstRtpGSTDepay *rtpgstdepay;
582 GstStateChangeReturn ret;
584 rtpgstdepay = GST_RTP_GST_DEPAY (element);
586 switch (transition) {
587 case GST_STATE_CHANGE_READY_TO_PAUSED:
588 gst_rtp_gst_depay_reset (rtpgstdepay, TRUE);
594 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
596 switch (transition) {
597 case GST_STATE_CHANGE_PAUSED_TO_READY:
598 gst_rtp_gst_depay_reset (rtpgstdepay, TRUE);
608 gst_rtp_gst_depay_plugin_init (GstPlugin * plugin)
610 return gst_element_register (plugin, "rtpgstdepay",
611 GST_RANK_MARGINAL, GST_TYPE_RTP_GST_DEPAY);