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 GstFlowReturn gst_gdp_depay_chain (GstPad * pad, GstBuffer * buffer);
85 static GstStateChangeReturn gst_gdp_depay_change_state (GstElement *
86 element, GstStateChange transition);
88 static void gst_gdp_depay_finalize (GObject * object);
91 gst_gdp_depay_base_init (gpointer g_class)
93 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
95 gst_element_class_set_details (element_class, &gdp_depay_details);
97 gst_element_class_add_pad_template (element_class,
98 gst_static_pad_template_get (&gdp_depay_sink_template));
99 gst_element_class_add_pad_template (element_class,
100 gst_static_pad_template_get (&gdp_depay_src_template));
104 gst_gdp_depay_class_init (GstGDPDepayClass * klass)
106 GObjectClass *gobject_class;
107 GstElementClass *gstelement_class;
109 gobject_class = (GObjectClass *) klass;
110 gstelement_class = (GstElementClass *) klass;
112 parent_class = g_type_class_peek_parent (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_setcaps_function (gdpdepay->sinkpad,
125 GST_DEBUG_FUNCPTR (gst_pad_proxy_setcaps));
126 gst_pad_set_getcaps_function (gdpdepay->sinkpad,
127 GST_DEBUG_FUNCPTR (gst_pad_proxy_getcaps));
128 gst_pad_set_chain_function (gdpdepay->sinkpad,
129 GST_DEBUG_FUNCPTR (gst_gdp_depay_chain));
130 gst_element_add_pad (GST_ELEMENT (gdpdepay), gdpdepay->sinkpad);
133 gst_pad_new_from_static_template (&gdp_depay_src_template, "src");
134 gst_element_add_pad (GST_ELEMENT (gdpdepay), gdpdepay->srcpad);
136 /* our caps will always be decided by the incoming GDP caps buffers */
137 gst_pad_use_fixed_caps (gdpdepay->srcpad);
139 gdpdepay->adapter = gst_adapter_new ();
143 gst_gdp_depay_finalize (GObject * gobject)
147 this = GST_GDP_DEPAY (gobject);
149 gst_caps_unref (this->caps);
151 g_free (this->header);
152 gst_adapter_clear (this->adapter);
153 g_object_unref (this->adapter);
155 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (gobject));
159 gst_gdp_depay_chain (GstPad * pad, GstBuffer * buffer)
162 GstFlowReturn ret = GST_FLOW_OK;
166 guint8 *header = NULL;
167 guint8 *payload = NULL;
169 gboolean running = TRUE;
171 this = GST_GDP_DEPAY (gst_pad_get_parent (pad));
173 gst_adapter_push (this->adapter, buffer);
176 switch (this->state) {
177 case GST_GDP_DEPAY_STATE_HEADER:
178 available = gst_adapter_available (this->adapter);
179 if (available < GST_DP_HEADER_LENGTH) {
185 g_free (this->header);
186 GST_LOG_OBJECT (this, "reading GDP header from adapter");
187 header = gst_adapter_take (this->adapter, GST_DP_HEADER_LENGTH);
188 if (!gst_dp_validate_header (GST_DP_HEADER_LENGTH, header))
189 goto header_validate_error;
191 this->payload_length = gst_dp_header_payload_length (header);
192 this->payload_type = gst_dp_header_payload_type (header);
193 this->header = header;
194 GST_LOG_OBJECT (this,
195 "read GDP header, payload size %d, switching to state PAYLOAD",
196 this->payload_length);
197 this->state = GST_GDP_DEPAY_STATE_PAYLOAD;
200 case GST_GDP_DEPAY_STATE_PAYLOAD:
201 available = gst_adapter_available (this->adapter);
202 if (available < this->payload_length) {
207 /* change state based on type */
208 if (this->payload_type == GST_DP_PAYLOAD_BUFFER) {
209 GST_LOG_OBJECT (this, "switching to state BUFFER");
210 this->state = GST_GDP_DEPAY_STATE_BUFFER;
211 } else if (this->payload_type == GST_DP_PAYLOAD_CAPS) {
212 GST_LOG_OBJECT (this, "switching to state CAPS");
213 this->state = GST_GDP_DEPAY_STATE_CAPS;
214 } else if (this->payload_type >= GST_DP_PAYLOAD_EVENT_NONE) {
215 GST_LOG_OBJECT (this, "switching to state EVENT");
216 this->state = GST_GDP_DEPAY_STATE_EVENT;
221 case GST_GDP_DEPAY_STATE_BUFFER:
223 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
224 ("Received a buffer without first receiving caps"));
225 ret = GST_FLOW_NOT_NEGOTIATED;
229 GST_LOG_OBJECT (this, "reading GDP buffer from adapter");
230 buf = gst_dp_buffer_from_header (GST_DP_HEADER_LENGTH, this->header);
232 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
233 ("could not create buffer from GDP packet"));
234 ret = GST_FLOW_ERROR;
238 payload = gst_adapter_take (this->adapter, this->payload_length);
239 memcpy (GST_BUFFER_DATA (buf), payload, this->payload_length);
243 gst_buffer_set_caps (buf, this->caps);
244 ret = gst_pad_push (this->srcpad, buf);
245 if (ret != GST_FLOW_OK) {
246 GST_WARNING_OBJECT (this, "pushing depayloaded buffer returned %d",
251 GST_LOG_OBJECT (this, "switching to state HEADER");
252 this->state = GST_GDP_DEPAY_STATE_HEADER;
255 case GST_GDP_DEPAY_STATE_CAPS:
256 GST_LOG_OBJECT (this, "reading GDP caps from adapter");
257 payload = gst_adapter_take (this->adapter, this->payload_length);
258 caps = gst_dp_caps_from_packet (GST_DP_HEADER_LENGTH, this->header,
263 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
264 ("could not create caps from GDP packet"));
265 ret = GST_FLOW_ERROR;
268 GST_DEBUG_OBJECT (this, "read caps %" GST_PTR_FORMAT, caps);
269 gst_caps_replace (&(this->caps), caps);
270 gst_pad_set_caps (this->srcpad, caps);
271 /* drop the creation ref we still have */
272 gst_caps_unref (caps);
274 GST_LOG_OBJECT (this, "switching to state HEADER");
275 this->state = GST_GDP_DEPAY_STATE_HEADER;
278 case GST_GDP_DEPAY_STATE_EVENT:
279 GST_LOG_OBJECT (this, "reading GDP event from adapter");
280 /* adapter doesn't like 0 length payload */
281 if (this->payload_length > 0)
282 payload = gst_adapter_take (this->adapter, this->payload_length);
283 event = gst_dp_event_from_packet (GST_DP_HEADER_LENGTH, this->header,
290 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
291 ("could not create event from GDP packet"));
292 ret = GST_FLOW_ERROR;
295 /* FIXME: set me as source ? */
296 GST_DEBUG_OBJECT (this, "sending deserialized event %p of type %s",
297 event, gst_event_type_get_name (event->type));
298 gst_pad_push_event (this->srcpad, event);
300 GST_LOG_OBJECT (this, "switching to state HEADER");
301 this->state = GST_GDP_DEPAY_STATE_HEADER;
307 header_validate_error:
308 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
309 ("GDP packet header does not validate"));
311 ret = GST_FLOW_ERROR;
315 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
316 ("GDP packet header is of wrong type"));
318 ret = GST_FLOW_ERROR;
322 gst_object_unref (this);
326 static GstStateChangeReturn
327 gst_gdp_depay_change_state (GstElement * element, GstStateChange transition)
329 GstStateChangeReturn ret;
330 GstGDPDepay *this = GST_GDP_DEPAY (element);
332 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
334 switch (transition) {
335 case GST_STATE_CHANGE_READY_TO_NULL:
337 gst_caps_unref (this->caps);
349 gst_gdp_depay_plugin_init (GstPlugin * plugin)
351 if (!gst_element_register (plugin, "gdpdepay", GST_RANK_NONE,