rtpvorbisdepay: remove dead code
[platform/upstream/gst-plugins-good.git] / 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  * @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
72 static void gst_rtp_klv_depay_reset (GstRtpKlvDepay * klvdepay);
73
74 static void
75 gst_rtp_klv_depay_class_init (GstRtpKlvDepayClass * klass)
76 {
77   GstElementClass *element_class = (GstElementClass *) klass;
78   GObjectClass *gobject_class = (GObjectClass *) klass;
79   GstRTPBaseDepayloadClass *rtpbasedepayload_class;
80
81   GST_DEBUG_CATEGORY_INIT (klvdepay_debug, "klvdepay", 0,
82       "RTP KLV Depayloader");
83
84   gobject_class->finalize = gst_rtp_klv_depay_finalize;
85
86   element_class->change_state = gst_rtp_klv_depay_change_state;
87
88   gst_element_class_add_static_pad_template (element_class, &src_template);
89   gst_element_class_add_static_pad_template (element_class, &sink_template);
90
91   gst_element_class_set_static_metadata (element_class,
92       "RTP KLV Depayloader", "Codec/Depayloader/Network/RTP",
93       "Extracts KLV (SMPTE ST 336) metadata from RTP packets",
94       "Tim-Philipp Müller <tim@centricular.com>");
95
96   rtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
97
98   rtpbasedepayload_class->set_caps = gst_rtp_klv_depay_setcaps;
99   rtpbasedepayload_class->process_rtp_packet = gst_rtp_klv_depay_process;
100 }
101
102 static void
103 gst_rtp_klv_depay_init (GstRtpKlvDepay * klvdepay)
104 {
105   klvdepay->adapter = gst_adapter_new ();
106 }
107
108 static void
109 gst_rtp_klv_depay_finalize (GObject * object)
110 {
111   GstRtpKlvDepay *klvdepay;
112
113   klvdepay = GST_RTP_KLV_DEPAY (object);
114
115   gst_rtp_klv_depay_reset (klvdepay);
116   g_object_unref (klvdepay->adapter);
117
118   G_OBJECT_CLASS (parent_class)->finalize (object);
119 }
120
121 static void
122 gst_rtp_klv_depay_reset (GstRtpKlvDepay * klvdepay)
123 {
124   GST_DEBUG_OBJECT (klvdepay, "resetting");
125   gst_adapter_clear (klvdepay->adapter);
126   klvdepay->resync = TRUE;
127   klvdepay->last_rtp_ts = -1;
128 }
129
130 static gboolean
131 gst_rtp_klv_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
132 {
133   GstStructure *s;
134   GstCaps *src_caps;
135   gboolean res;
136   gint clock_rate;
137
138   s = gst_caps_get_structure (caps, 0);
139
140   if (!gst_structure_get_int (s, "clock-rate", &clock_rate))
141     return FALSE;
142
143   depayload->clock_rate = clock_rate;
144
145   src_caps = gst_static_pad_template_get_caps (&src_template);
146   res = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), src_caps);
147   gst_caps_unref (src_caps);
148
149   return res;
150 }
151
152 static gboolean
153 klv_get_vlen (const guint8 * data, guint data_len, guint64 * v_len,
154     gsize * len_size)
155 {
156   guint8 first_byte, len_len;
157   guint64 len;
158
159   g_assert (data_len > 0);
160
161   first_byte = *data++;
162
163   if ((first_byte & 0x80) == 0) {
164     *v_len = first_byte & 0x7f;
165     *len_size = 1;
166     return TRUE;
167   }
168
169   len_len = first_byte & 0x7f;
170
171   if (len_len == 0 || len_len > 8)
172     return FALSE;
173
174   if ((1 + len_len) > data_len)
175     return FALSE;
176
177   *len_size = 1 + len_len;
178
179   len = 0;
180   while (len_len > 0) {
181     len = len << 8 | *data++;
182     --len_len;
183   }
184
185   *v_len = len;
186
187   return TRUE;
188 }
189
190 static GstBuffer *
191 gst_rtp_klv_depay_process_data (GstRtpKlvDepay * klvdepay)
192 {
193   gsize avail, data_len, len_size;
194   GstBuffer *outbuf;
195   guint8 data[1 + 8];
196   guint64 v_len;
197
198   avail = gst_adapter_available (klvdepay->adapter);
199
200   GST_TRACE_OBJECT (klvdepay, "%" G_GSIZE_FORMAT " bytes in adapter", avail);
201
202   if (avail == 0)
203     return NULL;
204
205   /* need at least 16 bytes of UL key plus 1 byte of length */
206   if (avail < 16 + 1)
207     goto bad_klv_packet;
208
209   /* check if the declared KLV unit size matches actual bytes available */
210   data_len = MIN (avail - 16, 1 + 8);
211   gst_adapter_copy (klvdepay->adapter, data, 16, data_len);
212   if (!klv_get_vlen (data, data_len, &v_len, &len_size))
213     goto bad_klv_packet;
214
215   GST_LOG_OBJECT (klvdepay, "want %" G_GUINT64_FORMAT " bytes, "
216       "have %" G_GSIZE_FORMAT " bytes", 16 + len_size + v_len, avail);
217
218   if (avail < 16 + len_size + v_len)
219     goto incomplete_klv_packet;
220
221   /* something is wrong, this shouldn't ever happen */
222   if (avail > 16 + len_size + v_len)
223     goto bad_klv_packet;
224
225   outbuf = gst_adapter_take_buffer (klvdepay->adapter, avail);
226
227   /* Mark buffers as key unit to signal this is the start of a KLV unit
228    * (for now all buffers will be flagged like this, since all buffers are
229    * self-contained KLV units, but in future that might change) */
230   outbuf = gst_buffer_make_writable (outbuf);
231   GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
232
233   return outbuf;
234
235 /* ERRORS */
236 bad_klv_packet:
237   {
238     GST_WARNING_OBJECT (klvdepay, "bad KLV packet, dropping");
239     gst_rtp_klv_depay_reset (klvdepay);
240     return NULL;
241   }
242 incomplete_klv_packet:
243   {
244     GST_DEBUG_OBJECT (klvdepay, "partial KLV packet: have %u bytes, want %u",
245         (guint) avail, (guint) (16 + len_size + v_len));
246     return NULL;
247   }
248 }
249
250 /* We're trying to be pragmatic here, not quite as strict as the spec wants
251  * us to be with regard to marker bits and resyncing after packet loss */
252 static GstBuffer *
253 gst_rtp_klv_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
254 {
255   GstRtpKlvDepay *klvdepay = GST_RTP_KLV_DEPAY (depayload);
256   GstBuffer *payload, *outbuf = NULL;
257   gboolean marker, start = FALSE, maybe_start;
258   guint32 rtp_ts;
259   guint16 seq;
260   guint payload_len;
261
262   /* Ignore DISCONT on first buffer and on buffers following a discont */
263   if (GST_BUFFER_IS_DISCONT (rtp->buffer) && klvdepay->last_rtp_ts != -1) {
264     GST_WARNING_OBJECT (klvdepay, "DISCONT, need to resync");
265     gst_rtp_klv_depay_reset (klvdepay);
266   }
267
268   payload_len = gst_rtp_buffer_get_payload_len (rtp);
269
270   /* marker bit signals last fragment of a KLV unit */
271   marker = gst_rtp_buffer_get_marker (rtp);
272
273   seq = gst_rtp_buffer_get_seq (rtp);
274
275   /* packet directly after one with marker bit set => start */
276   start = klvdepay->last_marker_seq != -1
277       && gst_rtp_buffer_compare_seqnum (klvdepay->last_marker_seq, seq) == 1;
278
279   /* deduce start of new KLV unit in case sender doesn't set marker bits
280    * (it's not like the spec is ambiguous about that, but what can you do) */
281   rtp_ts = gst_rtp_buffer_get_timestamp (rtp);
282
283   maybe_start = klvdepay->last_rtp_ts == -1 || klvdepay->last_rtp_ts != rtp_ts;
284
285   klvdepay->last_rtp_ts = rtp_ts;
286
287   /* fallback to detect self-contained single KLV unit (usual case) */
288   if ((!start || !marker || maybe_start) && payload_len > 16) {
289     const guint8 *data;
290     guint64 v_len;
291     gsize len_size;
292
293     data = gst_rtp_buffer_get_payload (rtp);
294     if (GST_READ_UINT32_BE (data) == 0x060e2b34 &&
295         klv_get_vlen (data + 16, payload_len - 16, &v_len, &len_size)) {
296       if (16 + len_size + v_len == payload_len) {
297         GST_LOG_OBJECT (klvdepay, "Looks like a self-contained KLV unit");
298         marker = TRUE;
299         start = TRUE;
300       } else if (16 + len_size + v_len > payload_len) {
301         GST_LOG_OBJECT (klvdepay,
302             "Looks like the start of a fragmented KLV unit");
303         start = TRUE;
304       }
305     }
306   }
307
308   /* If this is the first packet and looks like a start, clear resync flag */
309   if (klvdepay->resync && klvdepay->last_marker_seq == -1 && start)
310     klvdepay->resync = FALSE;
311
312   if (marker)
313     klvdepay->last_marker_seq = seq;
314
315   GST_LOG_OBJECT (klvdepay, "payload of %u bytes, marker=%d, start=%d",
316       payload_len, marker, start);
317
318   if (klvdepay->resync && !start) {
319     GST_DEBUG_OBJECT (klvdepay, "Dropping buffer, waiting to resync");
320
321     if (marker)
322       klvdepay->resync = FALSE;
323
324     goto done;
325   }
326
327   if (start && !marker)
328     outbuf = gst_rtp_klv_depay_process_data (klvdepay);
329
330   payload = gst_rtp_buffer_get_payload_buffer (rtp);
331   gst_adapter_push (klvdepay->adapter, payload);
332
333   if (marker)
334     outbuf = gst_rtp_klv_depay_process_data (klvdepay);
335
336 done:
337
338   return outbuf;
339 }
340
341 static GstStateChangeReturn
342 gst_rtp_klv_depay_change_state (GstElement * element, GstStateChange transition)
343 {
344   GstRtpKlvDepay *klvdepay;
345   GstStateChangeReturn ret;
346
347   klvdepay = GST_RTP_KLV_DEPAY (element);
348
349   switch (transition) {
350     case GST_STATE_CHANGE_READY_TO_PAUSED:
351       gst_rtp_klv_depay_reset (klvdepay);
352       klvdepay->last_marker_seq = -1;
353       break;
354     default:
355       break;
356   }
357
358   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
359
360   switch (transition) {
361     case GST_STATE_CHANGE_PAUSED_TO_READY:
362       gst_rtp_klv_depay_reset (klvdepay);
363       break;
364     default:
365       break;
366   }
367   return ret;
368 }
369
370 gboolean
371 gst_rtp_klv_depay_plugin_init (GstPlugin * plugin)
372 {
373   return gst_element_register (plugin, "rtpklvdepay",
374       GST_RANK_SECONDARY, GST_TYPE_RTP_KLV_DEPAY);
375 }