2 * Copyright (C) 2006 Thomas Vander Stichele <thomas at apestaart dot org>
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.
21 * SECTION:element-gdpdepay
26 * This element depayloads GStreamer Data Protocol buffers back to deserialized
31 * gst-launch -v -m filesrc location=test.gdp ! gdpdepay ! xvimagesink
33 * This pipeline plays back a serialized video stream as created in the
45 #include <gst/dataprotocol/dataprotocol.h>
47 #include "gstgdpdepay.h"
49 /* elementfactory information */
50 static const GstElementDetails gdp_depay_details =
51 GST_ELEMENT_DETAILS ("GDP Depayloader",
53 "Depayloads GStreamer Data Protocol buffers",
54 "Thomas Vander Stichele <thomas at apestaart dot org>");
62 static GstStaticPadTemplate gdp_depay_sink_template =
63 GST_STATIC_PAD_TEMPLATE ("sink",
66 GST_STATIC_CAPS ("application/x-gdp"));
68 static GstStaticPadTemplate gdp_depay_src_template =
69 GST_STATIC_PAD_TEMPLATE ("src",
74 GST_DEBUG_CATEGORY_STATIC (gst_gdp_depay_debug);
75 #define GST_CAT_DEFAULT gst_gdp_depay_debug
78 GST_DEBUG_CATEGORY_INIT (gst_gdp_depay_debug, "gdpdepay", 0, \
81 GST_BOILERPLATE_FULL (GstGDPDepay, gst_gdp_depay, GstElement,
82 GST_TYPE_ELEMENT, _do_init);
84 static gboolean gst_gdp_depay_sink_event (GstPad * pad, GstEvent * event);
85 static GstFlowReturn gst_gdp_depay_chain (GstPad * pad, GstBuffer * buffer);
87 static GstStateChangeReturn gst_gdp_depay_change_state (GstElement *
88 element, GstStateChange transition);
90 static void gst_gdp_depay_finalize (GObject * object);
93 gst_gdp_depay_base_init (gpointer g_class)
95 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
97 gst_element_class_set_details (element_class, &gdp_depay_details);
99 gst_element_class_add_pad_template (element_class,
100 gst_static_pad_template_get (&gdp_depay_sink_template));
101 gst_element_class_add_pad_template (element_class,
102 gst_static_pad_template_get (&gdp_depay_src_template));
106 gst_gdp_depay_class_init (GstGDPDepayClass * klass)
108 GObjectClass *gobject_class;
109 GstElementClass *gstelement_class;
111 gobject_class = (GObjectClass *) klass;
112 gstelement_class = (GstElementClass *) klass;
114 gstelement_class->change_state =
115 GST_DEBUG_FUNCPTR (gst_gdp_depay_change_state);
116 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_gdp_depay_finalize);
120 gst_gdp_depay_init (GstGDPDepay * gdpdepay, GstGDPDepayClass * g_class)
123 gst_pad_new_from_static_template (&gdp_depay_sink_template, "sink");
124 gst_pad_set_chain_function (gdpdepay->sinkpad,
125 GST_DEBUG_FUNCPTR (gst_gdp_depay_chain));
126 gst_pad_set_event_function (gdpdepay->sinkpad,
127 GST_DEBUG_FUNCPTR (gst_gdp_depay_sink_event));
128 gst_element_add_pad (GST_ELEMENT (gdpdepay), gdpdepay->sinkpad);
131 gst_pad_new_from_static_template (&gdp_depay_src_template, "src");
132 /* our caps will always be decided by the incoming GDP caps buffers */
133 gst_pad_use_fixed_caps (gdpdepay->srcpad);
134 gst_element_add_pad (GST_ELEMENT (gdpdepay), gdpdepay->srcpad);
136 gdpdepay->adapter = gst_adapter_new ();
140 gst_gdp_depay_finalize (GObject * gobject)
144 this = GST_GDP_DEPAY (gobject);
146 gst_caps_unref (this->caps);
148 g_free (this->header);
149 gst_adapter_clear (this->adapter);
150 g_object_unref (this->adapter);
152 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (gobject));
155 /* we ignore most events because the only events we want to output are those
156 * found in the gdp data stream.
157 * An exception is the EOS event, if we get that on the sinkpad, we certainly
158 * are not going to produce more data.
161 gst_gdp_depay_sink_event (GstPad * pad, GstEvent * event)
166 this = GST_GDP_DEPAY (gst_pad_get_parent (pad));
168 switch (GST_EVENT_TYPE (event)) {
170 res = gst_pad_push_event (this->srcpad, event);
173 gst_event_unref (event);
176 gst_object_unref (this);
182 gst_gdp_depay_chain (GstPad * pad, GstBuffer * buffer)
185 GstFlowReturn ret = GST_FLOW_OK;
189 guint8 *header = NULL;
190 guint8 *payload = NULL;
192 gboolean running = TRUE;
194 this = GST_GDP_DEPAY (gst_pad_get_parent (pad));
196 gst_adapter_push (this->adapter, buffer);
199 switch (this->state) {
200 case GST_GDP_DEPAY_STATE_HEADER:
201 available = gst_adapter_available (this->adapter);
202 if (available < GST_DP_HEADER_LENGTH) {
208 g_free (this->header);
209 GST_LOG_OBJECT (this, "reading GDP header from adapter");
210 header = gst_adapter_take (this->adapter, GST_DP_HEADER_LENGTH);
211 if (!gst_dp_validate_header (GST_DP_HEADER_LENGTH, header))
212 goto header_validate_error;
214 this->payload_length = gst_dp_header_payload_length (header);
215 this->payload_type = gst_dp_header_payload_type (header);
216 this->header = header;
217 GST_LOG_OBJECT (this,
218 "read GDP header, payload size %d, switching to state PAYLOAD",
219 this->payload_length);
220 this->state = GST_GDP_DEPAY_STATE_PAYLOAD;
223 case GST_GDP_DEPAY_STATE_PAYLOAD:
224 available = gst_adapter_available (this->adapter);
225 if (available < this->payload_length) {
230 /* change state based on type */
231 if (this->payload_type == GST_DP_PAYLOAD_BUFFER) {
232 GST_LOG_OBJECT (this, "switching to state BUFFER");
233 this->state = GST_GDP_DEPAY_STATE_BUFFER;
234 } else if (this->payload_type == GST_DP_PAYLOAD_CAPS) {
235 GST_LOG_OBJECT (this, "switching to state CAPS");
236 this->state = GST_GDP_DEPAY_STATE_CAPS;
237 } else if (this->payload_type >= GST_DP_PAYLOAD_EVENT_NONE) {
238 GST_LOG_OBJECT (this, "switching to state EVENT");
239 this->state = GST_GDP_DEPAY_STATE_EVENT;
244 case GST_GDP_DEPAY_STATE_BUFFER:
246 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
247 ("Received a buffer without first receiving caps"));
248 ret = GST_FLOW_NOT_NEGOTIATED;
252 GST_LOG_OBJECT (this, "reading GDP buffer from adapter");
253 buf = gst_dp_buffer_from_header (GST_DP_HEADER_LENGTH, this->header);
255 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
256 ("could not create buffer from GDP packet"));
257 ret = GST_FLOW_ERROR;
261 payload = gst_adapter_take (this->adapter, this->payload_length);
262 memcpy (GST_BUFFER_DATA (buf), payload, this->payload_length);
266 gst_buffer_set_caps (buf, this->caps);
267 ret = gst_pad_push (this->srcpad, buf);
268 if (ret != GST_FLOW_OK) {
269 GST_WARNING_OBJECT (this, "pushing depayloaded buffer returned %d",
274 GST_LOG_OBJECT (this, "switching to state HEADER");
275 this->state = GST_GDP_DEPAY_STATE_HEADER;
278 case GST_GDP_DEPAY_STATE_CAPS:
279 GST_LOG_OBJECT (this, "reading GDP caps from adapter");
280 payload = gst_adapter_take (this->adapter, this->payload_length);
281 caps = gst_dp_caps_from_packet (GST_DP_HEADER_LENGTH, this->header,
286 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
287 ("could not create caps from GDP packet"));
288 ret = GST_FLOW_ERROR;
291 GST_DEBUG_OBJECT (this, "read caps %" GST_PTR_FORMAT, caps);
292 gst_caps_replace (&(this->caps), caps);
293 gst_pad_set_caps (this->srcpad, caps);
294 /* drop the creation ref we still have */
295 gst_caps_unref (caps);
297 GST_LOG_OBJECT (this, "switching to state HEADER");
298 this->state = GST_GDP_DEPAY_STATE_HEADER;
301 case GST_GDP_DEPAY_STATE_EVENT:
302 GST_LOG_OBJECT (this, "reading GDP event from adapter");
303 /* adapter doesn't like 0 length payload */
304 if (this->payload_length > 0)
305 payload = gst_adapter_take (this->adapter, this->payload_length);
306 event = gst_dp_event_from_packet (GST_DP_HEADER_LENGTH, this->header,
313 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
314 ("could not create event from GDP packet"));
315 ret = GST_FLOW_ERROR;
318 GST_DEBUG_OBJECT (this, "sending deserialized event %p of type %s",
319 event, gst_event_type_get_name (event->type));
320 gst_pad_push_event (this->srcpad, event);
322 GST_LOG_OBJECT (this, "switching to state HEADER");
323 this->state = GST_GDP_DEPAY_STATE_HEADER;
329 header_validate_error:
330 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
331 ("GDP packet header does not validate"));
333 ret = GST_FLOW_ERROR;
337 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
338 ("GDP packet header is of wrong type"));
340 ret = GST_FLOW_ERROR;
344 gst_object_unref (this);
348 static GstStateChangeReturn
349 gst_gdp_depay_change_state (GstElement * element, GstStateChange transition)
351 GstStateChangeReturn ret;
352 GstGDPDepay *this = GST_GDP_DEPAY (element);
354 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
356 switch (transition) {
357 case GST_STATE_CHANGE_READY_TO_NULL:
359 gst_caps_unref (this->caps);
371 gst_gdp_depay_plugin_init (GstPlugin * plugin)
373 if (!gst_element_register (plugin, "gdpdepay", GST_RANK_NONE,