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