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       gsize size;
229       guint8 *data;
230       GstBuffer *subbuf;
231
232       /* C bit, we have inline caps */
233       data = gst_buffer_map (outbuf, &size, NULL, GST_MAP_READ);
234
235       /* start reading the length, we need this to skip to the data later */
236       csize = offset = 0;
237       left = size;
238       do {
239         if (offset >= left) {
240           gst_buffer_unmap (outbuf, data, size);
241           goto too_small;
242         }
243         b = data[offset++];
244         csize = (csize << 7) | (b & 0x7f);
245       } while (b & 0x80);
246
247       if (left < csize) {
248         gst_buffer_unmap (outbuf, data, size);
249         goto too_small;
250       }
251
252       /* parse and store in cache */
253       outcaps = gst_caps_from_string ((gchar *) & data[offset]);
254       store_cache (rtpgstdepay, CV, outcaps);
255
256       /* skip caps */
257       offset += csize;
258       left -= csize;
259
260       GST_DEBUG_OBJECT (rtpgstdepay,
261           "inline caps %u, length %u, %" GST_PTR_FORMAT, CV, csize, outcaps);
262
263       /* create real data buffer when needed */
264       if (size)
265         subbuf =
266             gst_buffer_copy_region (outbuf, GST_BUFFER_COPY_ALL, offset, left);
267       else
268         subbuf = NULL;
269
270       gst_buffer_unmap (outbuf, data, size);
271       gst_buffer_unref (outbuf);
272       outbuf = subbuf;
273     }
274
275     /* see what caps we need */
276     if (CV != rtpgstdepay->current_CV) {
277       /* we need to switch caps, check if we have the caps */
278       if ((outcaps = rtpgstdepay->CV_cache[CV]) == NULL)
279         goto missing_caps;
280
281       GST_DEBUG_OBJECT (rtpgstdepay,
282           "need caps switch from %u to %u, %" GST_PTR_FORMAT,
283           rtpgstdepay->current_CV, CV, outcaps);
284
285       /* and set caps */
286       if (gst_pad_set_caps (depayload->srcpad, outcaps))
287         rtpgstdepay->current_CV = CV;
288     }
289
290     if (outbuf) {
291       if (payload[0] & 0x8)
292         GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
293     }
294   }
295   return outbuf;
296
297   /* ERRORS */
298 empty_packet:
299   {
300     GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
301         ("Empty Payload."), (NULL));
302     gst_rtp_buffer_unmap (&rtp);
303     return NULL;
304   }
305 too_small:
306   {
307     GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
308         ("Buffer too small."), (NULL));
309     if (outbuf)
310       gst_buffer_unref (outbuf);
311     gst_rtp_buffer_unmap (&rtp);
312     return NULL;
313   }
314 missing_caps:
315   {
316     GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
317         ("Missing caps %u.", CV), (NULL));
318     if (outbuf)
319       gst_buffer_unref (outbuf);
320     gst_rtp_buffer_unmap (&rtp);
321     return NULL;
322   }
323 }
324
325 static GstStateChangeReturn
326 gst_rtp_gst_depay_change_state (GstElement * element, GstStateChange transition)
327 {
328   GstRtpGSTDepay *rtpgstdepay;
329   GstStateChangeReturn ret;
330
331   rtpgstdepay = GST_RTP_GST_DEPAY (element);
332
333   switch (transition) {
334     case GST_STATE_CHANGE_READY_TO_PAUSED:
335       gst_rtp_gst_depay_reset (rtpgstdepay);
336       break;
337     default:
338       break;
339   }
340
341   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
342
343   switch (transition) {
344     case GST_STATE_CHANGE_PAUSED_TO_READY:
345       gst_rtp_gst_depay_reset (rtpgstdepay);
346       break;
347     default:
348       break;
349   }
350   return ret;
351 }
352
353
354 gboolean
355 gst_rtp_gst_depay_plugin_init (GstPlugin * plugin)
356 {
357   return gst_element_register (plugin, "rtpgstdepay",
358       GST_RANK_MARGINAL, GST_TYPE_RTP_GST_DEPAY);
359 }