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_chain_function (gdpdepay->sinkpad,
123 GST_DEBUG_FUNCPTR (gst_gdp_depay_chain));
124 gst_element_add_pad (GST_ELEMENT (gdpdepay), gdpdepay->sinkpad);
127 gst_pad_new_from_static_template (&gdp_depay_src_template, "src");
128 gst_element_add_pad (GST_ELEMENT (gdpdepay), gdpdepay->srcpad);
130 /* our caps will always be decided by the incoming GDP caps buffers */
131 gst_pad_use_fixed_caps (gdpdepay->srcpad);
133 gdpdepay->adapter = gst_adapter_new ();
137 gst_gdp_depay_finalize (GObject * gobject)
141 this = GST_GDP_DEPAY (gobject);
143 gst_caps_unref (this->caps);
145 g_free (this->header);
146 gst_adapter_clear (this->adapter);
147 g_object_unref (this->adapter);
149 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (gobject));
153 gst_gdp_depay_chain (GstPad * pad, GstBuffer * buffer)
156 GstFlowReturn ret = GST_FLOW_OK;
160 guint8 *header = NULL;
161 guint8 *payload = NULL;
163 gboolean running = TRUE;
165 this = GST_GDP_DEPAY (gst_pad_get_parent (pad));
167 gst_adapter_push (this->adapter, buffer);
170 switch (this->state) {
171 case GST_GDP_DEPAY_STATE_HEADER:
172 available = gst_adapter_available (this->adapter);
173 if (available < GST_DP_HEADER_LENGTH) {
179 g_free (this->header);
180 GST_LOG_OBJECT (this, "reading GDP header from adapter");
181 header = gst_adapter_take (this->adapter, GST_DP_HEADER_LENGTH);
182 if (!gst_dp_validate_header (GST_DP_HEADER_LENGTH, header))
183 goto header_validate_error;
185 this->payload_length = gst_dp_header_payload_length (header);
186 this->payload_type = gst_dp_header_payload_type (header);
187 this->header = header;
188 GST_LOG_OBJECT (this,
189 "read GDP header, payload size %d, switching to state PAYLOAD",
190 this->payload_length);
191 this->state = GST_GDP_DEPAY_STATE_PAYLOAD;
194 case GST_GDP_DEPAY_STATE_PAYLOAD:
195 available = gst_adapter_available (this->adapter);
196 if (available < this->payload_length) {
201 /* change state based on type */
202 if (this->payload_type == GST_DP_PAYLOAD_BUFFER) {
203 GST_LOG_OBJECT (this, "switching to state BUFFER");
204 this->state = GST_GDP_DEPAY_STATE_BUFFER;
205 } else if (this->payload_type == GST_DP_PAYLOAD_CAPS) {
206 GST_LOG_OBJECT (this, "switching to state CAPS");
207 this->state = GST_GDP_DEPAY_STATE_CAPS;
208 } else if (this->payload_type >= GST_DP_PAYLOAD_EVENT_NONE) {
209 GST_LOG_OBJECT (this, "switching to state EVENT");
210 this->state = GST_GDP_DEPAY_STATE_EVENT;
215 case GST_GDP_DEPAY_STATE_BUFFER:
217 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
218 ("Received a buffer without first receiving caps"));
219 ret = GST_FLOW_NOT_NEGOTIATED;
223 GST_LOG_OBJECT (this, "reading GDP buffer from adapter");
224 buf = gst_dp_buffer_from_header (GST_DP_HEADER_LENGTH, this->header);
226 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
227 ("could not create buffer from GDP packet"));
228 ret = GST_FLOW_ERROR;
232 payload = gst_adapter_take (this->adapter, this->payload_length);
233 memcpy (GST_BUFFER_DATA (buf), payload, this->payload_length);
237 gst_buffer_set_caps (buf, this->caps);
238 ret = gst_pad_push (this->srcpad, buf);
239 if (ret != GST_FLOW_OK) {
240 GST_WARNING_OBJECT (this, "pushing depayloaded buffer returned %d",
245 GST_LOG_OBJECT (this, "switching to state HEADER");
246 this->state = GST_GDP_DEPAY_STATE_HEADER;
249 case GST_GDP_DEPAY_STATE_CAPS:
250 GST_LOG_OBJECT (this, "reading GDP caps from adapter");
251 payload = gst_adapter_take (this->adapter, this->payload_length);
252 caps = gst_dp_caps_from_packet (GST_DP_HEADER_LENGTH, this->header,
257 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
258 ("could not create caps from GDP packet"));
259 ret = GST_FLOW_ERROR;
262 GST_DEBUG_OBJECT (this, "read caps %" GST_PTR_FORMAT, caps);
263 gst_caps_replace (&(this->caps), caps);
264 gst_pad_set_caps (this->srcpad, caps);
265 /* drop the creation ref we still have */
266 gst_caps_unref (caps);
268 GST_LOG_OBJECT (this, "switching to state HEADER");
269 this->state = GST_GDP_DEPAY_STATE_HEADER;
272 case GST_GDP_DEPAY_STATE_EVENT:
273 GST_LOG_OBJECT (this, "reading GDP event from adapter");
274 /* adapter doesn't like 0 length payload */
275 if (this->payload_length > 0)
276 payload = gst_adapter_take (this->adapter, this->payload_length);
277 event = gst_dp_event_from_packet (GST_DP_HEADER_LENGTH, this->header,
284 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
285 ("could not create event from GDP packet"));
286 ret = GST_FLOW_ERROR;
289 /* FIXME: set me as source ? */
290 GST_DEBUG_OBJECT (this, "sending deserialized event %p of type %s",
291 event, gst_event_type_get_name (event->type));
292 gst_pad_push_event (this->srcpad, event);
294 GST_LOG_OBJECT (this, "switching to state HEADER");
295 this->state = GST_GDP_DEPAY_STATE_HEADER;
301 header_validate_error:
302 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
303 ("GDP packet header does not validate"));
305 ret = GST_FLOW_ERROR;
309 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
310 ("GDP packet header is of wrong type"));
312 ret = GST_FLOW_ERROR;
316 gst_object_unref (this);
320 static GstStateChangeReturn
321 gst_gdp_depay_change_state (GstElement * element, GstStateChange transition)
323 GstStateChangeReturn ret;
324 GstGDPDepay *this = GST_GDP_DEPAY (element);
326 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
328 switch (transition) {
329 case GST_STATE_CHANGE_READY_TO_NULL:
331 gst_caps_unref (this->caps);
343 gst_gdp_depay_plugin_init (GstPlugin * plugin)
345 if (!gst_element_register (plugin, "gdpdepay", GST_RANK_NONE,