upload tizen1.0 source
[framework/multimedia/gst-plugins-good0.10.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 GST_BOILERPLATE (GstRtpGSTDepay, gst_rtp_gst_depay, GstBaseRTPDepayload,
49     GST_TYPE_BASE_RTP_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 (GstBaseRTPDepayload * depayload,
58     GstCaps * caps);
59 static GstBuffer *gst_rtp_gst_depay_process (GstBaseRTPDepayload * depayload,
60     GstBuffer * buf);
61
62 static void
63 gst_rtp_gst_depay_base_init (gpointer klass)
64 {
65   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
66
67   gst_element_class_add_pad_template (element_class,
68       gst_static_pad_template_get (&gst_rtp_gst_depay_src_template));
69   gst_element_class_add_pad_template (element_class,
70       gst_static_pad_template_get (&gst_rtp_gst_depay_sink_template));
71
72   gst_element_class_set_details_simple (element_class,
73       "GStreamer depayloader", "Codec/Depayloader/Network",
74       "Extracts GStreamer buffers from RTP packets",
75       "Wim Taymans <wim.taymans@gmail.com>");
76 }
77
78 static void
79 gst_rtp_gst_depay_class_init (GstRtpGSTDepayClass * klass)
80 {
81   GObjectClass *gobject_class;
82   GstElementClass *gstelement_class;
83   GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
84
85   gobject_class = (GObjectClass *) klass;
86   gstelement_class = (GstElementClass *) klass;
87   gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
88
89   gobject_class->finalize = gst_rtp_gst_depay_finalize;
90
91   gstelement_class->change_state = gst_rtp_gst_depay_change_state;
92
93   gstbasertpdepayload_class->set_caps = gst_rtp_gst_depay_setcaps;
94   gstbasertpdepayload_class->process = gst_rtp_gst_depay_process;
95
96   GST_DEBUG_CATEGORY_INIT (rtpgstdepay_debug, "rtpgstdepay", 0,
97       "Gstreamer RTP Depayloader");
98 }
99
100 static void
101 gst_rtp_gst_depay_init (GstRtpGSTDepay * rtpgstdepay,
102     GstRtpGSTDepayClass * klass)
103 {
104   rtpgstdepay->adapter = gst_adapter_new ();
105 }
106
107 static void
108 gst_rtp_gst_depay_finalize (GObject * object)
109 {
110   GstRtpGSTDepay *rtpgstdepay;
111
112   rtpgstdepay = GST_RTP_GST_DEPAY (object);
113
114   gst_rtp_gst_depay_reset (rtpgstdepay);
115   g_object_unref (rtpgstdepay->adapter);
116
117   G_OBJECT_CLASS (parent_class)->finalize (object);
118 }
119
120 static void
121 store_cache (GstRtpGSTDepay * rtpgstdepay, guint CV, GstCaps * caps)
122 {
123   if (rtpgstdepay->CV_cache[CV])
124     gst_caps_unref (rtpgstdepay->CV_cache[CV]);
125   rtpgstdepay->CV_cache[CV] = caps;
126 }
127
128 static void
129 gst_rtp_gst_depay_reset (GstRtpGSTDepay * rtpgstdepay)
130 {
131   guint i;
132
133   gst_adapter_clear (rtpgstdepay->adapter);
134   rtpgstdepay->current_CV = 0;
135   for (i = 0; i < 8; i++)
136     store_cache (rtpgstdepay, i, NULL);
137 }
138
139 static gboolean
140 gst_rtp_gst_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
141 {
142   GstRtpGSTDepay *rtpgstdepay;
143   GstStructure *structure;
144   GstCaps *outcaps;
145   gint clock_rate;
146   gboolean res;
147   const gchar *capsenc;
148   gchar *capsstr;
149
150   rtpgstdepay = GST_RTP_GST_DEPAY (depayload);
151
152   structure = gst_caps_get_structure (caps, 0);
153
154   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
155     clock_rate = 90000;
156   depayload->clock_rate = clock_rate;
157
158   capsenc = gst_structure_get_string (structure, "caps");
159   if (capsenc) {
160     gsize out_len;
161
162     capsstr = (gchar *) g_base64_decode (capsenc, &out_len);
163     outcaps = gst_caps_from_string (capsstr);
164     g_free (capsstr);
165
166     /* we have the SDP caps as output caps */
167     rtpgstdepay->current_CV = 0;
168     gst_caps_ref (outcaps);
169     store_cache (rtpgstdepay, 0, outcaps);
170   } else {
171     outcaps = gst_caps_new_any ();
172   }
173   res = gst_pad_set_caps (depayload->srcpad, outcaps);
174   gst_caps_unref (outcaps);
175
176   return res;
177 }
178
179 static GstBuffer *
180 gst_rtp_gst_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
181 {
182   GstRtpGSTDepay *rtpgstdepay;
183   GstBuffer *subbuf, *outbuf = NULL;
184   gint payload_len;
185   guint8 *payload;
186   guint CV;
187
188   rtpgstdepay = GST_RTP_GST_DEPAY (depayload);
189
190   payload_len = gst_rtp_buffer_get_payload_len (buf);
191
192   if (payload_len <= 8)
193     goto empty_packet;
194
195   if (GST_BUFFER_IS_DISCONT (buf)) {
196     GST_WARNING_OBJECT (rtpgstdepay, "DISCONT, clear adapter");
197     gst_adapter_clear (rtpgstdepay->adapter);
198   }
199
200   payload = gst_rtp_buffer_get_payload (buf);
201
202   /* strip off header
203    *
204    *  0                   1                   2                   3
205    *  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
206    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
207    * |C| CV  |D|X|Y|Z|                  MBZ                          |
208    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
209    * |                          Frag_offset                          |
210    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
211    */
212   /* frag_offset =
213    *   (payload[4] << 24) | (payload[5] << 16) | (payload[6] << 8) | payload[7];
214    */
215
216   /* subbuffer skipping the 8 header bytes */
217   subbuf = gst_rtp_buffer_get_payload_subbuffer (buf, 8, -1);
218   gst_adapter_push (rtpgstdepay->adapter, subbuf);
219
220   if (gst_rtp_buffer_get_marker (buf)) {
221     guint avail;
222     GstCaps *outcaps;
223
224     /* take the buffer */
225     avail = gst_adapter_available (rtpgstdepay->adapter);
226     outbuf = gst_adapter_take_buffer (rtpgstdepay->adapter, avail);
227
228     CV = (payload[0] >> 4) & 0x7;
229
230     if (payload[0] & 0x80) {
231       guint b, csize, size, offset;
232       guint8 *data;
233       GstBuffer *subbuf;
234
235       /* C bit, we have inline caps */
236       data = GST_BUFFER_DATA (outbuf);
237       size = GST_BUFFER_SIZE (outbuf);
238
239       /* start reading the length, we need this to skip to the data later */
240       csize = offset = 0;
241       do {
242         if (offset >= size)
243           goto too_small;
244         b = data[offset++];
245         csize = (csize << 7) | (b & 0x7f);
246       } while (b & 0x80);
247
248       if (size < csize)
249         goto too_small;
250
251       /* parse and store in cache */
252       outcaps = gst_caps_from_string ((gchar *) & data[offset]);
253       store_cache (rtpgstdepay, CV, outcaps);
254
255       /* skip caps */
256       offset += csize;
257       size -= 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 (size)
264         subbuf = gst_buffer_create_sub (outbuf, offset, size);
265       else
266         subbuf = NULL;
267
268       gst_buffer_unref (outbuf);
269       outbuf = subbuf;
270     }
271
272     /* see what caps we need */
273     if (CV != rtpgstdepay->current_CV) {
274       /* we need to switch caps, check if we have the caps */
275       if ((outcaps = rtpgstdepay->CV_cache[CV]) == NULL)
276         goto missing_caps;
277
278       GST_DEBUG_OBJECT (rtpgstdepay,
279           "need caps switch from %u to %u, %" GST_PTR_FORMAT,
280           rtpgstdepay->current_CV, CV, outcaps);
281
282       /* and set caps */
283       if (gst_pad_set_caps (depayload->srcpad, outcaps))
284         rtpgstdepay->current_CV = CV;
285     }
286
287     if (outbuf) {
288       if (payload[0] & 0x8)
289         GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
290       if (payload[0] & 0x4)
291         GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_MEDIA1);
292       if (payload[0] & 0x2)
293         GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_MEDIA2);
294       if (payload[0] & 0x1)
295         GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_MEDIA3);
296     }
297   }
298   return outbuf;
299
300   /* ERRORS */
301 empty_packet:
302   {
303     GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
304         ("Empty Payload."), (NULL));
305     return NULL;
306   }
307 too_small:
308   {
309     GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
310         ("Buffer too small."), (NULL));
311     if (outbuf)
312       gst_buffer_unref (outbuf);
313     return NULL;
314   }
315 missing_caps:
316   {
317     GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
318         ("Missing caps %u.", CV), (NULL));
319     if (outbuf)
320       gst_buffer_unref (outbuf);
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 }