rtpdepay: remove payload type restrictions
[platform/upstream/gst-plugins-good.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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, 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         "clock-rate = (int) 90000, " "encoding-name = (string) \"X-GST\"")
45     );
46
47 #define gst_rtp_gst_depay_parent_class parent_class
48 G_DEFINE_TYPE (GstRtpGSTDepay, gst_rtp_gst_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
49
50 static void gst_rtp_gst_depay_finalize (GObject * object);
51
52 static GstStateChangeReturn gst_rtp_gst_depay_change_state (GstElement *
53     element, GstStateChange transition);
54
55 static void gst_rtp_gst_depay_reset (GstRtpGSTDepay * rtpgstdepay);
56 static gboolean gst_rtp_gst_depay_setcaps (GstRTPBaseDepayload * depayload,
57     GstCaps * caps);
58 static GstBuffer *gst_rtp_gst_depay_process (GstRTPBaseDepayload * depayload,
59     GstBuffer * buf);
60
61 static void
62 gst_rtp_gst_depay_class_init (GstRtpGSTDepayClass * klass)
63 {
64   GObjectClass *gobject_class;
65   GstElementClass *gstelement_class;
66   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
67
68   GST_DEBUG_CATEGORY_INIT (rtpgstdepay_debug, "rtpgstdepay", 0,
69       "Gstreamer RTP Depayloader");
70
71   gobject_class = (GObjectClass *) klass;
72   gstelement_class = (GstElementClass *) klass;
73   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
74
75   gobject_class->finalize = gst_rtp_gst_depay_finalize;
76
77   gstelement_class->change_state = gst_rtp_gst_depay_change_state;
78
79   gst_element_class_add_pad_template (gstelement_class,
80       gst_static_pad_template_get (&gst_rtp_gst_depay_src_template));
81   gst_element_class_add_pad_template (gstelement_class,
82       gst_static_pad_template_get (&gst_rtp_gst_depay_sink_template));
83
84   gst_element_class_set_static_metadata (gstelement_class,
85       "GStreamer depayloader", "Codec/Depayloader/Network",
86       "Extracts GStreamer buffers from RTP packets",
87       "Wim Taymans <wim.taymans@gmail.com>");
88
89   gstrtpbasedepayload_class->set_caps = gst_rtp_gst_depay_setcaps;
90   gstrtpbasedepayload_class->process = gst_rtp_gst_depay_process;
91 }
92
93 static void
94 gst_rtp_gst_depay_init (GstRtpGSTDepay * rtpgstdepay)
95 {
96   rtpgstdepay->adapter = gst_adapter_new ();
97 }
98
99 static void
100 gst_rtp_gst_depay_finalize (GObject * object)
101 {
102   GstRtpGSTDepay *rtpgstdepay;
103
104   rtpgstdepay = GST_RTP_GST_DEPAY (object);
105
106   gst_rtp_gst_depay_reset (rtpgstdepay);
107   g_object_unref (rtpgstdepay->adapter);
108
109   G_OBJECT_CLASS (parent_class)->finalize (object);
110 }
111
112 static void
113 store_cache (GstRtpGSTDepay * rtpgstdepay, guint CV, GstCaps * caps)
114 {
115   if (rtpgstdepay->CV_cache[CV])
116     gst_caps_unref (rtpgstdepay->CV_cache[CV]);
117   rtpgstdepay->CV_cache[CV] = caps;
118 }
119
120 static void
121 gst_rtp_gst_depay_reset (GstRtpGSTDepay * rtpgstdepay)
122 {
123   guint i;
124
125   gst_adapter_clear (rtpgstdepay->adapter);
126   rtpgstdepay->current_CV = 0;
127   for (i = 0; i < 8; i++)
128     store_cache (rtpgstdepay, i, NULL);
129 }
130
131 static gboolean
132 gst_rtp_gst_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
133 {
134   GstRtpGSTDepay *rtpgstdepay;
135   GstStructure *structure;
136   gint clock_rate;
137   gboolean res;
138   const gchar *capsenc;
139
140   rtpgstdepay = GST_RTP_GST_DEPAY (depayload);
141
142   structure = gst_caps_get_structure (caps, 0);
143
144   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
145     clock_rate = 90000;
146   depayload->clock_rate = clock_rate;
147
148   capsenc = gst_structure_get_string (structure, "caps");
149   if (capsenc) {
150     GstCaps *outcaps;
151     gsize out_len;
152     gchar *capsstr;
153     const gchar *capsver;
154     guint CV;
155
156     /* decode caps */
157     capsstr = (gchar *) g_base64_decode (capsenc, &out_len);
158     outcaps = gst_caps_from_string (capsstr);
159     g_free (capsstr);
160
161     /* parse version */
162     capsver = gst_structure_get_string (structure, "capsversion");
163     if (capsver) {
164       CV = atoi (capsver);
165     } else {
166       /* no version, assume 0 */
167       CV = 0;
168     }
169     /* store in cache */
170     rtpgstdepay->current_CV = CV;
171     gst_caps_ref (outcaps);
172     store_cache (rtpgstdepay, CV, outcaps);
173
174     res = gst_pad_set_caps (depayload->srcpad, outcaps);
175     gst_caps_unref (outcaps);
176   } else {
177     GST_WARNING_OBJECT (depayload, "no caps given");
178     rtpgstdepay->current_CV = -1;
179     res = TRUE;
180   }
181
182   return res;
183 }
184
185 static gboolean
186 read_length (GstRtpGSTDepay * rtpgstdepay, guint8 * data, guint size,
187     guint * length, guint * skip)
188 {
189   guint b, len, offset;
190
191   /* start reading the length, we need this to skip to the data later */
192   len = offset = 0;
193   do {
194     if (offset >= size)
195       return FALSE;
196     b = data[offset++];
197     len = (len << 7) | (b & 0x7f);
198   } while (b & 0x80);
199
200   /* check remaining buffer size */
201   if (size - offset < len)
202     return FALSE;
203
204   *length = len;
205   *skip = offset;
206
207   return TRUE;
208 }
209
210 static GstCaps *
211 read_caps (GstRtpGSTDepay * rtpgstdepay, GstBuffer * buf, guint * skip)
212 {
213   guint offset, length;
214   GstCaps *caps;
215   GstMapInfo map;
216
217   gst_buffer_map (buf, &map, GST_MAP_READ);
218
219   GST_DEBUG_OBJECT (rtpgstdepay, "buffer size %" G_GSIZE_FORMAT, map.size);
220
221   if (!read_length (rtpgstdepay, map.data, map.size, &length, &offset))
222     goto too_small;
223
224   GST_DEBUG_OBJECT (rtpgstdepay, "parsing caps %s", &map.data[offset]);
225
226   /* parse and store in cache */
227   caps = gst_caps_from_string ((gchar *) & map.data[offset]);
228   gst_buffer_unmap (buf, &map);
229
230   *skip = length + offset;
231
232   return caps;
233
234 too_small:
235   {
236     GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
237         ("Buffer too small."), (NULL));
238     gst_buffer_unmap (buf, &map);
239     return NULL;
240   }
241 }
242
243 static GstEvent *
244 read_event (GstRtpGSTDepay * rtpgstdepay, guint type,
245     GstBuffer * buf, guint * skip)
246 {
247   guint offset, length;
248   GstStructure *s;
249   GstEvent *event;
250   GstEventType etype;
251   gchar *end;
252   GstMapInfo map;
253
254   gst_buffer_map (buf, &map, GST_MAP_READ);
255
256   GST_DEBUG_OBJECT (rtpgstdepay, "buffer size %" G_GSIZE_FORMAT, map.size);
257
258   if (!read_length (rtpgstdepay, map.data, map.size, &length, &offset))
259     goto too_small;
260
261   GST_DEBUG_OBJECT (rtpgstdepay, "parsing event %s", &map.data[offset]);
262
263   /* parse */
264   s = gst_structure_from_string ((gchar *) & map.data[offset], &end);
265   gst_buffer_unmap (buf, &map);
266
267   switch (type) {
268     case 1:
269       etype = GST_EVENT_TAG;
270       break;
271     case 2:
272       etype = GST_EVENT_CUSTOM_DOWNSTREAM;
273       break;
274     case 3:
275       etype = GST_EVENT_CUSTOM_BOTH;
276       break;
277     default:
278       goto unknown_event;
279   }
280   event = gst_event_new_custom (etype, s);
281
282   *skip = length + offset;
283
284   return event;
285
286 too_small:
287   {
288     GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
289         ("Buffer too small."), (NULL));
290     gst_buffer_unmap (buf, &map);
291     return NULL;
292   }
293 unknown_event:
294   {
295     GST_DEBUG_OBJECT (rtpgstdepay, "unknown event type");
296     gst_structure_free (s);
297     return NULL;
298   }
299 }
300
301 static GstBuffer *
302 gst_rtp_gst_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
303 {
304   GstRtpGSTDepay *rtpgstdepay;
305   GstBuffer *subbuf, *outbuf = NULL;
306   gint payload_len;
307   guint8 *payload;
308   guint CV, frag_offset, avail, offset;
309   GstRTPBuffer rtp = { NULL };
310
311   rtpgstdepay = GST_RTP_GST_DEPAY (depayload);
312
313   gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
314
315   payload_len = gst_rtp_buffer_get_payload_len (&rtp);
316
317   if (payload_len <= 8)
318     goto empty_packet;
319
320   if (GST_BUFFER_IS_DISCONT (buf)) {
321     GST_WARNING_OBJECT (rtpgstdepay, "DISCONT, clear adapter");
322     gst_adapter_clear (rtpgstdepay->adapter);
323   }
324
325   payload = gst_rtp_buffer_get_payload (&rtp);
326
327   /* strip off header
328    *
329    *  0                   1                   2                   3
330    *  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
331    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
332    * |C| CV  |D|0|0|0|     ETYPE     |  MBZ                          |
333    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
334    * |                          Frag_offset                          |
335    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
336    */
337   frag_offset =
338       (payload[4] << 24) | (payload[5] << 16) | (payload[6] << 8) | payload[7];
339
340   avail = gst_adapter_available (rtpgstdepay->adapter);
341   if (avail != frag_offset)
342     goto wrong_frag;
343
344   /* subbuffer skipping the 8 header bytes */
345   subbuf = gst_rtp_buffer_get_payload_subbuffer (&rtp, 8, -1);
346   gst_adapter_push (rtpgstdepay->adapter, subbuf);
347
348   offset = 0;
349   if (gst_rtp_buffer_get_marker (&rtp)) {
350     guint avail;
351     GstCaps *outcaps;
352
353     /* take the buffer */
354     avail = gst_adapter_available (rtpgstdepay->adapter);
355     outbuf = gst_adapter_take_buffer (rtpgstdepay->adapter, avail);
356
357     CV = (payload[0] >> 4) & 0x7;
358
359     if (payload[0] & 0x80) {
360       guint size;
361
362       /* C bit, we have inline caps */
363       outcaps = read_caps (rtpgstdepay, outbuf, &size);
364       if (outcaps == NULL)
365         goto no_caps;
366
367       GST_DEBUG_OBJECT (rtpgstdepay,
368           "inline caps %u, length %u, %" GST_PTR_FORMAT, CV, size, outcaps);
369
370       store_cache (rtpgstdepay, CV, outcaps);
371
372       /* skip caps */
373       offset += size;
374       avail -= size;
375     }
376     if (payload[1]) {
377       guint size;
378       GstEvent *event;
379
380       /* we have an event */
381       event = read_event (rtpgstdepay, payload[1], outbuf, &size);
382       if (event == NULL)
383         goto no_event;
384
385       GST_DEBUG_OBJECT (rtpgstdepay,
386           "inline event, length %u, %" GST_PTR_FORMAT, size, event);
387
388       gst_pad_push_event (depayload->srcpad, event);
389
390       /* no buffer after event */
391       avail = 0;
392     }
393
394     if (avail) {
395       if (offset != 0) {
396         GstBuffer *temp;
397
398         GST_DEBUG_OBJECT (rtpgstdepay, "sub buffer: offset %u, size %u", offset,
399             avail);
400
401         temp =
402             gst_buffer_copy_region (outbuf, GST_BUFFER_COPY_ALL, offset, avail);
403
404         gst_buffer_unref (outbuf);
405         outbuf = temp;
406       }
407
408       /* see what caps we need */
409       if (CV != rtpgstdepay->current_CV) {
410         /* we need to switch caps, check if we have the caps */
411         if ((outcaps = rtpgstdepay->CV_cache[CV]) == NULL)
412           goto missing_caps;
413
414         GST_DEBUG_OBJECT (rtpgstdepay,
415             "need caps switch from %u to %u, %" GST_PTR_FORMAT,
416             rtpgstdepay->current_CV, CV, outcaps);
417
418         /* and set caps */
419         if (gst_pad_set_caps (depayload->srcpad, outcaps))
420           rtpgstdepay->current_CV = CV;
421       }
422
423       if (payload[0] & 0x8)
424         GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
425     } else {
426       gst_buffer_unref (outbuf);
427       outbuf = NULL;
428     }
429   }
430   gst_rtp_buffer_unmap (&rtp);
431
432   return outbuf;
433
434   /* ERRORS */
435 empty_packet:
436   {
437     GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
438         ("Empty Payload."), (NULL));
439     gst_rtp_buffer_unmap (&rtp);
440     return NULL;
441   }
442 wrong_frag:
443   {
444     gst_adapter_clear (rtpgstdepay->adapter);
445     gst_rtp_buffer_unmap (&rtp);
446     GST_LOG_OBJECT (rtpgstdepay, "wrong fragment, skipping");
447     return NULL;
448   }
449 no_caps:
450   {
451     GST_WARNING_OBJECT (rtpgstdepay, "failed to parse caps");
452     gst_buffer_unref (outbuf);
453     gst_rtp_buffer_unmap (&rtp);
454     return NULL;
455   }
456 no_event:
457   {
458     GST_WARNING_OBJECT (rtpgstdepay, "failed to parse event");
459     gst_buffer_unref (outbuf);
460     gst_rtp_buffer_unmap (&rtp);
461     return NULL;
462   }
463 missing_caps:
464   {
465     GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
466         ("Missing caps %u.", CV), (NULL));
467     gst_buffer_unref (outbuf);
468     gst_rtp_buffer_unmap (&rtp);
469     return NULL;
470   }
471 }
472
473 static GstStateChangeReturn
474 gst_rtp_gst_depay_change_state (GstElement * element, GstStateChange transition)
475 {
476   GstRtpGSTDepay *rtpgstdepay;
477   GstStateChangeReturn ret;
478
479   rtpgstdepay = GST_RTP_GST_DEPAY (element);
480
481   switch (transition) {
482     case GST_STATE_CHANGE_READY_TO_PAUSED:
483       gst_rtp_gst_depay_reset (rtpgstdepay);
484       break;
485     default:
486       break;
487   }
488
489   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
490
491   switch (transition) {
492     case GST_STATE_CHANGE_PAUSED_TO_READY:
493       gst_rtp_gst_depay_reset (rtpgstdepay);
494       break;
495     default:
496       break;
497   }
498   return ret;
499 }
500
501
502 gboolean
503 gst_rtp_gst_depay_plugin_init (GstPlugin * plugin)
504 {
505   return gst_element_register (plugin, "rtpgstdepay",
506       GST_RANK_MARGINAL, GST_TYPE_RTP_GST_DEPAY);
507 }