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 gstelement_class->change_state =
113 GST_DEBUG_FUNCPTR (gst_gdp_depay_change_state);
114 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_gdp_depay_finalize);
118 gst_gdp_depay_init (GstGDPDepay * gdpdepay, GstGDPDepayClass * g_class)
121 gst_pad_new_from_static_template (&gdp_depay_sink_template, "sink");
122 gst_pad_set_setcaps_function (gdpdepay->sinkpad,
123 GST_DEBUG_FUNCPTR (gst_pad_proxy_setcaps));
124 gst_pad_set_getcaps_function (gdpdepay->sinkpad,
125 GST_DEBUG_FUNCPTR (gst_pad_proxy_getcaps));
126 gst_pad_set_chain_function (gdpdepay->sinkpad,
127 GST_DEBUG_FUNCPTR (gst_gdp_depay_chain));
128 gst_element_add_pad (GST_ELEMENT (gdpdepay), gdpdepay->sinkpad);
131 gst_pad_new_from_static_template (&gdp_depay_src_template, "src");
132 gst_element_add_pad (GST_ELEMENT (gdpdepay), gdpdepay->srcpad);
134 /* our caps will always be decided by the incoming GDP caps buffers */
135 gst_pad_use_fixed_caps (gdpdepay->srcpad);
137 gdpdepay->adapter = gst_adapter_new ();
141 gst_gdp_depay_finalize (GObject * gobject)
145 this = GST_GDP_DEPAY (gobject);
147 gst_caps_unref (this->caps);
149 g_free (this->header);
150 gst_adapter_clear (this->adapter);
151 g_object_unref (this->adapter);
153 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (gobject));
157 gst_gdp_depay_chain (GstPad * pad, GstBuffer * buffer)
160 GstFlowReturn ret = GST_FLOW_OK;
164 guint8 *header = NULL;
165 guint8 *payload = NULL;
167 gboolean running = TRUE;
169 this = GST_GDP_DEPAY (gst_pad_get_parent (pad));
171 gst_adapter_push (this->adapter, buffer);
174 switch (this->state) {
175 case GST_GDP_DEPAY_STATE_HEADER:
176 available = gst_adapter_available (this->adapter);
177 if (available < GST_DP_HEADER_LENGTH) {
183 g_free (this->header);
184 GST_LOG_OBJECT (this, "reading GDP header from adapter");
185 header = gst_adapter_take (this->adapter, GST_DP_HEADER_LENGTH);
186 if (!gst_dp_validate_header (GST_DP_HEADER_LENGTH, header))
187 goto header_validate_error;
189 this->payload_length = gst_dp_header_payload_length (header);
190 this->payload_type = gst_dp_header_payload_type (header);
191 this->header = header;
192 GST_LOG_OBJECT (this,
193 "read GDP header, payload size %d, switching to state PAYLOAD",
194 this->payload_length);
195 this->state = GST_GDP_DEPAY_STATE_PAYLOAD;
198 case GST_GDP_DEPAY_STATE_PAYLOAD:
199 available = gst_adapter_available (this->adapter);
200 if (available < this->payload_length) {
205 /* change state based on type */
206 if (this->payload_type == GST_DP_PAYLOAD_BUFFER) {
207 GST_LOG_OBJECT (this, "switching to state BUFFER");
208 this->state = GST_GDP_DEPAY_STATE_BUFFER;
209 } else if (this->payload_type == GST_DP_PAYLOAD_CAPS) {
210 GST_LOG_OBJECT (this, "switching to state CAPS");
211 this->state = GST_GDP_DEPAY_STATE_CAPS;
212 } else if (this->payload_type >= GST_DP_PAYLOAD_EVENT_NONE) {
213 GST_LOG_OBJECT (this, "switching to state EVENT");
214 this->state = GST_GDP_DEPAY_STATE_EVENT;
219 case GST_GDP_DEPAY_STATE_BUFFER:
221 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
222 ("Received a buffer without first receiving caps"));
223 ret = GST_FLOW_NOT_NEGOTIATED;
227 GST_LOG_OBJECT (this, "reading GDP buffer from adapter");
228 buf = gst_dp_buffer_from_header (GST_DP_HEADER_LENGTH, this->header);
230 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
231 ("could not create buffer from GDP packet"));
232 ret = GST_FLOW_ERROR;
236 payload = gst_adapter_take (this->adapter, this->payload_length);
237 memcpy (GST_BUFFER_DATA (buf), payload, this->payload_length);
241 gst_buffer_set_caps (buf, this->caps);
242 ret = gst_pad_push (this->srcpad, buf);
243 if (ret != GST_FLOW_OK) {
244 GST_WARNING_OBJECT (this, "pushing depayloaded buffer returned %d",
249 GST_LOG_OBJECT (this, "switching to state HEADER");
250 this->state = GST_GDP_DEPAY_STATE_HEADER;
253 case GST_GDP_DEPAY_STATE_CAPS:
254 GST_LOG_OBJECT (this, "reading GDP caps from adapter");
255 payload = gst_adapter_take (this->adapter, this->payload_length);
256 caps = gst_dp_caps_from_packet (GST_DP_HEADER_LENGTH, this->header,
261 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
262 ("could not create caps from GDP packet"));
263 ret = GST_FLOW_ERROR;
266 GST_DEBUG_OBJECT (this, "read caps %" GST_PTR_FORMAT, caps);
267 gst_caps_replace (&(this->caps), caps);
268 gst_pad_set_caps (this->srcpad, caps);
269 /* drop the creation ref we still have */
270 gst_caps_unref (caps);
272 GST_LOG_OBJECT (this, "switching to state HEADER");
273 this->state = GST_GDP_DEPAY_STATE_HEADER;
276 case GST_GDP_DEPAY_STATE_EVENT:
277 GST_LOG_OBJECT (this, "reading GDP event from adapter");
278 /* adapter doesn't like 0 length payload */
279 if (this->payload_length > 0)
280 payload = gst_adapter_take (this->adapter, this->payload_length);
281 event = gst_dp_event_from_packet (GST_DP_HEADER_LENGTH, this->header,
288 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
289 ("could not create event from GDP packet"));
290 ret = GST_FLOW_ERROR;
293 /* FIXME: set me as source ? */
294 GST_DEBUG_OBJECT (this, "sending deserialized event %p of type %s",
295 event, gst_event_type_get_name (event->type));
296 gst_pad_push_event (this->srcpad, event);
298 GST_LOG_OBJECT (this, "switching to state HEADER");
299 this->state = GST_GDP_DEPAY_STATE_HEADER;
305 header_validate_error:
306 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
307 ("GDP packet header does not validate"));
309 ret = GST_FLOW_ERROR;
313 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
314 ("GDP packet header is of wrong type"));
316 ret = GST_FLOW_ERROR;
320 gst_object_unref (this);
324 static GstStateChangeReturn
325 gst_gdp_depay_change_state (GstElement * element, GstStateChange transition)
327 GstStateChangeReturn ret;
328 GstGDPDepay *this = GST_GDP_DEPAY (element);
330 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
332 switch (transition) {
333 case GST_STATE_CHANGE_READY_TO_NULL:
335 gst_caps_unref (this->caps);
347 gst_gdp_depay_plugin_init (GstPlugin * plugin)
349 if (!gst_element_register (plugin, "gdpdepay", GST_RANK_NONE,