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.
24 #include <gst/rtp/gstrtpbuffer.h>
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 GST_BOILERPLATE (GstRtpGSTDepay, gst_rtp_gst_depay, GstBaseRTPDepayload,
49 GST_TYPE_BASE_RTP_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 (GstBaseRTPDepayload * depayload,
59 static GstBuffer *gst_rtp_gst_depay_process (GstBaseRTPDepayload * depayload,
63 gst_rtp_gst_depay_base_init (gpointer klass)
65 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
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));
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>");
79 gst_rtp_gst_depay_class_init (GstRtpGSTDepayClass * klass)
81 GObjectClass *gobject_class;
82 GstElementClass *gstelement_class;
83 GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
85 gobject_class = (GObjectClass *) klass;
86 gstelement_class = (GstElementClass *) klass;
87 gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
89 gobject_class->finalize = gst_rtp_gst_depay_finalize;
91 gstelement_class->change_state = gst_rtp_gst_depay_change_state;
93 gstbasertpdepayload_class->set_caps = gst_rtp_gst_depay_setcaps;
94 gstbasertpdepayload_class->process = gst_rtp_gst_depay_process;
96 GST_DEBUG_CATEGORY_INIT (rtpgstdepay_debug, "rtpgstdepay", 0,
97 "Gstreamer RTP Depayloader");
101 gst_rtp_gst_depay_init (GstRtpGSTDepay * rtpgstdepay,
102 GstRtpGSTDepayClass * klass)
104 rtpgstdepay->adapter = gst_adapter_new ();
108 gst_rtp_gst_depay_finalize (GObject * object)
110 GstRtpGSTDepay *rtpgstdepay;
112 rtpgstdepay = GST_RTP_GST_DEPAY (object);
114 gst_rtp_gst_depay_reset (rtpgstdepay);
115 g_object_unref (rtpgstdepay->adapter);
117 G_OBJECT_CLASS (parent_class)->finalize (object);
121 store_cache (GstRtpGSTDepay * rtpgstdepay, guint CV, GstCaps * caps)
123 if (rtpgstdepay->CV_cache[CV])
124 gst_caps_unref (rtpgstdepay->CV_cache[CV]);
125 rtpgstdepay->CV_cache[CV] = caps;
129 gst_rtp_gst_depay_reset (GstRtpGSTDepay * rtpgstdepay)
133 gst_adapter_clear (rtpgstdepay->adapter);
134 rtpgstdepay->current_CV = 0;
135 for (i = 0; i < 8; i++)
136 store_cache (rtpgstdepay, i, NULL);
140 gst_rtp_gst_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
142 GstRtpGSTDepay *rtpgstdepay;
143 GstStructure *structure;
147 const gchar *capsenc;
150 rtpgstdepay = GST_RTP_GST_DEPAY (depayload);
152 structure = gst_caps_get_structure (caps, 0);
154 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
156 depayload->clock_rate = clock_rate;
158 capsenc = gst_structure_get_string (structure, "caps");
162 capsstr = (gchar *) g_base64_decode (capsenc, &out_len);
163 outcaps = gst_caps_from_string (capsstr);
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);
171 outcaps = gst_caps_new_any ();
173 res = gst_pad_set_caps (depayload->srcpad, outcaps);
174 gst_caps_unref (outcaps);
180 gst_rtp_gst_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
182 GstRtpGSTDepay *rtpgstdepay;
183 GstBuffer *subbuf, *outbuf = NULL;
187 GstRTPBuffer rtp = { NULL };
189 rtpgstdepay = GST_RTP_GST_DEPAY (depayload);
191 gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
193 payload_len = gst_rtp_buffer_get_payload_len (&rtp);
195 if (payload_len <= 8)
198 if (GST_BUFFER_IS_DISCONT (buf)) {
199 GST_WARNING_OBJECT (rtpgstdepay, "DISCONT, clear adapter");
200 gst_adapter_clear (rtpgstdepay->adapter);
203 payload = gst_rtp_buffer_get_payload (&rtp);
208 * 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
209 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
210 * |C| CV |D|X|Y|Z| MBZ |
211 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
213 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
216 * (payload[4] << 24) | (payload[5] << 16) | (payload[6] << 8) | payload[7];
219 /* subbuffer skipping the 8 header bytes */
220 subbuf = gst_rtp_buffer_get_payload_subbuffer (&rtp, 8, -1);
221 gst_adapter_push (rtpgstdepay->adapter, subbuf);
223 if (gst_rtp_buffer_get_marker (&rtp)) {
227 /* take the buffer */
228 avail = gst_adapter_available (rtpgstdepay->adapter);
229 outbuf = gst_adapter_take_buffer (rtpgstdepay->adapter, avail);
231 CV = (payload[0] >> 4) & 0x7;
233 if (payload[0] & 0x80) {
234 guint b, csize, left, offset;
239 /* C bit, we have inline caps */
240 data = gst_buffer_map (outbuf, &size, NULL, GST_MAP_READ);
242 /* start reading the length, we need this to skip to the data later */
246 if (offset >= left) {
247 gst_buffer_unmap (outbuf, data, size);
251 csize = (csize << 7) | (b & 0x7f);
255 gst_buffer_unmap (outbuf, data, size);
259 /* parse and store in cache */
260 outcaps = gst_caps_from_string ((gchar *) & data[offset]);
261 store_cache (rtpgstdepay, CV, outcaps);
267 GST_DEBUG_OBJECT (rtpgstdepay,
268 "inline caps %u, length %u, %" GST_PTR_FORMAT, CV, csize, outcaps);
270 /* create real data buffer when needed */
273 gst_buffer_copy_region (outbuf, GST_BUFFER_COPY_ALL, offset, left);
277 gst_buffer_unmap (outbuf, data, size);
278 gst_buffer_unref (outbuf);
282 /* see what caps we need */
283 if (CV != rtpgstdepay->current_CV) {
284 /* we need to switch caps, check if we have the caps */
285 if ((outcaps = rtpgstdepay->CV_cache[CV]) == NULL)
288 GST_DEBUG_OBJECT (rtpgstdepay,
289 "need caps switch from %u to %u, %" GST_PTR_FORMAT,
290 rtpgstdepay->current_CV, CV, outcaps);
293 if (gst_pad_set_caps (depayload->srcpad, outcaps))
294 rtpgstdepay->current_CV = CV;
298 if (payload[0] & 0x8)
299 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
300 if (payload[0] & 0x4)
301 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_MEDIA1);
302 if (payload[0] & 0x2)
303 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_MEDIA2);
304 if (payload[0] & 0x1)
305 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_MEDIA3);
313 GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
314 ("Empty Payload."), (NULL));
315 gst_rtp_buffer_unmap (&rtp);
320 GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
321 ("Buffer too small."), (NULL));
323 gst_buffer_unref (outbuf);
324 gst_rtp_buffer_unmap (&rtp);
329 GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
330 ("Missing caps %u.", CV), (NULL));
332 gst_buffer_unref (outbuf);
333 gst_rtp_buffer_unmap (&rtp);
338 static GstStateChangeReturn
339 gst_rtp_gst_depay_change_state (GstElement * element, GstStateChange transition)
341 GstRtpGSTDepay *rtpgstdepay;
342 GstStateChangeReturn ret;
344 rtpgstdepay = GST_RTP_GST_DEPAY (element);
346 switch (transition) {
347 case GST_STATE_CHANGE_READY_TO_PAUSED:
348 gst_rtp_gst_depay_reset (rtpgstdepay);
354 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
356 switch (transition) {
357 case GST_STATE_CHANGE_PAUSED_TO_READY:
358 gst_rtp_gst_depay_reset (rtpgstdepay);
368 gst_rtp_gst_depay_plugin_init (GstPlugin * plugin)
370 return gst_element_register (plugin, "rtpgstdepay",
371 GST_RANK_MARGINAL, GST_TYPE_RTP_GST_DEPAY);