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