1 /* GStreamer RTP KLV Depayloader
2  * Copyright (C) 2014-2015 Tim-Philipp Müller <tim@centricular.com>>
3  * Copyright (C) 2014-2015 Centricular Ltd
4  *
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.
9  *
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.
14  *
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.
19  */
20 
21 /**
22  * SECTION:element-rtpklvdepay
23  * @see_also: rtpklvpay
24  *
25  * Extract KLV metadata from RTP packets according to RFC 6597.
26  * For detailed information see: http://tools.ietf.org/html/rfc6597
27  *
28  * <refsect2>
29  * <title>Example pipeline</title>
30  * |[
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.
34  * </refsect2>
35  */
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39 
40 #include "gstrtpklvdepay.h"
41 
42 #include <string.h>
43 
44 GST_DEBUG_CATEGORY_STATIC (klvdepay_debug);
45 #define GST_CAT_DEFAULT (klvdepay_debug)
46 
47 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
48     GST_PAD_SRC,
49     GST_PAD_ALWAYS,
50     GST_STATIC_CAPS ("meta/x-klv, parsed = (bool) true"));
51 
52 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
53     GST_PAD_SINK,
54     GST_PAD_ALWAYS,
55     GST_STATIC_CAPS ("application/x-rtp, "
56         "media = (string) application, clock-rate = (int) [1, MAX], "
57         "encoding-name = (string) SMPTE336M")
58     );
59 
60 #define gst_rtp_klv_depay_parent_class parent_class
61 G_DEFINE_TYPE (GstRtpKlvDepay, gst_rtp_klv_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
62 
63 static void gst_rtp_klv_depay_finalize (GObject * object);
64 
65 static GstStateChangeReturn gst_rtp_klv_depay_change_state (GstElement *
66     element, GstStateChange transition);
67 static gboolean gst_rtp_klv_depay_setcaps (GstRTPBaseDepayload * depayload,
68     GstCaps * caps);
69 static GstBuffer *gst_rtp_klv_depay_process (GstRTPBaseDepayload * depayload,
70     GstRTPBuffer * rtp);
71 static gboolean gst_rtp_klv_depay_handle_event (GstRTPBaseDepayload * depay,
72     GstEvent * ev);
73 
74 static void gst_rtp_klv_depay_reset (GstRtpKlvDepay * klvdepay);
75 
76 static void
gst_rtp_klv_depay_class_init(GstRtpKlvDepayClass * klass)77 gst_rtp_klv_depay_class_init (GstRtpKlvDepayClass * klass)
78 {
79   GstElementClass *element_class = (GstElementClass *) klass;
80   GObjectClass *gobject_class = (GObjectClass *) klass;
81   GstRTPBaseDepayloadClass *rtpbasedepayload_class;
82 
83   GST_DEBUG_CATEGORY_INIT (klvdepay_debug, "klvdepay", 0,
84       "RTP KLV Depayloader");
85 
86   gobject_class->finalize = gst_rtp_klv_depay_finalize;
87 
88   element_class->change_state = gst_rtp_klv_depay_change_state;
89 
90   gst_element_class_add_static_pad_template (element_class, &src_template);
91   gst_element_class_add_static_pad_template (element_class, &sink_template);
92 
93   gst_element_class_set_static_metadata (element_class,
94       "RTP KLV Depayloader", "Codec/Depayloader/Network/RTP",
95       "Extracts KLV (SMPTE ST 336) metadata from RTP packets",
96       "Tim-Philipp Müller <tim@centricular.com>");
97 
98   rtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
99 
100   rtpbasedepayload_class->set_caps = gst_rtp_klv_depay_setcaps;
101   rtpbasedepayload_class->process_rtp_packet = gst_rtp_klv_depay_process;
102   rtpbasedepayload_class->handle_event = gst_rtp_klv_depay_handle_event;
103 }
104 
105 static void
gst_rtp_klv_depay_init(GstRtpKlvDepay * klvdepay)106 gst_rtp_klv_depay_init (GstRtpKlvDepay * klvdepay)
107 {
108   klvdepay->adapter = gst_adapter_new ();
109 }
110 
111 static void
gst_rtp_klv_depay_finalize(GObject * object)112 gst_rtp_klv_depay_finalize (GObject * object)
113 {
114   GstRtpKlvDepay *klvdepay;
115 
116   klvdepay = GST_RTP_KLV_DEPAY (object);
117 
118   gst_rtp_klv_depay_reset (klvdepay);
119   g_object_unref (klvdepay->adapter);
120 
121   G_OBJECT_CLASS (parent_class)->finalize (object);
122 }
123 
124 static void
gst_rtp_klv_depay_reset(GstRtpKlvDepay * klvdepay)125 gst_rtp_klv_depay_reset (GstRtpKlvDepay * klvdepay)
126 {
127   GST_DEBUG_OBJECT (klvdepay, "resetting");
128   gst_adapter_clear (klvdepay->adapter);
129   klvdepay->resync = TRUE;
130   klvdepay->last_rtp_ts = -1;
131 }
132 
133 static gboolean
gst_rtp_klv_depay_handle_event(GstRTPBaseDepayload * depay,GstEvent * ev)134 gst_rtp_klv_depay_handle_event (GstRTPBaseDepayload * depay, GstEvent * ev)
135 {
136   switch (GST_EVENT_TYPE (ev)) {
137     case GST_EVENT_STREAM_START:{
138       GstStreamFlags flags;
139 
140       ev = gst_event_make_writable (ev);
141       gst_event_parse_stream_flags (ev, &flags);
142       gst_event_set_stream_flags (ev, flags | GST_STREAM_FLAG_SPARSE);
143       break;
144     }
145     default:
146       break;
147   }
148 
149   return GST_RTP_BASE_DEPAYLOAD_CLASS (parent_class)->handle_event (depay, ev);
150 }
151 
152 static gboolean
gst_rtp_klv_depay_setcaps(GstRTPBaseDepayload * depayload,GstCaps * caps)153 gst_rtp_klv_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
154 {
155   GstStructure *s;
156   GstCaps *src_caps;
157   gboolean res;
158   gint clock_rate;
159 
160   s = gst_caps_get_structure (caps, 0);
161 
162   if (!gst_structure_get_int (s, "clock-rate", &clock_rate))
163     return FALSE;
164 
165   depayload->clock_rate = clock_rate;
166 
167   src_caps = gst_static_pad_template_get_caps (&src_template);
168   res = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), src_caps);
169   gst_caps_unref (src_caps);
170 
171   return res;
172 }
173 
174 static gboolean
klv_get_vlen(const guint8 * data,guint data_len,guint64 * v_len,gsize * len_size)175 klv_get_vlen (const guint8 * data, guint data_len, guint64 * v_len,
176     gsize * len_size)
177 {
178   guint8 first_byte, len_len;
179   guint64 len;
180 
181   g_assert (data_len > 0);
182 
183   first_byte = *data++;
184 
185   if ((first_byte & 0x80) == 0) {
186     *v_len = first_byte & 0x7f;
187     *len_size = 1;
188     return TRUE;
189   }
190 
191   len_len = first_byte & 0x7f;
192 
193   if (len_len == 0 || len_len > 8)
194     return FALSE;
195 
196   if ((1 + len_len) > data_len)
197     return FALSE;
198 
199   *len_size = 1 + len_len;
200 
201   len = 0;
202   while (len_len > 0) {
203     len = len << 8 | *data++;
204     --len_len;
205   }
206 
207   *v_len = len;
208 
209   return TRUE;
210 }
211 
212 static GstBuffer *
gst_rtp_klv_depay_process_data(GstRtpKlvDepay * klvdepay)213 gst_rtp_klv_depay_process_data (GstRtpKlvDepay * klvdepay)
214 {
215   gsize avail, data_len, len_size;
216   GstBuffer *outbuf;
217   guint8 data[1 + 8];
218   guint64 v_len;
219 
220   avail = gst_adapter_available (klvdepay->adapter);
221 
222   GST_TRACE_OBJECT (klvdepay, "%" G_GSIZE_FORMAT " bytes in adapter", avail);
223 
224   if (avail == 0)
225     return NULL;
226 
227   /* need at least 16 bytes of UL key plus 1 byte of length */
228   if (avail < 16 + 1)
229     goto bad_klv_packet;
230 
231   /* check if the declared KLV unit size matches actual bytes available */
232   data_len = MIN (avail - 16, 1 + 8);
233   gst_adapter_copy (klvdepay->adapter, data, 16, data_len);
234   if (!klv_get_vlen (data, data_len, &v_len, &len_size))
235     goto bad_klv_packet;
236 
237   GST_LOG_OBJECT (klvdepay, "want %" G_GUINT64_FORMAT " bytes, "
238       "have %" G_GSIZE_FORMAT " bytes", 16 + len_size + v_len, avail);
239 
240   if (avail < 16 + len_size + v_len)
241     goto incomplete_klv_packet;
242 
243   /* something is wrong, this shouldn't ever happen */
244   if (avail > 16 + len_size + v_len)
245     goto bad_klv_packet;
246 
247   outbuf = gst_adapter_take_buffer (klvdepay->adapter, avail);
248 
249   /* Mark buffers as key unit to signal this is the start of a KLV unit
250    * (for now all buffers will be flagged like this, since all buffers are
251    * self-contained KLV units, but in future that might change) */
252   outbuf = gst_buffer_make_writable (outbuf);
253   GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
254 
255   return outbuf;
256 
257 /* ERRORS */
258 bad_klv_packet:
259   {
260     GST_WARNING_OBJECT (klvdepay, "bad KLV packet, dropping");
261     gst_rtp_klv_depay_reset (klvdepay);
262     return NULL;
263   }
264 incomplete_klv_packet:
265   {
266     GST_DEBUG_OBJECT (klvdepay, "partial KLV packet: have %u bytes, want %u",
267         (guint) avail, (guint) (16 + len_size + v_len));
268     return NULL;
269   }
270 }
271 
272 /* We're trying to be pragmatic here, not quite as strict as the spec wants
273  * us to be with regard to marker bits and resyncing after packet loss */
274 static GstBuffer *
gst_rtp_klv_depay_process(GstRTPBaseDepayload * depayload,GstRTPBuffer * rtp)275 gst_rtp_klv_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
276 {
277   GstRtpKlvDepay *klvdepay = GST_RTP_KLV_DEPAY (depayload);
278   GstBuffer *payload, *outbuf = NULL;
279   gboolean marker, start = FALSE, maybe_start;
280   guint32 rtp_ts;
281   guint16 seq;
282   guint payload_len;
283 
284   /* Ignore DISCONT on first buffer and on buffers following a discont */
285   if (GST_BUFFER_IS_DISCONT (rtp->buffer) && klvdepay->last_rtp_ts != -1) {
286     GST_WARNING_OBJECT (klvdepay, "DISCONT, need to resync");
287     gst_rtp_klv_depay_reset (klvdepay);
288   }
289 
290   payload_len = gst_rtp_buffer_get_payload_len (rtp);
291 
292   /* marker bit signals last fragment of a KLV unit */
293   marker = gst_rtp_buffer_get_marker (rtp);
294 
295   seq = gst_rtp_buffer_get_seq (rtp);
296 
297   /* packet directly after one with marker bit set => start */
298   start = klvdepay->last_marker_seq != -1
299       && gst_rtp_buffer_compare_seqnum (klvdepay->last_marker_seq, seq) == 1;
300 
301   /* deduce start of new KLV unit in case sender doesn't set marker bits
302    * (it's not like the spec is ambiguous about that, but what can you do) */
303   rtp_ts = gst_rtp_buffer_get_timestamp (rtp);
304 
305   maybe_start = klvdepay->last_rtp_ts == -1 || klvdepay->last_rtp_ts != rtp_ts;
306 
307   klvdepay->last_rtp_ts = rtp_ts;
308 
309   /* fallback to detect self-contained single KLV unit (usual case) */
310   if ((!start || !marker || maybe_start) && payload_len > 16) {
311     const guint8 *data;
312     guint64 v_len;
313     gsize len_size;
314 
315     data = gst_rtp_buffer_get_payload (rtp);
316     if (GST_READ_UINT32_BE (data) == 0x060e2b34 &&
317         klv_get_vlen (data + 16, payload_len - 16, &v_len, &len_size)) {
318       if (16 + len_size + v_len == payload_len) {
319         GST_LOG_OBJECT (klvdepay, "Looks like a self-contained KLV unit");
320         marker = TRUE;
321         start = TRUE;
322       } else if (16 + len_size + v_len > payload_len) {
323         GST_LOG_OBJECT (klvdepay,
324             "Looks like the start of a fragmented KLV unit");
325         start = TRUE;
326       }
327     }
328   }
329 
330   /* If this is the first packet and looks like a start, clear resync flag */
331   if (klvdepay->resync && klvdepay->last_marker_seq == -1 && start)
332     klvdepay->resync = FALSE;
333 
334   if (marker)
335     klvdepay->last_marker_seq = seq;
336 
337   GST_LOG_OBJECT (klvdepay, "payload of %u bytes, marker=%d, start=%d",
338       payload_len, marker, start);
339 
340   if (klvdepay->resync && !start) {
341     GST_DEBUG_OBJECT (klvdepay, "Dropping buffer, waiting to resync");
342 
343     if (marker)
344       klvdepay->resync = FALSE;
345 
346     goto done;
347   }
348 
349   if (start && !marker)
350     outbuf = gst_rtp_klv_depay_process_data (klvdepay);
351 
352   payload = gst_rtp_buffer_get_payload_buffer (rtp);
353   gst_adapter_push (klvdepay->adapter, payload);
354 
355   if (marker)
356     outbuf = gst_rtp_klv_depay_process_data (klvdepay);
357 
358 done:
359 
360   return outbuf;
361 }
362 
363 static GstStateChangeReturn
gst_rtp_klv_depay_change_state(GstElement * element,GstStateChange transition)364 gst_rtp_klv_depay_change_state (GstElement * element, GstStateChange transition)
365 {
366   GstRtpKlvDepay *klvdepay;
367   GstStateChangeReturn ret;
368 
369   klvdepay = GST_RTP_KLV_DEPAY (element);
370 
371   switch (transition) {
372     case GST_STATE_CHANGE_READY_TO_PAUSED:
373       gst_rtp_klv_depay_reset (klvdepay);
374       klvdepay->last_marker_seq = -1;
375       break;
376     default:
377       break;
378   }
379 
380   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
381 
382   switch (transition) {
383     case GST_STATE_CHANGE_PAUSED_TO_READY:
384       gst_rtp_klv_depay_reset (klvdepay);
385       break;
386     default:
387       break;
388   }
389   return ret;
390 }
391 
392 gboolean
gst_rtp_klv_depay_plugin_init(GstPlugin * plugin)393 gst_rtp_klv_depay_plugin_init (GstPlugin * plugin)
394 {
395   return gst_element_register (plugin, "rtpklvdepay",
396       GST_RANK_SECONDARY, GST_TYPE_RTP_KLV_DEPAY);
397 }
398