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