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 store_cache (GstRtpGSTDepay * rtpgstdepay, guint CV, GstCaps * caps)
122 gboolean changed = FALSE;
124 if (caps && rtpgstdepay->CV_cache[CV])
125 changed = !gst_caps_is_strictly_equal (caps, rtpgstdepay->CV_cache[CV]);
127 if (rtpgstdepay->CV_cache[CV])
128 gst_caps_unref (rtpgstdepay->CV_cache[CV]);
129 rtpgstdepay->CV_cache[CV] = caps;
135 gst_rtp_gst_depay_reset (GstRtpGSTDepay * rtpgstdepay, gboolean full)
139 gst_adapter_clear (rtpgstdepay->adapter);
141 rtpgstdepay->current_CV = 0;
142 for (i = 0; i < 8; i++)
143 store_cache (rtpgstdepay, i, NULL);
144 g_free (rtpgstdepay->stream_id);
145 rtpgstdepay->stream_id = NULL;
146 if (rtpgstdepay->tags)
147 gst_tag_list_unref (rtpgstdepay->tags);
148 rtpgstdepay->tags = NULL;
153 gst_rtp_gst_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
155 GstRtpGSTDepay *rtpgstdepay;
156 GstStructure *structure;
159 const gchar *capsenc;
161 rtpgstdepay = GST_RTP_GST_DEPAY (depayload);
163 structure = gst_caps_get_structure (caps, 0);
165 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
167 depayload->clock_rate = clock_rate;
169 capsenc = gst_structure_get_string (structure, "caps");
174 const gchar *capsver;
178 capsstr = (gchar *) g_base64_decode (capsenc, &out_len);
179 outcaps = gst_caps_from_string (capsstr);
183 capsver = gst_structure_get_string (structure, "capsversion");
187 /* no version, assume 0 */
191 rtpgstdepay->current_CV = CV;
192 gst_caps_ref (outcaps);
193 store_cache (rtpgstdepay, CV, outcaps);
195 res = gst_pad_set_caps (depayload->srcpad, outcaps);
196 gst_caps_unref (outcaps);
198 GST_WARNING_OBJECT (depayload, "no caps given");
199 rtpgstdepay->current_CV = -1;
207 read_length (GstRtpGSTDepay * rtpgstdepay, guint8 * data, guint size,
208 guint * length, guint * skip)
210 guint b, len, offset;
212 /* start reading the length, we need this to skip to the data later */
218 len = (len << 7) | (b & 0x7f);
221 /* check remaining buffer size */
222 if (size - offset < len)
232 read_caps (GstRtpGSTDepay * rtpgstdepay, GstBuffer * buf, guint * skip)
234 guint offset, length;
238 gst_buffer_map (buf, &map, GST_MAP_READ);
240 GST_DEBUG_OBJECT (rtpgstdepay, "buffer size %" G_GSIZE_FORMAT, map.size);
242 if (!read_length (rtpgstdepay, map.data, map.size, &length, &offset))
245 if (length == 0 || map.data[offset + length - 1] != '\0')
248 GST_DEBUG_OBJECT (rtpgstdepay, "parsing caps %s", &map.data[offset]);
250 /* parse and store in cache */
251 caps = gst_caps_from_string ((gchar *) & map.data[offset]);
252 gst_buffer_unmap (buf, &map);
254 *skip = length + offset;
260 GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
261 ("Buffer too small."), (NULL));
262 gst_buffer_unmap (buf, &map);
267 GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
268 ("caps string not 0-terminated."), (NULL));
269 gst_buffer_unmap (buf, &map);
275 read_event (GstRtpGSTDepay * rtpgstdepay, guint type,
276 GstBuffer * buf, guint * skip)
278 guint offset, length;
285 gst_buffer_map (buf, &map, GST_MAP_READ);
287 GST_DEBUG_OBJECT (rtpgstdepay, "buffer size %" G_GSIZE_FORMAT, map.size);
289 if (!read_length (rtpgstdepay, map.data, map.size, &length, &offset))
294 /* backward compat, old payloader did not put 0-byte at the end */
295 if (map.data[offset + length - 1] != '\0'
296 && map.data[offset + length - 1] != ';')
299 GST_DEBUG_OBJECT (rtpgstdepay, "parsing event %s", &map.data[offset]);
302 s = gst_structure_from_string ((gchar *) & map.data[offset], &end);
303 gst_buffer_unmap (buf, &map);
310 etype = GST_EVENT_TAG;
313 etype = GST_EVENT_CUSTOM_DOWNSTREAM;
316 etype = GST_EVENT_CUSTOM_BOTH;
319 etype = GST_EVENT_STREAM_START;
324 event = gst_event_new_custom (etype, s);
326 *skip = length + offset;
332 GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
333 ("Buffer too small."), (NULL));
334 gst_buffer_unmap (buf, &map);
339 GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
340 ("event string not 0-terminated."), (NULL));
341 gst_buffer_unmap (buf, &map);
346 GST_WARNING_OBJECT (rtpgstdepay, "could not parse event");
351 GST_DEBUG_OBJECT (rtpgstdepay, "unknown event type");
352 gst_structure_free (s);
358 store_event (GstRtpGSTDepay * rtpgstdepay, GstEvent * event)
360 gboolean do_push = FALSE;
362 switch (GST_EVENT_TYPE (event)) {
365 GstTagList *old, *tags;
367 gst_event_parse_tag (event, &tags);
369 old = rtpgstdepay->tags;
370 if (!old || !gst_tag_list_is_equal (old, tags)) {
373 gst_tag_list_unref (old);
374 rtpgstdepay->tags = gst_tag_list_ref (tags);
378 case GST_EVENT_CUSTOM_DOWNSTREAM:
379 case GST_EVENT_CUSTOM_BOTH:
380 /* always push custom events */
383 case GST_EVENT_STREAM_START:
386 const gchar *stream_id = NULL;
388 gst_event_parse_stream_start (event, &stream_id);
390 old = rtpgstdepay->stream_id;
391 if (!old || g_strcmp0 (old, stream_id)) {
394 rtpgstdepay->stream_id = g_strdup (stream_id);
399 /* unknown event, don't push */
403 gst_pad_push_event (GST_RTP_BASE_DEPAYLOAD (rtpgstdepay)->srcpad, event);
405 gst_event_unref (event);
409 gst_rtp_gst_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
411 GstRtpGSTDepay *rtpgstdepay;
412 GstBuffer *subbuf, *outbuf = NULL;
415 guint CV, frag_offset, avail, offset;
417 rtpgstdepay = GST_RTP_GST_DEPAY (depayload);
419 payload_len = gst_rtp_buffer_get_payload_len (rtp);
421 if (payload_len <= 8)
424 if (GST_BUFFER_IS_DISCONT (rtp->buffer)) {
425 GST_WARNING_OBJECT (rtpgstdepay, "DISCONT, clear adapter");
426 gst_adapter_clear (rtpgstdepay->adapter);
429 payload = gst_rtp_buffer_get_payload (rtp);
434 * 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
435 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
436 * |C| CV |D|0|0|0| ETYPE | MBZ |
437 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
439 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
442 (payload[4] << 24) | (payload[5] << 16) | (payload[6] << 8) | payload[7];
444 avail = gst_adapter_available (rtpgstdepay->adapter);
445 if (avail != frag_offset)
448 /* subbuffer skipping the 8 header bytes */
449 subbuf = gst_rtp_buffer_get_payload_subbuffer (rtp, 8, -1);
450 gst_adapter_push (rtpgstdepay->adapter, subbuf);
453 if (gst_rtp_buffer_get_marker (rtp)) {
457 /* take the buffer */
458 avail = gst_adapter_available (rtpgstdepay->adapter);
459 outbuf = gst_adapter_take_buffer (rtpgstdepay->adapter, avail);
461 CV = (payload[0] >> 4) & 0x7;
463 if (payload[0] & 0x80) {
466 /* C bit, we have inline caps */
467 outcaps = read_caps (rtpgstdepay, outbuf, &size);
471 GST_DEBUG_OBJECT (rtpgstdepay,
472 "inline caps %u, length %u, %" GST_PTR_FORMAT, CV, size, outcaps);
474 if (store_cache (rtpgstdepay, CV, outcaps))
475 gst_pad_set_caps (depayload->srcpad, outcaps);
485 /* we have an event */
486 event = read_event (rtpgstdepay, payload[1], outbuf, &size);
490 GST_DEBUG_OBJECT (rtpgstdepay,
491 "inline event, length %u, %" GST_PTR_FORMAT, size, event);
493 store_event (rtpgstdepay, event);
495 /* no buffer after event */
503 GST_DEBUG_OBJECT (rtpgstdepay, "sub buffer: offset %u, size %u", offset,
507 gst_buffer_copy_region (outbuf, GST_BUFFER_COPY_ALL, offset, avail);
509 gst_buffer_unref (outbuf);
513 /* see what caps we need */
514 if (CV != rtpgstdepay->current_CV) {
515 /* we need to switch caps, check if we have the caps */
516 if ((outcaps = rtpgstdepay->CV_cache[CV]) == NULL)
519 GST_DEBUG_OBJECT (rtpgstdepay,
520 "need caps switch from %u to %u, %" GST_PTR_FORMAT,
521 rtpgstdepay->current_CV, CV, outcaps);
524 if (gst_pad_set_caps (depayload->srcpad, outcaps))
525 rtpgstdepay->current_CV = CV;
528 if (payload[0] & 0x8)
529 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
531 gst_buffer_unref (outbuf);
537 gst_rtp_drop_meta (GST_ELEMENT_CAST (rtpgstdepay), outbuf, 0);
545 GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
546 ("Empty Payload."), (NULL));
551 gst_adapter_clear (rtpgstdepay->adapter);
552 GST_LOG_OBJECT (rtpgstdepay, "wrong fragment, skipping");
557 GST_WARNING_OBJECT (rtpgstdepay, "failed to parse caps");
558 gst_buffer_unref (outbuf);
563 GST_WARNING_OBJECT (rtpgstdepay, "failed to parse event");
564 gst_buffer_unref (outbuf);
569 GST_INFO_OBJECT (rtpgstdepay, "No caps received yet %u", CV);
570 gst_buffer_unref (outbuf);
572 gst_pad_push_event (GST_RTP_BASE_DEPAYLOAD_SINKPAD (rtpgstdepay),
573 gst_video_event_new_upstream_force_key_unit (GST_CLOCK_TIME_NONE,
581 gst_rtp_gst_depay_handle_event (GstRTPBaseDepayload * depay, GstEvent * event)
583 GstRtpGSTDepay *rtpgstdepay;
585 rtpgstdepay = GST_RTP_GST_DEPAY (depay);
587 switch (GST_EVENT_TYPE (event)) {
588 case GST_EVENT_FLUSH_STOP:
589 gst_rtp_gst_depay_reset (rtpgstdepay, FALSE);
596 GST_RTP_BASE_DEPAYLOAD_CLASS (parent_class)->handle_event (depay, event);
600 static GstStateChangeReturn
601 gst_rtp_gst_depay_change_state (GstElement * element, GstStateChange transition)
603 GstRtpGSTDepay *rtpgstdepay;
604 GstStateChangeReturn ret;
606 rtpgstdepay = GST_RTP_GST_DEPAY (element);
608 switch (transition) {
609 case GST_STATE_CHANGE_READY_TO_PAUSED:
610 gst_rtp_gst_depay_reset (rtpgstdepay, TRUE);
616 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
618 switch (transition) {
619 case GST_STATE_CHANGE_PAUSED_TO_READY:
620 gst_rtp_gst_depay_reset (rtpgstdepay, TRUE);
630 gst_rtp_gst_depay_plugin_init (GstPlugin * plugin)
632 return gst_element_register (plugin, "rtpgstdepay",
633 GST_RANK_MARGINAL, GST_TYPE_RTP_GST_DEPAY);