2 * Copyright (C) <2010> Wim Taymans <wim.taymans@gmail.com>
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.
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.
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.
27 #include "gstrtpgstdepay.h"
29 GST_DEBUG_CATEGORY_STATIC (rtpgstdepay_debug);
30 #define GST_CAT_DEFAULT (rtpgstdepay_debug)
32 static GstStaticPadTemplate gst_rtp_gst_depay_src_template =
33 GST_STATIC_PAD_TEMPLATE ("src",
38 static GstStaticPadTemplate gst_rtp_gst_depay_sink_template =
39 GST_STATIC_PAD_TEMPLATE ("sink",
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\"")
48 #define gst_rtp_gst_depay_parent_class parent_class
49 G_DEFINE_TYPE (GstRtpGSTDepay, gst_rtp_gst_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
51 static void gst_rtp_gst_depay_finalize (GObject * object);
53 static GstStateChangeReturn gst_rtp_gst_depay_change_state (GstElement *
54 element, GstStateChange transition);
56 static void gst_rtp_gst_depay_reset (GstRtpGSTDepay * rtpgstdepay);
57 static gboolean gst_rtp_gst_depay_setcaps (GstRTPBaseDepayload * depayload,
59 static GstBuffer *gst_rtp_gst_depay_process (GstRTPBaseDepayload * depayload,
63 gst_rtp_gst_depay_class_init (GstRtpGSTDepayClass * klass)
65 GObjectClass *gobject_class;
66 GstElementClass *gstelement_class;
67 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
69 GST_DEBUG_CATEGORY_INIT (rtpgstdepay_debug, "rtpgstdepay", 0,
70 "Gstreamer RTP Depayloader");
72 gobject_class = (GObjectClass *) klass;
73 gstelement_class = (GstElementClass *) klass;
74 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
76 gobject_class->finalize = gst_rtp_gst_depay_finalize;
78 gstelement_class->change_state = gst_rtp_gst_depay_change_state;
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));
85 gst_element_class_set_static_metadata (gstelement_class,
86 "GStreamer depayloader", "Codec/Depayloader/Network",
87 "Extracts GStreamer buffers from RTP packets",
88 "Wim Taymans <wim.taymans@gmail.com>");
90 gstrtpbasedepayload_class->set_caps = gst_rtp_gst_depay_setcaps;
91 gstrtpbasedepayload_class->process = gst_rtp_gst_depay_process;
95 gst_rtp_gst_depay_init (GstRtpGSTDepay * rtpgstdepay)
97 rtpgstdepay->adapter = gst_adapter_new ();
101 gst_rtp_gst_depay_finalize (GObject * object)
103 GstRtpGSTDepay *rtpgstdepay;
105 rtpgstdepay = GST_RTP_GST_DEPAY (object);
107 gst_rtp_gst_depay_reset (rtpgstdepay);
108 g_object_unref (rtpgstdepay->adapter);
110 G_OBJECT_CLASS (parent_class)->finalize (object);
114 store_cache (GstRtpGSTDepay * rtpgstdepay, guint CV, GstCaps * caps)
116 if (rtpgstdepay->CV_cache[CV])
117 gst_caps_unref (rtpgstdepay->CV_cache[CV]);
118 rtpgstdepay->CV_cache[CV] = caps;
122 gst_rtp_gst_depay_reset (GstRtpGSTDepay * rtpgstdepay)
126 gst_adapter_clear (rtpgstdepay->adapter);
127 rtpgstdepay->current_CV = 0;
128 for (i = 0; i < 8; i++)
129 store_cache (rtpgstdepay, i, NULL);
133 gst_rtp_gst_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
135 GstRtpGSTDepay *rtpgstdepay;
136 GstStructure *structure;
139 const gchar *capsenc;
141 rtpgstdepay = GST_RTP_GST_DEPAY (depayload);
143 structure = gst_caps_get_structure (caps, 0);
145 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
147 depayload->clock_rate = clock_rate;
149 capsenc = gst_structure_get_string (structure, "caps");
154 const gchar *capsver;
158 capsstr = (gchar *) g_base64_decode (capsenc, &out_len);
159 outcaps = gst_caps_from_string (capsstr);
163 capsver = gst_structure_get_string (structure, "capsversion");
167 /* no version, assume 0 */
171 rtpgstdepay->current_CV = CV;
172 gst_caps_ref (outcaps);
173 store_cache (rtpgstdepay, CV, outcaps);
175 res = gst_pad_set_caps (depayload->srcpad, outcaps);
176 gst_caps_unref (outcaps);
178 GST_WARNING_OBJECT (depayload, "no caps given");
179 rtpgstdepay->current_CV = -1;
187 gst_rtp_gst_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
189 GstRtpGSTDepay *rtpgstdepay;
190 GstBuffer *subbuf, *outbuf = NULL;
193 guint CV, frag_offset, avail;
194 GstRTPBuffer rtp = { NULL };
196 rtpgstdepay = GST_RTP_GST_DEPAY (depayload);
198 gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
200 payload_len = gst_rtp_buffer_get_payload_len (&rtp);
202 if (payload_len <= 8)
205 if (GST_BUFFER_IS_DISCONT (buf)) {
206 GST_WARNING_OBJECT (rtpgstdepay, "DISCONT, clear adapter");
207 gst_adapter_clear (rtpgstdepay->adapter);
210 payload = gst_rtp_buffer_get_payload (&rtp);
215 * 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
216 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
217 * |C| CV |D|0|0|0| MBZ |
218 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
220 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
223 (payload[4] << 24) | (payload[5] << 16) | (payload[6] << 8) | payload[7];
225 avail = gst_adapter_available (rtpgstdepay->adapter);
226 if (avail != frag_offset)
229 /* subbuffer skipping the 8 header bytes */
230 subbuf = gst_rtp_buffer_get_payload_subbuffer (&rtp, 8, -1);
231 gst_adapter_push (rtpgstdepay->adapter, subbuf);
233 if (gst_rtp_buffer_get_marker (&rtp)) {
237 /* take the buffer */
238 avail = gst_adapter_available (rtpgstdepay->adapter);
239 outbuf = gst_adapter_take_buffer (rtpgstdepay->adapter, avail);
241 CV = (payload[0] >> 4) & 0x7;
243 if (payload[0] & 0x80) {
244 guint b, csize, left, offset;
248 /* C bit, we have inline caps */
249 gst_buffer_map (outbuf, &map, GST_MAP_READ);
251 GST_DEBUG_OBJECT (rtpgstdepay, "buffer size %u", map.size);
253 /* start reading the length, we need this to skip to the data later */
257 if (offset >= left) {
258 gst_buffer_unmap (outbuf, &map);
261 b = map.data[offset++];
262 csize = (csize << 7) | (b & 0x7f);
265 /* we have read csize, reduce remaining buffer size */
268 gst_buffer_unmap (outbuf, &map);
272 GST_DEBUG_OBJECT (rtpgstdepay, "parsing caps %s", &map.data[offset]);
274 /* parse and store in cache */
275 outcaps = gst_caps_from_string ((gchar *) & map.data[offset]);
276 store_cache (rtpgstdepay, CV, outcaps);
282 GST_DEBUG_OBJECT (rtpgstdepay,
283 "inline caps %u, length %u, %" GST_PTR_FORMAT, CV, csize, outcaps);
285 GST_DEBUG_OBJECT (rtpgstdepay, "sub buffer: offset %u, size %u", offset,
287 /* create real data buffer when needed */
290 gst_buffer_copy_region (outbuf, GST_BUFFER_COPY_ALL, offset, left);
294 gst_buffer_unmap (outbuf, &map);
295 gst_buffer_unref (outbuf);
299 /* see what caps we need */
300 if (CV != rtpgstdepay->current_CV) {
301 /* we need to switch caps, check if we have the caps */
302 if ((outcaps = rtpgstdepay->CV_cache[CV]) == NULL)
305 GST_DEBUG_OBJECT (rtpgstdepay,
306 "need caps switch from %u to %u, %" GST_PTR_FORMAT,
307 rtpgstdepay->current_CV, CV, outcaps);
310 if (gst_pad_set_caps (depayload->srcpad, outcaps))
311 rtpgstdepay->current_CV = CV;
315 if (payload[0] & 0x8)
316 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
319 gst_rtp_buffer_unmap (&rtp);
325 GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
326 ("Empty Payload."), (NULL));
327 gst_rtp_buffer_unmap (&rtp);
332 gst_adapter_clear (rtpgstdepay->adapter);
333 gst_rtp_buffer_unmap (&rtp);
334 GST_LOG_OBJECT (rtpgstdepay, "wrong fragment, skipping");
339 GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
340 ("Buffer too small."), (NULL));
342 gst_buffer_unref (outbuf);
343 gst_rtp_buffer_unmap (&rtp);
348 GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
349 ("Missing caps %u.", CV), (NULL));
351 gst_buffer_unref (outbuf);
352 gst_rtp_buffer_unmap (&rtp);
357 static GstStateChangeReturn
358 gst_rtp_gst_depay_change_state (GstElement * element, GstStateChange transition)
360 GstRtpGSTDepay *rtpgstdepay;
361 GstStateChangeReturn ret;
363 rtpgstdepay = GST_RTP_GST_DEPAY (element);
365 switch (transition) {
366 case GST_STATE_CHANGE_READY_TO_PAUSED:
367 gst_rtp_gst_depay_reset (rtpgstdepay);
373 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
375 switch (transition) {
376 case GST_STATE_CHANGE_PAUSED_TO_READY:
377 gst_rtp_gst_depay_reset (rtpgstdepay);
387 gst_rtp_gst_depay_plugin_init (GstPlugin * plugin)
389 return gst_element_register (plugin, "rtpgstdepay",
390 GST_RANK_MARGINAL, GST_TYPE_RTP_GST_DEPAY);