webrtc/nice: support consent-freshness RFC7675
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-good / gst / rtp / gstrtpklvdepay.c
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  * @title: rtpklvdepay
24  * @see_also: rtpklvpay
25  *
26  * Extract KLV metadata from RTP packets according to RFC 6597.
27  * For detailed information see: http://tools.ietf.org/html/rfc6597
28  *
29  * ## Example pipeline
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  *
35  */
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include "gstrtpelements.h"
41 #include "gstrtpklvdepay.h"
42
43 #include <string.h>
44
45 GST_DEBUG_CATEGORY_STATIC (klvdepay_debug);
46 #define GST_CAT_DEFAULT (klvdepay_debug)
47
48 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
49     GST_PAD_SRC,
50     GST_PAD_ALWAYS,
51     GST_STATIC_CAPS ("meta/x-klv, parsed = (bool) true"));
52
53 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
54     GST_PAD_SINK,
55     GST_PAD_ALWAYS,
56     GST_STATIC_CAPS ("application/x-rtp, "
57         "media = (string) application, clock-rate = (int) [1, MAX], "
58         "encoding-name = (string) SMPTE336M")
59     );
60
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));
65
66 static void gst_rtp_klv_depay_finalize (GObject * object);
67
68 static GstStateChangeReturn gst_rtp_klv_depay_change_state (GstElement *
69     element, GstStateChange transition);
70 static gboolean gst_rtp_klv_depay_setcaps (GstRTPBaseDepayload * depayload,
71     GstCaps * caps);
72 static GstBuffer *gst_rtp_klv_depay_process (GstRTPBaseDepayload * depayload,
73     GstRTPBuffer * rtp);
74 static gboolean gst_rtp_klv_depay_handle_event (GstRTPBaseDepayload * depay,
75     GstEvent * ev);
76
77 static void gst_rtp_klv_depay_reset (GstRtpKlvDepay * klvdepay);
78
79 static void
80 gst_rtp_klv_depay_class_init (GstRtpKlvDepayClass * klass)
81 {
82   GstElementClass *element_class = (GstElementClass *) klass;
83   GObjectClass *gobject_class = (GObjectClass *) klass;
84   GstRTPBaseDepayloadClass *rtpbasedepayload_class;
85
86   GST_DEBUG_CATEGORY_INIT (klvdepay_debug, "klvdepay", 0,
87       "RTP KLV Depayloader");
88
89   gobject_class->finalize = gst_rtp_klv_depay_finalize;
90
91   element_class->change_state = gst_rtp_klv_depay_change_state;
92
93   gst_element_class_add_static_pad_template (element_class, &src_template);
94   gst_element_class_add_static_pad_template (element_class, &sink_template);
95
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>");
100
101   rtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
102
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;
106 }
107
108 static void
109 gst_rtp_klv_depay_init (GstRtpKlvDepay * klvdepay)
110 {
111   klvdepay->adapter = gst_adapter_new ();
112 }
113
114 static void
115 gst_rtp_klv_depay_finalize (GObject * object)
116 {
117   GstRtpKlvDepay *klvdepay;
118
119   klvdepay = GST_RTP_KLV_DEPAY (object);
120
121   gst_rtp_klv_depay_reset (klvdepay);
122   g_object_unref (klvdepay->adapter);
123
124   G_OBJECT_CLASS (parent_class)->finalize (object);
125 }
126
127 static void
128 gst_rtp_klv_depay_reset (GstRtpKlvDepay * klvdepay)
129 {
130   GST_DEBUG_OBJECT (klvdepay, "resetting");
131   gst_adapter_clear (klvdepay->adapter);
132   klvdepay->resync = TRUE;
133   klvdepay->last_rtp_ts = -1;
134 }
135
136 static gboolean
137 gst_rtp_klv_depay_handle_event (GstRTPBaseDepayload * depay, GstEvent * ev)
138 {
139   switch (GST_EVENT_TYPE (ev)) {
140     case GST_EVENT_STREAM_START:{
141       GstStreamFlags flags;
142
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);
146       break;
147     }
148     default:
149       break;
150   }
151
152   return GST_RTP_BASE_DEPAYLOAD_CLASS (parent_class)->handle_event (depay, ev);
153 }
154
155 static gboolean
156 gst_rtp_klv_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
157 {
158   GstStructure *s;
159   GstCaps *src_caps;
160   gboolean res;
161   gint clock_rate;
162
163   s = gst_caps_get_structure (caps, 0);
164
165   if (!gst_structure_get_int (s, "clock-rate", &clock_rate))
166     return FALSE;
167
168   depayload->clock_rate = clock_rate;
169
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);
173
174   return res;
175 }
176
177 static gboolean
178 klv_get_vlen (const guint8 * data, guint data_len, guint64 * v_len,
179     gsize * len_size)
180 {
181   guint8 first_byte, len_len;
182   guint64 len;
183
184   g_assert (data_len > 0);
185
186   first_byte = *data++;
187
188   if ((first_byte & 0x80) == 0) {
189     *v_len = first_byte & 0x7f;
190     *len_size = 1;
191     return TRUE;
192   }
193
194   len_len = first_byte & 0x7f;
195
196   if (len_len == 0 || len_len > 8)
197     return FALSE;
198
199   if ((1 + len_len) > data_len)
200     return FALSE;
201
202   *len_size = 1 + len_len;
203
204   len = 0;
205   while (len_len > 0) {
206     len = len << 8 | *data++;
207     --len_len;
208   }
209
210   *v_len = len;
211
212   return TRUE;
213 }
214
215 static GstBuffer *
216 gst_rtp_klv_depay_process_data (GstRtpKlvDepay * klvdepay)
217 {
218   gsize avail, data_len, len_size;
219   GstBuffer *outbuf;
220   guint8 data[1 + 8];
221   guint64 v_len;
222
223   avail = gst_adapter_available (klvdepay->adapter);
224
225   GST_TRACE_OBJECT (klvdepay, "%" G_GSIZE_FORMAT " bytes in adapter", avail);
226
227   if (avail == 0)
228     return NULL;
229
230   /* need at least 16 bytes of UL key plus 1 byte of length */
231   if (avail < 16 + 1)
232     goto bad_klv_packet;
233
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))
238     goto bad_klv_packet;
239
240   GST_LOG_OBJECT (klvdepay, "want %" G_GUINT64_FORMAT " bytes, "
241       "have %" G_GSIZE_FORMAT " bytes", 16 + len_size + v_len, avail);
242
243   if (avail < 16 + len_size + v_len)
244     goto incomplete_klv_packet;
245
246   /* something is wrong, this shouldn't ever happen */
247   if (avail > 16 + len_size + v_len)
248     goto bad_klv_packet;
249
250   outbuf = gst_adapter_take_buffer (klvdepay->adapter, avail);
251
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);
257
258   return outbuf;
259
260 /* ERRORS */
261 bad_klv_packet:
262   {
263     GST_WARNING_OBJECT (klvdepay, "bad KLV packet, dropping");
264     gst_rtp_klv_depay_reset (klvdepay);
265     return NULL;
266   }
267 incomplete_klv_packet:
268   {
269     GST_DEBUG_OBJECT (klvdepay, "partial KLV packet: have %u bytes, want %u",
270         (guint) avail, (guint) (16 + len_size + v_len));
271     return NULL;
272   }
273 }
274
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 */
277 static GstBuffer *
278 gst_rtp_klv_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
279 {
280   GstRtpKlvDepay *klvdepay = GST_RTP_KLV_DEPAY (depayload);
281   GstBuffer *payload, *outbuf = NULL;
282   gboolean marker, start = FALSE, maybe_start;
283   guint32 rtp_ts;
284   guint16 seq;
285   guint payload_len;
286
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);
291   }
292
293   payload_len = gst_rtp_buffer_get_payload_len (rtp);
294
295   /* marker bit signals last fragment of a KLV unit */
296   marker = gst_rtp_buffer_get_marker (rtp);
297
298   seq = gst_rtp_buffer_get_seq (rtp);
299
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;
303
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);
307
308   maybe_start = klvdepay->last_rtp_ts == -1 || klvdepay->last_rtp_ts != rtp_ts;
309
310   klvdepay->last_rtp_ts = rtp_ts;
311
312   /* fallback to detect self-contained single KLV unit (usual case) */
313   if ((!start || !marker || maybe_start) && payload_len > 16) {
314     const guint8 *data;
315     guint64 v_len;
316     gsize len_size;
317
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");
323         marker = TRUE;
324         start = TRUE;
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");
328         start = TRUE;
329       }
330     }
331   }
332
333   /* If this looks like a start, clear the resync flag */
334   if (klvdepay->resync && start)
335     klvdepay->resync = FALSE;
336
337   if (marker)
338     klvdepay->last_marker_seq = seq;
339
340   GST_LOG_OBJECT (klvdepay, "payload of %u bytes, marker=%d, start=%d",
341       payload_len, marker, start);
342
343   if (klvdepay->resync && !start) {
344     GST_DEBUG_OBJECT (klvdepay, "Dropping buffer, waiting to resync");
345
346     if (marker)
347       klvdepay->resync = FALSE;
348
349     goto done;
350   }
351
352   if (start && !marker)
353     outbuf = gst_rtp_klv_depay_process_data (klvdepay);
354
355   payload = gst_rtp_buffer_get_payload_buffer (rtp);
356   gst_adapter_push (klvdepay->adapter, payload);
357
358   if (marker)
359     outbuf = gst_rtp_klv_depay_process_data (klvdepay);
360
361 done:
362
363   return outbuf;
364 }
365
366 static GstStateChangeReturn
367 gst_rtp_klv_depay_change_state (GstElement * element, GstStateChange transition)
368 {
369   GstRtpKlvDepay *klvdepay;
370   GstStateChangeReturn ret;
371
372   klvdepay = GST_RTP_KLV_DEPAY (element);
373
374   switch (transition) {
375     case GST_STATE_CHANGE_READY_TO_PAUSED:
376       gst_rtp_klv_depay_reset (klvdepay);
377       klvdepay->last_marker_seq = -1;
378       break;
379     default:
380       break;
381   }
382
383   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
384
385   switch (transition) {
386     case GST_STATE_CHANGE_PAUSED_TO_READY:
387       gst_rtp_klv_depay_reset (klvdepay);
388       break;
389     default:
390       break;
391   }
392   return ret;
393 }