gstdepay: check for correct fragment offset
[platform/upstream/gstreamer.git] / gst / rtp / gstrtpgstdepay.c
1 /* GStreamer
2  * Copyright (C) <2010> Wim Taymans <wim.taymans@gmail.com>
3  *
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.
8  *
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.
13  *
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., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <string.h>
25 #include <stdlib.h>
26
27 #include "gstrtpgstdepay.h"
28
29 GST_DEBUG_CATEGORY_STATIC (rtpgstdepay_debug);
30 #define GST_CAT_DEFAULT (rtpgstdepay_debug)
31
32 static GstStaticPadTemplate gst_rtp_gst_depay_src_template =
33 GST_STATIC_PAD_TEMPLATE ("src",
34     GST_PAD_SRC,
35     GST_PAD_ALWAYS,
36     GST_STATIC_CAPS_ANY);
37
38 static GstStaticPadTemplate gst_rtp_gst_depay_sink_template =
39 GST_STATIC_PAD_TEMPLATE ("sink",
40     GST_PAD_SINK,
41     GST_PAD_ALWAYS,
42     GST_STATIC_CAPS ("application/x-rtp, "
43         "media = (string) \"application\", "
44         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
45         "clock-rate = (int) 90000, " "encoding-name = (string) \"X-GST\"")
46     );
47
48 #define gst_rtp_gst_depay_parent_class parent_class
49 G_DEFINE_TYPE (GstRtpGSTDepay, gst_rtp_gst_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
50
51 static void gst_rtp_gst_depay_finalize (GObject * object);
52
53 static GstStateChangeReturn gst_rtp_gst_depay_change_state (GstElement *
54     element, GstStateChange transition);
55
56 static void gst_rtp_gst_depay_reset (GstRtpGSTDepay * rtpgstdepay);
57 static gboolean gst_rtp_gst_depay_setcaps (GstRTPBaseDepayload * depayload,
58     GstCaps * caps);
59 static GstBuffer *gst_rtp_gst_depay_process (GstRTPBaseDepayload * depayload,
60     GstBuffer * buf);
61
62 static void
63 gst_rtp_gst_depay_class_init (GstRtpGSTDepayClass * klass)
64 {
65   GObjectClass *gobject_class;
66   GstElementClass *gstelement_class;
67   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
68
69   GST_DEBUG_CATEGORY_INIT (rtpgstdepay_debug, "rtpgstdepay", 0,
70       "Gstreamer RTP Depayloader");
71
72   gobject_class = (GObjectClass *) klass;
73   gstelement_class = (GstElementClass *) klass;
74   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
75
76   gobject_class->finalize = gst_rtp_gst_depay_finalize;
77
78   gstelement_class->change_state = gst_rtp_gst_depay_change_state;
79
80   gst_element_class_add_pad_template (gstelement_class,
81       gst_static_pad_template_get (&gst_rtp_gst_depay_src_template));
82   gst_element_class_add_pad_template (gstelement_class,
83       gst_static_pad_template_get (&gst_rtp_gst_depay_sink_template));
84
85   gst_element_class_set_static_metadata (gstelement_class,
86       "GStreamer depayloader", "Codec/Depayloader/Network",
87       "Extracts GStreamer buffers from RTP packets",
88       "Wim Taymans <wim.taymans@gmail.com>");
89
90   gstrtpbasedepayload_class->set_caps = gst_rtp_gst_depay_setcaps;
91   gstrtpbasedepayload_class->process = gst_rtp_gst_depay_process;
92 }
93
94 static void
95 gst_rtp_gst_depay_init (GstRtpGSTDepay * rtpgstdepay)
96 {
97   rtpgstdepay->adapter = gst_adapter_new ();
98 }
99
100 static void
101 gst_rtp_gst_depay_finalize (GObject * object)
102 {
103   GstRtpGSTDepay *rtpgstdepay;
104
105   rtpgstdepay = GST_RTP_GST_DEPAY (object);
106
107   gst_rtp_gst_depay_reset (rtpgstdepay);
108   g_object_unref (rtpgstdepay->adapter);
109
110   G_OBJECT_CLASS (parent_class)->finalize (object);
111 }
112
113 static void
114 store_cache (GstRtpGSTDepay * rtpgstdepay, guint CV, GstCaps * caps)
115 {
116   if (rtpgstdepay->CV_cache[CV])
117     gst_caps_unref (rtpgstdepay->CV_cache[CV]);
118   rtpgstdepay->CV_cache[CV] = caps;
119 }
120
121 static void
122 gst_rtp_gst_depay_reset (GstRtpGSTDepay * rtpgstdepay)
123 {
124   guint i;
125
126   gst_adapter_clear (rtpgstdepay->adapter);
127   rtpgstdepay->current_CV = 0;
128   for (i = 0; i < 8; i++)
129     store_cache (rtpgstdepay, i, NULL);
130 }
131
132 static gboolean
133 gst_rtp_gst_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
134 {
135   GstRtpGSTDepay *rtpgstdepay;
136   GstStructure *structure;
137   gint clock_rate;
138   gboolean res;
139   const gchar *capsenc;
140
141   rtpgstdepay = GST_RTP_GST_DEPAY (depayload);
142
143   structure = gst_caps_get_structure (caps, 0);
144
145   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
146     clock_rate = 90000;
147   depayload->clock_rate = clock_rate;
148
149   capsenc = gst_structure_get_string (structure, "caps");
150   if (capsenc) {
151     GstCaps *outcaps;
152     gsize out_len;
153     gchar *capsstr;
154     const gchar *capsver;
155     guint CV;
156
157     /* decode caps */
158     capsstr = (gchar *) g_base64_decode (capsenc, &out_len);
159     outcaps = gst_caps_from_string (capsstr);
160     g_free (capsstr);
161
162     /* parse version */
163     capsver = gst_structure_get_string (structure, "capsversion");
164     if (capsver) {
165       CV = atoi (capsver);
166     } else {
167       /* no version, assume 0 */
168       CV = 0;
169     }
170     /* store in cache */
171     rtpgstdepay->current_CV = CV;
172     gst_caps_ref (outcaps);
173     store_cache (rtpgstdepay, CV, outcaps);
174
175     res = gst_pad_set_caps (depayload->srcpad, outcaps);
176     gst_caps_unref (outcaps);
177   } else {
178     GST_WARNING_OBJECT (depayload, "no caps given");
179     rtpgstdepay->current_CV = -1;
180     res = TRUE;
181   }
182
183   return res;
184 }
185
186 static GstBuffer *
187 gst_rtp_gst_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
188 {
189   GstRtpGSTDepay *rtpgstdepay;
190   GstBuffer *subbuf, *outbuf = NULL;
191   gint payload_len;
192   guint8 *payload;
193   guint CV, frag_offset, avail;
194   GstRTPBuffer rtp = { NULL };
195
196   rtpgstdepay = GST_RTP_GST_DEPAY (depayload);
197
198   gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
199
200   payload_len = gst_rtp_buffer_get_payload_len (&rtp);
201
202   if (payload_len <= 8)
203     goto empty_packet;
204
205   if (GST_BUFFER_IS_DISCONT (buf)) {
206     GST_WARNING_OBJECT (rtpgstdepay, "DISCONT, clear adapter");
207     gst_adapter_clear (rtpgstdepay->adapter);
208   }
209
210   payload = gst_rtp_buffer_get_payload (&rtp);
211
212   /* strip off header
213    *
214    *  0                   1                   2                   3
215    *  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
216    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
217    * |C| CV  |D|0|0|0|                  MBZ                          |
218    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
219    * |                          Frag_offset                          |
220    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
221    */
222   frag_offset =
223       (payload[4] << 24) | (payload[5] << 16) | (payload[6] << 8) | payload[7];
224
225   avail = gst_adapter_available (rtpgstdepay->adapter);
226   if (avail != frag_offset)
227     goto wrong_frag;
228
229   /* subbuffer skipping the 8 header bytes */
230   subbuf = gst_rtp_buffer_get_payload_subbuffer (&rtp, 8, -1);
231   gst_adapter_push (rtpgstdepay->adapter, subbuf);
232
233   if (gst_rtp_buffer_get_marker (&rtp)) {
234     guint avail;
235     GstCaps *outcaps;
236
237     /* take the buffer */
238     avail = gst_adapter_available (rtpgstdepay->adapter);
239     outbuf = gst_adapter_take_buffer (rtpgstdepay->adapter, avail);
240
241     CV = (payload[0] >> 4) & 0x7;
242
243     if (payload[0] & 0x80) {
244       guint b, csize, left, offset;
245       GstMapInfo map;
246       GstBuffer *subbuf;
247
248       /* C bit, we have inline caps */
249       gst_buffer_map (outbuf, &map, GST_MAP_READ);
250
251       GST_DEBUG_OBJECT (rtpgstdepay, "buffer size %u", map.size);
252
253       /* start reading the length, we need this to skip to the data later */
254       csize = offset = 0;
255       left = map.size;
256       do {
257         if (offset >= left) {
258           gst_buffer_unmap (outbuf, &map);
259           goto too_small;
260         }
261         b = map.data[offset++];
262         csize = (csize << 7) | (b & 0x7f);
263       } while (b & 0x80);
264
265       /* we have read csize, reduce remaining buffer size */
266       left -= offset;
267       if (left < csize) {
268         gst_buffer_unmap (outbuf, &map);
269         goto too_small;
270       }
271
272       GST_DEBUG_OBJECT (rtpgstdepay, "parsing caps %s", &map.data[offset]);
273
274       /* parse and store in cache */
275       outcaps = gst_caps_from_string ((gchar *) & map.data[offset]);
276       store_cache (rtpgstdepay, CV, outcaps);
277
278       /* skip caps */
279       offset += csize;
280       left -= csize;
281
282       GST_DEBUG_OBJECT (rtpgstdepay,
283           "inline caps %u, length %u, %" GST_PTR_FORMAT, CV, csize, outcaps);
284
285       GST_DEBUG_OBJECT (rtpgstdepay, "sub buffer: offset %u, size %u", offset,
286           left);
287       /* create real data buffer when needed */
288       if (left)
289         subbuf =
290             gst_buffer_copy_region (outbuf, GST_BUFFER_COPY_ALL, offset, left);
291       else
292         subbuf = NULL;
293
294       gst_buffer_unmap (outbuf, &map);
295       gst_buffer_unref (outbuf);
296       outbuf = subbuf;
297     }
298
299     /* see what caps we need */
300     if (CV != rtpgstdepay->current_CV) {
301       /* we need to switch caps, check if we have the caps */
302       if ((outcaps = rtpgstdepay->CV_cache[CV]) == NULL)
303         goto missing_caps;
304
305       GST_DEBUG_OBJECT (rtpgstdepay,
306           "need caps switch from %u to %u, %" GST_PTR_FORMAT,
307           rtpgstdepay->current_CV, CV, outcaps);
308
309       /* and set caps */
310       if (gst_pad_set_caps (depayload->srcpad, outcaps))
311         rtpgstdepay->current_CV = CV;
312     }
313
314     if (outbuf) {
315       if (payload[0] & 0x8)
316         GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
317     }
318   }
319   gst_rtp_buffer_unmap (&rtp);
320   return outbuf;
321
322   /* ERRORS */
323 empty_packet:
324   {
325     GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
326         ("Empty Payload."), (NULL));
327     gst_rtp_buffer_unmap (&rtp);
328     return NULL;
329   }
330 wrong_frag:
331   {
332     gst_adapter_clear (rtpgstdepay->adapter);
333     gst_rtp_buffer_unmap (&rtp);
334     GST_LOG_OBJECT (rtpgstdepay, "wrong fragment, skipping");
335     return NULL;
336   }
337 too_small:
338   {
339     GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
340         ("Buffer too small."), (NULL));
341     if (outbuf)
342       gst_buffer_unref (outbuf);
343     gst_rtp_buffer_unmap (&rtp);
344     return NULL;
345   }
346 missing_caps:
347   {
348     GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
349         ("Missing caps %u.", CV), (NULL));
350     if (outbuf)
351       gst_buffer_unref (outbuf);
352     gst_rtp_buffer_unmap (&rtp);
353     return NULL;
354   }
355 }
356
357 static GstStateChangeReturn
358 gst_rtp_gst_depay_change_state (GstElement * element, GstStateChange transition)
359 {
360   GstRtpGSTDepay *rtpgstdepay;
361   GstStateChangeReturn ret;
362
363   rtpgstdepay = GST_RTP_GST_DEPAY (element);
364
365   switch (transition) {
366     case GST_STATE_CHANGE_READY_TO_PAUSED:
367       gst_rtp_gst_depay_reset (rtpgstdepay);
368       break;
369     default:
370       break;
371   }
372
373   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
374
375   switch (transition) {
376     case GST_STATE_CHANGE_PAUSED_TO_READY:
377       gst_rtp_gst_depay_reset (rtpgstdepay);
378       break;
379     default:
380       break;
381   }
382   return ret;
383 }
384
385
386 gboolean
387 gst_rtp_gst_depay_plugin_init (GstPlugin * plugin)
388 {
389   return gst_element_register (plugin, "rtpgstdepay",
390       GST_RANK_MARGINAL, GST_TYPE_RTP_GST_DEPAY);
391 }