Merge branch 'master' into 0.11
[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., 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 <gst/rtp/gstrtpbuffer.h>
25
26 #include <string.h>
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_details_simple (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   GstCaps *outcaps;
138   gint clock_rate;
139   gboolean res;
140   const gchar *capsenc;
141   gchar *capsstr;
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     gsize out_len;
154
155     capsstr = (gchar *) g_base64_decode (capsenc, &out_len);
156     outcaps = gst_caps_from_string (capsstr);
157     g_free (capsstr);
158
159     /* we have the SDP caps as output caps */
160     rtpgstdepay->current_CV = 0;
161     gst_caps_ref (outcaps);
162     store_cache (rtpgstdepay, 0, outcaps);
163   } else {
164     outcaps = gst_caps_new_any ();
165   }
166   res = gst_pad_set_caps (depayload->srcpad, outcaps);
167   gst_caps_unref (outcaps);
168
169   return res;
170 }
171
172 static GstBuffer *
173 gst_rtp_gst_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
174 {
175   GstRtpGSTDepay *rtpgstdepay;
176   GstBuffer *subbuf, *outbuf = NULL;
177   gint payload_len;
178   guint8 *payload;
179   guint CV;
180   GstRTPBuffer rtp = { NULL };
181
182   rtpgstdepay = GST_RTP_GST_DEPAY (depayload);
183
184   gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
185
186   payload_len = gst_rtp_buffer_get_payload_len (&rtp);
187
188   if (payload_len <= 8)
189     goto empty_packet;
190
191   if (GST_BUFFER_IS_DISCONT (buf)) {
192     GST_WARNING_OBJECT (rtpgstdepay, "DISCONT, clear adapter");
193     gst_adapter_clear (rtpgstdepay->adapter);
194   }
195
196   payload = gst_rtp_buffer_get_payload (&rtp);
197
198   /* strip off header
199    *
200    *  0                   1                   2                   3
201    *  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
202    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
203    * |C| CV  |D|0|0|0|                  MBZ                          |
204    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
205    * |                          Frag_offset                          |
206    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
207    */
208   /* frag_offset =
209    *   (payload[4] << 24) | (payload[5] << 16) | (payload[6] << 8) | payload[7];
210    */
211
212   /* subbuffer skipping the 8 header bytes */
213   subbuf = gst_rtp_buffer_get_payload_subbuffer (&rtp, 8, -1);
214   gst_adapter_push (rtpgstdepay->adapter, subbuf);
215
216   if (gst_rtp_buffer_get_marker (&rtp)) {
217     guint avail;
218     GstCaps *outcaps;
219
220     /* take the buffer */
221     avail = gst_adapter_available (rtpgstdepay->adapter);
222     outbuf = gst_adapter_take_buffer (rtpgstdepay->adapter, avail);
223
224     CV = (payload[0] >> 4) & 0x7;
225
226     if (payload[0] & 0x80) {
227       guint b, csize, left, offset;
228       GstMapInfo map;
229       GstBuffer *subbuf;
230
231       /* C bit, we have inline caps */
232       gst_buffer_map (outbuf, &map, GST_MAP_READ);
233
234       /* start reading the length, we need this to skip to the data later */
235       csize = offset = 0;
236       left = map.size;
237       do {
238         if (offset >= left) {
239           gst_buffer_unmap (outbuf, &map);
240           goto too_small;
241         }
242         b = map.data[offset++];
243         csize = (csize << 7) | (b & 0x7f);
244       } while (b & 0x80);
245
246       if (left < csize) {
247         gst_buffer_unmap (outbuf, &map);
248         goto too_small;
249       }
250
251       /* parse and store in cache */
252       outcaps = gst_caps_from_string ((gchar *) & map.data[offset]);
253       store_cache (rtpgstdepay, CV, outcaps);
254
255       /* skip caps */
256       offset += csize;
257       left -= csize;
258
259       GST_DEBUG_OBJECT (rtpgstdepay,
260           "inline caps %u, length %u, %" GST_PTR_FORMAT, CV, csize, outcaps);
261
262       /* create real data buffer when needed */
263       if (map.size)
264         subbuf =
265             gst_buffer_copy_region (outbuf, GST_BUFFER_COPY_ALL, offset, left);
266       else
267         subbuf = NULL;
268
269       gst_buffer_unmap (outbuf, &map);
270       gst_buffer_unref (outbuf);
271       outbuf = subbuf;
272     }
273
274     /* see what caps we need */
275     if (CV != rtpgstdepay->current_CV) {
276       /* we need to switch caps, check if we have the caps */
277       if ((outcaps = rtpgstdepay->CV_cache[CV]) == NULL)
278         goto missing_caps;
279
280       GST_DEBUG_OBJECT (rtpgstdepay,
281           "need caps switch from %u to %u, %" GST_PTR_FORMAT,
282           rtpgstdepay->current_CV, CV, outcaps);
283
284       /* and set caps */
285       if (gst_pad_set_caps (depayload->srcpad, outcaps))
286         rtpgstdepay->current_CV = CV;
287     }
288
289     if (outbuf) {
290       if (payload[0] & 0x8)
291         GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
292     }
293   }
294   return outbuf;
295
296   /* ERRORS */
297 empty_packet:
298   {
299     GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
300         ("Empty Payload."), (NULL));
301     gst_rtp_buffer_unmap (&rtp);
302     return NULL;
303   }
304 too_small:
305   {
306     GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
307         ("Buffer too small."), (NULL));
308     if (outbuf)
309       gst_buffer_unref (outbuf);
310     gst_rtp_buffer_unmap (&rtp);
311     return NULL;
312   }
313 missing_caps:
314   {
315     GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
316         ("Missing caps %u.", CV), (NULL));
317     if (outbuf)
318       gst_buffer_unref (outbuf);
319     gst_rtp_buffer_unmap (&rtp);
320     return NULL;
321   }
322 }
323
324 static GstStateChangeReturn
325 gst_rtp_gst_depay_change_state (GstElement * element, GstStateChange transition)
326 {
327   GstRtpGSTDepay *rtpgstdepay;
328   GstStateChangeReturn ret;
329
330   rtpgstdepay = GST_RTP_GST_DEPAY (element);
331
332   switch (transition) {
333     case GST_STATE_CHANGE_READY_TO_PAUSED:
334       gst_rtp_gst_depay_reset (rtpgstdepay);
335       break;
336     default:
337       break;
338   }
339
340   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
341
342   switch (transition) {
343     case GST_STATE_CHANGE_PAUSED_TO_READY:
344       gst_rtp_gst_depay_reset (rtpgstdepay);
345       break;
346     default:
347       break;
348   }
349   return ret;
350 }
351
352
353 gboolean
354 gst_rtp_gst_depay_plugin_init (GstPlugin * plugin)
355 {
356   return gst_element_register (plugin, "rtpgstdepay",
357       GST_RANK_MARGINAL, GST_TYPE_RTP_GST_DEPAY);
358 }