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>");
61 static GstStaticPadTemplate gdp_depay_sink_template =
62 GST_STATIC_PAD_TEMPLATE ("sink",
65 GST_STATIC_CAPS ("application/x-gdp"));
67 static GstStaticPadTemplate gdp_depay_src_template =
68 GST_STATIC_PAD_TEMPLATE ("src",
73 GST_DEBUG_CATEGORY_STATIC (gst_gdp_depay_debug);
74 #define GST_CAT_DEFAULT gst_gdp_depay_debug
77 GST_DEBUG_CATEGORY_INIT (gst_gdp_depay_debug, "gdpdepay", 0, \
80 GST_BOILERPLATE_FULL (GstGDPDepay, gst_gdp_depay, GstElement,
81 GST_TYPE_ELEMENT, _do_init);
83 static gboolean gst_gdp_depay_sink_event (GstPad * pad, GstEvent * event);
84 static gboolean gst_gdp_depay_src_event (GstPad * pad, GstEvent * event);
86 static GstFlowReturn gst_gdp_depay_chain (GstPad * pad, GstBuffer * buffer);
88 static GstStateChangeReturn gst_gdp_depay_change_state (GstElement *
89 element, GstStateChange transition);
91 static void gst_gdp_depay_finalize (GObject * object);
94 gst_gdp_depay_base_init (gpointer g_class)
96 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
98 gst_element_class_set_details (element_class, &gdp_depay_details);
100 gst_element_class_add_pad_template (element_class,
101 gst_static_pad_template_get (&gdp_depay_sink_template));
102 gst_element_class_add_pad_template (element_class,
103 gst_static_pad_template_get (&gdp_depay_src_template));
107 gst_gdp_depay_class_init (GstGDPDepayClass * klass)
109 GObjectClass *gobject_class;
110 GstElementClass *gstelement_class;
112 gobject_class = (GObjectClass *) klass;
113 gstelement_class = (GstElementClass *) klass;
115 gstelement_class->change_state =
116 GST_DEBUG_FUNCPTR (gst_gdp_depay_change_state);
117 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_gdp_depay_finalize);
121 gst_gdp_depay_init (GstGDPDepay * gdpdepay, GstGDPDepayClass * g_class)
124 gst_pad_new_from_static_template (&gdp_depay_sink_template, "sink");
125 gst_pad_set_chain_function (gdpdepay->sinkpad,
126 GST_DEBUG_FUNCPTR (gst_gdp_depay_chain));
127 gst_pad_set_event_function (gdpdepay->sinkpad,
128 GST_DEBUG_FUNCPTR (gst_gdp_depay_sink_event));
129 gst_element_add_pad (GST_ELEMENT (gdpdepay), gdpdepay->sinkpad);
132 gst_pad_new_from_static_template (&gdp_depay_src_template, "src");
133 gst_pad_set_event_function (gdpdepay->srcpad,
134 GST_DEBUG_FUNCPTR (gst_gdp_depay_src_event));
135 /* our caps will always be decided by the incoming GDP caps buffers */
136 gst_pad_use_fixed_caps (gdpdepay->srcpad);
137 gst_element_add_pad (GST_ELEMENT (gdpdepay), 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);
150 g_free (this->header);
151 gst_adapter_clear (this->adapter);
152 g_object_unref (this->adapter);
154 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (gobject));
158 gst_gdp_depay_sink_event (GstPad * pad, GstEvent * event)
163 this = GST_GDP_DEPAY (gst_pad_get_parent (pad));
165 switch (GST_EVENT_TYPE (event)) {
166 case GST_EVENT_FLUSH_START:
167 case GST_EVENT_FLUSH_STOP:
168 /* forward flush start and stop */
169 res = gst_pad_push_event (this->srcpad, event);
172 /* after EOS, we don't expect to output anything anymore */
173 res = gst_pad_push_event (this->srcpad, event);
175 case GST_EVENT_NEWSEGMENT:
177 case GST_EVENT_BUFFERSIZE:
179 /* we unref most events as we take them from the datastream */
180 gst_event_unref (event);
183 gst_object_unref (this);
189 gst_gdp_depay_src_event (GstPad * pad, GstEvent * event)
194 this = GST_GDP_DEPAY (gst_pad_get_parent (pad));
196 switch (GST_EVENT_TYPE (event)) {
198 /* we refuse seek for now. */
199 gst_event_unref (event);
203 case GST_EVENT_NAVIGATION:
205 /* everything else is passed */
206 res = gst_pad_push_event (this->sinkpad, event);
209 gst_object_unref (this);
215 gst_gdp_depay_chain (GstPad * pad, GstBuffer * buffer)
218 GstFlowReturn ret = GST_FLOW_OK;
224 this = GST_GDP_DEPAY (gst_pad_get_parent (pad));
226 /* On DISCONT, get rid of accumulated data. We assume a buffer after the
227 * DISCONT contains (part of) a new valid header, if not we error because we
229 if (GST_BUFFER_IS_DISCONT (buffer)) {
230 gst_adapter_clear (this->adapter);
231 this->state = GST_GDP_DEPAY_STATE_HEADER;
233 gst_adapter_push (this->adapter, buffer);
236 switch (this->state) {
237 case GST_GDP_DEPAY_STATE_HEADER:
241 /* collect a complete header, validate and store the header. Figure out
242 * the payload length and switch to the PAYLOAD state */
243 available = gst_adapter_available (this->adapter);
244 if (available < GST_DP_HEADER_LENGTH)
247 GST_LOG_OBJECT (this, "reading GDP header from adapter");
248 header = gst_adapter_take (this->adapter, GST_DP_HEADER_LENGTH);
249 if (!gst_dp_validate_header (GST_DP_HEADER_LENGTH, header)) {
251 goto header_validate_error;
254 /* store types and payload length. Also store the header, which we need
255 * to make the payload. */
256 this->payload_length = gst_dp_header_payload_length (header);
257 this->payload_type = gst_dp_header_payload_type (header);
258 /* free previous header and store new one. */
259 g_free (this->header);
260 this->header = header;
262 GST_LOG_OBJECT (this,
263 "read GDP header, payload size %d, switching to state PAYLOAD",
264 this->payload_length);
265 this->state = GST_GDP_DEPAY_STATE_PAYLOAD;
268 case GST_GDP_DEPAY_STATE_PAYLOAD:
270 /* in this state we wait for all the payload data to be available in the
271 * adapter. Then we switch to the state where we actually process the
273 available = gst_adapter_available (this->adapter);
274 if (available < this->payload_length)
277 /* change state based on type */
278 if (this->payload_type == GST_DP_PAYLOAD_BUFFER) {
279 GST_LOG_OBJECT (this, "switching to state BUFFER");
280 this->state = GST_GDP_DEPAY_STATE_BUFFER;
281 } else if (this->payload_type == GST_DP_PAYLOAD_CAPS) {
282 GST_LOG_OBJECT (this, "switching to state CAPS");
283 this->state = GST_GDP_DEPAY_STATE_CAPS;
284 } else if (this->payload_type >= GST_DP_PAYLOAD_EVENT_NONE) {
285 GST_LOG_OBJECT (this, "switching to state EVENT");
286 this->state = GST_GDP_DEPAY_STATE_EVENT;
292 case GST_GDP_DEPAY_STATE_BUFFER:
295 /* if we receive a buffer without caps first, we error out */
299 GST_LOG_OBJECT (this, "reading GDP buffer from adapter");
300 buf = gst_dp_buffer_from_header (GST_DP_HEADER_LENGTH, this->header);
304 /* now take the payload if there is any */
305 if (this->payload_length > 0) {
308 payload = gst_adapter_take (this->adapter, this->payload_length);
309 memcpy (GST_BUFFER_DATA (buf), payload, this->payload_length);
313 /* set caps and push */
314 gst_buffer_set_caps (buf, this->caps);
315 GST_LOG_OBJECT (this, "pushing buffer %p, timestamp %"
316 GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT
317 ", offset %" G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT,
319 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
320 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)),
321 GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET_END (buf));
322 ret = gst_pad_push (this->srcpad, buf);
323 if (ret != GST_FLOW_OK)
326 GST_LOG_OBJECT (this, "switching to state HEADER");
327 this->state = GST_GDP_DEPAY_STATE_HEADER;
330 case GST_GDP_DEPAY_STATE_CAPS:
334 /* take the payload of the caps */
335 GST_LOG_OBJECT (this, "reading GDP caps from adapter");
336 payload = gst_adapter_take (this->adapter, this->payload_length);
337 caps = gst_dp_caps_from_packet (GST_DP_HEADER_LENGTH, this->header,
343 GST_DEBUG_OBJECT (this, "read caps %" GST_PTR_FORMAT, caps);
344 gst_caps_replace (&(this->caps), caps);
345 gst_pad_set_caps (this->srcpad, caps);
346 /* drop the creation ref we still have */
347 gst_caps_unref (caps);
349 GST_LOG_OBJECT (this, "switching to state HEADER");
350 this->state = GST_GDP_DEPAY_STATE_HEADER;
353 case GST_GDP_DEPAY_STATE_EVENT:
357 GST_LOG_OBJECT (this, "reading GDP event from adapter");
359 /* adapter doesn't like 0 length payload */
360 if (this->payload_length > 0)
361 payload = gst_adapter_take (this->adapter, this->payload_length);
364 event = gst_dp_event_from_packet (GST_DP_HEADER_LENGTH, this->header,
370 GST_DEBUG_OBJECT (this, "sending deserialized event %p of type %s",
371 event, gst_event_type_get_name (event->type));
372 gst_pad_push_event (this->srcpad, event);
374 GST_LOG_OBJECT (this, "switching to state HEADER");
375 this->state = GST_GDP_DEPAY_STATE_HEADER;
382 gst_object_unref (this);
386 header_validate_error:
388 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
389 ("GDP packet header does not validate"));
390 ret = GST_FLOW_ERROR;
395 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
396 ("GDP packet header is of wrong type"));
397 ret = GST_FLOW_ERROR;
402 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
403 ("Received a buffer without first receiving caps"));
404 ret = GST_FLOW_NOT_NEGOTIATED;
409 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
410 ("could not create buffer from GDP packet"));
411 ret = GST_FLOW_ERROR;
416 GST_WARNING_OBJECT (this, "pushing depayloaded buffer returned %d", ret);
421 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
422 ("could not create caps from GDP packet"));
423 ret = GST_FLOW_ERROR;
428 GST_ELEMENT_ERROR (this, STREAM, DECODE, (NULL),
429 ("could not create event from GDP packet"));
430 ret = GST_FLOW_ERROR;
435 static GstStateChangeReturn
436 gst_gdp_depay_change_state (GstElement * element, GstStateChange transition)
438 GstStateChangeReturn ret;
439 GstGDPDepay *this = GST_GDP_DEPAY (element);
441 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
443 switch (transition) {
444 case GST_STATE_CHANGE_PAUSED_TO_READY:
446 gst_caps_unref (this->caps);
457 gst_gdp_depay_plugin_init (GstPlugin * plugin)
459 if (!gst_element_register (plugin, "gdpdepay", GST_RANK_NONE,