rtpvp8: port some more to new memory API
[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 gboolean
187 read_length (GstRtpGSTDepay * rtpgstdepay, guint8 * data, guint size,
188     guint * length, guint * skip)
189 {
190   guint b, len, offset;
191
192   /* start reading the length, we need this to skip to the data later */
193   len = offset = 0;
194   do {
195     if (offset >= size)
196       return FALSE;
197     b = data[offset++];
198     len = (len << 7) | (b & 0x7f);
199   } while (b & 0x80);
200
201   /* check remaining buffer size */
202   if (size - offset < len)
203     return FALSE;
204
205   *length = len;
206   *skip = offset;
207
208   return TRUE;
209 }
210
211 static GstCaps *
212 read_caps (GstRtpGSTDepay * rtpgstdepay, GstBuffer * buf, guint * skip)
213 {
214   guint offset, length;
215   GstCaps *caps;
216   GstMapInfo map;
217
218   gst_buffer_map (buf, &map, GST_MAP_READ);
219
220   GST_DEBUG_OBJECT (rtpgstdepay, "buffer size %u", map.size);
221
222   if (!read_length (rtpgstdepay, map.data, map.size, &length, &offset))
223     goto too_small;
224
225   GST_DEBUG_OBJECT (rtpgstdepay, "parsing caps %s", &map.data[offset]);
226
227   /* parse and store in cache */
228   caps = gst_caps_from_string ((gchar *) & map.data[offset]);
229   gst_buffer_unmap (buf, &map);
230
231   *skip = length + offset;
232
233   return caps;
234
235 too_small:
236   {
237     GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
238         ("Buffer too small."), (NULL));
239     gst_buffer_unmap (buf, &map);
240     return NULL;
241   }
242 }
243
244 static GstEvent *
245 read_event (GstRtpGSTDepay * rtpgstdepay, guint type,
246     GstBuffer * buf, guint * skip)
247 {
248   guint offset, length;
249   GstStructure *s;
250   GstEvent *event;
251   GstEventType etype;
252   gchar *end;
253   GstMapInfo map;
254
255   gst_buffer_map (buf, &map, GST_MAP_READ);
256
257   GST_DEBUG_OBJECT (rtpgstdepay, "buffer size %u", map.size);
258
259   if (!read_length (rtpgstdepay, map.data, map.size, &length, &offset))
260     goto too_small;
261
262   GST_DEBUG_OBJECT (rtpgstdepay, "parsing event %s", &map.data[offset]);
263
264   /* parse */
265   s = gst_structure_from_string ((gchar *) & map.data[offset], &end);
266   gst_buffer_unmap (buf, &map);
267
268   switch (type) {
269     case 1:
270       etype = GST_EVENT_TAG;
271       break;
272     case 2:
273       etype = GST_EVENT_CUSTOM_DOWNSTREAM;
274       break;
275     case 3:
276       etype = GST_EVENT_CUSTOM_BOTH;
277       break;
278     default:
279       goto unknown_event;
280   }
281   event = gst_event_new_custom (etype, s);
282
283   *skip = length + offset;
284
285   return event;
286
287 too_small:
288   {
289     GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
290         ("Buffer too small."), (NULL));
291     gst_buffer_unmap (buf, &map);
292     return NULL;
293   }
294 unknown_event:
295   {
296     GST_DEBUG_OBJECT (rtpgstdepay, "unknown event type");
297     gst_structure_free (s);
298     return NULL;
299   }
300 }
301
302 static GstBuffer *
303 gst_rtp_gst_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
304 {
305   GstRtpGSTDepay *rtpgstdepay;
306   GstBuffer *subbuf, *outbuf = NULL;
307   gint payload_len;
308   guint8 *payload;
309   guint CV, frag_offset, avail, offset;
310   GstRTPBuffer rtp = { NULL };
311
312   rtpgstdepay = GST_RTP_GST_DEPAY (depayload);
313
314   gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
315
316   payload_len = gst_rtp_buffer_get_payload_len (&rtp);
317
318   if (payload_len <= 8)
319     goto empty_packet;
320
321   if (GST_BUFFER_IS_DISCONT (buf)) {
322     GST_WARNING_OBJECT (rtpgstdepay, "DISCONT, clear adapter");
323     gst_adapter_clear (rtpgstdepay->adapter);
324   }
325
326   payload = gst_rtp_buffer_get_payload (&rtp);
327
328   /* strip off header
329    *
330    *  0                   1                   2                   3
331    *  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
332    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
333    * |C| CV  |D|0|0|0|     ETYPE     |  MBZ                          |
334    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
335    * |                          Frag_offset                          |
336    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
337    */
338   frag_offset =
339       (payload[4] << 24) | (payload[5] << 16) | (payload[6] << 8) | payload[7];
340
341   avail = gst_adapter_available (rtpgstdepay->adapter);
342   if (avail != frag_offset)
343     goto wrong_frag;
344
345   /* subbuffer skipping the 8 header bytes */
346   subbuf = gst_rtp_buffer_get_payload_subbuffer (&rtp, 8, -1);
347   gst_adapter_push (rtpgstdepay->adapter, subbuf);
348
349   offset = 0;
350   if (gst_rtp_buffer_get_marker (&rtp)) {
351     guint avail;
352     GstCaps *outcaps;
353
354     /* take the buffer */
355     avail = gst_adapter_available (rtpgstdepay->adapter);
356     outbuf = gst_adapter_take_buffer (rtpgstdepay->adapter, avail);
357
358     CV = (payload[0] >> 4) & 0x7;
359
360     if (payload[0] & 0x80) {
361       guint size;
362
363       /* C bit, we have inline caps */
364       outcaps = read_caps (rtpgstdepay, outbuf, &size);
365       if (outcaps == NULL)
366         goto no_caps;
367
368       GST_DEBUG_OBJECT (rtpgstdepay,
369           "inline caps %u, length %u, %" GST_PTR_FORMAT, CV, size, outcaps);
370
371       store_cache (rtpgstdepay, CV, outcaps);
372
373       /* skip caps */
374       offset += size;
375       avail -= size;
376     }
377     if (payload[1]) {
378       guint size;
379       GstEvent *event;
380
381       /* we have an event */
382       event = read_event (rtpgstdepay, payload[1], outbuf, &size);
383       if (event == NULL)
384         goto no_event;
385
386       GST_DEBUG_OBJECT (rtpgstdepay,
387           "inline event, length %u, %" GST_PTR_FORMAT, size, event);
388
389       gst_pad_push_event (depayload->srcpad, event);
390
391       /* no buffer after event */
392       avail = 0;
393     }
394
395     if (avail) {
396       if (offset != 0) {
397         GstBuffer *temp;
398
399         GST_DEBUG_OBJECT (rtpgstdepay, "sub buffer: offset %u, size %u", offset,
400             avail);
401
402         temp =
403             gst_buffer_copy_region (outbuf, GST_BUFFER_COPY_ALL, offset, avail);
404
405         gst_buffer_unref (outbuf);
406         outbuf = temp;
407       }
408
409       /* see what caps we need */
410       if (CV != rtpgstdepay->current_CV) {
411         /* we need to switch caps, check if we have the caps */
412         if ((outcaps = rtpgstdepay->CV_cache[CV]) == NULL)
413           goto missing_caps;
414
415         GST_DEBUG_OBJECT (rtpgstdepay,
416             "need caps switch from %u to %u, %" GST_PTR_FORMAT,
417             rtpgstdepay->current_CV, CV, outcaps);
418
419         /* and set caps */
420         if (gst_pad_set_caps (depayload->srcpad, outcaps))
421           rtpgstdepay->current_CV = CV;
422       }
423
424       if (payload[0] & 0x8)
425         GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
426     } else {
427       gst_buffer_unref (outbuf);
428       outbuf = NULL;
429     }
430   }
431   gst_rtp_buffer_unmap (&rtp);
432
433   return outbuf;
434
435   /* ERRORS */
436 empty_packet:
437   {
438     GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
439         ("Empty Payload."), (NULL));
440     gst_rtp_buffer_unmap (&rtp);
441     return NULL;
442   }
443 wrong_frag:
444   {
445     gst_adapter_clear (rtpgstdepay->adapter);
446     gst_rtp_buffer_unmap (&rtp);
447     GST_LOG_OBJECT (rtpgstdepay, "wrong fragment, skipping");
448     return NULL;
449   }
450 no_caps:
451   {
452     GST_WARNING_OBJECT (rtpgstdepay, "failed to parse caps");
453     gst_buffer_unref (outbuf);
454     gst_rtp_buffer_unmap (&rtp);
455     return NULL;
456   }
457 no_event:
458   {
459     GST_WARNING_OBJECT (rtpgstdepay, "failed to parse event");
460     gst_buffer_unref (outbuf);
461     gst_rtp_buffer_unmap (&rtp);
462     return NULL;
463   }
464 missing_caps:
465   {
466     GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
467         ("Missing caps %u.", CV), (NULL));
468     gst_buffer_unref (outbuf);
469     gst_rtp_buffer_unmap (&rtp);
470     return NULL;
471   }
472 }
473
474 static GstStateChangeReturn
475 gst_rtp_gst_depay_change_state (GstElement * element, GstStateChange transition)
476 {
477   GstRtpGSTDepay *rtpgstdepay;
478   GstStateChangeReturn ret;
479
480   rtpgstdepay = GST_RTP_GST_DEPAY (element);
481
482   switch (transition) {
483     case GST_STATE_CHANGE_READY_TO_PAUSED:
484       gst_rtp_gst_depay_reset (rtpgstdepay);
485       break;
486     default:
487       break;
488   }
489
490   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
491
492   switch (transition) {
493     case GST_STATE_CHANGE_PAUSED_TO_READY:
494       gst_rtp_gst_depay_reset (rtpgstdepay);
495       break;
496     default:
497       break;
498   }
499   return ret;
500 }
501
502
503 gboolean
504 gst_rtp_gst_depay_plugin_init (GstPlugin * plugin)
505 {
506   return gst_element_register (plugin, "rtpgstdepay",
507       GST_RANK_MARGINAL, GST_TYPE_RTP_GST_DEPAY);
508 }