1 /* GStreamer RTP KLV Depayloader
2 * Copyright (C) 2014-2015 Tim-Philipp Müller <tim@centricular.com>>
3 * Copyright (C) 2014-2015 Centricular Ltd
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 * SECTION:element-rtpklvdepay
24 * @see_also: rtpklvpay
26 * Extract KLV metadata from RTP packets according to RFC 6597.
27 * For detailed information see: http://tools.ietf.org/html/rfc6597
31 * gst-launch-1.0 udpsrc caps='application/x-rtp, media=(string)application, clock-rate=(int)90000, encoding-name=(string)SMPTE336M' ! rtpklvdepay ! fakesink dump=true
32 * ]| This example pipeline will depayload an RTP KLV stream and display
33 * a hexdump of the KLV data on stdout.
40 #include "gstrtpelements.h"
41 #include "gstrtpklvdepay.h"
45 GST_DEBUG_CATEGORY_STATIC (klvdepay_debug);
46 #define GST_CAT_DEFAULT (klvdepay_debug)
48 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
51 GST_STATIC_CAPS ("meta/x-klv, parsed = (bool) true"));
53 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
56 GST_STATIC_CAPS ("application/x-rtp, "
57 "media = (string) application, clock-rate = (int) [1, MAX], "
58 "encoding-name = (string) SMPTE336M")
61 #define gst_rtp_klv_depay_parent_class parent_class
62 G_DEFINE_TYPE (GstRtpKlvDepay, gst_rtp_klv_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
63 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpklvdepay, "rtpklvdepay",
64 GST_RANK_SECONDARY, GST_TYPE_RTP_KLV_DEPAY, rtp_element_init (plugin));
66 static void gst_rtp_klv_depay_finalize (GObject * object);
68 static GstStateChangeReturn gst_rtp_klv_depay_change_state (GstElement *
69 element, GstStateChange transition);
70 static gboolean gst_rtp_klv_depay_setcaps (GstRTPBaseDepayload * depayload,
72 static GstBuffer *gst_rtp_klv_depay_process (GstRTPBaseDepayload * depayload,
74 static gboolean gst_rtp_klv_depay_handle_event (GstRTPBaseDepayload * depay,
77 static void gst_rtp_klv_depay_reset (GstRtpKlvDepay * klvdepay);
80 gst_rtp_klv_depay_class_init (GstRtpKlvDepayClass * klass)
82 GstElementClass *element_class = (GstElementClass *) klass;
83 GObjectClass *gobject_class = (GObjectClass *) klass;
84 GstRTPBaseDepayloadClass *rtpbasedepayload_class;
86 GST_DEBUG_CATEGORY_INIT (klvdepay_debug, "klvdepay", 0,
87 "RTP KLV Depayloader");
89 gobject_class->finalize = gst_rtp_klv_depay_finalize;
91 element_class->change_state = gst_rtp_klv_depay_change_state;
93 gst_element_class_add_static_pad_template (element_class, &src_template);
94 gst_element_class_add_static_pad_template (element_class, &sink_template);
96 gst_element_class_set_static_metadata (element_class,
97 "RTP KLV Depayloader", "Codec/Depayloader/Network/RTP",
98 "Extracts KLV (SMPTE ST 336) metadata from RTP packets",
99 "Tim-Philipp Müller <tim@centricular.com>");
101 rtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
103 rtpbasedepayload_class->set_caps = gst_rtp_klv_depay_setcaps;
104 rtpbasedepayload_class->process_rtp_packet = gst_rtp_klv_depay_process;
105 rtpbasedepayload_class->handle_event = gst_rtp_klv_depay_handle_event;
109 gst_rtp_klv_depay_init (GstRtpKlvDepay * klvdepay)
111 klvdepay->adapter = gst_adapter_new ();
115 gst_rtp_klv_depay_finalize (GObject * object)
117 GstRtpKlvDepay *klvdepay;
119 klvdepay = GST_RTP_KLV_DEPAY (object);
121 gst_rtp_klv_depay_reset (klvdepay);
122 g_object_unref (klvdepay->adapter);
124 G_OBJECT_CLASS (parent_class)->finalize (object);
128 gst_rtp_klv_depay_reset (GstRtpKlvDepay * klvdepay)
130 GST_DEBUG_OBJECT (klvdepay, "resetting");
131 gst_adapter_clear (klvdepay->adapter);
132 klvdepay->resync = TRUE;
133 klvdepay->last_rtp_ts = -1;
137 gst_rtp_klv_depay_handle_event (GstRTPBaseDepayload * depay, GstEvent * ev)
139 switch (GST_EVENT_TYPE (ev)) {
140 case GST_EVENT_STREAM_START:{
141 GstStreamFlags flags;
143 ev = gst_event_make_writable (ev);
144 gst_event_parse_stream_flags (ev, &flags);
145 gst_event_set_stream_flags (ev, flags | GST_STREAM_FLAG_SPARSE);
152 return GST_RTP_BASE_DEPAYLOAD_CLASS (parent_class)->handle_event (depay, ev);
156 gst_rtp_klv_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
163 s = gst_caps_get_structure (caps, 0);
165 if (!gst_structure_get_int (s, "clock-rate", &clock_rate))
168 depayload->clock_rate = clock_rate;
170 src_caps = gst_static_pad_template_get_caps (&src_template);
171 res = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), src_caps);
172 gst_caps_unref (src_caps);
178 klv_get_vlen (const guint8 * data, guint data_len, guint64 * v_len,
181 guint8 first_byte, len_len;
184 g_assert (data_len > 0);
186 first_byte = *data++;
188 if ((first_byte & 0x80) == 0) {
189 *v_len = first_byte & 0x7f;
194 len_len = first_byte & 0x7f;
196 if (len_len == 0 || len_len > 8)
199 if ((1 + len_len) > data_len)
202 *len_size = 1 + len_len;
205 while (len_len > 0) {
206 len = len << 8 | *data++;
216 gst_rtp_klv_depay_process_data (GstRtpKlvDepay * klvdepay)
218 gsize avail, data_len, len_size;
223 avail = gst_adapter_available (klvdepay->adapter);
225 GST_TRACE_OBJECT (klvdepay, "%" G_GSIZE_FORMAT " bytes in adapter", avail);
230 /* need at least 16 bytes of UL key plus 1 byte of length */
234 /* check if the declared KLV unit size matches actual bytes available */
235 data_len = MIN (avail - 16, 1 + 8);
236 gst_adapter_copy (klvdepay->adapter, data, 16, data_len);
237 if (!klv_get_vlen (data, data_len, &v_len, &len_size))
240 GST_LOG_OBJECT (klvdepay, "want %" G_GUINT64_FORMAT " bytes, "
241 "have %" G_GSIZE_FORMAT " bytes", 16 + len_size + v_len, avail);
243 if (avail < 16 + len_size + v_len)
244 goto incomplete_klv_packet;
246 /* something is wrong, this shouldn't ever happen */
247 if (avail > 16 + len_size + v_len)
250 outbuf = gst_adapter_take_buffer (klvdepay->adapter, avail);
252 /* Mark buffers as key unit to signal this is the start of a KLV unit
253 * (for now all buffers will be flagged like this, since all buffers are
254 * self-contained KLV units, but in future that might change) */
255 outbuf = gst_buffer_make_writable (outbuf);
256 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
263 GST_WARNING_OBJECT (klvdepay, "bad KLV packet, dropping");
264 gst_rtp_klv_depay_reset (klvdepay);
267 incomplete_klv_packet:
269 GST_DEBUG_OBJECT (klvdepay, "partial KLV packet: have %u bytes, want %u",
270 (guint) avail, (guint) (16 + len_size + v_len));
275 /* We're trying to be pragmatic here, not quite as strict as the spec wants
276 * us to be with regard to marker bits and resyncing after packet loss */
278 gst_rtp_klv_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
280 GstRtpKlvDepay *klvdepay = GST_RTP_KLV_DEPAY (depayload);
281 GstBuffer *payload, *outbuf = NULL;
282 gboolean marker, start = FALSE, maybe_start;
287 /* Ignore DISCONT on first buffer and on buffers following a discont */
288 if (GST_BUFFER_IS_DISCONT (rtp->buffer) && klvdepay->last_rtp_ts != -1) {
289 GST_WARNING_OBJECT (klvdepay, "DISCONT, need to resync");
290 gst_rtp_klv_depay_reset (klvdepay);
293 payload_len = gst_rtp_buffer_get_payload_len (rtp);
295 /* marker bit signals last fragment of a KLV unit */
296 marker = gst_rtp_buffer_get_marker (rtp);
298 seq = gst_rtp_buffer_get_seq (rtp);
300 /* packet directly after one with marker bit set => start */
301 start = klvdepay->last_marker_seq != -1
302 && gst_rtp_buffer_compare_seqnum (klvdepay->last_marker_seq, seq) == 1;
304 /* deduce start of new KLV unit in case sender doesn't set marker bits
305 * (it's not like the spec is ambiguous about that, but what can you do) */
306 rtp_ts = gst_rtp_buffer_get_timestamp (rtp);
308 maybe_start = klvdepay->last_rtp_ts == -1 || klvdepay->last_rtp_ts != rtp_ts;
310 klvdepay->last_rtp_ts = rtp_ts;
312 /* fallback to detect self-contained single KLV unit (usual case) */
313 if ((!start || !marker || maybe_start) && payload_len > 16) {
318 data = gst_rtp_buffer_get_payload (rtp);
319 if (GST_READ_UINT32_BE (data) == 0x060e2b34 &&
320 klv_get_vlen (data + 16, payload_len - 16, &v_len, &len_size)) {
321 if (16 + len_size + v_len == payload_len) {
322 GST_LOG_OBJECT (klvdepay, "Looks like a self-contained KLV unit");
325 } else if (16 + len_size + v_len > payload_len) {
326 GST_LOG_OBJECT (klvdepay,
327 "Looks like the start of a fragmented KLV unit");
333 /* If this is the first packet and looks like a start, clear resync flag */
334 if (klvdepay->resync && klvdepay->last_marker_seq == -1 && start)
335 klvdepay->resync = FALSE;
338 klvdepay->last_marker_seq = seq;
340 GST_LOG_OBJECT (klvdepay, "payload of %u bytes, marker=%d, start=%d",
341 payload_len, marker, start);
343 if (klvdepay->resync && !start) {
344 GST_DEBUG_OBJECT (klvdepay, "Dropping buffer, waiting to resync");
347 klvdepay->resync = FALSE;
352 if (start && !marker)
353 outbuf = gst_rtp_klv_depay_process_data (klvdepay);
355 payload = gst_rtp_buffer_get_payload_buffer (rtp);
356 gst_adapter_push (klvdepay->adapter, payload);
359 outbuf = gst_rtp_klv_depay_process_data (klvdepay);
366 static GstStateChangeReturn
367 gst_rtp_klv_depay_change_state (GstElement * element, GstStateChange transition)
369 GstRtpKlvDepay *klvdepay;
370 GstStateChangeReturn ret;
372 klvdepay = GST_RTP_KLV_DEPAY (element);
374 switch (transition) {
375 case GST_STATE_CHANGE_READY_TO_PAUSED:
376 gst_rtp_klv_depay_reset (klvdepay);
377 klvdepay->last_marker_seq = -1;
383 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
385 switch (transition) {
386 case GST_STATE_CHANGE_PAUSED_TO_READY:
387 gst_rtp_klv_depay_reset (klvdepay);