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 "gstrtpelements.h"
28 #include "gstrtpgstdepay.h"
29 #include "gstrtputils.h"
31 #include <gst/video/video.h>
33 GST_DEBUG_CATEGORY_STATIC (rtpgstdepay_debug);
34 #define GST_CAT_DEFAULT (rtpgstdepay_debug)
36 static GstStaticPadTemplate gst_rtp_gst_depay_src_template =
37 GST_STATIC_PAD_TEMPLATE ("src",
42 static GstStaticPadTemplate gst_rtp_gst_depay_sink_template =
43 GST_STATIC_PAD_TEMPLATE ("sink",
46 GST_STATIC_CAPS ("application/x-rtp, "
47 "media = (string) \"application\", "
48 "clock-rate = (int) 90000, " "encoding-name = (string) \"X-GST\"")
51 #define gst_rtp_gst_depay_parent_class parent_class
52 G_DEFINE_TYPE (GstRtpGSTDepay, gst_rtp_gst_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
53 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpgstdepay, "rtpgstdepay",
54 GST_RANK_MARGINAL, GST_TYPE_RTP_GST_DEPAY, rtp_element_init (plugin));
56 static void gst_rtp_gst_depay_finalize (GObject * object);
58 static gboolean gst_rtp_gst_depay_handle_event (GstRTPBaseDepayload * depay,
60 static GstStateChangeReturn gst_rtp_gst_depay_change_state (GstElement *
61 element, GstStateChange transition);
63 static void gst_rtp_gst_depay_reset (GstRtpGSTDepay * rtpgstdepay, gboolean
65 static gboolean gst_rtp_gst_depay_setcaps (GstRTPBaseDepayload * depayload,
67 static GstBuffer *gst_rtp_gst_depay_process (GstRTPBaseDepayload * depayload,
71 gst_rtp_gst_depay_class_init (GstRtpGSTDepayClass * klass)
73 GObjectClass *gobject_class;
74 GstElementClass *gstelement_class;
75 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
77 GST_DEBUG_CATEGORY_INIT (rtpgstdepay_debug, "rtpgstdepay", 0,
78 "Gstreamer RTP Depayloader");
80 gobject_class = (GObjectClass *) klass;
81 gstelement_class = (GstElementClass *) klass;
82 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
84 gobject_class->finalize = gst_rtp_gst_depay_finalize;
86 gstelement_class->change_state = gst_rtp_gst_depay_change_state;
88 gst_element_class_add_static_pad_template (gstelement_class,
89 &gst_rtp_gst_depay_src_template);
90 gst_element_class_add_static_pad_template (gstelement_class,
91 &gst_rtp_gst_depay_sink_template);
93 gst_element_class_set_static_metadata (gstelement_class,
94 "GStreamer depayloader", "Codec/Depayloader/Network",
95 "Extracts GStreamer buffers from RTP packets",
96 "Wim Taymans <wim.taymans@gmail.com>");
98 gstrtpbasedepayload_class->handle_event = gst_rtp_gst_depay_handle_event;
99 gstrtpbasedepayload_class->set_caps = gst_rtp_gst_depay_setcaps;
100 gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_gst_depay_process;
104 gst_rtp_gst_depay_init (GstRtpGSTDepay * rtpgstdepay)
106 rtpgstdepay->adapter = gst_adapter_new ();
110 gst_rtp_gst_depay_finalize (GObject * object)
112 GstRtpGSTDepay *rtpgstdepay;
114 rtpgstdepay = GST_RTP_GST_DEPAY (object);
116 gst_rtp_gst_depay_reset (rtpgstdepay, TRUE);
117 g_object_unref (rtpgstdepay->adapter);
119 G_OBJECT_CLASS (parent_class)->finalize (object);
123 gst_rtp_gst_depay_reset (GstRtpGSTDepay * rtpgstdepay, gboolean full)
125 gst_adapter_clear (rtpgstdepay->adapter);
127 rtpgstdepay->current_CV = 0;
128 gst_caps_replace (&rtpgstdepay->current_caps, NULL);
129 g_free (rtpgstdepay->stream_id);
130 rtpgstdepay->stream_id = NULL;
131 if (rtpgstdepay->tags)
132 gst_tag_list_unref (rtpgstdepay->tags);
133 rtpgstdepay->tags = NULL;
138 gst_rtp_gst_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
140 GstRtpGSTDepay *rtpgstdepay;
141 GstStructure *structure;
144 const gchar *capsenc;
146 rtpgstdepay = GST_RTP_GST_DEPAY (depayload);
148 structure = gst_caps_get_structure (caps, 0);
150 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
152 depayload->clock_rate = clock_rate;
154 capsenc = gst_structure_get_string (structure, "caps");
159 const gchar *capsver;
163 capsstr = (gchar *) g_base64_decode (capsenc, &out_len);
164 outcaps = gst_caps_from_string (capsstr);
168 capsver = gst_structure_get_string (structure, "capsversion");
172 /* no version, assume 0 */
176 rtpgstdepay->current_CV = CV;
177 gst_caps_replace (&rtpgstdepay->current_caps, outcaps);
179 res = gst_pad_set_caps (depayload->srcpad, outcaps);
180 gst_caps_unref (outcaps);
182 GST_WARNING_OBJECT (depayload, "no caps given");
183 rtpgstdepay->current_CV = -1;
184 gst_caps_replace (&rtpgstdepay->current_caps, NULL);
192 read_length (GstRtpGSTDepay * rtpgstdepay, guint8 * data, guint size,
193 guint * length, guint * skip)
195 guint b, len, offset;
197 /* start reading the length, we need this to skip to the data later */
203 len = (len << 7) | (b & 0x7f);
206 /* check remaining buffer size */
207 if (size - offset < len)
217 read_caps (GstRtpGSTDepay * rtpgstdepay, GstBuffer * buf, guint * skip)
219 guint offset, length;
223 gst_buffer_map (buf, &map, GST_MAP_READ);
225 GST_DEBUG_OBJECT (rtpgstdepay, "buffer size %" G_GSIZE_FORMAT, map.size);
227 if (!read_length (rtpgstdepay, map.data, map.size, &length, &offset))
230 if (length == 0 || map.data[offset + length - 1] != '\0')
233 GST_DEBUG_OBJECT (rtpgstdepay, "parsing caps %s", &map.data[offset]);
235 /* parse and store in cache */
236 caps = gst_caps_from_string ((gchar *) & map.data[offset]);
237 gst_buffer_unmap (buf, &map);
239 *skip = length + offset;
245 GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
246 ("Buffer too small."), (NULL));
247 gst_buffer_unmap (buf, &map);
252 GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
253 ("caps string not 0-terminated."), (NULL));
254 gst_buffer_unmap (buf, &map);
260 read_event (GstRtpGSTDepay * rtpgstdepay, guint type,
261 GstBuffer * buf, guint * skip)
263 guint offset, length;
270 gst_buffer_map (buf, &map, GST_MAP_READ);
272 GST_DEBUG_OBJECT (rtpgstdepay, "buffer size %" G_GSIZE_FORMAT, map.size);
274 if (!read_length (rtpgstdepay, map.data, map.size, &length, &offset))
279 /* backward compat, old payloader did not put 0-byte at the end */
280 if (map.data[offset + length - 1] != '\0'
281 && map.data[offset + length - 1] != ';')
284 GST_DEBUG_OBJECT (rtpgstdepay, "parsing event %s", &map.data[offset]);
287 s = gst_structure_from_string ((gchar *) & map.data[offset], &end);
288 gst_buffer_unmap (buf, &map);
295 etype = GST_EVENT_TAG;
298 etype = GST_EVENT_CUSTOM_DOWNSTREAM;
301 etype = GST_EVENT_CUSTOM_BOTH;
304 etype = GST_EVENT_STREAM_START;
309 event = gst_event_new_custom (etype, s);
311 *skip = length + offset;
317 GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
318 ("Buffer too small."), (NULL));
319 gst_buffer_unmap (buf, &map);
324 GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
325 ("event string not 0-terminated."), (NULL));
326 gst_buffer_unmap (buf, &map);
331 GST_WARNING_OBJECT (rtpgstdepay, "could not parse event");
336 GST_DEBUG_OBJECT (rtpgstdepay, "unknown event type");
337 gst_structure_free (s);
343 store_event (GstRtpGSTDepay * rtpgstdepay, GstEvent * event)
345 gboolean do_push = FALSE;
347 switch (GST_EVENT_TYPE (event)) {
350 GstTagList *old, *tags;
352 gst_event_parse_tag (event, &tags);
354 old = rtpgstdepay->tags;
355 if (!old || !gst_tag_list_is_equal (old, tags)) {
358 gst_tag_list_unref (old);
359 rtpgstdepay->tags = gst_tag_list_ref (tags);
363 case GST_EVENT_CUSTOM_DOWNSTREAM:
364 case GST_EVENT_CUSTOM_BOTH:
365 /* always push custom events */
368 case GST_EVENT_STREAM_START:
371 const gchar *stream_id = NULL;
373 gst_event_parse_stream_start (event, &stream_id);
375 old = rtpgstdepay->stream_id;
376 if (!old || g_strcmp0 (old, stream_id)) {
379 rtpgstdepay->stream_id = g_strdup (stream_id);
384 /* unknown event, don't push */
388 gst_pad_push_event (GST_RTP_BASE_DEPAYLOAD (rtpgstdepay)->srcpad, event);
390 gst_event_unref (event);
394 gst_rtp_gst_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
396 GstRtpGSTDepay *rtpgstdepay;
397 GstBuffer *subbuf, *outbuf = NULL;
400 guint CV, frag_offset, avail, offset;
402 rtpgstdepay = GST_RTP_GST_DEPAY (depayload);
404 payload_len = gst_rtp_buffer_get_payload_len (rtp);
406 if (payload_len <= 8)
409 if (GST_BUFFER_IS_DISCONT (rtp->buffer)) {
410 GST_WARNING_OBJECT (rtpgstdepay, "DISCONT, clear adapter");
411 gst_adapter_clear (rtpgstdepay->adapter);
414 payload = gst_rtp_buffer_get_payload (rtp);
419 * 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
420 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
421 * |C| CV |D|0|0|0| ETYPE | MBZ |
422 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
424 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
427 (payload[4] << 24) | (payload[5] << 16) | (payload[6] << 8) | payload[7];
429 avail = gst_adapter_available (rtpgstdepay->adapter);
430 if (avail != frag_offset)
433 /* subbuffer skipping the 8 header bytes */
434 subbuf = gst_rtp_buffer_get_payload_subbuffer (rtp, 8, -1);
435 gst_adapter_push (rtpgstdepay->adapter, subbuf);
438 if (gst_rtp_buffer_get_marker (rtp)) {
442 /* take the buffer */
443 avail = gst_adapter_available (rtpgstdepay->adapter);
444 outbuf = gst_adapter_take_buffer (rtpgstdepay->adapter, avail);
446 CV = (payload[0] >> 4) & 0x7;
448 if (payload[0] & 0x80) {
451 /* C bit, we have inline caps */
452 outcaps = read_caps (rtpgstdepay, outbuf, &size);
456 GST_DEBUG_OBJECT (rtpgstdepay,
457 "inline caps %u, length %u, %" GST_PTR_FORMAT, CV, size, outcaps);
459 if (!rtpgstdepay->current_caps
460 || !gst_caps_is_strictly_equal (rtpgstdepay->current_caps, outcaps))
461 gst_pad_set_caps (depayload->srcpad, outcaps);
462 gst_caps_replace (&rtpgstdepay->current_caps, outcaps);
463 gst_caps_unref (outcaps);
464 rtpgstdepay->current_CV = CV;
474 /* we have an event */
475 event = read_event (rtpgstdepay, payload[1], outbuf, &size);
479 GST_DEBUG_OBJECT (rtpgstdepay,
480 "inline event, length %u, %" GST_PTR_FORMAT, size, event);
482 store_event (rtpgstdepay, event);
484 /* no buffer after event */
492 GST_DEBUG_OBJECT (rtpgstdepay, "sub buffer: offset %u, size %u", offset,
496 gst_buffer_copy_region (outbuf, GST_BUFFER_COPY_ALL, offset, avail);
498 gst_buffer_unref (outbuf);
502 /* see what caps we need */
503 if (CV != rtpgstdepay->current_CV) {
504 /* we need to switch caps but didn't receive the new caps yet */
505 gst_caps_replace (&rtpgstdepay->current_caps, NULL);
509 if (payload[0] & 0x8)
510 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
512 gst_buffer_unref (outbuf);
518 gst_rtp_drop_meta (GST_ELEMENT_CAST (rtpgstdepay), outbuf, 0);
526 GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
527 ("Empty Payload."), (NULL));
532 gst_adapter_clear (rtpgstdepay->adapter);
533 GST_LOG_OBJECT (rtpgstdepay, "wrong fragment, skipping");
538 GST_WARNING_OBJECT (rtpgstdepay, "failed to parse caps");
539 gst_buffer_unref (outbuf);
544 GST_WARNING_OBJECT (rtpgstdepay, "failed to parse event");
545 gst_buffer_unref (outbuf);
550 GST_INFO_OBJECT (rtpgstdepay, "No caps received yet %u", CV);
551 gst_buffer_unref (outbuf);
553 gst_pad_push_event (GST_RTP_BASE_DEPAYLOAD_SINKPAD (rtpgstdepay),
554 gst_video_event_new_upstream_force_key_unit (GST_CLOCK_TIME_NONE,
562 gst_rtp_gst_depay_handle_event (GstRTPBaseDepayload * depay, GstEvent * event)
564 GstRtpGSTDepay *rtpgstdepay;
566 rtpgstdepay = GST_RTP_GST_DEPAY (depay);
568 switch (GST_EVENT_TYPE (event)) {
569 case GST_EVENT_FLUSH_STOP:
570 gst_rtp_gst_depay_reset (rtpgstdepay, FALSE);
577 GST_RTP_BASE_DEPAYLOAD_CLASS (parent_class)->handle_event (depay, event);
581 static GstStateChangeReturn
582 gst_rtp_gst_depay_change_state (GstElement * element, GstStateChange transition)
584 GstRtpGSTDepay *rtpgstdepay;
585 GstStateChangeReturn ret;
587 rtpgstdepay = GST_RTP_GST_DEPAY (element);
589 switch (transition) {
590 case GST_STATE_CHANGE_READY_TO_PAUSED:
591 gst_rtp_gst_depay_reset (rtpgstdepay, TRUE);
597 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
599 switch (transition) {
600 case GST_STATE_CHANGE_PAUSED_TO_READY:
601 gst_rtp_gst_depay_reset (rtpgstdepay, TRUE);