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
22 * @see_also: videorate, ffmpegcolorspace
26 * This element depayloads GStreamer Data Protocol buffers back to deserialized
38 #include <gst/dataprotocol/dataprotocol.h>
40 #include "gstgdpdepay.h"
42 /* elementfactory information */
43 static const GstElementDetails gdp_depay_details =
44 GST_ELEMENT_DETAILS ("GDP Depayloader",
46 "Depayloads GStreamer Data Protocol buffers",
47 "Thomas Vander Stichele <thomas at apestaart dot org>");
55 static GstStaticPadTemplate gdp_depay_sink_template =
56 GST_STATIC_PAD_TEMPLATE ("sink",
59 GST_STATIC_CAPS ("application/x-gdp"));
61 static GstStaticPadTemplate gdp_depay_src_template =
62 GST_STATIC_PAD_TEMPLATE ("src",
67 GST_DEBUG_CATEGORY (gst_gdp_depay_debug);
68 #define GST_CAT_DEFAULT gst_gdp_depay_debug
71 GST_DEBUG_CATEGORY_INIT (gst_gdp_depay_debug, "gdpdepay", 0, \
74 GST_BOILERPLATE_FULL (GstGDPDepay, gst_gdp_depay, GstElement,
75 GST_TYPE_ELEMENT, _do_init);
77 static GstFlowReturn gst_gdp_depay_chain (GstPad * pad, GstBuffer * buffer);
78 static GstStateChangeReturn gst_gdp_depay_change_state (GstElement *
79 element, GstStateChange transition);
81 static void gst_gdp_depay_finalize (GObject * object);
84 gst_gdp_depay_base_init (gpointer g_class)
86 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
88 gst_element_class_set_details (element_class, &gdp_depay_details);
90 gst_element_class_add_pad_template (element_class,
91 gst_static_pad_template_get (&gdp_depay_sink_template));
92 gst_element_class_add_pad_template (element_class,
93 gst_static_pad_template_get (&gdp_depay_src_template));
97 gst_gdp_depay_class_init (GstGDPDepayClass * klass)
99 GObjectClass *gobject_class;
100 GstElementClass *gstelement_class;
102 gobject_class = (GObjectClass *) klass;
103 gstelement_class = (GstElementClass *) klass;
105 parent_class = g_type_class_peek_parent (klass);
107 gstelement_class->change_state =
108 GST_DEBUG_FUNCPTR (gst_gdp_depay_change_state);
109 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_gdp_depay_finalize);
113 gst_gdp_depay_init (GstGDPDepay * gdpdepay, GstGDPDepayClass * g_class)
116 gst_pad_new_from_static_template (&gdp_depay_sink_template, "sink");
117 gst_pad_set_setcaps_function (gdpdepay->sinkpad,
118 GST_DEBUG_FUNCPTR (gst_pad_proxy_setcaps));
119 gst_pad_set_getcaps_function (gdpdepay->sinkpad,
120 GST_DEBUG_FUNCPTR (gst_pad_proxy_getcaps));
121 gst_pad_set_chain_function (gdpdepay->sinkpad,
122 GST_DEBUG_FUNCPTR (gst_gdp_depay_chain));
123 gst_element_add_pad (GST_ELEMENT (gdpdepay), gdpdepay->sinkpad);
126 gst_pad_new_from_static_template (&gdp_depay_src_template, "src");
127 gst_element_add_pad (GST_ELEMENT (gdpdepay), gdpdepay->srcpad);
129 /* our caps will always be decided by the incoming GDP caps buffers */
130 gst_pad_use_fixed_caps (gdpdepay->srcpad);
132 gdpdepay->adapter = gst_adapter_new ();
136 gst_gdp_depay_finalize (GObject * gobject)
140 this = GST_GDP_DEPAY (gobject);
142 gst_caps_unref (this->caps);
144 g_free (this->header);
145 gst_adapter_clear (this->adapter);
146 g_object_unref (this->adapter);
148 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (gobject));
152 gst_gdp_depay_chain (GstPad * pad, GstBuffer * buffer)
155 GstFlowReturn ret = GST_FLOW_OK;
159 guint8 *header = NULL;
160 guint8 *payload = NULL;
162 gboolean running = TRUE;
164 this = GST_GDP_DEPAY (gst_pad_get_parent (pad));
166 gst_adapter_push (this->adapter, buffer);
169 switch (this->state) {
170 case GST_GDP_DEPAY_STATE_HEADER:
171 available = gst_adapter_available (this->adapter);
172 if (available < GST_DP_HEADER_LENGTH) {
178 g_free (this->header);
179 GST_LOG_OBJECT (this, "reading GDP header from adapter");
180 header = gst_adapter_take (this->adapter, GST_DP_HEADER_LENGTH);
181 if (!gst_dp_validate_header (GST_DP_HEADER_LENGTH, header))
182 goto header_validate_error;
184 this->payload_length = gst_dp_header_payload_length (header);
185 this->payload_type = gst_dp_header_payload_type (header);
186 this->header = header;
187 GST_LOG_OBJECT (this,
188 "read GDP header, payload size %d, switching to state PAYLOAD",
189 this->payload_length);
190 this->state = GST_GDP_DEPAY_STATE_PAYLOAD;
193 case GST_GDP_DEPAY_STATE_PAYLOAD:
194 available = gst_adapter_available (this->adapter);
195 if (available < this->payload_length) {
200 /* change state based on type */
201 if (this->payload_type == GST_DP_PAYLOAD_BUFFER) {
202 GST_LOG_OBJECT (this, "switching to state BUFFER");
203 this->state = GST_GDP_DEPAY_STATE_BUFFER;
204 } else if (this->payload_type == GST_DP_PAYLOAD_CAPS) {
205 GST_LOG_OBJECT (this, "switching to state CAPS");
206 this->state = GST_GDP_DEPAY_STATE_CAPS;
207 } else if (this->payload_type >= GST_DP_PAYLOAD_EVENT_NONE) {
208 GST_LOG_OBJECT (this, "switching to state EVENT");
209 this->state = GST_GDP_DEPAY_STATE_EVENT;
214 case GST_GDP_DEPAY_STATE_BUFFER:
216 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
217 ("Received a buffer without first receiving caps"));
218 ret = GST_FLOW_NOT_NEGOTIATED;
222 GST_LOG_OBJECT (this, "reading GDP buffer from adapter");
223 buf = gst_dp_buffer_from_header (GST_DP_HEADER_LENGTH, this->header);
225 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
226 ("could not create buffer from GDP packet"));
227 ret = GST_FLOW_ERROR;
231 payload = gst_adapter_take (this->adapter, this->payload_length);
232 memcpy (GST_BUFFER_DATA (buf), payload, this->payload_length);
235 gst_buffer_set_caps (buf, this->caps);
236 ret = gst_pad_push (this->srcpad, buf);
237 if (ret != GST_FLOW_OK) {
238 GST_WARNING_OBJECT (this, "pushing depayloaded buffer returned %d",
243 GST_LOG_OBJECT (this, "switching to state HEADER");
244 this->state = GST_GDP_DEPAY_STATE_HEADER;
247 case GST_GDP_DEPAY_STATE_CAPS:
248 GST_LOG_OBJECT (this, "reading GDP caps from adapter");
249 payload = gst_adapter_take (this->adapter, this->payload_length);
250 caps = gst_dp_caps_from_packet (GST_DP_HEADER_LENGTH, this->header,
254 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
255 ("could not create caps from GDP packet"));
256 ret = GST_FLOW_ERROR;
259 GST_DEBUG_OBJECT (this, "read caps %" GST_PTR_FORMAT, caps);
260 gst_caps_replace (&(this->caps), caps);
261 gst_pad_set_caps (this->srcpad, caps);
262 /* drop the creation ref we still have */
263 gst_caps_unref (caps);
265 GST_LOG_OBJECT (this, "switching to state HEADER");
266 this->state = GST_GDP_DEPAY_STATE_HEADER;
269 case GST_GDP_DEPAY_STATE_EVENT:
270 GST_LOG_OBJECT (this, "reading GDP event from adapter");
271 /* adapter doesn't like 0 length payload */
272 if (this->payload_length > 0)
273 payload = gst_adapter_take (this->adapter, this->payload_length);
274 event = gst_dp_event_from_packet (GST_DP_HEADER_LENGTH, this->header,
279 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
280 ("could not create event from GDP packet"));
281 ret = GST_FLOW_ERROR;
284 /* FIXME: set me as source ? */
285 gst_pad_push_event (this->srcpad, event);
287 GST_LOG_OBJECT (this, "switching to state HEADER");
288 this->state = GST_GDP_DEPAY_STATE_HEADER;
294 header_validate_error:
295 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
296 ("GDP packet header does not validate"));
298 ret = GST_FLOW_ERROR;
302 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
303 ("GDP packet header is of wrong type"));
305 ret = GST_FLOW_ERROR;
309 gst_object_unref (this);
313 static GstStateChangeReturn
314 gst_gdp_depay_change_state (GstElement * element, GstStateChange transition)
316 GstStateChangeReturn ret;
317 GstGDPDepay *this = GST_GDP_DEPAY (element);
319 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
321 switch (transition) {
322 case GST_STATE_CHANGE_READY_TO_NULL:
324 gst_caps_unref (this->caps);
336 gst_gdp_depay_plugin_init (GstPlugin * plugin)
338 if (!gst_element_register (plugin, "gdpdepay", GST_RANK_NONE,